From 5586250c2ebbbd1c0d394c068c249081e3d34949 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Tue, 2 Apr 2019 11:10:57 -0700 Subject: [PATCH] Only use HTTP/2 with curl >= 7.60.0 --- lib/HttpClient/CurlClient.php | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/HttpClient/CurlClient.php b/lib/HttpClient/CurlClient.php index c9155ca61..01ed67203 100644 --- a/lib/HttpClient/CurlClient.php +++ b/lib/HttpClient/CurlClient.php @@ -44,6 +44,8 @@ public static function instance() protected $enablePersistentConnections = null; + protected $enableHttp2 = null; + protected $curlHandle = null; /** @@ -68,6 +70,8 @@ public function __construct($defaultOptions = null, $randomGenerator = null) // TODO: curl_reset requires PHP >= 5.5.0. Once we drop support for PHP 5.4, we can simply // initialize this to true. $this->enablePersistentConnections = function_exists('curl_reset'); + + $this->enableHttp2 = $this->canSafelyUseHttp2(); } public function __destruct() @@ -110,6 +114,22 @@ public function setEnablePersistentConnections($enable) $this->enablePersistentConnections = $enable; } + /** + * @return boolean + */ + public function getEnableHttp2() + { + return $this->enableHttp2; + } + + /** + * @param boolean $enable + */ + public function setEnableHttp2($enable) + { + $this->enableHttp2 = $enable; + } + // USER DEFINED TIMEOUTS const DEFAULT_TIMEOUT = 80; @@ -228,7 +248,7 @@ public function request($method, $absUrl, $headers, $params, $hasFile) $opts[CURLOPT_SSL_VERIFYPEER] = false; } - if (!isset($opts[CURLOPT_HTTP_VERSION])) { + if (!isset($opts[CURLOPT_HTTP_VERSION]) && $this->getEnableHttp2()) { // For HTTPS requests, enable HTTP/2, if supported $opts[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_2TLS; } @@ -406,4 +426,17 @@ private function resetCurlHandle() $this->initCurlHandle(); } } + + /** + * Indicates whether it is safe to use HTTP/2 or not. + * + * @return boolean + */ + private function canSafelyUseHttp2() + { + // Versions of curl older than 7.60.0 don't respect GOAWAY frames + // (cf. https://github.com/curl/curl/issues/2416), which Stripe use. + $curlVersion = curl_version()['version']; + return (version_compare($curlVersion, '7.60.0') >= 0); + } }