Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrayIterator committed Mar 9, 2024
1 parent 5cc441f commit 23c03cb
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 13 deletions.
21 changes: 16 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,33 @@ public function guessType(string $target): ?array
if (!$target) {
return null;
}
if (str_contains($target, ':')) {
$target = CIDR::filterIp6($target);
return $target ? [self::IPV6, $target] : null;
if (str_contains($target, '/') && ($cidr = CIDR::cidrToRange($target))) {
if (str_contains($cidr[0], ':') || str_contains($cidr[1], ':')) {
if ($target = CIDR::filterIp6($cidr[0])) {
return [self::IPV6, $target];
}
}
if (str_contains($cidr[0], '.') || str_contains($cidr[1], '.')) {
if ($target = CIDR::filterIp4($cidr[0])) {
return [self::IPV4, $target];
}
}
}
if (preg_match('~^(?:ASN?)?([0-9]+)$~i', $target, $match)) {
$target = $match[1] > 0 && $match[1] <= AsnService::MAX_INTEGER
? $match[1]
: null;
return $target ? [self::ASN, $target] : null;
}
if ($ip4 = CIDR::filterIp4($target)) {
return [self::IPV4, $ip4];
if (str_contains($target, ':') && ($ip6 = CIDR::filterIp6($target))) {
return [self::IPV6, $ip6];
}
if (!str_contains($target, '.')) {
return null;
}
if ($ip4 = CIDR::filterIp4($target)) {
return [self::IPV4, $ip4];
}
$target = idn_to_ascii($target)?:null;
if (!$target) {
return null;
Expand Down
4 changes: 3 additions & 1 deletion src/Interfaces/RdapResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace ArrayAccess\RdapClient\Interfaces;

interface RdapResponseInterface
use JsonSerializable;

interface RdapResponseInterface extends JsonSerializable
{
const CONTENT_TYPE = 'application/rdap+json';

Expand Down
2 changes: 1 addition & 1 deletion src/Protocols/RdapRequestProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function withRdapSearchURL(string $url) : static
// validate target
$path = array_pop($targetUrl);
$searchPath = rtrim($this->getProtocol()->getSearchPath(), '/');
if (str_ends_with($searchPath, $path)) {
if (!str_ends_with($searchPath, $path)) {
throw new MismatchProtocolBehaviorException(
'Target RDAP search path is mismatch'
);
Expand Down
5 changes: 5 additions & 0 deletions src/Response/Abstracts/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,9 @@ public function getProtocol(): RdapProtocolInterface
{
return $this->protocol;
}

public function jsonSerialize() : mixed
{
return json_decode($this->getResponseJson(), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(

public function rootOnly(): bool
{
return true;
return false;
}

public function getAllowedKeys(): ?array
Expand Down
28 changes: 23 additions & 5 deletions src/Response/Definitions/AbstractResponseDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace ArrayAccess\RdapClient\Response\Definitions;

use ArrayAccess\RdapClient\Exceptions\InvalidDataTypeException;
use ArrayAccess\RdapClient\Exceptions\MismatchProtocolBehaviorException;
use ArrayAccess\RdapClient\Interfaces\RdapRequestInterface;
use ArrayAccess\RdapClient\Interfaces\RdapResponseDefinitionInterface;
use ArrayAccess\RdapClient\Interfaces\RdapResponseInterface;
Expand Down Expand Up @@ -651,18 +652,35 @@ public function getRelatedRequest(): ?RdapRequestInterface
}
$this->relatedRequest = false;
foreach (($this->getLinks()?->getLinks()??[]) as $link) {
if ($link->getRel()?->getPlainData() === 'related'
&& ($url = $link->getHref()?->getPlainData())
) {
return $this->relatedRequest = $this
if ($link->getRel()?->getPlainData() !== 'related') {
continue;
}
$url = $link->getValue()?->getPlainData();
if ($url && ($this->relatedRequest = $this->createObjectRdapRequestURL($url)??false)) {
return $this->relatedRequest;
}
$url = $link->getHref()?->getPlainData();
if ($url && ($this->relatedRequest = $this->createObjectRdapRequestURL($url)??false)) {
return $this->relatedRequest;
}
}
return null;
}

private function createObjectRdapRequestURL(?string $url): ?RdapRequestInterface
{
if ($url && preg_match('~^https?://~i', $url)) {
try {
return $this
->getRdapResponseObject()
->getRequest()
->withRdapSearchURL($url);
} catch (MismatchProtocolBehaviorException) {
}
}

return null;
}

public function __set(string $name, $value): void
{
// pass
Expand Down

0 comments on commit 23c03cb

Please sign in to comment.