getHeaderLine('Content-Length'); if ($contentLength !== '' && \ctype_digit($contentLength) && (int) $contentLength > $maxBytes) { throw new \LengthException(\sprintf('The response body exceeds the %d-byte limit.', $maxBytes)); } $stream = $response->getBody(); $body = ''; while (!$stream->eof()) { $remaining = $maxBytes + 1 - \strlen($body); if ($remaining <= 0) { throw new \LengthException(\sprintf('The response body exceeds the %d-byte limit.', $maxBytes)); } $chunk = $stream->read(\min(8_192, $remaining)); if ($chunk === '') { break; } $body .= $chunk; } if (\strlen($body) > $maxBytes) { throw new \LengthException(\sprintf('The response body exceeds the %d-byte limit.', $maxBytes)); } return $body; } /** * Reads a seekable body from the beginning and restores its position. * * Returns NULL when the body cannot be inspected without consuming it or * when it exceeds the configured limit. */ public static function readPreservingPosition(ResponseInterface $response, int $maxBytes): ?string { $stream = $response->getBody(); if (!$stream->isSeekable()) { return NULL; } $position = $stream->tell(); $stream->rewind(); try { return self::read($response, $maxBytes); } catch (\LengthException) { return NULL; } finally { $stream->seek($position); } } }