Skip to content

Commit

Permalink
Fixed HTTP/2 fallback in Guzzle\HttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoit Viguier committed Nov 20, 2019
1 parent f1bd1c3 commit 16609e5
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Adapter/Guzzle/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct(EventLoop $eventLoop, GuzzleClientWrapper $clientWra
*/
public function sendRequest(RequestInterface $request): Promise
{
$request = $this->http2fallback($request);
$deferred = $this->eventLoop->deferred();

$this->clientWrapper->getClient()->sendAsync($request)->then(
Expand All @@ -59,6 +60,25 @@ function (\Exception $exception) use ($deferred) {
return $deferred->getPromise();
}

private function http2fallback(RequestInterface $request): RequestInterface
{
if ($request->getProtocolVersion() !== '2.0') {
return $request;
}

// Check that HTTP/2 is effectively supported by the system, and fallback to HTTP/1.1 if needed.
// Inspired from https://github.com/symfony/http-client/blob/master/CurlHttpClient.php
if (
'https' !== $request->getUri()->getScheme()
|| !\defined('CURL_VERSION_HTTP2')
|| !(CURL_VERSION_HTTP2 & curl_version()['features'])
) {
return $request->withProtocolVersion('1.1');
}

return $request;
}

private function guzzleEventLoop(): \Generator
{
do {
Expand Down

0 comments on commit 16609e5

Please sign in to comment.