| @@ -1,19 +1,92 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace YoastSEO_Vendor\GuzzleHttp\Psr7; |
| 4 | 5 | |
| 6 | +/** | |
| 7 | + * @internal | |
| 8 | + */ | |
| 5 | 9 | final class Rfc7230 |
| 6 | 10 | { |
| 7 | 11 | /** |
| 8 | - * Header related regular expressions (copied from amphp/http package) | |
| 9 | - * (Note: once we require PHP 7.x we could just depend on the upstream package) | |
| 12 | + * Header related regular expressions (based on amphp/http package) | |
| 10 | 13 | * |
| 11 | 14 | * Note: header delimiter (\r\n) is modified to \r?\n to accept line feed only delimiters for BC reasons. |
| 12 | 15 | * |
| 13 | - * @link https://github.com/amphp/http/blob/v1.0.1/src/Rfc7230.php#L12-L15 | |
| 16 | + * @see https://github.com/amphp/http/blob/v1.0.1/src/Rfc7230.php#L12-L15 | |
| 14 | 17 | * |
| 15 | 18 | * @license https://github.com/amphp/http/blob/v1.0.1/LICENSE |
| 16 | 19 | */ |
| 17 | - const HEADER_REGEX = "(^([^()<>@,;:\\\"/[\\]?={}\1- ]++):[ \t]*+((?:[ \t]*+[!-~�-�]++)*+)[ \t]*+\r?\n)m"; | |
| 18 | - const HEADER_FOLD_REGEX = "(\r?\n[ \t]++)"; | |
| 20 | + public const HEADER_REGEX = "(^([^()<>@,;:\\\"/[\\]?={}\x01- ]++):[ \t]*+((?:[ \t]*+[!-~\x80-\xff]++)*+)[ \t]*+\r?\n)m"; | |
| 21 | + public const HEADER_FOLD_REGEX = "(\r?\n[ \t]++)"; | |
| 22 | + /** | |
| 23 | + * @return array{0: string, 1: int|null}|null | |
| 24 | + */ | |
| 25 | + public static function parseHostHeader(string $authority) : ?array | |
| 26 | + { | |
| 27 | + if ($authority === '') { | |
| 28 | + return null; | |
| 29 | + } | |
| 30 | + $host = $authority; | |
| 31 | + $port = null; | |
| 32 | + if ($authority[0] === '[') { | |
| 33 | + $closingBracket = \strpos($authority, ']'); | |
| 34 | + if ($closingBracket === \false) { | |
| 35 | + return null; | |
| 36 | + } | |
| 37 | + $host = \substr($authority, 0, $closingBracket + 1); | |
| 38 | + $remainder = \substr($authority, $closingBracket + 1); | |
| 39 | + if ($remainder !== '') { | |
| 40 | + if ($remainder[0] !== ':') { | |
| 41 | + return null; | |
| 42 | + } | |
| 43 | + $port = self::parseAuthorityPort(\substr($remainder, 1)); | |
| 44 | + if ($port === null) { | |
| 45 | + return null; | |
| 46 | + } | |
| 47 | + } | |
| 48 | + } elseif (\false !== ($colon = \strpos($authority, ':'))) { | |
| 49 | + $host = \substr($authority, 0, $colon); | |
| 50 | + $port = self::parseAuthorityPort(\substr($authority, $colon + 1)); | |
| 51 | + if ($port === null) { | |
| 52 | + return null; | |
| 53 | + } | |
| 54 | + } | |
| 55 | + if ($host === '' || !self::isValidHostHeaderHost($host)) { | |
| 56 | + return null; | |
| 57 | + } | |
| 58 | + return [$host, $port]; | |
| 59 | + } | |
| 60 | + private static function isValidHostHeaderHost(string $host) : bool | |
| 61 | + { | |
| 62 | + $invalidHost = \preg_match('/[\\x00-\\x20\\x7F\\/\\?#@\\\\]/', $host); | |
| 63 | + if ($invalidHost === \false) { | |
| 64 | + return \false; | |
| 65 | + } | |
| 66 | + if ($invalidHost === 1) { | |
| 67 | + return \false; | |
| 68 | + } | |
| 69 | + if (\strpos($host, '[') !== \false || \strpos($host, ']') !== \false) { | |
| 70 | + if ($host[0] !== '[' || \substr($host, -1) !== ']') { | |
| 71 | + return \false; | |
| 72 | + } | |
| 73 | + $address = \substr($host, 1, -1); | |
| 74 | + return \filter_var($address, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6) !== \false || \preg_match('/^v[0-9a-f]+\\.[' . \YoastSEO_Vendor\GuzzleHttp\Psr7\Rfc3986::CHAR_UNRESERVED . \YoastSEO_Vendor\GuzzleHttp\Psr7\Rfc3986::CHAR_SUB_DELIMS . ':]+$/iD', $address) === 1; | |
| 75 | + } | |
| 76 | + return \strpos($host, ':') === \false; | |
| 77 | + } | |
| 78 | + private static function parseAuthorityPort(string $port) : ?int | |
| 79 | + { | |
| 80 | + if ($port === '' || !\ctype_digit($port)) { | |
| 81 | + return null; | |
| 82 | + } | |
| 83 | + $normalized = \ltrim($port, '0'); | |
| 84 | + if ($normalized === '') { | |
| 85 | + return 0; | |
| 86 | + } | |
| 87 | + if (\strlen($normalized) > 5 || (int) $normalized > 0xffff) { | |
| 88 | + return null; | |
| 89 | + } | |
| 90 | + return (int) $normalized; | |
| 91 | + } | |
| 19 | 92 | } |