Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpClient] Fix type error with http_version 1.1 #51876

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

Merged
merged 1 commit into from
Oct 7, 2023
Merged

[HttpClient] Fix type error with http_version 1.1 #51876

merged 1 commit into from
Oct 7, 2023

Conversation

Filnor
Copy link
Contributor

@Filnor Filnor commented Oct 6, 2023

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 46a66956 the lines 114-118 of AmpHttpClient.php were changed to use the match() expression instead of switch-case:

-            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

// 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:

$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'],
});

Fix a type error by removing a nested setProtocolVersions() call in AmpHttpClient::request()
@Filnor Filnor marked this pull request as ready for review October 6, 2023 15:52
@fabpot
Copy link
Member

fabpot commented Oct 7, 2023

Good catch, thanks @Filnor.

@fabpot fabpot merged commit 82f21e8 into symfony:6.3 Oct 7, 2023
@fabpot fabpot mentioned this pull request Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants