\strlen($max) || (\strlen($length) === \strlen($max) && \strcmp($length, $max) > 0) ) { return null; } return (int) $length; } public static function assertContentLengthWithinPlatformLimit(?string $length): void { if ($length === null || self::contentLengthToInt($length) !== null) { return; } throw new \OverflowException('Content-Length exceeds the maximum integer size supported on this platform'); } /** * Validates response framing and returns its normalized Content-Length. * Returns null when absent or when ordinary body framing does not apply. * * @param array $headers * * @throws \RuntimeException when Content-Length is malformed, conflicting, * or combined with Transfer-Encoding, or when a * transfer coding is unsupported or follows * chunked on the same field line */ public static function validateResponseFraming( string $method, int $status, array $headers ): ?string { if (!self::responseCanHaveBody($method, $status)) { return null; } $normalizedKeys = self::normalizeHeaderKeys($headers); $contentLength = self::removeHeader('Content-Length', $headers); try { $length = self::parseContentLength($contentLength); } catch (\RuntimeException $e) { throw new \RuntimeException('Invalid Content-Length response header: '.$e->getMessage(), 0, $e); } if ($length !== null && isset($normalizedKeys['transfer-encoding'])) { throw new \RuntimeException('A response must not contain both Content-Length and Transfer-Encoding'); } self::parseTransferEncoding($headers); return $length; } /** * Returns whether the response uses the chunked transfer coding. * * Guzzle never asks for a transfer coding, so like libcurl 8.14 and newer * it accepts only identity and chunked, with nothing but chunked after * chunked on the same field line. Empty list elements are ignored. * * @param array $headers * * @throws \RuntimeException when a coding is unsupported or follows chunked * on the same field line */ public static function parseTransferEncoding(array $headers): bool { $chunked = false; foreach ($headers as $name => $values) { if (!Psr7\Utils::caselessEquals((string) $name, 'Transfer-Encoding')) { continue; } foreach ($values as $value) { $afterChunked = false; foreach (\explode(',', $value) as $coding) { $coding = \trim($coding, " \t"); if ($coding === '') { continue; } if (Psr7\Utils::caselessEquals($coding, 'chunked')) { $chunked = true; $afterChunked = true; } elseif ($afterChunked) { throw new \RuntimeException('Invalid Transfer-Encoding response header: chunked is not the final transfer coding'); } elseif (!Psr7\Utils::caselessEquals($coding, 'identity')) { throw new \RuntimeException(\sprintf( 'Invalid Transfer-Encoding response header: unsupported transfer coding "%s"', Psr7\DiagnosticValue::escape($coding) )); } } } } return $chunked; } /** * Removes every case-insensitive occurrence of a header and returns all * removed values in their original field order. * * @param array $headers * * @return string[] Removed values across all header-name casings */ public static function removeHeader(string $name, array &$headers): array { $values = []; foreach ($headers as $key => $headerValues) { if (Psr7\Utils::caselessEquals((string) $key, $name)) { \array_push($values, ...$headerValues); unset($headers[$key]); } } return $values; } /** * Creates an associative array of lowercase header names to the actual * header casing. */ public static function normalizeHeaderKeys(array $headers): array { $result = []; foreach (\array_keys($headers) as $key) { $result[Psr7\Utils::asciiToLower((string) $key)] = $key; } return $result; } /** * Whether a response uses ordinary body framing. A response to HEAD, a * response with a 1xx, 204, or 304 status code, or a 2xx response to * CONNECT never has a body, whatever its framing headers claim. A 205 * remains subject to framing even though its semantics require no content. */ public static function responseCanHaveBody(string $method, int $status): bool { return $method !== 'HEAD' && !($method === 'CONNECT' && $status >= 200 && $status < 300) && $status >= 200 && $status !== 204 && $status !== 304; } /** * @param non-empty-list $headers * * @return list */ private static function getLastHeaderBlock(array $headers): array { $lastStatusLine = 0; foreach ($headers as $index => $line) { if (self::isStatusLineCandidate($line)) { $lastStatusLine = $index; } } return \array_slice($headers, $lastStatusLine); } }