Changeset 52244 for trunk/src/wp-includes/Requests/Transport/Curl.php
- Timestamp:
- 11/25/2021 01:10:30 AM (5 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/Requests/Transport/Curl.php
r52243 r52244 3 3 * cURL HTTP transport 4 4 * 5 * @package Requests 6 * @subpackage Transport 5 * @package Requests\Transport 7 6 */ 7 8 namespace WpOrg\Requests\Transport; 9 10 use RecursiveArrayIterator; 11 use RecursiveIteratorIterator; 12 use WpOrg\Requests\Capability; 13 use WpOrg\Requests\Exception; 14 use WpOrg\Requests\Exception\InvalidArgument; 15 use WpOrg\Requests\Exception\Transport\Curl as CurlException; 16 use WpOrg\Requests\Requests; 17 use WpOrg\Requests\Transport; 18 use WpOrg\Requests\Utility\InputValidator; 8 19 9 20 /** 10 21 * cURL HTTP transport 11 22 * 12 * @package Requests 13 * @subpackage Transport 23 * @package Requests\Transport 14 24 */ 15 class Requests_Transport_cURL implements Requests_Transport {25 final class Curl implements Transport { 16 26 const CURL_7_10_5 = 0x070A05; 17 27 const CURL_7_16_2 = 0x071002; … … 34 44 * Information on the current request 35 45 * 36 * @var array cURL information array, see {@ see https://secure.php.net/curl_getinfo}46 * @var array cURL information array, see {@link https://www.php.net/curl_getinfo} 37 47 */ 38 48 public $info; … … 48 58 * cURL handle 49 59 * 60 * @var resource|\CurlHandle Resource in PHP < 8.0, Instance of CurlHandle in PHP >= 8.0. 61 */ 62 private $handle; 63 64 /** 65 * Hook dispatcher instance 66 * 67 * @var \WpOrg\Requests\Hooks 68 */ 69 private $hooks; 70 71 /** 72 * Have we finished the headers yet? 73 * 74 * @var boolean 75 */ 76 private $done_headers = false; 77 78 /** 79 * If streaming to a file, keep the file pointer 80 * 50 81 * @var resource 51 82 */ 52 protected $handle; 53 54 /** 55 * Hook dispatcher instance 56 * 57 * @var Requests_Hooks 58 */ 59 protected $hooks; 60 61 /** 62 * Have we finished the headers yet? 63 * 64 * @var boolean 65 */ 66 protected $done_headers = false; 67 68 /** 69 * If streaming to a file, keep the file pointer 70 * 71 * @var resource 72 */ 73 protected $stream_handle; 83 private $stream_handle; 74 84 75 85 /** … … 78 88 * @var int 79 89 */ 80 pr otected$response_bytes;90 private $response_bytes; 81 91 82 92 /** … … 85 95 * @var int|bool Byte count, or false if no limit. 86 96 */ 87 pr otected$response_byte_limit;97 private $response_byte_limit; 88 98 89 99 /** … … 122 132 * Perform a request 123 133 * 124 * @throws Requests_Exception On a cURL error (`curlerror`) 125 * 126 * @param string $url URL to request 134 * @param string|Stringable $url URL to request 127 135 * @param array $headers Associative array of request headers 128 136 * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD 129 * @param array $options Request options, see {@see Requests::response()} for documentation137 * @param array $options Request options, see {@see \WpOrg\Requests\Requests::response()} for documentation 130 138 * @return string Raw HTTP result 131 */ 132 public function request($url, $headers = array(), $data = array(), $options = array()) { 139 * 140 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $url argument is not a string or Stringable. 141 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $headers argument is not an array. 142 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $data parameter is not an array or string. 143 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array. 144 * @throws \WpOrg\Requests\Exception On a cURL error (`curlerror`) 145 */ 146 public function request($url, $headers = [], $data = [], $options = []) { 147 if (InputValidator::is_string_or_stringable($url) === false) { 148 throw InvalidArgument::create(1, '$url', 'string|Stringable', gettype($url)); 149 } 150 151 if (is_array($headers) === false) { 152 throw InvalidArgument::create(2, '$headers', 'array', gettype($headers)); 153 } 154 155 if (!is_array($data) && !is_string($data)) { 156 if ($data === null) { 157 $data = ''; 158 } else { 159 throw InvalidArgument::create(3, '$data', 'array|string', gettype($data)); 160 } 161 } 162 163 if (is_array($options) === false) { 164 throw InvalidArgument::create(4, '$options', 'array', gettype($options)); 165 } 166 133 167 $this->hooks = $options['hooks']; 134 168 135 169 $this->setup_handle($url, $headers, $data, $options); 136 170 137 $options['hooks']->dispatch('curl.before_send', array(&$this->handle));171 $options['hooks']->dispatch('curl.before_send', [&$this->handle]); 138 172 139 173 if ($options['filename'] !== false) { 140 $this->stream_handle = fopen($options['filename'], 'wb'); 174 // phpcs:ignore WordPress.PHP.NoSilencedErrors -- Silenced the PHP native warning in favour of throwing an exception. 175 $this->stream_handle = @fopen($options['filename'], 'wb'); 176 if ($this->stream_handle === false) { 177 $error = error_get_last(); 178 throw new Exception($error['message'], 'fopen'); 179 } 141 180 } 142 181 … … 165 204 $response = $this->response_data; 166 205 167 $options['hooks']->dispatch('curl.after_send', array());168 169 if (curl_errno($this->handle) === 23 || curl_errno($this->handle) === 61) {206 $options['hooks']->dispatch('curl.after_send', []); 207 208 if (curl_errno($this->handle) === CURLE_WRITE_ERROR || curl_errno($this->handle) === CURLE_BAD_CONTENT_ENCODING) { 170 209 // Reset encoding and try again 171 210 curl_setopt($this->handle, CURLOPT_ENCODING, 'none'); … … 180 219 181 220 // Need to remove the $this reference from the curl handle. 182 // Otherwise Requests_Transport_cURL wont be garbage collected and the curl_close() will never be called.221 // Otherwise \WpOrg\Requests\Transport\Curl won't be garbage collected and the curl_close() will never be called. 183 222 curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, null); 184 223 curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, null); … … 192 231 * @param array $requests Request data 193 232 * @param array $options Global options 194 * @return array Array of Requests_Response objects (may contain Requests_Exception or string responses as well) 233 * @return array Array of \WpOrg\Requests\Response objects (may contain \WpOrg\Requests\Exception or string responses as well) 234 * 235 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $requests argument is not an array or iterable object with array access. 236 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array. 195 237 */ 196 238 public function request_multiple($requests, $options) { 197 239 // If you're not requesting, we can't get any responses ¯\_(ツ)_/¯ 198 240 if (empty($requests)) { 199 return array(); 241 return []; 242 } 243 244 if (InputValidator::has_array_access($requests) === false || InputValidator::is_iterable($requests) === false) { 245 throw InvalidArgument::create(1, '$requests', 'array|ArrayAccess&Traversable', gettype($requests)); 246 } 247 248 if (is_array($options) === false) { 249 throw InvalidArgument::create(2, '$options', 'array', gettype($options)); 200 250 } 201 251 202 252 $multihandle = curl_multi_init(); 203 $subrequests = array();204 $subhandles = array();253 $subrequests = []; 254 $subhandles = []; 205 255 206 256 $class = get_class($this); … … 208 258 $subrequests[$id] = new $class(); 209 259 $subhandles[$id] = $subrequests[$id]->get_subrequest_handle($request['url'], $request['headers'], $request['data'], $request['options']); 210 $request['options']['hooks']->dispatch('curl.before_multi_add', array(&$subhandles[$id]));260 $request['options']['hooks']->dispatch('curl.before_multi_add', [&$subhandles[$id]]); 211 261 curl_multi_add_handle($multihandle, $subhandles[$id]); 212 262 } 213 263 214 264 $completed = 0; 215 $responses = array();265 $responses = []; 216 266 $subrequestcount = count($subrequests); 217 267 218 $request['options']['hooks']->dispatch('curl.before_multi_exec', array(&$multihandle));268 $request['options']['hooks']->dispatch('curl.before_multi_exec', [&$multihandle]); 219 269 220 270 do { … … 226 276 while ($status === CURLM_CALL_MULTI_PERFORM); 227 277 228 $to_process = array();278 $to_process = []; 229 279 230 280 // Read the information as needed … … 242 292 //get error string for handle. 243 293 $reason = curl_error($done['handle']); 244 $exception = new Requests_Exception_Transport_cURL(294 $exception = new CurlException( 245 295 $reason, 246 Requests_Exception_Transport_cURL::EASY,296 CurlException::EASY, 247 297 $done['handle'], 248 298 $done['result'] 249 299 ); 250 300 $responses[$key] = $exception; 251 $options['hooks']->dispatch('transport.internal.parse_error', array(&$responses[$key], $requests[$key]));301 $options['hooks']->dispatch('transport.internal.parse_error', [&$responses[$key], $requests[$key]]); 252 302 } 253 303 else { 254 304 $responses[$key] = $subrequests[$key]->process_response($subrequests[$key]->response_data, $options); 255 305 256 $options['hooks']->dispatch('transport.internal.parse_response', array(&$responses[$key], $requests[$key]));306 $options['hooks']->dispatch('transport.internal.parse_response', [&$responses[$key], $requests[$key]]); 257 307 } 258 308 … … 261 311 262 312 if (!is_string($responses[$key])) { 263 $options['hooks']->dispatch('multiple.request.complete', array(&$responses[$key], $key));313 $options['hooks']->dispatch('multiple.request.complete', [&$responses[$key], $key]); 264 314 } 265 315 $completed++; … … 268 318 while ($active || $completed < $subrequestcount); 269 319 270 $request['options']['hooks']->dispatch('curl.after_multi_exec', array(&$multihandle));320 $request['options']['hooks']->dispatch('curl.after_multi_exec', [&$multihandle]); 271 321 272 322 curl_multi_close($multihandle); … … 281 331 * @param array $headers Associative array of request headers 282 332 * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD 283 * @param array $options Request options, see {@see Requests::response()} for documentation284 * @return resource Subrequest's cURL handle333 * @param array $options Request options, see {@see \WpOrg\Requests\Requests::response()} for documentation 334 * @return resource|\CurlHandle Subrequest's cURL handle 285 335 */ 286 336 public function &get_subrequest_handle($url, $headers, $data, $options) { … … 308 358 * @param array $headers Associative array of request headers 309 359 * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD 310 * @param array $options Request options, see {@see Requests::response()} for documentation311 */ 312 pr otectedfunction setup_handle($url, $headers, $data, $options) {313 $options['hooks']->dispatch('curl.before_request', array(&$this->handle));360 * @param array $options Request options, see {@see \WpOrg\Requests\Requests::response()} for documentation 361 */ 362 private function setup_handle($url, $headers, $data, $options) { 363 $options['hooks']->dispatch('curl.before_request', [&$this->handle]); 314 364 315 365 // Force closing the connection for old versions of cURL (<7.22). … … 343 393 } 344 394 elseif (!is_string($data)) { 345 $data = http_build_query($data, null, '&');395 $data = http_build_query($data, '', '&'); 346 396 } 347 397 } … … 394 444 } 395 445 curl_setopt($this->handle, CURLOPT_URL, $url); 396 curl_setopt($this->handle, CURLOPT_REFERER, $url);397 446 curl_setopt($this->handle, CURLOPT_USERAGENT, $options['useragent']); 398 447 if (!empty($headers)) { … … 407 456 408 457 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'));458 curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, [$this, 'stream_headers']); 459 curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, [$this, 'stream_body']); 411 460 curl_setopt($this->handle, CURLOPT_BUFFERSIZE, Requests::BUFFER_SIZE); 412 461 } … … 419 468 * @param array $options Request options 420 469 * @return string|false HTTP response data including headers. False if non-blocking. 421 * @throws Requests_Exception470 * @throws \WpOrg\Requests\Exception 422 471 */ 423 472 public function process_response($response, $options) { 424 473 if ($options['blocking'] === false) { 425 474 $fake_headers = ''; 426 $options['hooks']->dispatch('curl.after_request', array(&$fake_headers));475 $options['hooks']->dispatch('curl.after_request', [&$fake_headers]); 427 476 return false; 428 477 } … … 441 490 curl_error($this->handle) 442 491 ); 443 throw new Requests_Exception($error, 'curlerror', $this->handle);492 throw new Exception($error, 'curlerror', $this->handle); 444 493 } 445 494 $this->info = curl_getinfo($this->handle); 446 495 447 $options['hooks']->dispatch('curl.after_request', array(&$this->headers, &$this->info));496 $options['hooks']->dispatch('curl.after_request', [&$this->headers, &$this->info]); 448 497 return $this->headers; 449 498 } … … 452 501 * Collect the headers as they are received 453 502 * 454 * @param resource $handle cURL resource503 * @param resource|\CurlHandle $handle cURL handle 455 504 * @param string $headers Header string 456 505 * @return integer Length of provided header … … 477 526 * @since 1.6.1 478 527 * 479 * @param resource $handle cURL resource528 * @param resource|\CurlHandle $handle cURL handle 480 529 * @param string $data Body data 481 530 * @return integer Length of provided data 482 531 */ 483 532 public function stream_body($handle, $data) { 484 $this->hooks->dispatch('request.progress', array($data, $this->response_bytes, $this->response_byte_limit));533 $this->hooks->dispatch('request.progress', [$data, $this->response_bytes, $this->response_byte_limit]); 485 534 $data_length = strlen($data); 486 535 … … 514 563 * 515 564 * @param string $url 516 * @param array|object $data Data to build query using, see {@ see https://secure.php.net/http_build_query}565 * @param array|object $data Data to build query using, see {@link https://www.php.net/http_build_query} 517 566 * @return string URL with data 518 567 */ 519 pr otectedstatic function format_get($url, $data) {568 private static function format_get($url, $data) { 520 569 if (!empty($data)) { 521 570 $query = ''; … … 528 577 } 529 578 530 $query .= '&' . http_build_query($data, null, '&');579 $query .= '&' . http_build_query($data, '', '&'); 531 580 $query = trim($query, '&'); 532 581 … … 542 591 543 592 /** 544 * Whether this transport is valid 593 * Self-test whether the transport can be used. 594 * 595 * The available capabilities to test for can be found in {@see \WpOrg\Requests\Capability}. 545 596 * 546 597 * @codeCoverageIgnore 547 * @return boolean True if the transport is valid, false otherwise. 548 */ 549 public static function test($capabilities = array()) { 598 * @param array<string, bool> $capabilities Optional. Associative array of capabilities to test against, i.e. `['<capability>' => true]`. 599 * @return bool Whether the transport can be used. 600 */ 601 public static function test($capabilities = []) { 550 602 if (!function_exists('curl_init') || !function_exists('curl_exec')) { 551 603 return false; … … 553 605 554 606 // If needed, check that our installed curl version supports SSL 555 if (isset($capabilities[ 'ssl']) && $capabilities['ssl']) {607 if (isset($capabilities[Capability::SSL]) && $capabilities[Capability::SSL]) { 556 608 $curl_version = curl_version(); 557 609 if (!(CURL_VERSION_SSL & $curl_version['features'])) { … … 569 621 * @return string The "Expect" header. 570 622 */ 571 pr otectedfunction get_expect_header($data) {623 private function get_expect_header($data) { 572 624 if (!is_array($data)) { 573 625 return strlen((string) $data) >= 1048576 ? '100-Continue' : '';
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)