Skip to content
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

Only use HTTP/2 with curl >= 7.60.0 #623

Merged
merged 1 commit into from
Apr 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion lib/HttpClient/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public static function instance()

protected $enablePersistentConnections = null;

protected $enableHttp2 = null;

protected $curlHandle = null;

/**
Expand All @@ -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()
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
}