From 23c03cb471bd3d3b1952f73848ec335f3fdac636 Mon Sep 17 00:00:00 2001 From: ArrayIterator Date: Sat, 9 Mar 2024 20:27:08 +0700 Subject: [PATCH] patch --- src/Client.php | 21 ++++++++++---- src/Interfaces/RdapResponseInterface.php | 4 ++- src/Protocols/RdapRequestProtocol.php | 2 +- src/Response/Abstracts/AbstractResponse.php | 5 ++++ .../Definitions/RdapCustomConformanceData.php | 2 +- .../AbstractResponseDefinition.php | 28 +++++++++++++++---- 6 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/Client.php b/src/Client.php index da67e21..9ab47d2 100644 --- a/src/Client.php +++ b/src/Client.php @@ -92,9 +92,17 @@ 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 @@ -102,12 +110,15 @@ public function guessType(string $target): ?array : 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; diff --git a/src/Interfaces/RdapResponseInterface.php b/src/Interfaces/RdapResponseInterface.php index 2306e9d..53589b2 100644 --- a/src/Interfaces/RdapResponseInterface.php +++ b/src/Interfaces/RdapResponseInterface.php @@ -3,7 +3,9 @@ namespace ArrayAccess\RdapClient\Interfaces; -interface RdapResponseInterface +use JsonSerializable; + +interface RdapResponseInterface extends JsonSerializable { const CONTENT_TYPE = 'application/rdap+json'; diff --git a/src/Protocols/RdapRequestProtocol.php b/src/Protocols/RdapRequestProtocol.php index a907878..308ecfc 100644 --- a/src/Protocols/RdapRequestProtocol.php +++ b/src/Protocols/RdapRequestProtocol.php @@ -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' ); diff --git a/src/Response/Abstracts/AbstractResponse.php b/src/Response/Abstracts/AbstractResponse.php index bd674cd..cd05b6c 100644 --- a/src/Response/Abstracts/AbstractResponse.php +++ b/src/Response/Abstracts/AbstractResponse.php @@ -70,4 +70,9 @@ public function getProtocol(): RdapProtocolInterface { return $this->protocol; } + + public function jsonSerialize() : mixed + { + return json_decode($this->getResponseJson(), true); + } } diff --git a/src/Response/Data/Definitions/RdapCustomConformanceData.php b/src/Response/Data/Definitions/RdapCustomConformanceData.php index 46ef480..0c2a55d 100644 --- a/src/Response/Data/Definitions/RdapCustomConformanceData.php +++ b/src/Response/Data/Definitions/RdapCustomConformanceData.php @@ -33,7 +33,7 @@ public function __construct( public function rootOnly(): bool { - return true; + return false; } public function getAllowedKeys(): ?array diff --git a/src/Response/Definitions/AbstractResponseDefinition.php b/src/Response/Definitions/AbstractResponseDefinition.php index 7642b50..40ede2c 100644 --- a/src/Response/Definitions/AbstractResponseDefinition.php +++ b/src/Response/Definitions/AbstractResponseDefinition.php @@ -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; @@ -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