diff --git a/src/Symfony/Component/HttpClient/Internal/HttplugWaitLoop.php b/src/Symfony/Component/HttpClient/Internal/HttplugWaitLoop.php index 7edbc59480d7..772aa7f7c18a 100644 --- a/src/Symfony/Component/HttpClient/Internal/HttplugWaitLoop.php +++ b/src/Symfony/Component/HttpClient/Internal/HttplugWaitLoop.php @@ -116,7 +116,12 @@ public function wait(?ResponseInterface $pendingResponse, float $maxDuration = n public function createPsr7Response(ResponseInterface $response, bool $buffer = false): Psr7ResponseInterface { - $psrResponse = $this->responseFactory->createResponse($response->getStatusCode()); + $responseParameters = [$response->getStatusCode()]; + if ($phrase = $this->getReasonPhrase($response)) { + $responseParameters[] = $phrase; + } + + $psrResponse = $this->responseFactory->createResponse(...$responseParameters); foreach ($response->getHeaders(false) as $name => $values) { foreach ($values as $value) { @@ -142,4 +147,17 @@ public function createPsr7Response(ResponseInterface $response, bool $buffer = f return $psrResponse->withBody($body); } + + private function getReasonPhrase(ResponseInterface $response): ?string + { + $reason = null; + + foreach ($response->getInfo('response_headers') as $h) { + if (11 <= \strlen($h) && '/' === $h[4] && preg_match('#^HTTP/\d+(?:\.\d+)? (?:\d\d\d) (.+)#', $h, $m)) { + $reason = $m[1]; + } + } + + return $reason; + } } diff --git a/src/Symfony/Component/HttpClient/Psr18Client.php b/src/Symfony/Component/HttpClient/Psr18Client.php index 13bf5b578a2c..86e0fb227c41 100644 --- a/src/Symfony/Component/HttpClient/Psr18Client.php +++ b/src/Symfony/Component/HttpClient/Psr18Client.php @@ -31,6 +31,7 @@ use Symfony\Component\HttpClient\Response\StreamWrapper; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; +use Symfony\Contracts\HttpClient\ResponseInterface as HttpClientResponseInterface; use Symfony\Contracts\Service\ResetInterface; if (!interface_exists(ClientInterface::class)) { @@ -105,7 +106,12 @@ public function sendRequest(RequestInterface $request): ResponseInterface $response = $this->client->request($request->getMethod(), (string) $request->getUri(), $options); - $psrResponse = $this->responseFactory->createResponse($response->getStatusCode()); + $responseParameters = [$response->getStatusCode()]; + if ($phrase = $this->getReasonPhrase($response)) { + $responseParameters[] = $phrase; + } + + $psrResponse = $this->responseFactory->createResponse(...$responseParameters); foreach ($response->getHeaders(false) as $name => $values) { foreach ($values as $value) { @@ -195,6 +201,19 @@ public function reset(): void $this->client->reset(); } } + + private function getReasonPhrase(HttpClientResponseInterface $response): ?string + { + $reason = null; + + foreach ($response->getInfo('response_headers') as $h) { + if (11 <= \strlen($h) && '/' === $h[4] && preg_match('#^HTTP/\d+(?:\.\d+)? (?:\d\d\d) (.+)#', $h, $m)) { + $reason = $m[1]; + } + } + + return $reason; + } } /** diff --git a/src/Symfony/Component/HttpClient/Tests/HttplugClientTest.php b/src/Symfony/Component/HttpClient/Tests/HttplugClientTest.php index 86dd197e3ca6..247588cd359a 100644 --- a/src/Symfony/Component/HttpClient/Tests/HttplugClientTest.php +++ b/src/Symfony/Component/HttpClient/Tests/HttplugClientTest.php @@ -281,4 +281,19 @@ public function testInvalidHeaderResponse() $resultResponse = $client->sendRequest($request); $this->assertCount(1, $resultResponse->getHeaders()); } + + public function testResponseReasonPhrase() + { + $responseHeaders = [ + 'HTTP/1.1 103 Very Early Hints', + ]; + $response = new MockResponse('body', ['response_headers' => $responseHeaders]); + + $client = new HttplugClient(new MockHttpClient($response)); + $request = $client->createRequest('POST', 'http://localhost:8057/post') + ->withBody($client->createStream('foo=0123456789')); + + $resultResponse = $client->sendRequest($request); + $this->assertSame('Very Early Hints', $resultResponse->getReasonPhrase()); + } } diff --git a/src/Symfony/Component/HttpClient/Tests/Psr18ClientTest.php b/src/Symfony/Component/HttpClient/Tests/Psr18ClientTest.php index 366d555ae03f..d4bae3ab5c4a 100644 --- a/src/Symfony/Component/HttpClient/Tests/Psr18ClientTest.php +++ b/src/Symfony/Component/HttpClient/Tests/Psr18ClientTest.php @@ -101,4 +101,19 @@ public function testInvalidHeaderResponse() $resultResponse = $client->sendRequest($request); $this->assertCount(1, $resultResponse->getHeaders()); } + + public function testResponseReasonPhrase() + { + $responseHeaders = [ + 'HTTP/1.1 103 Very Early Hints', + ]; + $response = new MockResponse('body', ['response_headers' => $responseHeaders]); + + $client = new Psr18Client(new MockHttpClient($response)); + $request = $client->createRequest('POST', 'http://localhost:8057/post') + ->withBody($client->createStream('foo=0123456789')); + + $resultResponse = $client->sendRequest($request); + $this->assertSame('Very Early Hints', $resultResponse->getReasonPhrase()); + } }
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: