Skip to content

[HttpClient] Psr18Client: parse HTTP Reason Phrase for Response #52329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Symfony/Component/HttpClient/Internal/HttplugWaitLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
21 changes: 20 additions & 1 deletion src/Symfony/Component/HttpClient/Psr18Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/HttplugClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
15 changes: 15 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/Psr18ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy