Skip to content

Commit 82f21e8

Browse files
committed
bug #51876 [HttpClient] Fix type error with http_version 1.1 (Filnor)
This PR was merged into the 6.3 branch. Discussion ---------- [HttpClient] Fix type error with http_version 1.1 Fix a type error by removing a nested setProtocolVersions() call in AmpHttpClient::request() | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #51868 | License | MIT ### Description In commit [46a6695](46a66956) the lines 114-118 of AmpHttpClient.php were changed to use the `match()` expression instead of `switch-case`: ```diff - switch ((float) $options['http_version']) { - case 1.0: $request->setProtocolVersions(['1.0']); break; - case 1.1: $request->setProtocolVersions(['1.1', '1.0']); break; - default: $request->setProtocolVersions(['2', '1.1', '1.0']); break; - } + $request->setProtocolVersions(match ((float) $options['http_version']) { + 1.0 => ['1.0'], + 1.1 => $request->setProtocolVersions(['1.1', '1.0']), + default => ['2', '1.1', '1.0'], + }); ``` If the provided `$options['http_version']` is `1.1`, the code will call `$request->setProtocolVersions()` inside the `match()`, unlike with 1.0 and the default case. The `match` will use the return value of `setProtocolVersions()`, but since it's a void function, the return value is `null`. This then results in a `TypeError` as `setProtocolVersions()` (the "outer" one) expects an array. ### How to reproduce ```php // First, run "composer require symfony/http-client" and " composer require amphp/http-client" // Then, execute this file: require_once __DIR__.'/vendor/autoload.php'; use Symfony\Component\HttpClient\AmpHttpClient; $client = new AmpHttpClient(); $response = $client->request( 'GET', 'http://example.com', [ 'http_version' => '1.1' ] ); // The script will fail with the TypeError and never reach this point var_dump($response->getStatusCode()); ``` ### Solution Change line 116 to match line 115 & 117: ```diff $request->setProtocolVersions(match ((float) $options['http_version']) { 1.0 => ['1.0'], - 1.1 => $request->setProtocolVersions(['1.1', '1.0']), + 1.1 => ['1.1', '1.0'], default => ['2', '1.1', '1.0'], }); ``` Commits ------- 2ddf09b [HttpClient] Fix type error with http_version 1.1
2 parents d14d1a9 + 2ddf09b commit 82f21e8

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/Symfony/Component/HttpClient/AmpHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function request(string $method, string $url, array $options = []): Respo
122122
if ($options['http_version']) {
123123
$request->setProtocolVersions(match ((float) $options['http_version']) {
124124
1.0 => ['1.0'],
125-
1.1 => $request->setProtocolVersions(['1.1', '1.0']),
125+
1.1 => ['1.1', '1.0'],
126126
default => ['2', '1.1', '1.0'],
127127
});
128128
}

0 commit comments

Comments
 (0)
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