@,;:\\\"/[\]?={}\x01-\x20\x7F]++):[ \t]*+((?:[ \t]*+[\x21-\x7E\x80-\xFF]++)*+)[ \t]*+\r?\n)m"; public const HEADER_FOLD_REGEX = "(\r?\n[ \t]++)"; public static function isValidProtocolVersion(string $version): bool { return preg_match('/^'.self::PROTOCOL_VERSION_PATTERN.'$/D', $version) === 1; } public static function isValidRequestTarget(string $target): bool { return preg_match('/^'.self::REQUEST_TARGET_PATTERN.'$/D', $target) === 1; } public static function isValidReasonPhrase(string $reasonPhrase): bool { return Rfc9110::isFieldValue($reasonPhrase); } /** * @return array{0: string, 1: int|null}|null */ public static function parseHostHeader(string $authority): ?array { if ($authority === '') { return null; } $host = $authority; $port = null; if (str_starts_with($authority, '[')) { $closingBracket = strpos($authority, ']'); if ($closingBracket === false) { return null; } $host = substr($authority, 0, $closingBracket + 1); $remainder = substr($authority, $closingBracket + 1); if ($remainder !== '') { if (!str_starts_with($remainder, ':')) { return null; } $port = self::parsePort(substr($remainder, 1)); if ($port === null) { return null; } } } elseif (false !== ($colon = strpos($authority, ':'))) { $host = substr($authority, 0, $colon); $port = self::parsePort(substr($authority, $colon + 1)); if ($port === null) { return null; } } if ($host === '' || !Rfc3986::isValidHost($host)) { return null; } return [$host, $port]; } public static function isAbsoluteFormRequestTarget(string $target): bool { return preg_match('/^[A-Za-z][A-Za-z0-9+.-]*:\/\//D', $target) === 1; } public static function isAsteriskFormRequestTarget(string $method, string $target): bool { return $method === 'OPTIONS' && $target === '*'; } public static function isConnectAuthorityFormRequestTarget(string $method, string $target): bool { return $method === 'CONNECT' && strpbrk($target, '/?#') === false; } public static function parsePort(string $port): ?int { if (!Rfc3986::isValidPort($port)) { return null; } // A zero port is valid per RFC 3986 but meaningless for an HTTP // authority, so reject it on top of the generic syntax check. $parsed = (int) ltrim($port, '0'); return $parsed === 0 ? null : $parsed; } }