Skip to content

[HttpClient] Setting option http_version to 1.1 causes TypeError #51868

@Filnor

Description

@Filnor

Symfony version(s) affected

>= 6.3

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());

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

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      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