Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Deprecate using amphp/http-client < 5
* Add RFC 9111–based caching support to `CachingHttpClient`
* Deprecate passing an instance of `StoreInterface` as `$cache` argument to `CachingHttpClient` constructor
* Add option `auto_upgrade_http_version` to control how the request HTTP version is handled in `HttplugClient` and `Psr18Client`

7.3
---
Expand Down
9 changes: 7 additions & 2 deletions src/Symfony/Component/HttpClient/HttplugClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ final class HttplugClient implements ClientInterface, HttpAsyncClient, RequestFa
private HttpClientInterface $client;
private ResponseFactoryInterface $responseFactory;
private StreamFactoryInterface $streamFactory;
private bool $autoUpgradeHttpVersion = true;

/**
* @var \SplObjectStorage<ResponseInterface, array{RequestInterface, Promise}>|null
Expand Down Expand Up @@ -96,6 +97,10 @@ public function __construct(?HttpClientInterface $client = null, ?ResponseFactor
public function withOptions(array $options): static
{
$clone = clone $this;
if (\array_key_exists('auto_upgrade_http_version', $options)) {
$clone->autoUpgradeHttpVersion = $options['auto_upgrade_http_version'];
unset($options['auto_upgrade_http_version']);
}
$clone->client = $clone->client->withOptions($options);

return $clone;
Expand Down Expand Up @@ -265,8 +270,8 @@ private function sendPsr7Request(RequestInterface $request, ?bool $buffer = null
'buffer' => $buffer,
];

if ('1.0' === $request->getProtocolVersion()) {
$options['http_version'] = '1.0';
if (!$this->autoUpgradeHttpVersion || '1.0' === $request->getProtocolVersion()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we deprecate the exceptional behavior for version 1.0?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't: all PSR-7 implementations default to 1.1, so if we see 1.0 here, it's on purpose.
Also no need to change something that ain't bokrne :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if we see 2.0 or 3.0 here is on purpose too.

Copy link
Member

@nicolas-grekas nicolas-grekas Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I'd consider explicit HTTP version selection something of the past - TLS negotiation makes it mostly useless.
I wouldn't change. The option you added is fine to reclaim control for use cases that care.

$options['http_version'] = $request->getProtocolVersion();
}

return $this->client->request($request->getMethod(), (string) $request->getUri(), $options);
Expand Down
9 changes: 7 additions & 2 deletions src/Symfony/Component/HttpClient/Psr18Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ final class Psr18Client implements ClientInterface, RequestFactoryInterface, Str
private HttpClientInterface $client;
private ResponseFactoryInterface $responseFactory;
private StreamFactoryInterface $streamFactory;
private bool $autoUpgradeHttpVersion = true;

public function __construct(?HttpClientInterface $client = null, ?ResponseFactoryInterface $responseFactory = null, ?StreamFactoryInterface $streamFactory = null)
{
Expand All @@ -79,6 +80,10 @@ public function __construct(?HttpClientInterface $client = null, ?ResponseFactor
public function withOptions(array $options): static
{
$clone = clone $this;
if (\array_key_exists('auto_upgrade_http_version', $options)) {
$clone->autoUpgradeHttpVersion = $options['auto_upgrade_http_version'];
unset($options['auto_upgrade_http_version']);
}
$clone->client = $clone->client->withOptions($options);

return $clone;
Expand Down Expand Up @@ -128,8 +133,8 @@ public function sendRequest(RequestInterface $request): ResponseInterface
'body' => $body,
];

if ('1.0' === $request->getProtocolVersion()) {
$options['http_version'] = '1.0';
if (!$this->autoUpgradeHttpVersion || '1.0' === $request->getProtocolVersion()) {
$options['http_version'] = $request->getProtocolVersion();
}

$response = $this->client->request($request->getMethod(), (string) $request->getUri(), $options);
Expand Down
31 changes: 31 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/HttplugClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,35 @@ public function testResponseReasonPhrase()
$resultResponse = $client->sendRequest($request);
$this->assertSame('Very Early Hints', $resultResponse->getReasonPhrase());
}

public function testAutoUpgradeHttpVersion()
{
$clientWithoutOption = new HttplugClient(new MockHttpClient(function (string $method, string $url, array $options) {
return new MockResponse(json_encode([
'SERVER_PROTOCOL' => 'HTTP/'.$options['http_version'] ?? '',
]), [
'response_headers' => [
'Content-Type' => 'application/json',
],
]);
}));
$clientWithOptionFalse = $clientWithoutOption->withOptions(['auto_upgrade_http_version' => false]);

foreach (['1.0', '1.1', '2.0', '3.0'] as $httpVersion) {
$request = $clientWithoutOption->createRequest('GET', 'http://localhost:8057')
->withProtocolVersion($httpVersion);

$responseWithoutOption = $clientWithoutOption->sendRequest($request);
$bodyWithoutOption = json_decode((string) $responseWithoutOption->getBody(), true);
if ('1.0' === $httpVersion) {
$this->assertSame('HTTP/1.0', $bodyWithoutOption['SERVER_PROTOCOL']);
} else {
$this->assertSame('HTTP/', $bodyWithoutOption['SERVER_PROTOCOL']);
}

$responseWithOptionFalse = $clientWithOptionFalse->sendRequest($request);
$bodyWithOptionFalse = json_decode((string) $responseWithOptionFalse->getBody(), true);
$this->assertSame('HTTP/'.$httpVersion, $bodyWithOptionFalse['SERVER_PROTOCOL']);
}
}
}
31 changes: 31 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/Psr18ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,35 @@ public function testResponseReasonPhrase()
$resultResponse = $client->sendRequest($request);
$this->assertSame('Very Early Hints', $resultResponse->getReasonPhrase());
}

public function testAutoUpgradeHttpVersion()
{
$clientWithoutOption = new Psr18Client(new MockHttpClient(function (string $method, string $url, array $options) {
return new MockResponse(json_encode([
'SERVER_PROTOCOL' => 'HTTP/'.$options['http_version'] ?? '',
]), [
'response_headers' => [
'Content-Type' => 'application/json',
],
]);
}));
$clientWithOptionFalse = $clientWithoutOption->withOptions(['auto_upgrade_http_version' => false]);

foreach (['1.0', '1.1', '2.0', '3.0'] as $httpVersion) {
$request = $clientWithoutOption->createRequest('GET', 'http://localhost:8057')
->withProtocolVersion($httpVersion);

$responseWithoutOption = $clientWithoutOption->sendRequest($request);
$bodyWithoutOption = json_decode((string) $responseWithoutOption->getBody(), true);
if ('1.0' === $httpVersion) {
$this->assertSame('HTTP/1.0', $bodyWithoutOption['SERVER_PROTOCOL']);
} else {
$this->assertSame('HTTP/', $bodyWithoutOption['SERVER_PROTOCOL']);
}

$responseWithOptionFalse = $clientWithOptionFalse->sendRequest($request);
$bodyWithOptionFalse = json_decode((string) $responseWithOptionFalse->getBody(), true);
$this->assertSame('HTTP/'.$httpVersion, $bodyWithOptionFalse['SERVER_PROTOCOL']);
}
}
}