Changeset 50842 for trunk/src/wp-includes/Requests/Transport/cURL.php
- Timestamp:
- 05/11/2021 07:40:41 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/Requests/Transport/cURL.php
r46586 r50842 39 39 40 40 /** 41 * Version string42 * 43 * @var long41 * cURL version number 42 * 43 * @var int 44 44 */ 45 45 public $version; … … 91 91 */ 92 92 public function __construct() { 93 $curl = curl_version();93 $curl = curl_version(); 94 94 $this->version = $curl['version_number']; 95 $this->handle = curl_init();95 $this->handle = curl_init(); 96 96 97 97 curl_setopt($this->handle, CURLOPT_HEADER, false); … … 101 101 } 102 102 if (defined('CURLOPT_PROTOCOLS')) { 103 // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_protocolsFound 103 104 curl_setopt($this->handle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); 104 105 } 105 106 if (defined('CURLOPT_REDIR_PROTOCOLS')) { 107 // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_redir_protocolsFound 106 108 curl_setopt($this->handle, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); 107 109 } … … 139 141 } 140 142 141 $this->response_data = '';142 $this->response_bytes = 0;143 $this->response_data = ''; 144 $this->response_bytes = 0; 143 145 $this->response_byte_limit = false; 144 146 if ($options['max_bytes'] !== false) { … … 169 171 curl_setopt($this->handle, CURLOPT_ENCODING, 'none'); 170 172 171 $this->response_data = '';173 $this->response_data = ''; 172 174 $this->response_bytes = 0; 173 175 curl_exec($this->handle); … … 200 202 $multihandle = curl_multi_init(); 201 203 $subrequests = array(); 202 $subhandles = array();204 $subhandles = array(); 203 205 204 206 $class = get_class($this); 205 207 foreach ($requests as $id => $request) { 206 208 $subrequests[$id] = new $class(); 207 $subhandles[$id] = $subrequests[$id]->get_subrequest_handle($request['url'], $request['headers'], $request['data'], $request['options']);209 $subhandles[$id] = $subrequests[$id]->get_subrequest_handle($request['url'], $request['headers'], $request['data'], $request['options']); 208 210 $request['options']['hooks']->dispatch('curl.before_multi_add', array(&$subhandles[$id])); 209 211 curl_multi_add_handle($multihandle, $subhandles[$id]); 210 212 } 211 213 212 $completed = 0; 213 $responses = array(); 214 $completed = 0; 215 $responses = array(); 216 $subrequestcount = count($subrequests); 214 217 215 218 $request['options']['hooks']->dispatch('curl.before_multi_exec', array(&$multihandle)); 216 219 217 220 do { 218 $active = false;221 $active = 0; 219 222 220 223 do { … … 236 239 foreach ($to_process as $key => $done) { 237 240 $options = $requests[$key]['options']; 238 if ( CURLE_OK !== $done['result']) {241 if ($done['result'] !== CURLE_OK) { 239 242 //get error string for handle. 240 $reason = curl_error($done['handle']);241 $exception = new Requests_Exception_Transport_cURL(242 243 244 245 246 243 $reason = curl_error($done['handle']); 244 $exception = new Requests_Exception_Transport_cURL( 245 $reason, 246 Requests_Exception_Transport_cURL::EASY, 247 $done['handle'], 248 $done['result'] 249 ); 247 250 $responses[$key] = $exception; 248 251 $options['hooks']->dispatch('transport.internal.parse_error', array(&$responses[$key], $requests[$key])); … … 263 266 } 264 267 } 265 while ($active || $completed < count($subrequests));268 while ($active || $completed < $subrequestcount); 266 269 267 270 $request['options']['hooks']->dispatch('curl.after_multi_exec', array(&$multihandle)); … … 288 291 } 289 292 290 $this->response_data = '';291 $this->response_bytes = 0;293 $this->response_data = ''; 294 $this->response_bytes = 0; 292 295 $this->response_byte_limit = false; 293 296 if ($options['max_bytes'] !== false) { … … 311 314 312 315 // Force closing the connection for old versions of cURL (<7.22). 313 if ( ! isset( $headers['Connection'] )) {316 if (!isset($headers['Connection'])) { 314 317 $headers['Connection'] = 'close'; 318 } 319 320 /** 321 * Add "Expect" header. 322 * 323 * By default, cURL adds a "Expect: 100-Continue" to most requests. This header can 324 * add as much as a second to the time it takes for cURL to perform a request. To 325 * prevent this, we need to set an empty "Expect" header. To match the behaviour of 326 * Guzzle, we'll add the empty header to requests that are smaller than 1 MB and use 327 * HTTP/1.1. 328 * 329 * https://curl.se/mail/lib-2017-07/0013.html 330 */ 331 if (!isset($headers['Expect']) && $options['protocol_version'] === 1.1) { 332 $headers['Expect'] = $this->get_expect_header($data); 315 333 } 316 334 … … 321 339 322 340 if ($data_format === 'query') { 323 $url = self::format_get($url, $data);341 $url = self::format_get($url, $data); 324 342 $data = ''; 325 343 } … … 364 382 } 365 383 else { 384 // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_timeout_msFound 366 385 curl_setopt($this->handle, CURLOPT_TIMEOUT_MS, round($timeout * 1000)); 367 386 } … … 371 390 } 372 391 else { 392 // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_connecttimeout_msFound 373 393 curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT_MS, round($options['connect_timeout'] * 1000)); 374 394 } … … 386 406 } 387 407 388 if ( true === $options['blocking']) {389 curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, array( &$this, 'stream_headers'));390 curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, array( &$this, 'stream_body'));408 if ($options['blocking'] === true) { 409 curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, array($this, 'stream_headers')); 410 curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, array($this, 'stream_body')); 391 411 curl_setopt($this->handle, CURLOPT_BUFFERSIZE, Requests::BUFFER_SIZE); 392 412 } … … 398 418 * @param string $response Response data from the body 399 419 * @param array $options Request options 400 * @return string HTTP response data including headers 420 * @return string|false HTTP response data including headers. False if non-blocking. 421 * @throws Requests_Exception 401 422 */ 402 423 public function process_response($response, $options) { … … 406 427 return false; 407 428 } 408 if ($options['filename'] !== false ) {429 if ($options['filename'] !== false && $this->stream_handle) { 409 430 fclose($this->stream_handle); 410 431 $this->headers = trim($this->headers); … … 440 461 // (We may want to keep this somewhere just in case) 441 462 if ($this->done_headers) { 442 $this->headers = '';463 $this->headers = ''; 443 464 $this->done_headers = false; 444 465 } … … 474 495 // Limit the length 475 496 $limited_length = ($this->response_byte_limit - $this->response_bytes); 476 $data = substr($data, 0, $limited_length);497 $data = substr($data, 0, $limited_length); 477 498 } 478 499 } … … 498 519 protected static function format_get($url, $data) { 499 520 if (!empty($data)) { 521 $query = ''; 500 522 $url_parts = parse_url($url); 501 523 if (empty($url_parts['query'])) { 502 $ query = $url_parts['query'] = '';524 $url_parts['query'] = ''; 503 525 } 504 526 else { … … 507 529 508 530 $query .= '&' . http_build_query($data, null, '&'); 509 $query = trim($query, '&');531 $query = trim($query, '&'); 510 532 511 533 if (empty($url_parts['query'])) { … … 540 562 return true; 541 563 } 564 565 /** 566 * Get the correct "Expect" header for the given request data. 567 * 568 * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD. 569 * @return string The "Expect" header. 570 */ 571 protected function get_expect_header($data) { 572 if (!is_array($data)) { 573 return strlen((string) $data) >= 1048576 ? '100-Continue' : ''; 574 } 575 576 $bytesize = 0; 577 $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($data)); 578 579 foreach ($iterator as $datum) { 580 $bytesize += strlen((string) $datum); 581 582 if ($bytesize >= 1048576) { 583 return '100-Continue'; 584 } 585 } 586 587 return ''; 588 } 542 589 }
Note: See TracChangeset
for help on using the changeset viewer.