--- a/wp/wp-includes/Requests/Transport/cURL.php Tue Dec 15 15:52:01 2020 +0100
+++ b/wp/wp-includes/Requests/Transport/cURL.php Wed Sep 21 18:19:35 2022 +0200
@@ -38,9 +38,9 @@
public $info;
/**
- * Version string
+ * cURL version number
*
- * @var long
+ * @var int
*/
public $version;
@@ -90,9 +90,9 @@
* Constructor
*/
public function __construct() {
- $curl = curl_version();
+ $curl = curl_version();
$this->version = $curl['version_number'];
- $this->handle = curl_init();
+ $this->handle = curl_init();
curl_setopt($this->handle, CURLOPT_HEADER, false);
curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, 1);
@@ -100,9 +100,11 @@
curl_setopt($this->handle, CURLOPT_ENCODING, '');
}
if (defined('CURLOPT_PROTOCOLS')) {
+ // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_protocolsFound
curl_setopt($this->handle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
}
if (defined('CURLOPT_REDIR_PROTOCOLS')) {
+ // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_redir_protocolsFound
curl_setopt($this->handle, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
}
}
@@ -138,8 +140,8 @@
$this->stream_handle = fopen($options['filename'], 'wb');
}
- $this->response_data = '';
- $this->response_bytes = 0;
+ $this->response_data = '';
+ $this->response_bytes = 0;
$this->response_byte_limit = false;
if ($options['max_bytes'] !== false) {
$this->response_byte_limit = $options['max_bytes'];
@@ -168,7 +170,7 @@
// Reset encoding and try again
curl_setopt($this->handle, CURLOPT_ENCODING, 'none');
- $this->response_data = '';
+ $this->response_data = '';
$this->response_bytes = 0;
curl_exec($this->handle);
$response = $this->response_data;
@@ -199,23 +201,24 @@
$multihandle = curl_multi_init();
$subrequests = array();
- $subhandles = array();
+ $subhandles = array();
$class = get_class($this);
foreach ($requests as $id => $request) {
$subrequests[$id] = new $class();
- $subhandles[$id] = $subrequests[$id]->get_subrequest_handle($request['url'], $request['headers'], $request['data'], $request['options']);
+ $subhandles[$id] = $subrequests[$id]->get_subrequest_handle($request['url'], $request['headers'], $request['data'], $request['options']);
$request['options']['hooks']->dispatch('curl.before_multi_add', array(&$subhandles[$id]));
curl_multi_add_handle($multihandle, $subhandles[$id]);
}
- $completed = 0;
- $responses = array();
+ $completed = 0;
+ $responses = array();
+ $subrequestcount = count($subrequests);
$request['options']['hooks']->dispatch('curl.before_multi_exec', array(&$multihandle));
do {
- $active = false;
+ $active = 0;
do {
$status = curl_multi_exec($multihandle, $active);
@@ -235,15 +238,15 @@
// Parse the finished requests before we start getting the new ones
foreach ($to_process as $key => $done) {
$options = $requests[$key]['options'];
- if (CURLE_OK !== $done['result']) {
+ if ($done['result'] !== CURLE_OK) {
//get error string for handle.
- $reason = curl_error($done['handle']);
- $exception = new Requests_Exception_Transport_cURL(
- $reason,
- Requests_Exception_Transport_cURL::EASY,
- $done['handle'],
- $done['result']
- );
+ $reason = curl_error($done['handle']);
+ $exception = new Requests_Exception_Transport_cURL(
+ $reason,
+ Requests_Exception_Transport_cURL::EASY,
+ $done['handle'],
+ $done['result']
+ );
$responses[$key] = $exception;
$options['hooks']->dispatch('transport.internal.parse_error', array(&$responses[$key], $requests[$key]));
}
@@ -262,7 +265,7 @@
$completed++;
}
}
- while ($active || $completed < count($subrequests));
+ while ($active || $completed < $subrequestcount);
$request['options']['hooks']->dispatch('curl.after_multi_exec', array(&$multihandle));
@@ -287,8 +290,8 @@
$this->stream_handle = fopen($options['filename'], 'wb');
}
- $this->response_data = '';
- $this->response_bytes = 0;
+ $this->response_data = '';
+ $this->response_bytes = 0;
$this->response_byte_limit = false;
if ($options['max_bytes'] !== false) {
$this->response_byte_limit = $options['max_bytes'];
@@ -310,17 +313,32 @@
$options['hooks']->dispatch('curl.before_request', array(&$this->handle));
// Force closing the connection for old versions of cURL (<7.22).
- if ( ! isset( $headers['Connection'] ) ) {
+ if (!isset($headers['Connection'])) {
$headers['Connection'] = 'close';
}
+ /**
+ * Add "Expect" header.
+ *
+ * By default, cURL adds a "Expect: 100-Continue" to most requests. This header can
+ * add as much as a second to the time it takes for cURL to perform a request. To
+ * prevent this, we need to set an empty "Expect" header. To match the behaviour of
+ * Guzzle, we'll add the empty header to requests that are smaller than 1 MB and use
+ * HTTP/1.1.
+ *
+ * https://curl.se/mail/lib-2017-07/0013.html
+ */
+ if (!isset($headers['Expect']) && $options['protocol_version'] === 1.1) {
+ $headers['Expect'] = $this->get_expect_header($data);
+ }
+
$headers = Requests::flatten($headers);
if (!empty($data)) {
$data_format = $options['data_format'];
if ($data_format === 'query') {
- $url = self::format_get($url, $data);
+ $url = self::format_get($url, $data);
$data = '';
}
elseif (!is_string($data)) {
@@ -363,6 +381,7 @@
curl_setopt($this->handle, CURLOPT_TIMEOUT, ceil($timeout));
}
else {
+ // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_timeout_msFound
curl_setopt($this->handle, CURLOPT_TIMEOUT_MS, round($timeout * 1000));
}
@@ -370,6 +389,7 @@
curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT, ceil($options['connect_timeout']));
}
else {
+ // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_connecttimeout_msFound
curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT_MS, round($options['connect_timeout'] * 1000));
}
curl_setopt($this->handle, CURLOPT_URL, $url);
@@ -385,9 +405,9 @@
curl_setopt($this->handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
}
- if (true === $options['blocking']) {
- curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, array(&$this, 'stream_headers'));
- curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, array(&$this, 'stream_body'));
+ if ($options['blocking'] === true) {
+ curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, array($this, 'stream_headers'));
+ curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, array($this, 'stream_body'));
curl_setopt($this->handle, CURLOPT_BUFFERSIZE, Requests::BUFFER_SIZE);
}
}
@@ -397,7 +417,8 @@
*
* @param string $response Response data from the body
* @param array $options Request options
- * @return string HTTP response data including headers
+ * @return string|false HTTP response data including headers. False if non-blocking.
+ * @throws Requests_Exception
*/
public function process_response($response, $options) {
if ($options['blocking'] === false) {
@@ -405,7 +426,7 @@
$options['hooks']->dispatch('curl.after_request', array(&$fake_headers));
return false;
}
- if ($options['filename'] !== false) {
+ if ($options['filename'] !== false && $this->stream_handle) {
fclose($this->stream_handle);
$this->headers = trim($this->headers);
}
@@ -439,7 +460,7 @@
// interim responses, such as a 100 Continue. We don't need that.
// (We may want to keep this somewhere just in case)
if ($this->done_headers) {
- $this->headers = '';
+ $this->headers = '';
$this->done_headers = false;
}
$this->headers .= $headers;
@@ -473,7 +494,7 @@
if (($this->response_bytes + $data_length) > $this->response_byte_limit) {
// Limit the length
$limited_length = ($this->response_byte_limit - $this->response_bytes);
- $data = substr($data, 0, $limited_length);
+ $data = substr($data, 0, $limited_length);
}
}
@@ -497,16 +518,17 @@
*/
protected static function format_get($url, $data) {
if (!empty($data)) {
+ $query = '';
$url_parts = parse_url($url);
if (empty($url_parts['query'])) {
- $query = $url_parts['query'] = '';
+ $url_parts['query'] = '';
}
else {
$query = $url_parts['query'];
}
$query .= '&' . http_build_query($data, null, '&');
- $query = trim($query, '&');
+ $query = trim($query, '&');
if (empty($url_parts['query'])) {
$url .= '?' . $query;
@@ -539,4 +561,29 @@
return true;
}
+
+ /**
+ * Get the correct "Expect" header for the given request data.
+ *
+ * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD.
+ * @return string The "Expect" header.
+ */
+ protected function get_expect_header($data) {
+ if (!is_array($data)) {
+ return strlen((string) $data) >= 1048576 ? '100-Continue' : '';
+ }
+
+ $bytesize = 0;
+ $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
+
+ foreach ($iterator as $datum) {
+ $bytesize += strlen((string) $datum);
+
+ if ($bytesize >= 1048576) {
+ return '100-Continue';
+ }
+ }
+
+ return '';
+ }
}