Skip to content

Instantly share code, notes, and snippets.

@alcaeus
Created April 10, 2017 10:17
Show Gist options
  • Save alcaeus/cc24a214b8a33450d7ff62bd003ba26f to your computer and use it in GitHub Desktop.
Save alcaeus/cc24a214b8a33450d7ff62bd003ba26f to your computer and use it in GitHub Desktop.
HTTP/2 with PHP internal webserver

Demo to show PHP's internal webserver failing to handle HTTP/2 requests with cURL

  1. Install dependencies: composer install
  2. Start the webserver: php -S localhost:8989
  3. Run the client: php client.php

Expected behavior: request is completed successfully.

Actual behavior:

$ php client.php 
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8989 (#0)
> GET /server.php?foo=bar HTTP/1.1
Host: localhost:8989
Connection: Upgrade, HTTP2-Settings
Upgrade: h2c
HTTP2-Settings: AAMAAABkAARAAAAAAAIAAAAA
User-Agent: GuzzleHttp/6.2.1 curl/7.53.1 PHP/7.1.3

* Empty reply from server
* Connection #0 to host localhost left intact
PHP Fatal error:  Uncaught GuzzleHttp\Exception\ConnectException: cURL error 52: Empty reply from server (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) in vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:186
Stack trace:
#0 vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(150): GuzzleHttp\Handler\CurlFactory::createRejection(Object(GuzzleHttp\Handler\EasyHandle), Array)
#1 vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(103): GuzzleHttp\Handler\CurlFactory::finishError(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory))
#2 vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php(43): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory))
#3 /Users/alcaeus/Co in vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 186

Fatal error: Uncaught GuzzleHttp\Exception\ConnectException: cURL error 52: Empty reply from server (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) in vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 186

GuzzleHttp\Exception\ConnectException: cURL error 52: Empty reply from server (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) in vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 186

Call Stack:
    0.0003     356608   1. {main}() client.php:0
    0.0083    1112952   2. GuzzleHttp\Client->get() client.php:13
    0.0083    1113328   3. GuzzleHttp\Client->__call() client.php:13
    0.0083    1113328   4. GuzzleHttp\Client->request() vendor/guzzlehttp/guzzle/src/Client.php:87
    0.0181    1410488   5. GuzzleHttp\Promise\RejectedPromise->wait() vendor/guzzlehttp/guzzle/src/Client.php:129
<?php declare(strict_types = 1);
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client();
$options = [
// Removing the CURLOPT_HTTP_VERSION parameter fixes the error
'curl' => [CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2],
'debug' => true,
];
$response = $client->get('http://localhost:8989/server.php?foo=bar', $options);
var_dump($response->getBody());
{
"require": {
"guzzlehttp/guzzle": "^6.2"
}
}
<?php declare(strict_types = 1);
echo json_encode($_SERVER);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment