Changeset 52244
- Timestamp:
- 11/25/2021 01:10:30 AM (3 years ago)
- Location:
- trunk
- Files:
-
- 7 added
- 33 deleted
- 26 edited
- 34 copied
- 11 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/Requests/Auth.php
r50842 r52244 3 3 * Authentication provider interface 4 4 * 5 * @package Requests 6 * @subpackage Authentication 5 * @package Requests\Authentication 7 6 */ 7 8 namespace WpOrg\Requests; 9 10 use WpOrg\Requests\Hooks; 8 11 9 12 /** … … 15 18 * makes it much easier for users to use your provider. 16 19 * 17 * @see Requests_Hooks18 * @package Requests19 * @ subpackageAuthentication20 * @see \WpOrg\Requests\Hooks 21 * 22 * @package Requests\Authentication 20 23 */ 21 interface Requests_Auth {24 interface Auth { 22 25 /** 23 26 * Register hooks as needed 24 27 * 25 * This method is called in {@see Requests::request} when the user has set26 * an instance as the 'auth' option. Use this callback to register all the28 * This method is called in {@see \WpOrg\Requests\Requests::request()} when the user 29 * has set an instance as the 'auth' option. Use this callback to register all the 27 30 * hooks you'll need. 28 31 * 29 * @see Requests_Hooks::register30 * @param Requests_Hooks $hooks Hook system32 * @see \WpOrg\Requests\Hooks::register() 33 * @param \WpOrg\Requests\Hooks $hooks Hook system 31 34 */ 32 public function register( Requests_Hooks $hooks);35 public function register(Hooks $hooks); 33 36 } -
trunk/src/wp-includes/Requests/Auth/Basic.php
r50842 r52244 3 3 * Basic Authentication provider 4 4 * 5 * @package Requests 6 * @subpackage Authentication 5 * @package Requests\Authentication 7 6 */ 7 8 namespace WpOrg\Requests\Auth; 9 10 use WpOrg\Requests\Auth; 11 use WpOrg\Requests\Exception\ArgumentCount; 12 use WpOrg\Requests\Exception\InvalidArgument; 13 use WpOrg\Requests\Hooks; 8 14 9 15 /** … … 13 19 * header. 14 20 * 15 * @package Requests 16 * @subpackage Authentication 21 * @package Requests\Authentication 17 22 */ 18 class Requests_Auth_Basic implements Requests_Auth {23 class Basic implements Auth { 19 24 /** 20 25 * Username … … 34 39 * Constructor 35 40 * 36 * @throws Requests_Exception On incorrect number of arguments (`authbasicbadargs`) 41 * @since 2.0 Throws an `InvalidArgument` exception. 42 * @since 2.0 Throws an `ArgumentCount` exception instead of the Requests base `Exception. 43 * 37 44 * @param array|null $args Array of user and password. Must have exactly two elements 45 * 46 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not an array or null. 47 * @throws \WpOrg\Requests\Exception\ArgumentCount On incorrect number of array elements (`authbasicbadargs`). 38 48 */ 39 49 public function __construct($args = null) { 40 50 if (is_array($args)) { 41 51 if (count($args) !== 2) { 42 throw new Requests_Exception('Invalid number of arguments', 'authbasicbadargs');52 throw ArgumentCount::create('an array with exactly two elements', count($args), 'authbasicbadargs'); 43 53 } 44 54 45 55 list($this->user, $this->pass) = $args; 56 return; 57 } 58 59 if ($args !== null) { 60 throw InvalidArgument::create(1, '$args', 'array|null', gettype($args)); 46 61 } 47 62 } … … 50 65 * Register the necessary callbacks 51 66 * 52 * @see curl_before_send53 * @see fsockopen_header54 * @param Requests_Hooks $hooks Hook system67 * @see \WpOrg\Requests\Auth\Basic::curl_before_send() 68 * @see \WpOrg\Requests\Auth\Basic::fsockopen_header() 69 * @param \WpOrg\Requests\Hooks $hooks Hook system 55 70 */ 56 public function register( Requests_Hooks $hooks) {57 $hooks->register('curl.before_send', array($this, 'curl_before_send'));58 $hooks->register('fsockopen.after_headers', array($this, 'fsockopen_header'));71 public function register(Hooks $hooks) { 72 $hooks->register('curl.before_send', [$this, 'curl_before_send']); 73 $hooks->register('fsockopen.after_headers', [$this, 'fsockopen_header']); 59 74 } 60 75 … … 62 77 * Set cURL parameters before the data is sent 63 78 * 64 * @param resource $handle cURL resource79 * @param resource|\CurlHandle $handle cURL handle 65 80 */ 66 81 public function curl_before_send(&$handle) { -
trunk/src/wp-includes/Requests/Cookie.php
r50842 r52244 3 3 * Cookie storage object 4 4 * 5 * @package Requests 6 * @subpackage Cookies 5 * @package Requests\Cookies 7 6 */ 7 8 namespace WpOrg\Requests; 9 10 use WpOrg\Requests\Exception\InvalidArgument; 11 use WpOrg\Requests\Iri; 12 use WpOrg\Requests\Response\Headers; 13 use WpOrg\Requests\Utility\CaseInsensitiveDictionary; 14 use WpOrg\Requests\Utility\InputValidator; 8 15 9 16 /** 10 17 * Cookie storage object 11 18 * 12 * @package Requests 13 * @subpackage Cookies 19 * @package Requests\Cookies 14 20 */ 15 class Requests_Cookie {21 class Cookie { 16 22 /** 17 23 * Cookie name. … … 34 40 * httponly. 35 41 * 36 * @var Requests_Utility_CaseInsensitiveDictionary|array Array-like object37 */ 38 public $attributes = array();42 * @var \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array Array-like object 43 */ 44 public $attributes = []; 39 45 40 46 /** … … 46 52 * @var array 47 53 */ 48 public $flags = array();54 public $flags = []; 49 55 50 56 /** … … 63 69 * @param string $name 64 70 * @param string $value 65 * @param array|Requests_Utility_CaseInsensitiveDictionary $attributes Associative array of attribute data 66 */ 67 public function __construct($name, $value, $attributes = array(), $flags = array(), $reference_time = null) { 71 * @param array|\WpOrg\Requests\Utility\CaseInsensitiveDictionary $attributes Associative array of attribute data 72 * @param array $flags 73 * @param int|null $reference_time 74 * 75 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $name argument is not a string. 76 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $value argument is not a string. 77 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $attributes argument is not an array or iterable object with array access. 78 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $flags argument is not an array. 79 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $reference_time argument is not an integer or null. 80 */ 81 public function __construct($name, $value, $attributes = [], $flags = [], $reference_time = null) { 82 if (is_string($name) === false) { 83 throw InvalidArgument::create(1, '$name', 'string', gettype($name)); 84 } 85 86 if (is_string($value) === false) { 87 throw InvalidArgument::create(2, '$value', 'string', gettype($value)); 88 } 89 90 if (InputValidator::has_array_access($attributes) === false || InputValidator::is_iterable($attributes) === false) { 91 throw InvalidArgument::create(3, '$attributes', 'array|ArrayAccess&Traversable', gettype($attributes)); 92 } 93 94 if (is_array($flags) === false) { 95 throw InvalidArgument::create(4, '$flags', 'array', gettype($flags)); 96 } 97 98 if ($reference_time !== null && is_int($reference_time) === false) { 99 throw InvalidArgument::create(5, '$reference_time', 'integer|null', gettype($reference_time)); 100 } 101 68 102 $this->name = $name; 69 103 $this->value = $value; 70 104 $this->attributes = $attributes; 71 $default_flags = array(105 $default_flags = [ 72 106 'creation' => time(), 73 107 'last-access' => time(), 74 108 'persistent' => false, 75 109 'host-only' => true, 76 );110 ]; 77 111 $this->flags = array_merge($default_flags, $flags); 78 112 … … 83 117 84 118 $this->normalize(); 119 } 120 121 /** 122 * Get the cookie value 123 * 124 * Attributes and other data can be accessed via methods. 125 */ 126 public function __toString() { 127 return $this->value; 85 128 } 86 129 … … 114 157 * Check if a cookie is valid for a given URI 115 158 * 116 * @param Requests_IRI$uri URI to check159 * @param \WpOrg\Requests\Iri $uri URI to check 117 160 * @return boolean Whether the cookie is valid for the given URI 118 161 */ 119 public function uri_matches( Requests_IRI$uri) {162 public function uri_matches(Iri $uri) { 120 163 if (!$this->domain_matches($uri->host)) { 121 164 return false; … … 132 175 * Check if a cookie is valid for a given domain 133 176 * 134 * @param string $ stringDomain to check177 * @param string $domain Domain to check 135 178 * @return boolean Whether the cookie is valid for the given domain 136 179 */ 137 public function domain_matches($string) { 180 public function domain_matches($domain) { 181 if (is_string($domain) === false) { 182 return false; 183 } 184 138 185 if (!isset($this->attributes['domain'])) { 139 186 // Cookies created manually; cookies created by Requests will set … … 142 189 } 143 190 144 $ domain_string= $this->attributes['domain'];145 if ($ domain_string === $string) {146 // The domain string and the stringare identical.191 $cookie_domain = $this->attributes['domain']; 192 if ($cookie_domain === $domain) { 193 // The cookie domain and the passed domain are identical. 147 194 return true; 148 195 } … … 154 201 } 155 202 156 if (strlen($ string) <= strlen($domain_string)) {157 // For obvious reasons, the string cannot be a suffix if thedomain158 // is shorter than the domain string159 return false; 160 } 161 162 if (substr($ string, -1 * strlen($domain_string)) !== $domain_string) {163 // The domain string should be a suffix of the string.164 return false; 165 } 166 167 $prefix = substr($ string, 0, strlen($string) - strlen($domain_string));203 if (strlen($domain) <= strlen($cookie_domain)) { 204 // For obvious reasons, the cookie domain cannot be a suffix if the passed domain 205 // is shorter than the cookie domain 206 return false; 207 } 208 209 if (substr($domain, -1 * strlen($cookie_domain)) !== $cookie_domain) { 210 // The cookie domain should be a suffix of the passed domain. 211 return false; 212 } 213 214 $prefix = substr($domain, 0, strlen($domain) - strlen($cookie_domain)); 168 215 if (substr($prefix, -1) !== '.') { 169 // The last character of the stringthat is not included in the216 // The last character of the passed domain that is not included in the 170 217 // domain string should be a %x2E (".") character. 171 218 return false; 172 219 } 173 220 174 // The stringshould be a host name (i.e., not an IP address).175 return !preg_match('#^(.+\.)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $ string);221 // The passed domain should be a host name (i.e., not an IP address). 222 return !preg_match('#^(.+\.)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $domain); 176 223 } 177 224 … … 194 241 // the path to the requested path 195 242 return true; 243 } 244 245 if (is_scalar($request_path) === false) { 246 return false; 196 247 } 197 248 … … 318 369 319 370 /** 320 * Format a cookie for a Cookie header321 *322 * @codeCoverageIgnore323 * @deprecated Use {@see Requests_Cookie::format_for_header}324 * @return string325 */326 public function formatForHeader() {327 return $this->format_for_header();328 }329 330 /**331 371 * Format a cookie for a Set-Cookie header 332 372 * … … 339 379 $header_value = $this->format_for_header(); 340 380 if (!empty($this->attributes)) { 341 $parts = array();381 $parts = []; 342 382 foreach ($this->attributes as $key => $value) { 343 383 // Ignore non-associative attributes … … 356 396 357 397 /** 358 * Format a cookie for a Set-Cookie header359 *360 * @codeCoverageIgnore361 * @deprecated Use {@see Requests_Cookie::format_for_set_cookie}362 * @return string363 */364 public function formatForSetCookie() {365 return $this->format_for_set_cookie();366 }367 368 /**369 * Get the cookie value370 *371 * Attributes and other data can be accessed via methods.372 */373 public function __toString() {374 return $this->value;375 }376 377 /**378 398 * Parse a cookie string into a cookie object 379 399 * … … 382 402 * specifies some of this handling, but not in a thorough manner. 383 403 * 384 * @param string Cookie header value (from a Set-Cookie header) 385 * @return Requests_Cookie Parsed cookie object 386 */ 387 public static function parse($string, $name = '', $reference_time = null) { 388 $parts = explode(';', $string); 404 * @param string $cookie_header Cookie header value (from a Set-Cookie header) 405 * @param string $name 406 * @param int|null $reference_time 407 * @return \WpOrg\Requests\Cookie Parsed cookie object 408 * 409 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $cookie_header argument is not a string. 410 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $name argument is not a string. 411 */ 412 public static function parse($cookie_header, $name = '', $reference_time = null) { 413 if (is_string($cookie_header) === false) { 414 throw InvalidArgument::create(1, '$cookie_header', 'string', gettype($cookie_header)); 415 } 416 417 if (is_string($name) === false) { 418 throw InvalidArgument::create(2, '$name', 'string', gettype($name)); 419 } 420 421 $parts = explode(';', $cookie_header); 389 422 $kvparts = array_shift($parts); 390 423 391 424 if (!empty($name)) { 392 $value = $ string;425 $value = $cookie_header; 393 426 } 394 427 elseif (strpos($kvparts, '=') === false) { … … 407 440 $value = trim($value); 408 441 409 // Attribute key are handled case-insensitively410 $attributes = new Requests_Utility_CaseInsensitiveDictionary();442 // Attribute keys are handled case-insensitively 443 $attributes = new CaseInsensitiveDictionary(); 411 444 412 445 if (!empty($parts)) { … … 426 459 } 427 460 428 return new Requests_Cookie($name, $value, $attributes, array(), $reference_time);461 return new static($name, $value, $attributes, [], $reference_time); 429 462 } 430 463 … … 432 465 * Parse all Set-Cookie headers from request headers 433 466 * 434 * @param Requests_Response_Headers $headers Headers to parse from435 * @param Requests_IRI|null $origin URI for comparing cookie origins467 * @param \WpOrg\Requests\Response\Headers $headers Headers to parse from 468 * @param \WpOrg\Requests\Iri|null $origin URI for comparing cookie origins 436 469 * @param int|null $time Reference time for expiration calculation 437 470 * @return array 438 471 */ 439 public static function parse_from_headers( Requests_Response_Headers $headers, Requests_IRI$origin = null, $time = null) {472 public static function parse_from_headers(Headers $headers, Iri $origin = null, $time = null) { 440 473 $cookie_headers = $headers->getValues('Set-Cookie'); 441 474 if (empty($cookie_headers)) { 442 return array();443 } 444 445 $cookies = array();475 return []; 476 } 477 478 $cookies = []; 446 479 foreach ($cookie_headers as $header) { 447 480 $parsed = self::parse($header, '', $time); … … 492 525 return $cookies; 493 526 } 494 495 /**496 * Parse all Set-Cookie headers from request headers497 *498 * @codeCoverageIgnore499 * @deprecated Use {@see Requests_Cookie::parse_from_headers}500 * @return array501 */502 public static function parseFromHeaders(Requests_Response_Headers $headers) {503 return self::parse_from_headers($headers);504 }505 527 } -
trunk/src/wp-includes/Requests/Cookie/Jar.php
r50842 r52244 3 3 * Cookie holder object 4 4 * 5 * @package Requests 6 * @subpackage Cookies 5 * @package Requests\Cookies 7 6 */ 7 8 namespace WpOrg\Requests\Cookie; 9 10 use ArrayAccess; 11 use ArrayIterator; 12 use IteratorAggregate; 13 use ReturnTypeWillChange; 14 use WpOrg\Requests\Cookie; 15 use WpOrg\Requests\Exception; 16 use WpOrg\Requests\Exception\InvalidArgument; 17 use WpOrg\Requests\HookManager; 18 use WpOrg\Requests\Iri; 19 use WpOrg\Requests\Response; 8 20 9 21 /** 10 22 * Cookie holder object 11 23 * 12 * @package Requests 13 * @subpackage Cookies 24 * @package Requests\Cookies 14 25 */ 15 class Requests_Cookie_Jar implements ArrayAccess, IteratorAggregate {26 class Jar implements ArrayAccess, IteratorAggregate { 16 27 /** 17 28 * Actual item data … … 19 30 * @var array 20 31 */ 21 protected $cookies = array();32 protected $cookies = []; 22 33 23 34 /** … … 25 36 * 26 37 * @param array $cookies Existing cookie values 38 * 39 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not an array. 27 40 */ 28 public function __construct($cookies = array()) { 41 public function __construct($cookies = []) { 42 if (is_array($cookies) === false) { 43 throw InvalidArgument::create(1, '$cookies', 'array', gettype($cookies)); 44 } 45 29 46 $this->cookies = $cookies; 30 47 } 31 48 32 49 /** 33 * Normalise cookie data into a Requests_Cookie50 * Normalise cookie data into a \WpOrg\Requests\Cookie 34 51 * 35 * @param string| Requests_Cookie $cookie36 * @return Requests_Cookie52 * @param string|\WpOrg\Requests\Cookie $cookie 53 * @return \WpOrg\Requests\Cookie 37 54 */ 38 public function normalize_cookie($cookie, $key = null) {39 if ($cookie instanceof Requests_Cookie) {55 public function normalize_cookie($cookie, $key = '') { 56 if ($cookie instanceof Cookie) { 40 57 return $cookie; 41 58 } 42 59 43 return Requests_Cookie::parse($cookie, $key); 44 } 45 46 /** 47 * Normalise cookie data into a Requests_Cookie 48 * 49 * @codeCoverageIgnore 50 * @deprecated Use {@see Requests_Cookie_Jar::normalize_cookie} 51 * @return Requests_Cookie 52 */ 53 public function normalizeCookie($cookie, $key = null) { 54 return $this->normalize_cookie($cookie, $key); 60 return Cookie::parse($cookie, $key); 55 61 } 56 62 … … 58 64 * Check if the given item exists 59 65 * 60 * @param string $ keyItem key66 * @param string $offset Item key 61 67 * @return boolean Does the item exist? 62 68 */ 63 public function offsetExists($key) { 64 return isset($this->cookies[$key]); 69 #[ReturnTypeWillChange] 70 public function offsetExists($offset) { 71 return isset($this->cookies[$offset]); 65 72 } 66 73 … … 68 75 * Get the value for the item 69 76 * 70 * @param string $ keyItem key77 * @param string $offset Item key 71 78 * @return string|null Item value (null if offsetExists is false) 72 79 */ 73 public function offsetGet($key) { 74 if (!isset($this->cookies[$key])) { 80 #[ReturnTypeWillChange] 81 public function offsetGet($offset) { 82 if (!isset($this->cookies[$offset])) { 75 83 return null; 76 84 } 77 85 78 return $this->cookies[$ key];86 return $this->cookies[$offset]; 79 87 } 80 88 … … 82 90 * Set the given item 83 91 * 84 * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`) 92 * @param string $offset Item name 93 * @param string $value Item value 85 94 * 86 * @param string $key Item name 87 * @param string $value Item value 95 * @throws \WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`) 88 96 */ 89 public function offsetSet($key, $value) { 90 if ($key === null) { 91 throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset'); 97 #[ReturnTypeWillChange] 98 public function offsetSet($offset, $value) { 99 if ($offset === null) { 100 throw new Exception('Object is a dictionary, not a list', 'invalidset'); 92 101 } 93 102 94 $this->cookies[$ key] = $value;103 $this->cookies[$offset] = $value; 95 104 } 96 105 … … 98 107 * Unset the given header 99 108 * 100 * @param string $ key109 * @param string $offset 101 110 */ 102 public function offsetUnset($key) { 103 unset($this->cookies[$key]); 111 #[ReturnTypeWillChange] 112 public function offsetUnset($offset) { 113 unset($this->cookies[$offset]); 104 114 } 105 115 … … 107 117 * Get an iterator for the data 108 118 * 109 * @return ArrayIterator119 * @return \ArrayIterator 110 120 */ 121 #[ReturnTypeWillChange] 111 122 public function getIterator() { 112 123 return new ArrayIterator($this->cookies); … … 116 127 * Register the cookie handler with the request's hooking system 117 128 * 118 * @param Requests_Hooker $hooks Hooking system129 * @param \WpOrg\Requests\HookManager $hooks Hooking system 119 130 */ 120 public function register( Requests_Hooker $hooks) {121 $hooks->register('requests.before_request', array($this, 'before_request'));122 $hooks->register('requests.before_redirect_check', array($this, 'before_redirect_check'));131 public function register(HookManager $hooks) { 132 $hooks->register('requests.before_request', [$this, 'before_request']); 133 $hooks->register('requests.before_redirect_check', [$this, 'before_redirect_check']); 123 134 } 124 135 … … 135 146 */ 136 147 public function before_request($url, &$headers, &$data, &$type, &$options) { 137 if (!$url instanceof Requests_IRI) {138 $url = new Requests_IRI($url);148 if (!$url instanceof Iri) { 149 $url = new Iri($url); 139 150 } 140 151 141 152 if (!empty($this->cookies)) { 142 $cookies = array();153 $cookies = []; 143 154 foreach ($this->cookies as $key => $cookie) { 144 155 $cookie = $this->normalize_cookie($cookie, $key); … … 161 172 * Parse all cookies from a response and attach them to the response 162 173 * 163 * @ var Requests_Response $response174 * @param \WpOrg\Requests\Response $response 164 175 */ 165 public function before_redirect_check(Re quests_Response $return) {166 $url = $re turn->url;167 if (!$url instanceof Requests_IRI) {168 $url = new Requests_IRI($url);176 public function before_redirect_check(Response $response) { 177 $url = $response->url; 178 if (!$url instanceof Iri) { 179 $url = new Iri($url); 169 180 } 170 181 171 $cookies = Requests_Cookie::parse_from_headers($return->headers, $url);172 $this->cookies = array_merge($this->cookies, $cookies);173 $re turn->cookies = $this;182 $cookies = Cookie::parse_from_headers($response->headers, $url); 183 $this->cookies = array_merge($this->cookies, $cookies); 184 $response->cookies = $this; 174 185 } 175 186 } -
trunk/src/wp-includes/Requests/Exception.php
r50842 r52244 3 3 * Exception for HTTP requests 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests; 9 10 use Exception as PHPException; 7 11 8 12 /** 9 13 * Exception for HTTP requests 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception extendsException {17 class Exception extends PHPException { 14 18 /** 15 19 * Type of exception … … 42 46 43 47 /** 44 * Like {@see getCode()}, but a string code.48 * Like {@see \Exception::getCode()}, but a string code. 45 49 * 46 50 * @codeCoverageIgnore -
trunk/src/wp-includes/Requests/Exception/Http.php
r52243 r52244 3 3 * Exception based on HTTP response 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception; 9 10 use WpOrg\Requests\Exception; 11 use WpOrg\Requests\Exception\Http\StatusUnknown; 7 12 8 13 /** 9 14 * Exception based on HTTP response 10 15 * 11 * @package Requests 16 * @package Requests\Exceptions 12 17 */ 13 class Requests_Exception_HTTP extends Requests_Exception {18 class Http extends Exception { 14 19 /** 15 20 * HTTP status code … … 45 50 46 51 /** 47 * Get the status message 52 * Get the status message. 53 * 54 * @return string 48 55 */ 49 56 public function getReason() { … … 59 66 public static function get_class($code) { 60 67 if (!$code) { 61 return 'Requests_Exception_HTTP_Unknown';68 return StatusUnknown::class; 62 69 } 63 70 64 $class = sprintf(' Requests_Exception_HTTP_%d', $code);71 $class = sprintf('\WpOrg\Requests\Exception\Http\Status%d', $code); 65 72 if (class_exists($class)) { 66 73 return $class; 67 74 } 68 75 69 return 'Requests_Exception_HTTP_Unknown';76 return StatusUnknown::class; 70 77 } 71 78 } -
trunk/src/wp-includes/Requests/Exception/Http/Status304.php
r52243 r52244 3 3 * Exception for 304 Not Modified responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 304 Not Modified responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_304 extends Requests_Exception_HTTP{17 final class Status304 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status305.php
r52243 r52244 3 3 * Exception for 305 Use Proxy responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 305 Use Proxy responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_305 extends Requests_Exception_HTTP{17 final class Status305 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status306.php
r52243 r52244 3 3 * Exception for 306 Switch Proxy responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 306 Switch Proxy responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_306 extends Requests_Exception_HTTP{17 final class Status306 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status400.php
r52243 r52244 3 3 * Exception for 400 Bad Request responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 400 Bad Request responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_400 extends Requests_Exception_HTTP{17 final class Status400 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status401.php
r52243 r52244 3 3 * Exception for 401 Unauthorized responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 401 Unauthorized responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_401 extends Requests_Exception_HTTP{17 final class Status401 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status402.php
r52243 r52244 3 3 * Exception for 402 Payment Required responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 402 Payment Required responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_402 extends Requests_Exception_HTTP{17 final class Status402 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status403.php
r52243 r52244 3 3 * Exception for 403 Forbidden responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 403 Forbidden responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_403 extends Requests_Exception_HTTP{17 final class Status403 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status404.php
r52243 r52244 3 3 * Exception for 404 Not Found responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 404 Not Found responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_404 extends Requests_Exception_HTTP{17 final class Status404 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status405.php
r52243 r52244 3 3 * Exception for 405 Method Not Allowed responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 405 Method Not Allowed responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_405 extends Requests_Exception_HTTP{17 final class Status405 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status406.php
r52243 r52244 3 3 * Exception for 406 Not Acceptable responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 406 Not Acceptable responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_406 extends Requests_Exception_HTTP{17 final class Status406 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status407.php
r52243 r52244 3 3 * Exception for 407 Proxy Authentication Required responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 407 Proxy Authentication Required responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_407 extends Requests_Exception_HTTP{17 final class Status407 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status408.php
r52243 r52244 3 3 * Exception for 408 Request Timeout responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 408 Request Timeout responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_408 extends Requests_Exception_HTTP{17 final class Status408 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status409.php
r52243 r52244 3 3 * Exception for 409 Conflict responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 409 Conflict responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_409 extends Requests_Exception_HTTP{17 final class Status409 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status410.php
r52243 r52244 3 3 * Exception for 410 Gone responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 410 Gone responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_410 extends Requests_Exception_HTTP{17 final class Status410 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status411.php
r52243 r52244 3 3 * Exception for 411 Length Required responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 411 Length Required responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_411 extends Requests_Exception_HTTP{17 final class Status411 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status412.php
r52243 r52244 3 3 * Exception for 412 Precondition Failed responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 412 Precondition Failed responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_412 extends Requests_Exception_HTTP{17 final class Status412 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status413.php
r52243 r52244 3 3 * Exception for 413 Request Entity Too Large responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 413 Request Entity Too Large responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_413 extends Requests_Exception_HTTP{17 final class Status413 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status414.php
r52243 r52244 3 3 * Exception for 414 Request-URI Too Large responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 414 Request-URI Too Large responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_414 extends Requests_Exception_HTTP{17 final class Status414 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status415.php
r52243 r52244 3 3 * Exception for 415 Unsupported Media Type responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 415 Unsupported Media Type responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_415 extends Requests_Exception_HTTP{17 final class Status415 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status416.php
r52243 r52244 3 3 * Exception for 416 Requested Range Not Satisfiable responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 416 Requested Range Not Satisfiable responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_416 extends Requests_Exception_HTTP{17 final class Status416 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status417.php
r52243 r52244 3 3 * Exception for 417 Expectation Failed responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 417 Expectation Failed responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_417 extends Requests_Exception_HTTP{17 final class Status417 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status418.php
r52243 r52244 3 3 * Exception for 418 I'm A Teapot responses 4 4 * 5 * @see https://tools.ietf.org/html/rfc2324 6 * @package Requests 5 * @link https://tools.ietf.org/html/rfc2324 6 * 7 * @package Requests\Exceptions 7 8 */ 9 10 namespace WpOrg\Requests\Exception\Http; 11 12 use WpOrg\Requests\Exception\Http; 8 13 9 14 /** 10 15 * Exception for 418 I'm A Teapot responses 11 16 * 12 * @see https://tools.ietf.org/html/rfc2324 13 * @package Requests 17 * @link https://tools.ietf.org/html/rfc2324 18 * 19 * @package Requests\Exceptions 14 20 */ 15 class Requests_Exception_HTTP_418 extends Requests_Exception_HTTP{21 final class Status418 extends Http { 16 22 /** 17 23 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status428.php
r52243 r52244 3 3 * Exception for 428 Precondition Required responses 4 4 * 5 * @see https://tools.ietf.org/html/rfc6585 6 * @package Requests 5 * @link https://tools.ietf.org/html/rfc6585 6 * 7 * @package Requests\Exceptions 7 8 */ 9 10 namespace WpOrg\Requests\Exception\Http; 11 12 use WpOrg\Requests\Exception\Http; 8 13 9 14 /** 10 15 * Exception for 428 Precondition Required responses 11 16 * 12 * @see https://tools.ietf.org/html/rfc6585 13 * @package Requests 17 * @link https://tools.ietf.org/html/rfc6585 18 * 19 * @package Requests\Exceptions 14 20 */ 15 class Requests_Exception_HTTP_428 extends Requests_Exception_HTTP{21 final class Status428 extends Http { 16 22 /** 17 23 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status429.php
r52243 r52244 3 3 * Exception for 429 Too Many Requests responses 4 4 * 5 * @see https://tools.ietf.org/html/draft-nottingham-http-new-status-04 6 * @package Requests 5 * @link https://tools.ietf.org/html/draft-nottingham-http-new-status-04 6 * 7 * @package Requests\Exceptions 7 8 */ 9 10 namespace WpOrg\Requests\Exception\Http; 11 12 use WpOrg\Requests\Exception\Http; 8 13 9 14 /** 10 15 * Exception for 429 Too Many Requests responses 11 16 * 12 * @see https://tools.ietf.org/html/draft-nottingham-http-new-status-04 13 * @package Requests 17 * @link https://tools.ietf.org/html/draft-nottingham-http-new-status-04 18 * 19 * @package Requests\Exceptions 14 20 */ 15 class Requests_Exception_HTTP_429 extends Requests_Exception_HTTP{21 final class Status429 extends Http { 16 22 /** 17 23 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status431.php
r52243 r52244 3 3 * Exception for 431 Request Header Fields Too Large responses 4 4 * 5 * @see https://tools.ietf.org/html/rfc6585 6 * @package Requests 5 * @link https://tools.ietf.org/html/rfc6585 6 * 7 * @package Requests\Exceptions 7 8 */ 9 10 namespace WpOrg\Requests\Exception\Http; 11 12 use WpOrg\Requests\Exception\Http; 8 13 9 14 /** 10 15 * Exception for 431 Request Header Fields Too Large responses 11 16 * 12 * @see https://tools.ietf.org/html/rfc6585 13 * @package Requests 17 * @link https://tools.ietf.org/html/rfc6585 18 * 19 * @package Requests\Exceptions 14 20 */ 15 class Requests_Exception_HTTP_431 extends Requests_Exception_HTTP{21 final class Status431 extends Http { 16 22 /** 17 23 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status500.php
r52243 r52244 3 3 * Exception for 500 Internal Server Error responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 500 Internal Server Error responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_500 extends Requests_Exception_HTTP{17 final class Status500 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status501.php
r52243 r52244 3 3 * Exception for 501 Not Implemented responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 501 Not Implemented responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_501 extends Requests_Exception_HTTP{17 final class Status501 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status502.php
r52243 r52244 3 3 * Exception for 502 Bad Gateway responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 502 Bad Gateway responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_502 extends Requests_Exception_HTTP{17 final class Status502 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status503.php
r52243 r52244 3 3 * Exception for 503 Service Unavailable responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 503 Service Unavailable responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_503 extends Requests_Exception_HTTP{17 final class Status503 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status504.php
r52243 r52244 3 3 * Exception for 504 Gateway Timeout responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 504 Gateway Timeout responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_504 extends Requests_Exception_HTTP{17 final class Status504 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status505.php
r52243 r52244 3 3 * Exception for 505 HTTP Version Not Supported responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 7 11 8 12 /** 9 13 * Exception for 505 HTTP Version Not Supported responses 10 14 * 11 * @package Requests 15 * @package Requests\Exceptions 12 16 */ 13 class Requests_Exception_HTTP_505 extends Requests_Exception_HTTP{17 final class Status505 extends Http { 14 18 /** 15 19 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/Status511.php
r52243 r52244 3 3 * Exception for 511 Network Authentication Required responses 4 4 * 5 * @see https://tools.ietf.org/html/rfc6585 6 * @package Requests 5 * @link https://tools.ietf.org/html/rfc6585 6 * 7 * @package Requests\Exceptions 7 8 */ 9 10 namespace WpOrg\Requests\Exception\Http; 11 12 use WpOrg\Requests\Exception\Http; 8 13 9 14 /** 10 15 * Exception for 511 Network Authentication Required responses 11 16 * 12 * @see https://tools.ietf.org/html/rfc6585 13 * @package Requests 17 * @link https://tools.ietf.org/html/rfc6585 18 * 19 * @package Requests\Exceptions 14 20 */ 15 class Requests_Exception_HTTP_511 extends Requests_Exception_HTTP{21 final class Status511 extends Http { 16 22 /** 17 23 * HTTP status code -
trunk/src/wp-includes/Requests/Exception/Http/StatusUnknown.php
r52243 r52244 3 3 * Exception for unknown status responses 4 4 * 5 * @package Requests 5 * @package Requests\Exceptions 6 6 */ 7 8 namespace WpOrg\Requests\Exception\Http; 9 10 use WpOrg\Requests\Exception\Http; 11 use WpOrg\Requests\Response; 7 12 8 13 /** 9 14 * Exception for unknown status responses 10 15 * 11 * @package Requests 16 * @package Requests\Exceptions 12 17 */ 13 class Requests_Exception_HTTP_Unknown extends Requests_Exception_HTTP{18 final class StatusUnknown extends Http { 14 19 /** 15 20 * HTTP status code … … 29 34 * Create a new exception 30 35 * 31 * If `$data` is an instance of {@see Requests_Response}, uses the status36 * If `$data` is an instance of {@see \WpOrg\Requests\Response}, uses the status 32 37 * code from it. Otherwise, sets as 0 33 38 * … … 36 41 */ 37 42 public function __construct($reason = null, $data = null) { 38 if ($data instanceof Re quests_Response) {39 $this->code = $data->status_code;43 if ($data instanceof Response) { 44 $this->code = (int) $data->status_code; 40 45 } 41 46 -
trunk/src/wp-includes/Requests/Exception/Transport.php
r46586 r52244 1 1 <?php 2 /** 3 * Transport Exception 4 * 5 * @package Requests\Exceptions 6 */ 2 7 3 class Requests_Exception_Transport extends Requests_Exception { 8 namespace WpOrg\Requests\Exception; 4 9 5 } 10 use WpOrg\Requests\Exception; 11 12 /** 13 * Transport Exception 14 * 15 * @package Requests\Exceptions 16 */ 17 class Transport extends Exception {} -
trunk/src/wp-includes/Requests/Exception/Transport/Curl.php
r52243 r52244 1 1 <?php 2 /** 3 * CURL Transport Exception. 4 * 5 * @package Requests\Exceptions 6 */ 2 7 3 class Requests_Exception_Transport_cURL extends Requests_Exception_Transport { 8 namespace WpOrg\Requests\Exception\Transport; 9 10 use WpOrg\Requests\Exception\Transport; 11 12 /** 13 * CURL Transport Exception. 14 * 15 * @package Requests\Exceptions 16 */ 17 final class Curl extends Transport { 4 18 5 19 const EASY = 'cURLEasy'; … … 30 44 protected $reason = 'Unknown'; 31 45 46 /** 47 * Create a new exception. 48 * 49 * @param string $message Exception message. 50 * @param string $type Exception type. 51 * @param mixed $data Associated data, if applicable. 52 * @param int $code Exception numerical code, if applicable. 53 */ 32 54 public function __construct($message, $type, $data = null, $code = 0) { 33 55 if ($type !== null) { … … 36 58 37 59 if ($code !== null) { 38 $this->code = $code;60 $this->code = (int) $code; 39 61 } 40 62 … … 48 70 49 71 /** 50 * Get the error message 72 * Get the error message. 73 * 74 * @return string 51 75 */ 52 76 public function getReason() { -
trunk/src/wp-includes/Requests/HookManager.php
r52243 r52244 3 3 * Event dispatcher 4 4 * 5 * @package Requests 6 * @subpackage Utilities 5 * @package Requests\EventDispatcher 7 6 */ 7 8 namespace WpOrg\Requests; 8 9 9 10 /** 10 11 * Event dispatcher 11 12 * 12 * @package Requests 13 * @subpackage Utilities 13 * @package Requests\EventDispatcher 14 14 */ 15 interface Requests_Hooker {15 interface HookManager { 16 16 /** 17 17 * Register a callback for a hook 18 18 * 19 19 * @param string $hook Hook name 20 * @param call back$callback Function/method to call on event20 * @param callable $callback Function/method to call on event 21 21 * @param int $priority Priority number. <0 is executed earlier, >0 is executed later 22 22 */ … … 30 30 * @return boolean Successfulness 31 31 */ 32 public function dispatch($hook, $parameters = array());32 public function dispatch($hook, $parameters = []); 33 33 } -
trunk/src/wp-includes/Requests/Hooks.php
r50842 r52244 3 3 * Handles adding and dispatching events 4 4 * 5 * @package Requests 6 * @subpackage Utilities 5 * @package Requests\EventDispatcher 7 6 */ 7 8 namespace WpOrg\Requests; 9 10 use WpOrg\Requests\Exception\InvalidArgument; 11 use WpOrg\Requests\HookManager; 12 use WpOrg\Requests\Utility\InputValidator; 8 13 9 14 /** 10 15 * Handles adding and dispatching events 11 16 * 12 * @package Requests 13 * @subpackage Utilities 17 * @package Requests\EventDispatcher 14 18 */ 15 class Requests_Hooks implements Requests_Hooker {19 class Hooks implements HookManager { 16 20 /** 17 21 * Registered callbacks for each hook … … 19 23 * @var array 20 24 */ 21 protected $hooks = array(); 22 23 /** 24 * Constructor 25 */ 26 public function __construct() { 27 // pass 28 } 25 protected $hooks = []; 29 26 30 27 /** … … 32 29 * 33 30 * @param string $hook Hook name 34 * @param call back$callback Function/method to call on event31 * @param callable $callback Function/method to call on event 35 32 * @param int $priority Priority number. <0 is executed earlier, >0 is executed later 33 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $hook argument is not a string. 34 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $callback argument is not callable. 35 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $priority argument is not an integer. 36 36 */ 37 37 public function register($hook, $callback, $priority = 0) { 38 if (is_string($hook) === false) { 39 throw InvalidArgument::create(1, '$hook', 'string', gettype($hook)); 40 } 41 42 if (is_callable($callback) === false) { 43 throw InvalidArgument::create(2, '$callback', 'callable', gettype($callback)); 44 } 45 46 if (InputValidator::is_numeric_array_key($priority) === false) { 47 throw InvalidArgument::create(3, '$priority', 'integer', gettype($priority)); 48 } 49 38 50 if (!isset($this->hooks[$hook])) { 39 $this->hooks[$hook] = array(); 40 } 41 if (!isset($this->hooks[$hook][$priority])) { 42 $this->hooks[$hook][$priority] = array(); 51 $this->hooks[$hook] = [ 52 $priority => [], 53 ]; 54 } elseif (!isset($this->hooks[$hook][$priority])) { 55 $this->hooks[$hook][$priority] = []; 43 56 } 44 57 … … 52 65 * @param array $parameters Parameters to pass to callbacks 53 66 * @return boolean Successfulness 67 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $hook argument is not a string. 68 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $parameters argument is not an array. 54 69 */ 55 public function dispatch($hook, $parameters = array()) { 70 public function dispatch($hook, $parameters = []) { 71 if (is_string($hook) === false) { 72 throw InvalidArgument::create(1, '$hook', 'string', gettype($hook)); 73 } 74 75 // Check strictly against array, as Array* objects don't work in combination with `call_user_func_array()`. 76 if (is_array($parameters) === false) { 77 throw InvalidArgument::create(2, '$parameters', 'array', gettype($parameters)); 78 } 79 56 80 if (empty($this->hooks[$hook])) { 57 81 return false; 58 82 } 59 83 84 if (!empty($parameters)) { 85 // Strip potential keys from the array to prevent them being interpreted as parameter names in PHP 8.0. 86 $parameters = array_values($parameters); 87 } 88 60 89 foreach ($this->hooks[$hook] as $priority => $hooked) { 61 90 foreach ($hooked as $callback) { 62 call_user_func_array($callback,$parameters);91 $callback(...$parameters); 63 92 } 64 93 } -
trunk/src/wp-includes/Requests/IdnaEncoder.php
r52243 r52244 1 1 <?php 2 3 namespace WpOrg\Requests; 4 5 use WpOrg\Requests\Exception; 6 use WpOrg\Requests\Exception\InvalidArgument; 7 use WpOrg\Requests\Utility\InputValidator; 2 8 3 9 /** … … 6 12 * Note: Not fully compliant, as nameprep does nothing yet. 7 13 * 8 * @package Requests 9 * @subpackage Utilities10 * @ seehttps://tools.ietf.org/html/rfc3490 IDNA specification11 * @ seehttps://tools.ietf.org/html/rfc3492 Punycode/Bootstrap specification14 * @package Requests\Utilities 15 * 16 * @link https://tools.ietf.org/html/rfc3490 IDNA specification 17 * @link https://tools.ietf.org/html/rfc3492 Punycode/Bootstrap specification 12 18 */ 13 class Requests_IDNAEncoder {19 class IdnaEncoder { 14 20 /** 15 21 * ACE prefix used for IDNA 16 22 * 17 * @ seehttps://tools.ietf.org/html/rfc3490#section-523 * @link https://tools.ietf.org/html/rfc3490#section-5 18 24 * @var string 19 25 */ 20 26 const ACE_PREFIX = 'xn--'; 27 28 /** 29 * Maximum length of a IDNA URL in ASCII. 30 * 31 * @see \WpOrg\Requests\IdnaEncoder::to_ascii() 32 * 33 * @since 2.0.0 34 * 35 * @var int 36 */ 37 const MAX_LENGTH = 64; 21 38 22 39 /**#@+ 23 40 * Bootstrap constant for Punycode 24 41 * 25 * @ seehttps://tools.ietf.org/html/rfc3492#section-542 * @link https://tools.ietf.org/html/rfc3492#section-5 26 43 * @var int 27 44 */ … … 38 55 * Encode a hostname using Punycode 39 56 * 40 * @param string $stringHostname57 * @param string|Stringable $hostname Hostname 41 58 * @return string Punycode-encoded hostname 42 */ 43 public static function encode($string) { 44 $parts = explode('.', $string); 59 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not a string or a stringable object. 60 */ 61 public static function encode($hostname) { 62 if (InputValidator::is_string_or_stringable($hostname) === false) { 63 throw InvalidArgument::create(1, '$hostname', 'string|Stringable', gettype($hostname)); 64 } 65 66 $parts = explode('.', $hostname); 45 67 foreach ($parts as &$part) { 46 68 $part = self::to_ascii($part); … … 50 72 51 73 /** 52 * Convert a UTF-8 string to an ASCII string using Punycode 53 * 54 * @throws Requests_Exception Provided string longer than 64 ASCII characters (`idna.provided_too_long`) 55 * @throws Requests_Exception Prepared string longer than 64 ASCII characters (`idna.prepared_too_long`) 56 * @throws Requests_Exception Provided string already begins with xn-- (`idna.provided_is_prefixed`) 57 * @throws Requests_Exception Encoded string longer than 64 ASCII characters (`idna.encoded_too_long`) 58 * 59 * @param string $string ASCII or UTF-8 string (max length 64 characters) 74 * Convert a UTF-8 text string to an ASCII string using Punycode 75 * 76 * @param string $text ASCII or UTF-8 string (max length 64 characters) 60 77 * @return string ASCII string 61 */ 62 public static function to_ascii($string) { 63 // Step 1: Check if the string is already ASCII 64 if (self::is_ascii($string)) { 78 * 79 * @throws \WpOrg\Requests\Exception Provided string longer than 64 ASCII characters (`idna.provided_too_long`) 80 * @throws \WpOrg\Requests\Exception Prepared string longer than 64 ASCII characters (`idna.prepared_too_long`) 81 * @throws \WpOrg\Requests\Exception Provided string already begins with xn-- (`idna.provided_is_prefixed`) 82 * @throws \WpOrg\Requests\Exception Encoded string longer than 64 ASCII characters (`idna.encoded_too_long`) 83 */ 84 public static function to_ascii($text) { 85 // Step 1: Check if the text is already ASCII 86 if (self::is_ascii($text)) { 65 87 // Skip to step 7 66 if (strlen($ string) < 64) {67 return $ string;68 } 69 70 throw new Requests_Exception('Provided string is too long', 'idna.provided_too_long', $string);88 if (strlen($text) < self::MAX_LENGTH) { 89 return $text; 90 } 91 92 throw new Exception('Provided string is too long', 'idna.provided_too_long', $text); 71 93 } 72 94 73 95 // Step 2: nameprep 74 $ string = self::nameprep($string);96 $text = self::nameprep($text); 75 97 76 98 // Step 3: UseSTD3ASCIIRules is false, continue 77 99 // Step 4: Check if it's ASCII now 78 if (self::is_ascii($ string)) {100 if (self::is_ascii($text)) { 79 101 // Skip to step 7 80 if (strlen($string) < 64) { 81 return $string; 82 } 83 84 throw new Requests_Exception('Prepared string is too long', 'idna.prepared_too_long', $string); 102 /* 103 * As the `nameprep()` method returns the original string, this code will never be reached until 104 * that method is properly implemented. 105 */ 106 // @codeCoverageIgnoreStart 107 if (strlen($text) < self::MAX_LENGTH) { 108 return $text; 109 } 110 111 throw new Exception('Prepared string is too long', 'idna.prepared_too_long', $text); 112 // @codeCoverageIgnoreEnd 85 113 } 86 114 87 115 // Step 5: Check ACE prefix 88 if (strpos($ string, self::ACE_PREFIX) === 0) {89 throw new Requests_Exception('Provided string begins with ACE prefix', 'idna.provided_is_prefixed', $string);116 if (strpos($text, self::ACE_PREFIX) === 0) { 117 throw new Exception('Provided string begins with ACE prefix', 'idna.provided_is_prefixed', $text); 90 118 } 91 119 92 120 // Step 6: Encode with Punycode 93 $ string = self::punycode_encode($string);121 $text = self::punycode_encode($text); 94 122 95 123 // Step 7: Prepend ACE prefix 96 $ string = self::ACE_PREFIX . $string;124 $text = self::ACE_PREFIX . $text; 97 125 98 126 // Step 8: Check size 99 if (strlen($ string) < 64) {100 return $ string;101 } 102 103 throw new Requests_Exception('Encoded string is too long', 'idna.encoded_too_long', $string);104 } 105 106 /** 107 * Check whether a given string contains only ASCII characters127 if (strlen($text) < self::MAX_LENGTH) { 128 return $text; 129 } 130 131 throw new Exception('Encoded string is too long', 'idna.encoded_too_long', $text); 132 } 133 134 /** 135 * Check whether a given text string contains only ASCII characters 108 136 * 109 137 * @internal (Testing found regex was the fastest implementation) 110 138 * 111 * @param string $ string112 * @return bool Is the string ASCII-only?113 */ 114 protected static function is_ascii($ string) {115 return (preg_match('/(?:[^\x00-\x7F])/', $ string) !== 1);116 } 117 118 /** 119 * Prepare a string for use as an IDNA name139 * @param string $text 140 * @return bool Is the text string ASCII-only? 141 */ 142 protected static function is_ascii($text) { 143 return (preg_match('/(?:[^\x00-\x7F])/', $text) !== 1); 144 } 145 146 /** 147 * Prepare a text string for use as an IDNA name 120 148 * 121 149 * @todo Implement this based on RFC 3491 and the newer 5891 122 * @param string $ string150 * @param string $text 123 151 * @return string Prepared string 124 152 */ 125 protected static function nameprep($ string) {126 return $ string;153 protected static function nameprep($text) { 154 return $text; 127 155 } 128 156 … … 130 158 * Convert a UTF-8 string to a UCS-4 codepoint array 131 159 * 132 * Based on Requests_IRI::replace_invalid_with_pct_encoding() 133 * 134 * @throws Requests_Exception Invalid UTF-8 codepoint (`idna.invalidcodepoint`) 160 * Based on \WpOrg\Requests\Iri::replace_invalid_with_pct_encoding() 161 * 135 162 * @param string $input 136 163 * @return array Unicode code points 164 * 165 * @throws \WpOrg\Requests\Exception Invalid UTF-8 codepoint (`idna.invalidcodepoint`) 137 166 */ 138 167 protected static function utf8_to_codepoints($input) { 139 $codepoints = array();168 $codepoints = []; 140 169 141 170 // Get number of bytes … … 172 201 // Invalid byte: 173 202 else { 174 throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $value);203 throw new Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $value); 175 204 } 176 205 177 206 if ($remaining > 0) { 178 207 if ($position + $length > $strlen) { 179 throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);208 throw new Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); 180 209 } 181 210 for ($position++; $remaining > 0; $position++) { … … 184 213 // If it is invalid, count the sequence as invalid and reprocess the current byte: 185 214 if (($value & 0xC0) !== 0x80) { 186 throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);215 throw new Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); 187 216 } 188 217 … … 209 238 ) 210 239 ) { 211 throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character);240 throw new Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); 212 241 } 213 242 … … 222 251 * 223 252 * @internal Pseudo-code from Section 6.3 is commented with "#" next to relevant code 224 * @throws Requests_Exception On character outside of the domain (never happens with Punycode) (`idna.character_outside_domain`)225 253 * 226 254 * @param string $input UTF-8 encoded string to encode 227 255 * @return string Punycode-encoded string 256 * 257 * @throws \WpOrg\Requests\Exception On character outside of the domain (never happens with Punycode) (`idna.character_outside_domain`) 228 258 */ 229 259 public static function punycode_encode($input) { … … 240 270 // copy them to the output in order 241 271 $codepoints = self::utf8_to_codepoints($input); 242 $extended = array();272 $extended = []; 243 273 244 274 foreach ($codepoints as $char) { … … 253 283 // @codeCoverageIgnoreStart 254 284 elseif ($char < $n) { 255 throw new Requests_Exception('Invalid character', 'idna.character_outside_domain', $char);285 throw new Exception('Invalid character', 'idna.character_outside_domain', $char); 256 286 } 257 287 // @codeCoverageIgnoreEnd … … 333 363 * Convert a digit to its respective character 334 364 * 335 * @see https://tools.ietf.org/html/rfc3492#section-5 336 * @throws Requests_Exception On invalid digit (`idna.invalid_digit`) 365 * @link https://tools.ietf.org/html/rfc3492#section-5 337 366 * 338 367 * @param int $digit Digit in the range 0-35 339 368 * @return string Single character corresponding to digit 369 * 370 * @throws \WpOrg\Requests\Exception On invalid digit (`idna.invalid_digit`) 340 371 */ 341 372 protected static function digit_to_char($digit) { … … 343 374 // As far as I know, this never happens, but still good to be sure. 344 375 if ($digit < 0 || $digit > 35) { 345 throw new Requests_Exception(sprintf('Invalid digit %d', $digit), 'idna.invalid_digit', $digit);376 throw new Exception(sprintf('Invalid digit %d', $digit), 'idna.invalid_digit', $digit); 346 377 } 347 378 // @codeCoverageIgnoreEnd … … 353 384 * Adapt the bias 354 385 * 355 * @ seehttps://tools.ietf.org/html/rfc3492#section-6.1386 * @link https://tools.ietf.org/html/rfc3492#section-6.1 356 387 * @param int $delta 357 388 * @param int $numpoints -
trunk/src/wp-includes/Requests/Ipv6.php
r52243 r52244 3 3 * Class to validate and to work with IPv6 addresses 4 4 * 5 * @package Requests 6 * @subpackage Utilities 5 * @package Requests\Utilities 7 6 */ 7 8 namespace WpOrg\Requests; 9 10 use WpOrg\Requests\Exception\InvalidArgument; 11 use WpOrg\Requests\Utility\InputValidator; 8 12 9 13 /** … … 13 17 * entirely rewritten. 14 18 * 15 * @package Requests 16 * @subpackage Utilities 19 * @package Requests\Utilities 17 20 */ 18 class Requests_IPv6 {21 final class Ipv6 { 19 22 /** 20 23 * Uncompresses an IPv6 address … … 31 34 * @author Josh Peck <jmp at joshpeck dot org> 32 35 * @copyright 2003-2005 The PHP Group 33 * @license http://www.opensource.org/licenses/bsd-license.php 34 * @param string $ip An IPv6 address 36 * @license https://opensource.org/licenses/bsd-license.php 37 * 38 * @param string|Stringable $ip An IPv6 address 35 39 * @return string The uncompressed IPv6 address 40 * 41 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not a string or a stringable object. 36 42 */ 37 43 public static function uncompress($ip) { 44 if (InputValidator::is_string_or_stringable($ip) === false) { 45 throw InvalidArgument::create(1, '$ip', 'string|Stringable', gettype($ip)); 46 } 47 48 $ip = (string) $ip; 49 38 50 if (substr_count($ip, '::') !== 1) { 39 51 return $ip; … … 79 91 * 0:0:0:0:0:0:0:1 -> ::1 80 92 * 81 * @see uncompress() 93 * @see \WpOrg\Requests\IPv6::uncompress() 94 * 82 95 * @param string $ip An IPv6 address 83 96 * @return string The compressed IPv6 address 84 97 */ 85 98 public static function compress($ip) { 86 // Prepare the IP to be compressed 99 // Prepare the IP to be compressed. 100 // Note: Input validation is handled in the `uncompress()` method, which is the first call made in this method. 87 101 $ip = self::uncompress($ip); 88 102 $ip_parts = self::split_v6_v4($ip); … … 125 139 * @return string[] [0] contains the IPv6 represented part, and [1] the IPv4 represented part 126 140 */ 127 pr otectedstatic function split_v6_v4($ip) {141 private static function split_v6_v4($ip) { 128 142 if (strpos($ip, '.') !== false) { 129 143 $pos = strrpos($ip, ':'); 130 144 $ipv6_part = substr($ip, 0, $pos); 131 145 $ipv4_part = substr($ip, $pos + 1); 132 return array($ipv6_part, $ipv4_part);133 } 134 else { 135 return array($ip, '');146 return [$ipv6_part, $ipv4_part]; 147 } 148 else { 149 return [$ip, '']; 136 150 } 137 151 } … … 146 160 */ 147 161 public static function check_ipv6($ip) { 162 // Note: Input validation is handled in the `uncompress()` method, which is the first call made in this method. 148 163 $ip = self::uncompress($ip); 149 164 list($ipv6, $ipv4) = self::split_v6_v4($ip); -
trunk/src/wp-includes/Requests/Iri.php
r52243 r52244 3 3 * IRI parser/serialiser/normaliser 4 4 * 5 * @package Requests 6 * @subpackage Utilities 5 * @package Requests\Utilities 7 6 */ 7 8 namespace WpOrg\Requests; 9 10 use WpOrg\Requests\Exception; 11 use WpOrg\Requests\Exception\InvalidArgument; 12 use WpOrg\Requests\Ipv6; 13 use WpOrg\Requests\Port; 14 use WpOrg\Requests\Utility\InputValidator; 8 15 9 16 /** … … 39 46 * POSSIBILITY OF SUCH DAMAGE. 40 47 * 41 * @package Requests 42 * @subpackage Utilities 48 * @package Requests\Utilities 43 49 * @author Geoffrey Sneddon 44 50 * @author Steve Minutillo 45 51 * @copyright 2007-2009 Geoffrey Sneddon and Steve Minutillo 46 * @license http ://www.opensource.org/licenses/bsd-license.php52 * @license https://opensource.org/licenses/bsd-license.php 47 53 * @link http://hg.gsnedders.com/iri/ 48 54 * 49 55 * @property string $iri IRI we're working with 50 * @property-read string $uri IRI in URI form, {@see to_uri}56 * @property-read string $uri IRI in URI form, {@see \WpOrg\Requests\IRI::to_uri()} 51 57 * @property string $scheme Scheme part of the IRI 52 58 * @property string $authority Authority part, formatted for a URI (userinfo + host + port) … … 64 70 * @property string $ifragment Fragment part of the IRI (after '#') 65 71 */ 66 class Requests_IRI{72 class Iri { 67 73 /** 68 74 * Scheme … … 124 130 protected $normalization = array( 125 131 'acap' => array( 126 'port' => 674132 'port' => Port::ACAP, 127 133 ), 128 134 'dict' => array( 129 'port' => 2628135 'port' => Port::DICT, 130 136 ), 131 137 'file' => array( 132 'ihost' => 'localhost' 138 'ihost' => 'localhost', 133 139 ), 134 140 'http' => array( 135 'port' => 80,141 'port' => Port::HTTP, 136 142 ), 137 143 'https' => array( 138 'port' => 443,144 'port' => Port::HTTPS, 139 145 ), 140 146 ); … … 241 247 * Create a new IRI object, from a specified string 242 248 * 243 * @param string|null $iri 249 * @param string|Stringable|null $iri 250 * 251 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $iri argument is not a string, Stringable or null. 244 252 */ 245 253 public function __construct($iri = null) { 254 if ($iri !== null && InputValidator::is_string_or_stringable($iri) === false) { 255 throw InvalidArgument::create(1, '$iri', 'string|Stringable|null', gettype($iri)); 256 } 257 246 258 $this->set_iri($iri); 247 259 } … … 252 264 * Returns false if $base is not absolute, otherwise an IRI. 253 265 * 254 * @param Requests_IRI|string $base (Absolute) Base IRI255 * @param Requests_IRI|string $relative Relative IRI256 * @return Requests_IRI|false266 * @param \WpOrg\Requests\Iri|string $base (Absolute) Base IRI 267 * @param \WpOrg\Requests\Iri|string $relative Relative IRI 268 * @return \WpOrg\Requests\Iri|false 257 269 */ 258 270 public static function absolutize($base, $relative) { 259 if (!($relative instanceof Requests_IRI)) {260 $relative = new Requests_IRI($relative);271 if (!($relative instanceof self)) { 272 $relative = new self($relative); 261 273 } 262 274 if (!$relative->is_valid()) { … … 267 279 } 268 280 269 if (!($base instanceof Requests_IRI)) {270 $base = new Requests_IRI($base);281 if (!($base instanceof self)) { 282 $base = new self($base); 271 283 } 272 284 if ($base->scheme === null || !$base->is_valid()) { … … 280 292 } 281 293 else { 282 $target = new Requests_IRI;294 $target = new self; 283 295 $target->scheme = $base->scheme; 284 296 $target->iuserinfo = $base->iuserinfo; … … 331 343 $has_match = preg_match('/^((?P<scheme>[^:\/?#]+):)?(\/\/(?P<authority>[^\/?#]*))?(?P<path>[^?#]*)(\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$/', $iri, $match); 332 344 if (!$has_match) { 333 throw new Requests_Exception('Cannot parse supplied IRI', 'iri.cannot_parse', $iri);345 throw new Exception('Cannot parse supplied IRI', 'iri.cannot_parse', $iri); 334 346 } 335 347 … … 414 426 * Replace invalid character with percent encoding 415 427 * 416 * @param string $ stringInput string428 * @param string $text Input string 417 429 * @param string $extra_chars Valid characters not in iunreserved or 418 430 * iprivate (this is ASCII-only) … … 420 432 * @return string 421 433 */ 422 protected function replace_invalid_with_pct_encoding($ string, $extra_chars, $iprivate = false) {434 protected function replace_invalid_with_pct_encoding($text, $extra_chars, $iprivate = false) { 423 435 // Normalize as many pct-encoded sections as possible 424 $ string = preg_replace_callback('/(?:%[A-Fa-f0-9]{2})+/', array($this, 'remove_iunreserved_percent_encoded'), $string);436 $text = preg_replace_callback('/(?:%[A-Fa-f0-9]{2})+/', array($this, 'remove_iunreserved_percent_encoded'), $text); 425 437 426 438 // Replace invalid percent characters 427 $ string = preg_replace('/%(?![A-Fa-f0-9]{2})/', '%25', $string);439 $text = preg_replace('/%(?![A-Fa-f0-9]{2})/', '%25', $text); 428 440 429 441 // Add unreserved and % to $extra_chars (the latter is safe because all … … 433 445 // Now replace any bytes that aren't allowed with their pct-encoded versions 434 446 $position = 0; 435 $strlen = strlen($ string);436 while (($position += strspn($ string, $extra_chars, $position)) < $strlen) {437 $value = ord($ string[$position]);447 $strlen = strlen($text); 448 while (($position += strspn($text, $extra_chars, $position)) < $strlen) { 449 $value = ord($text[$position]); 438 450 439 451 // Start position … … 472 484 if ($position + $length <= $strlen) { 473 485 for ($position++; $remaining; $position++) { 474 $value = ord($ string[$position]);486 $value = ord($text[$position]); 475 487 476 488 // Check that the byte is valid, then add it to the character: … … 523 535 524 536 for ($j = $start; $j <= $position; $j++) { 525 $ string = substr_replace($string, sprintf('%%%02X', ord($string[$j])), $j, 1);537 $text = substr_replace($text, sprintf('%%%02X', ord($text[$j])), $j, 1); 526 538 $j += 2; 527 539 $position += 2; … … 531 543 } 532 544 533 return $ string;545 return $text; 534 546 } 535 547 … … 540 552 * encoded characters in iunreserved 541 553 * 542 * @param array $ match PCRE match554 * @param array $regex_match PCRE match 543 555 * @return string Replacement 544 556 */ 545 protected function remove_iunreserved_percent_encoded($ match) {557 protected function remove_iunreserved_percent_encoded($regex_match) { 546 558 // As we just have valid percent encoded sequences we can just explode 547 559 // and ignore the first member of the returned array (an empty string). 548 $bytes = explode('%', $ match[0]);560 $bytes = explode('%', $regex_match[0]); 549 561 550 562 // Initialize the new string (this is what will be returned) and that … … 722 734 return true; 723 735 } 736 737 $iri = (string) $iri; 738 724 739 if (isset($cache[$iri])) { 725 740 list($this->scheme, … … 734 749 } 735 750 736 $parsed = $this->parse_iri( (string)$iri);751 $parsed = $this->parse_iri($iri); 737 752 738 753 $return = $this->set_scheme($parsed['scheme']) … … 864 879 } 865 880 if (substr($ihost, 0, 1) === '[' && substr($ihost, -1) === ']') { 866 if ( Requests_IPv6::check_ipv6(substr($ihost, 1, -1))) {867 $this->ihost = '[' . Requests_IPv6::compress(substr($ihost, 1, -1)) . ']';881 if (Ipv6::check_ipv6(substr($ihost, 1, -1))) { 882 $this->ihost = '[' . Ipv6::compress(substr($ihost, 1, -1)) . ']'; 868 883 } 869 884 else { … … 986 1001 * Convert an IRI to a URI (or parts thereof) 987 1002 * 988 * @param string|bool IRI to convert (or false from {@see get_iri})1003 * @param string|bool $iri IRI to convert (or false from {@see \WpOrg\Requests\IRI::get_iri()}) 989 1004 * @return string|false URI if IRI is valid, false otherwise. 990 1005 */ 991 protected function to_uri($ string) {992 if (!is_string($ string)) {1006 protected function to_uri($iri) { 1007 if (!is_string($iri)) { 993 1008 return false; 994 1009 } … … 1000 1015 1001 1016 $position = 0; 1002 $strlen = strlen($ string);1003 while (($position += strcspn($ string, $non_ascii, $position)) < $strlen) {1004 $ string = substr_replace($string, sprintf('%%%02X', ord($string[$position])), $position, 1);1017 $strlen = strlen($iri); 1018 while (($position += strcspn($iri, $non_ascii, $position)) < $strlen) { 1019 $iri = substr_replace($iri, sprintf('%%%02X', ord($iri[$position])), $position, 1); 1005 1020 $position += 3; 1006 1021 $strlen += 2; 1007 1022 } 1008 1023 1009 return $ string;1024 return $iri; 1010 1025 } 1011 1026 -
trunk/src/wp-includes/Requests/Proxy.php
r50842 r52244 3 3 * Proxy connection interface 4 4 * 5 * @package Requests 6 * @subpackage Proxy 7 * @since 1.6 5 * @package Requests\Proxy 6 * @since 1.6 8 7 */ 8 9 namespace WpOrg\Requests; 10 11 use WpOrg\Requests\Hooks; 9 12 10 13 /** … … 16 19 * makes it much easier for users to use your provider. 17 20 * 18 * @see Requests_Hooks19 * @package Requests20 * @ subpackageProxy21 * @since 1.621 * @see \WpOrg\Requests\Hooks 22 * 23 * @package Requests\Proxy 24 * @since 1.6 22 25 */ 23 interface Requests_Proxy {26 interface Proxy { 24 27 /** 25 28 * Register hooks as needed 26 29 * 27 * This method is called in {@see Requests::request} when the user has set28 * an instance as the 'auth' option. Use this callback to register all the30 * This method is called in {@see \WpOrg\Requests\Requests::request()} when the user 31 * has set an instance as the 'auth' option. Use this callback to register all the 29 32 * hooks you'll need. 30 33 * 31 * @see Requests_Hooks::register32 * @param Requests_Hooks $hooks Hook system34 * @see \WpOrg\Requests\Hooks::register() 35 * @param \WpOrg\Requests\Hooks $hooks Hook system 33 36 */ 34 public function register( Requests_Hooks $hooks);37 public function register(Hooks $hooks); 35 38 } -
trunk/src/wp-includes/Requests/Proxy/Http.php
r52243 r52244 3 3 * HTTP Proxy connection interface 4 4 * 5 * @package Requests 6 * @subpackage Proxy 7 * @since 1.6 5 * @package Requests\Proxy 6 * @since 1.6 8 7 */ 8 9 namespace WpOrg\Requests\Proxy; 10 11 use WpOrg\Requests\Exception\ArgumentCount; 12 use WpOrg\Requests\Exception\InvalidArgument; 13 use WpOrg\Requests\Hooks; 14 use WpOrg\Requests\Proxy; 9 15 10 16 /** … … 13 19 * Provides a handler for connection via an HTTP proxy 14 20 * 15 * @package Requests 16 * @subpackage Proxy 17 * @since 1.6 21 * @package Requests\Proxy 22 * @since 1.6 18 23 */ 19 class Requests_Proxy_HTTP implements Requests_Proxy {24 final class Http implements Proxy { 20 25 /** 21 26 * Proxy host and port … … 52 57 * 53 58 * @since 1.6 54 * @throws Requests_Exception On incorrect number of arguments (`authbasicbadargs`) 55 * @param array|null $args Array of user and password. Must have exactly two elements 59 * 60 * @param array|string|null $args Proxy as a string or an array of proxy, user and password. 61 * When passed as an array, must have exactly one (proxy) 62 * or three elements (proxy, user, password). 63 * 64 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not an array, a string or null. 65 * @throws \WpOrg\Requests\Exception\ArgumentCount On incorrect number of arguments (`proxyhttpbadargs`) 56 66 */ 57 67 public function __construct($args = null) { … … 68 78 } 69 79 else { 70 throw new Requests_Exception('Invalid number of arguments', 'proxyhttpbadargs'); 80 throw ArgumentCount::create( 81 'an array with exactly one element or exactly three elements', 82 count($args), 83 'proxyhttpbadargs' 84 ); 71 85 } 86 } elseif ($args !== null) { 87 throw InvalidArgument::create(1, '$args', 'array|string|null', gettype($args)); 72 88 } 73 89 } … … 77 93 * 78 94 * @since 1.6 79 * @see curl_before_send80 * @see fsockopen_remote_socket81 * @see fsockopen_remote_host_path82 * @see fsockopen_header83 * @param Requests_Hooks $hooks Hook system95 * @see \WpOrg\Requests\Proxy\HTTP::curl_before_send() 96 * @see \WpOrg\Requests\Proxy\HTTP::fsockopen_remote_socket() 97 * @see \WpOrg\Requests\Proxy\HTTP::fsockopen_remote_host_path() 98 * @see \WpOrg\Requests\Proxy\HTTP::fsockopen_header() 99 * @param \WpOrg\Requests\Hooks $hooks Hook system 84 100 */ 85 public function register( Requests_Hooks $hooks) {86 $hooks->register('curl.before_send', array($this, 'curl_before_send'));101 public function register(Hooks $hooks) { 102 $hooks->register('curl.before_send', [$this, 'curl_before_send']); 87 103 88 $hooks->register('fsockopen.remote_socket', array($this, 'fsockopen_remote_socket'));89 $hooks->register('fsockopen.remote_host_path', array($this, 'fsockopen_remote_host_path'));104 $hooks->register('fsockopen.remote_socket', [$this, 'fsockopen_remote_socket']); 105 $hooks->register('fsockopen.remote_host_path', [$this, 'fsockopen_remote_host_path']); 90 106 if ($this->use_authentication) { 91 $hooks->register('fsockopen.after_headers', array($this, 'fsockopen_header'));107 $hooks->register('fsockopen.after_headers', [$this, 'fsockopen_header']); 92 108 } 93 109 } … … 97 113 * 98 114 * @since 1.6 99 * @param resource $handle cURL resource115 * @param resource|\CurlHandle $handle cURL handle 100 116 */ 101 117 public function curl_before_send(&$handle) { -
trunk/src/wp-includes/Requests/Requests.php
r52243 r52244 10 10 */ 11 11 12 namespace WpOrg\Requests; 13 14 use WpOrg\Requests\Auth\Basic; 15 use WpOrg\Requests\Capability; 16 use WpOrg\Requests\Cookie\Jar; 17 use WpOrg\Requests\Exception; 18 use WpOrg\Requests\Exception\InvalidArgument; 19 use WpOrg\Requests\Hooks; 20 use WpOrg\Requests\IdnaEncoder; 21 use WpOrg\Requests\Iri; 22 use WpOrg\Requests\Proxy\Http; 23 use WpOrg\Requests\Response; 24 use WpOrg\Requests\Transport\Curl; 25 use WpOrg\Requests\Transport\Fsockopen; 26 use WpOrg\Requests\Utility\InputValidator; 27 12 28 /** 13 29 * Requests for PHP … … 85 101 86 102 /** 103 * Option defaults. 104 * 105 * @see \WpOrg\Requests\Requests::get_default_options() 106 * @see \WpOrg\Requests\Requests::request() for values returned by this method 107 * 108 * @since 2.0.0 109 * 110 * @var array 111 */ 112 const OPTION_DEFAULTS = [ 113 'timeout' => 10, 114 'connect_timeout' => 10, 115 'useragent' => 'php-requests/' . self::VERSION, 116 'protocol_version' => 1.1, 117 'redirected' => 0, 118 'redirects' => 10, 119 'follow_redirects' => true, 120 'blocking' => true, 121 'type' => self::GET, 122 'filename' => false, 123 'auth' => false, 124 'proxy' => false, 125 'cookies' => false, 126 'max_bytes' => false, 127 'idn' => true, 128 'hooks' => null, 129 'transport' => null, 130 'verify' => null, 131 'verifyname' => true, 132 ]; 133 134 /** 135 * Default supported Transport classes. 136 * 137 * @since 2.0.0 138 * 139 * @var array 140 */ 141 const DEFAULT_TRANSPORTS = [ 142 Curl::class => Curl::class, 143 Fsockopen::class => Fsockopen::class, 144 ]; 145 146 /** 87 147 * Current version of Requests 88 148 * 89 149 * @var string 90 150 */ 91 const VERSION = '1.8.1'; 151 const VERSION = '2.0.0'; 152 153 /** 154 * Selected transport name 155 * 156 * Use {@see \WpOrg\Requests\Requests::get_transport()} instead 157 * 158 * @var array 159 */ 160 public static $transport = []; 92 161 93 162 /** … … 96 165 * @var array 97 166 */ 98 protected static $transports = array(); 99 100 /** 101 * Selected transport name 102 * 103 * Use {@see get_transport()} instead 167 protected static $transports = []; 168 169 /** 170 * Default certificate path. 171 * 172 * @see \WpOrg\Requests\Requests::get_certificate_path() 173 * @see \WpOrg\Requests\Requests::set_certificate_path() 174 * 175 * @var string 176 */ 177 protected static $certificate_path = __DIR__ . '/../certificates/cacert.pem'; 178 179 /** 180 * All (known) valid deflate, gzip header magic markers. 181 * 182 * These markers relate to different compression levels. 183 * 184 * @link https://stackoverflow.com/a/43170354/482864 Marker source. 185 * 186 * @since 2.0.0 104 187 * 105 188 * @var array 106 189 */ 107 public static $transport = array(); 108 109 /** 110 * Default certificate path. 111 * 112 * @see Requests::get_certificate_path() 113 * @see Requests::set_certificate_path() 114 * 115 * @var string 116 */ 117 protected static $certificate_path; 190 private static $magic_compression_headers = [ 191 "\x1f\x8b" => true, // Gzip marker. 192 "\x78\x01" => true, // Zlib marker - level 1. 193 "\x78\x5e" => true, // Zlib marker - level 2 to 5. 194 "\x78\x9c" => true, // Zlib marker - level 6. 195 "\x78\xda" => true, // Zlib marker - level 7 to 9. 196 ]; 118 197 119 198 /** … … 125 204 126 205 /** 127 * Autoloader for Requests128 *129 * Register this with {@see register_autoloader()} if you'd like to avoid130 * having to create your own.131 *132 * (You can also use `spl_autoload_register` directly if you'd prefer.)133 *134 * @codeCoverageIgnore135 *136 * @param string $class Class name to load137 */138 public static function autoloader($class) {139 // Check that the class starts with "Requests"140 if (strpos($class, 'Requests') !== 0) {141 return;142 }143 144 $file = str_replace('_', '/', $class);145 if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) {146 require_once dirname(__FILE__) . '/' . $file . '.php';147 }148 }149 150 /**151 * Register the built-in autoloader152 *153 * @codeCoverageIgnore154 */155 public static function register_autoloader() {156 spl_autoload_register(array('Requests', 'autoloader'));157 }158 159 /**160 206 * Register a transport 161 207 * 162 * @param string $transport Transport class to add, must support the Requests_Transport interface208 * @param string $transport Transport class to add, must support the \WpOrg\Requests\Transport interface 163 209 */ 164 210 public static function add_transport($transport) { 165 211 if (empty(self::$transports)) { 166 self::$transports = array( 167 'Requests_Transport_cURL', 168 'Requests_Transport_fsockopen', 169 ); 170 } 171 172 self::$transports = array_merge(self::$transports, array($transport)); 173 } 174 175 /** 176 * Get a working transport 177 * 178 * @throws Requests_Exception If no valid transport is found (`notransport`) 179 * @return Requests_Transport 180 */ 181 protected static function get_transport($capabilities = array()) { 182 // Caching code, don't bother testing coverage 212 self::$transports = self::DEFAULT_TRANSPORTS; 213 } 214 215 self::$transports[$transport] = $transport; 216 } 217 218 /** 219 * Get the fully qualified class name (FQCN) for a working transport. 220 * 221 * @param array<string, bool> $capabilities Optional. Associative array of capabilities to test against, i.e. `['<capability>' => true]`. 222 * @return string FQCN of the transport to use, or an empty string if no transport was 223 * found which provided the requested capabilities. 224 */ 225 protected static function get_transport_class(array $capabilities = []) { 226 // Caching code, don't bother testing coverage. 183 227 // @codeCoverageIgnoreStart 184 // array of capabilities as a string to be used as an array key228 // Array of capabilities as a string to be used as an array key. 185 229 ksort($capabilities); 186 230 $cap_string = serialize($capabilities); 187 231 188 // Don't search for a transport if it's already been done for these $capabilities 189 if (isset(self::$transport[$cap_string]) && self::$transport[$cap_string] !== null) { 190 $class = self::$transport[$cap_string]; 191 return new $class(); 192 } 232 // Don't search for a transport if it's already been done for these $capabilities. 233 if (isset(self::$transport[$cap_string])) { 234 return self::$transport[$cap_string]; 235 } 236 237 // Ensure we will not run this same check again later on. 238 self::$transport[$cap_string] = ''; 193 239 // @codeCoverageIgnoreEnd 194 240 195 241 if (empty(self::$transports)) { 196 self::$transports = array( 197 'Requests_Transport_cURL', 198 'Requests_Transport_fsockopen', 199 ); 200 } 201 202 // Find us a working transport 242 self::$transports = self::DEFAULT_TRANSPORTS; 243 } 244 245 // Find us a working transport. 203 246 foreach (self::$transports as $class) { 204 247 if (!class_exists($class)) { … … 206 249 } 207 250 208 $result = call_user_func(array($class, 'test'),$capabilities);209 if ($result ) {251 $result = $class::test($capabilities); 252 if ($result === true) { 210 253 self::$transport[$cap_string] = $class; 211 254 break; 212 255 } 213 256 } 214 if (self::$transport[$cap_string] === null) { 215 throw new Requests_Exception('No working transports found', 'notransport', self::$transports); 216 } 217 218 $class = self::$transport[$cap_string]; 257 258 return self::$transport[$cap_string]; 259 } 260 261 /** 262 * Get a working transport. 263 * 264 * @param array<string, bool> $capabilities Optional. Associative array of capabilities to test against, i.e. `['<capability>' => true]`. 265 * @return \WpOrg\Requests\Transport 266 * @throws \WpOrg\Requests\Exception If no valid transport is found (`notransport`). 267 */ 268 protected static function get_transport(array $capabilities = []) { 269 $class = self::get_transport_class($capabilities); 270 271 if ($class === '') { 272 throw new Exception('No working transports found', 'notransport', self::$transports); 273 } 274 219 275 return new $class(); 220 276 } 221 277 278 /** 279 * Checks to see if we have a transport for the capabilities requested. 280 * 281 * Supported capabilities can be found in the {@see \WpOrg\Requests\Capability} 282 * interface as constants. 283 * 284 * Example usage: 285 * `Requests::has_capabilities([Capability::SSL => true])`. 286 * 287 * @param array<string, bool> $capabilities Optional. Associative array of capabilities to test against, i.e. `['<capability>' => true]`. 288 * @return bool Whether the transport has the requested capabilities. 289 */ 290 public static function has_capabilities(array $capabilities = []) { 291 return self::get_transport_class($capabilities) !== ''; 292 } 293 222 294 /**#@+ 223 * @see request()295 * @see \WpOrg\Requests\Requests::request() 224 296 * @param string $url 225 297 * @param array $headers 226 298 * @param array $options 227 * @return Requests_Response299 * @return \WpOrg\Requests\Response 228 300 */ 229 301 /** 230 302 * Send a GET request 231 303 */ 232 public static function get($url, $headers = array(), $options = array()) {304 public static function get($url, $headers = [], $options = []) { 233 305 return self::request($url, $headers, null, self::GET, $options); 234 306 } … … 237 309 * Send a HEAD request 238 310 */ 239 public static function head($url, $headers = array(), $options = array()) {311 public static function head($url, $headers = [], $options = []) { 240 312 return self::request($url, $headers, null, self::HEAD, $options); 241 313 } … … 244 316 * Send a DELETE request 245 317 */ 246 public static function delete($url, $headers = array(), $options = array()) {318 public static function delete($url, $headers = [], $options = []) { 247 319 return self::request($url, $headers, null, self::DELETE, $options); 248 320 } … … 251 323 * Send a TRACE request 252 324 */ 253 public static function trace($url, $headers = array(), $options = array()) {325 public static function trace($url, $headers = [], $options = []) { 254 326 return self::request($url, $headers, null, self::TRACE, $options); 255 327 } … … 257 329 258 330 /**#@+ 259 * @see request()331 * @see \WpOrg\Requests\Requests::request() 260 332 * @param string $url 261 333 * @param array $headers 262 334 * @param array $data 263 335 * @param array $options 264 * @return Requests_Response336 * @return \WpOrg\Requests\Response 265 337 */ 266 338 /** 267 339 * Send a POST request 268 340 */ 269 public static function post($url, $headers = array(), $data = array(), $options = array()) {341 public static function post($url, $headers = [], $data = [], $options = []) { 270 342 return self::request($url, $headers, $data, self::POST, $options); 271 343 } … … 273 345 * Send a PUT request 274 346 */ 275 public static function put($url, $headers = array(), $data = array(), $options = array()) {347 public static function put($url, $headers = [], $data = [], $options = []) { 276 348 return self::request($url, $headers, $data, self::PUT, $options); 277 349 } … … 280 352 * Send an OPTIONS request 281 353 */ 282 public static function options($url, $headers = array(), $data = array(), $options = array()) {354 public static function options($url, $headers = [], $data = [], $options = []) { 283 355 return self::request($url, $headers, $data, self::OPTIONS, $options); 284 356 } … … 287 359 * Send a PATCH request 288 360 * 289 * Note: Unlike {@see post} and {@see put}, `$headers` is required, as the290 * specification recommends that should send an ETag361 * Note: Unlike {@see \WpOrg\Requests\Requests::post()} and {@see \WpOrg\Requests\Requests::put()}, 362 * `$headers` is required, as the specification recommends that should send an ETag 291 363 * 292 364 * @link https://tools.ietf.org/html/rfc5789 293 365 */ 294 public static function patch($url, $headers, $data = array(), $options = array()) {366 public static function patch($url, $headers, $data = [], $options = []) { 295 367 return self::request($url, $headers, $data, self::PATCH, $options); 296 368 } … … 324 396 * - `auth`: Authentication handler or array of user/password details to use 325 397 * for Basic authentication 326 * ( Requests_Auth|array|boolean, default: false)398 * (\WpOrg\Requests\Auth|array|boolean, default: false) 327 399 * - `proxy`: Proxy details to use for proxy by-passing and authentication 328 * ( Requests_Proxy|array|string|boolean, default: false)400 * (\WpOrg\Requests\Proxy|array|string|boolean, default: false) 329 401 * - `max_bytes`: Limit for the response body size. 330 402 * (integer|boolean, default: false) … … 333 405 * - `transport`: Custom transport. Either a class name, or a 334 406 * transport object. Defaults to the first working transport from 335 * {@see getTransport()}336 * (string| Requests_Transport, default: {@seegetTransport()})407 * {@see \WpOrg\Requests\Requests::getTransport()} 408 * (string|\WpOrg\Requests\Transport, default: {@see \WpOrg\Requests\Requests::getTransport()}) 337 409 * - `hooks`: Hooks handler. 338 * ( Requests_Hooker, default: new Requests_Hooks())410 * (\WpOrg\Requests\HookManager, default: new WpOrg\Requests\Hooks()) 339 411 * - `verify`: Should we verify SSL certificates? Allows passing in a custom 340 412 * certificate file as a string. (Using true uses the system-wide root 341 413 * certificate store instead, but this may have different behaviour 342 414 * across transports.) 343 * (string|boolean, default: library/Requests/Transport/cacert.pem)415 * (string|boolean, default: certificates/cacert.pem) 344 416 * - `verifyname`: Should we verify the common name in the SSL certificate? 345 417 * (boolean, default: true) … … 348 420 * HEAD/GET/DELETE, 'body' for POST/PUT/OPTIONS/PATCH) 349 421 * 350 * @throws Requests_Exception On invalid URLs (`nonhttp`) 351 * 352 * @param string $url URL to request 422 * @param string|Stringable $url URL to request 353 423 * @param array $headers Extra headers to send with the request 354 424 * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests 355 425 * @param string $type HTTP request type (use Requests constants) 356 426 * @param array $options Options for the request (see description for more information) 357 * @return Requests_Response 358 */ 359 public static function request($url, $headers = array(), $data = array(), $type = self::GET, $options = array()) { 427 * @return \WpOrg\Requests\Response 428 * 429 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $url argument is not a string or Stringable. 430 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $type argument is not a string. 431 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array. 432 * @throws \WpOrg\Requests\Exception On invalid URLs (`nonhttp`) 433 */ 434 public static function request($url, $headers = [], $data = [], $type = self::GET, $options = []) { 435 if (InputValidator::is_string_or_stringable($url) === false) { 436 throw InvalidArgument::create(1, '$url', 'string|Stringable', gettype($url)); 437 } 438 439 if (is_string($type) === false) { 440 throw InvalidArgument::create(4, '$type', 'string', gettype($type)); 441 } 442 443 if (is_array($options) === false) { 444 throw InvalidArgument::create(5, '$options', 'array', gettype($options)); 445 } 446 360 447 if (empty($options['type'])) { 361 448 $options['type'] = $type; … … 365 452 self::set_defaults($url, $headers, $data, $type, $options); 366 453 367 $options['hooks']->dispatch('requests.before_request', array(&$url, &$headers, &$data, &$type, &$options));454 $options['hooks']->dispatch('requests.before_request', [&$url, &$headers, &$data, &$type, &$options]); 368 455 369 456 if (!empty($options['transport'])) { … … 376 463 else { 377 464 $need_ssl = (stripos($url, 'https://') === 0); 378 $capabilities = array('ssl' => $need_ssl);465 $capabilities = [Capability::SSL => $need_ssl]; 379 466 $transport = self::get_transport($capabilities); 380 467 } 381 468 $response = $transport->request($url, $headers, $data, $options); 382 469 383 $options['hooks']->dispatch('requests.before_parse', array(&$response, $url, $headers, $data, $type, $options));470 $options['hooks']->dispatch('requests.before_parse', [&$response, $url, $headers, $data, $type, $options]); 384 471 385 472 return self::parse_response($response, $url, $headers, $data, $options); … … 397 484 * 398 485 * - `url`: Request URL Same as the `$url` parameter to 399 * {@see Requests::request}486 * {@see \WpOrg\Requests\Requests::request()} 400 487 * (string, required) 401 488 * - `headers`: Associative array of header fields. Same as the `$headers` 402 * parameter to {@see Requests::request}489 * parameter to {@see \WpOrg\Requests\Requests::request()} 403 490 * (array, default: `array()`) 404 491 * - `data`: Associative array of data fields or a string. Same as the 405 * `$data` parameter to {@see Requests::request}492 * `$data` parameter to {@see \WpOrg\Requests\Requests::request()} 406 493 * (array|string, default: `array()`) 407 * - `type`: HTTP request type (use Requests constants). Same as the `$type`408 * parameter to {@see Requests::request}409 * (string, default: ` Requests::GET`)494 * - `type`: HTTP request type (use \WpOrg\Requests\Requests constants). Same as the `$type` 495 * parameter to {@see \WpOrg\Requests\Requests::request()} 496 * (string, default: `\WpOrg\Requests\Requests::GET`) 410 497 * - `cookies`: Associative array of cookie name to value, or cookie jar. 411 * (array| Requests_Cookie_Jar)498 * (array|\WpOrg\Requests\Cookie\Jar) 412 499 * 413 500 * If the `$options` parameter is specified, individual requests will 414 501 * inherit options from it. This can be used to use a single hooking system, 415 * or set all the types to ` Requests::POST`, for example.502 * or set all the types to `\WpOrg\Requests\Requests::POST`, for example. 416 503 * 417 504 * In addition, the `$options` parameter takes the following global options: 418 505 * 419 506 * - `complete`: A callback for when a request is complete. Takes two 420 * parameters, a Requests_Response/Requests_Exception reference, and the507 * parameters, a \WpOrg\Requests\Response/\WpOrg\Requests\Exception reference, and the 421 508 * ID from the request array (Note: this can also be overridden on a 422 509 * per-request basis, although that's a little silly) … … 424 511 * 425 512 * @param array $requests Requests data (see description for more information) 426 * @param array $options Global and default options (see {@see Requests::request}) 427 * @return array Responses (either Requests_Response or a Requests_Exception object) 428 */ 429 public static function request_multiple($requests, $options = array()) { 513 * @param array $options Global and default options (see {@see \WpOrg\Requests\Requests::request()}) 514 * @return array Responses (either \WpOrg\Requests\Response or a \WpOrg\Requests\Exception object) 515 * 516 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $requests argument is not an array or iterable object with array access. 517 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array. 518 */ 519 public static function request_multiple($requests, $options = []) { 520 if (InputValidator::has_array_access($requests) === false || InputValidator::is_iterable($requests) === false) { 521 throw InvalidArgument::create(1, '$requests', 'array|ArrayAccess&Traversable', gettype($requests)); 522 } 523 524 if (is_array($options) === false) { 525 throw InvalidArgument::create(2, '$options', 'array', gettype($options)); 526 } 527 430 528 $options = array_merge(self::get_default_options(true), $options); 431 529 432 530 if (!empty($options['hooks'])) { 433 $options['hooks']->register('transport.internal.parse_response', array('Requests', 'parse_multiple'));531 $options['hooks']->register('transport.internal.parse_response', [static::class, 'parse_multiple']); 434 532 if (!empty($options['complete'])) { 435 533 $options['hooks']->register('multiple.request.complete', $options['complete']); … … 439 537 foreach ($requests as $id => &$request) { 440 538 if (!isset($request['headers'])) { 441 $request['headers'] = array();539 $request['headers'] = []; 442 540 } 443 541 if (!isset($request['data'])) { 444 $request['data'] = array();542 $request['data'] = []; 445 543 } 446 544 if (!isset($request['type'])) { … … 462 560 // Ensure we only hook in once 463 561 if ($request['options']['hooks'] !== $options['hooks']) { 464 $request['options']['hooks']->register('transport.internal.parse_response', array('Requests', 'parse_multiple'));562 $request['options']['hooks']->register('transport.internal.parse_response', [static::class, 'parse_multiple']); 465 563 if (!empty($request['options']['complete'])) { 466 564 $request['options']['hooks']->register('multiple.request.complete', $request['options']['complete']); … … 488 586 $request = $requests[$id]; 489 587 self::parse_multiple($response, $request); 490 $request['options']['hooks']->dispatch('multiple.request.complete', array(&$response, $id));588 $request['options']['hooks']->dispatch('multiple.request.complete', [&$response, $id]); 491 589 } 492 590 } … … 498 596 * Get the default options 499 597 * 500 * @see Requests::request() for values returned by this method598 * @see \WpOrg\Requests\Requests::request() for values returned by this method 501 599 * @param boolean $multirequest Is this a multirequest? 502 600 * @return array Default option values 503 601 */ 504 602 protected static function get_default_options($multirequest = false) { 505 $defaults = array( 506 'timeout' => 10, 507 'connect_timeout' => 10, 508 'useragent' => 'php-requests/' . self::VERSION, 509 'protocol_version' => 1.1, 510 'redirected' => 0, 511 'redirects' => 10, 512 'follow_redirects' => true, 513 'blocking' => true, 514 'type' => self::GET, 515 'filename' => false, 516 'auth' => false, 517 'proxy' => false, 518 'cookies' => false, 519 'max_bytes' => false, 520 'idn' => true, 521 'hooks' => null, 522 'transport' => null, 523 'verify' => self::get_certificate_path(), 524 'verifyname' => true, 525 ); 603 $defaults = static::OPTION_DEFAULTS; 604 $defaults['verify'] = self::$certificate_path; 605 526 606 if ($multirequest !== false) { 527 607 $defaults['complete'] = null; … … 536 616 */ 537 617 public static function get_certificate_path() { 538 if (!empty(self::$certificate_path)) { 539 return self::$certificate_path; 540 } 541 542 return dirname(__FILE__) . '/Requests/Transport/cacert.pem'; 618 return self::$certificate_path; 543 619 } 544 620 … … 546 622 * Set default certificate path. 547 623 * 548 * @param string $path Certificate path, pointing to a PEM file. 624 * @param string|Stringable|bool $path Certificate path, pointing to a PEM file. 625 * 626 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $url argument is not a string, Stringable or boolean. 549 627 */ 550 628 public static function set_certificate_path($path) { 629 if (InputValidator::is_string_or_stringable($path) === false && is_bool($path) === false) { 630 throw InvalidArgument::create(1, '$path', 'string|Stringable|bool', gettype($path)); 631 } 632 551 633 self::$certificate_path = $path; 552 634 } … … 560 642 * @param string $type HTTP request type 561 643 * @param array $options Options for the request 562 * @return array $options 644 * @return void $options is updated with the results 645 * 646 * @throws \WpOrg\Requests\Exception When the $url is not an http(s) URL. 563 647 */ 564 648 protected static function set_defaults(&$url, &$headers, &$data, &$type, &$options) { 565 649 if (!preg_match('/^http(s)?:\/\//i', $url, $matches)) { 566 throw new Requests_Exception('Only HTTP(S) requests are handled.', 'nonhttp', $url);650 throw new Exception('Only HTTP(S) requests are handled.', 'nonhttp', $url); 567 651 } 568 652 569 653 if (empty($options['hooks'])) { 570 $options['hooks'] = new Requests_Hooks();654 $options['hooks'] = new Hooks(); 571 655 } 572 656 573 657 if (is_array($options['auth'])) { 574 $options['auth'] = new Requests_Auth_Basic($options['auth']);658 $options['auth'] = new Basic($options['auth']); 575 659 } 576 660 if ($options['auth'] !== false) { … … 579 663 580 664 if (is_string($options['proxy']) || is_array($options['proxy'])) { 581 $options['proxy'] = new Requests_Proxy_HTTP($options['proxy']);665 $options['proxy'] = new Http($options['proxy']); 582 666 } 583 667 if ($options['proxy'] !== false) { … … 586 670 587 671 if (is_array($options['cookies'])) { 588 $options['cookies'] = new Requests_Cookie_Jar($options['cookies']);672 $options['cookies'] = new Jar($options['cookies']); 589 673 } 590 674 elseif (empty($options['cookies'])) { 591 $options['cookies'] = new Requests_Cookie_Jar();675 $options['cookies'] = new Jar(); 592 676 } 593 677 if ($options['cookies'] !== false) { … … 596 680 597 681 if ($options['idn'] !== false) { 598 $iri = new Requests_IRI($url);599 $iri->host = Requests_IDNAEncoder::encode($iri->ihost);682 $iri = new Iri($url); 683 $iri->host = IdnaEncoder::encode($iri->ihost); 600 684 $url = $iri->uri; 601 685 } … … 605 689 606 690 if (!isset($options['data_format'])) { 607 if (in_array($type, array(self::HEAD, self::GET, self::DELETE), true)) {691 if (in_array($type, [self::HEAD, self::GET, self::DELETE], true)) { 608 692 $options['data_format'] = 'query'; 609 693 } … … 616 700 /** 617 701 * HTTP response parser 618 *619 * @throws Requests_Exception On missing head/body separator (`requests.no_crlf_separator`)620 * @throws Requests_Exception On missing head/body separator (`noversion`)621 * @throws Requests_Exception On missing head/body separator (`toomanyredirects`)622 702 * 623 703 * @param string $headers Full response text including headers and body … … 626 706 * @param array $req_data Original $data array passed to {@link request()}, in case we need to follow redirects 627 707 * @param array $options Original $options array passed to {@link request()}, in case we need to follow redirects 628 * @return Requests_Response 708 * @return \WpOrg\Requests\Response 709 * 710 * @throws \WpOrg\Requests\Exception On missing head/body separator (`requests.no_crlf_separator`) 711 * @throws \WpOrg\Requests\Exception On missing head/body separator (`noversion`) 712 * @throws \WpOrg\Requests\Exception On missing head/body separator (`toomanyredirects`) 629 713 */ 630 714 protected static function parse_response($headers, $url, $req_headers, $req_data, $options) { 631 $return = new Re quests_Response();715 $return = new Response(); 632 716 if (!$options['blocking']) { 633 717 return $return; … … 642 726 if ($pos === false) { 643 727 // Crap! 644 throw new Requests_Exception('Missing header/body separator', 'requests.no_crlf_separator');728 throw new Exception('Missing header/body separator', 'requests.no_crlf_separator'); 645 729 } 646 730 … … 659 743 preg_match('#^HTTP/(1\.\d)[ \t]+(\d+)#i', array_shift($headers), $matches); 660 744 if (empty($matches)) { 661 throw new Requests_Exception('Response could not be parsed', 'noversion', $headers);745 throw new Exception('Response could not be parsed', 'noversion', $headers); 662 746 } 663 747 $return->protocol_version = (float) $matches[1]; … … 686 770 } 687 771 688 $options['hooks']->dispatch('requests.before_redirect_check', array(&$return, $req_headers, $req_data, $options));772 $options['hooks']->dispatch('requests.before_redirect_check', [&$return, $req_headers, $req_data, $options]); 689 773 690 774 if ($return->is_redirect() && $options['follow_redirects'] === true) { … … 697 781 if (strpos($location, 'http://') !== 0 && strpos($location, 'https://') !== 0) { 698 782 // relative redirect, for compatibility make it absolute 699 $location = Requests_IRI::absolutize($url, $location);783 $location = Iri::absolutize($url, $location); 700 784 $location = $location->uri; 701 785 } 702 786 703 $hook_args = array(787 $hook_args = [ 704 788 &$location, 705 789 &$req_headers, … … 707 791 &$options, 708 792 $return, 709 );793 ]; 710 794 $options['hooks']->dispatch('requests.before_redirect', $hook_args); 711 795 $redirected = self::request($location, $req_headers, $req_data, $options['type'], $options); … … 714 798 } 715 799 elseif ($options['redirected'] >= $options['redirects']) { 716 throw new Requests_Exception('Too many redirects', 'toomanyredirects', $return);800 throw new Exception('Too many redirects', 'toomanyredirects', $return); 717 801 } 718 802 } … … 720 804 $return->redirects = $options['redirected']; 721 805 722 $options['hooks']->dispatch('requests.after_request', array(&$return, $req_headers, $req_data, $options));806 $options['hooks']->dispatch('requests.after_request', [&$return, $req_headers, $req_data, $options]); 723 807 return $return; 724 808 } … … 727 811 * Callback for `transport.internal.parse_response` 728 812 * 729 * Internal use only. Converts a raw HTTP response to a Requests_Response813 * Internal use only. Converts a raw HTTP response to a \WpOrg\Requests\Response 730 814 * while still executing a multiple request. 731 815 * 732 816 * @param string $response Full response text including headers and body (will be overwritten with Response instance) 733 * @param array $request Request data as passed into {@see Requests::request_multiple()}734 * @return null `$response` is either set to a Requests_Response instance, or a Requests_Exception object817 * @param array $request Request data as passed into {@see \WpOrg\Requests\Requests::request_multiple()} 818 * @return void `$response` is either set to a \WpOrg\Requests\Response instance, or a \WpOrg\Requests\Exception object 735 819 */ 736 820 public static function parse_multiple(&$response, $request) { … … 742 826 $response = self::parse_response($response, $url, $headers, $data, $options); 743 827 } 744 catch ( Requests_Exception $e) {828 catch (Exception $e) { 745 829 $response = $e; 746 830 } … … 750 834 * Decoded a chunked body as per RFC 2616 751 835 * 752 * @ seehttps://tools.ietf.org/html/rfc2616#section-3.6.1836 * @link https://tools.ietf.org/html/rfc2616#section-3.6.1 753 837 * @param string $data Chunked body 754 838 * @return string Decoded body … … 792 876 * Convert a key => value array to a 'key: value' array for headers 793 877 * 794 * @param array $array Dictionary of header values878 * @param iterable $dictionary Dictionary of header values 795 879 * @return array List of headers 796 */ 797 public static function flatten($array) { 798 $return = array(); 799 foreach ($array as $key => $value) { 880 * 881 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not iterable. 882 */ 883 public static function flatten($dictionary) { 884 if (InputValidator::is_iterable($dictionary) === false) { 885 throw InvalidArgument::create(1, '$dictionary', 'iterable', gettype($dictionary)); 886 } 887 888 $return = []; 889 foreach ($dictionary as $key => $value) { 800 890 $return[] = sprintf('%s: %s', $key, $value); 801 891 } 802 892 return $return; 803 }804 805 /**806 * Convert a key => value array to a 'key: value' array for headers807 *808 * @codeCoverageIgnore809 * @deprecated Misspelling of {@see Requests::flatten}810 * @param array $array Dictionary of header values811 * @return array List of headers812 */813 public static function flattern($array) {814 return self::flatten($array);815 893 } 816 894 … … 823 901 * @param string $data Compressed data in one of the above formats 824 902 * @return string Decompressed string 903 * 904 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not a string. 825 905 */ 826 906 public static function decompress($data) { 827 if (substr($data, 0, 2) !== "\x1f\x8b" && substr($data, 0, 2) !== "\x78\x9c") { 907 if (is_string($data) === false) { 908 throw InvalidArgument::create(1, '$data', 'string', gettype($data)); 909 } 910 911 if (trim($data) === '') { 912 // Empty body does not need further processing. 913 return $data; 914 } 915 916 $marker = substr($data, 0, 2); 917 if (!isset(self::$magic_compression_headers[$marker])) { 828 918 // Not actually compressed. Probably cURL ruining this for us. 829 919 return $data; … … 831 921 832 922 if (function_exists('gzdecode')) { 833 // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.gzdecodeFound -- Wrapped in function_exists() for PHP 5.2.834 923 $decoded = @gzdecode($data); 835 924 if ($decoded !== false) { … … 872 961 * https://core.trac.wordpress.org/ticket/18273 873 962 * 874 * @since 2.8.1963 * @since 1.6.0 875 964 * @link https://core.trac.wordpress.org/ticket/18273 876 * @link https:// secure.php.net/manual/en/function.gzinflate.php#70875877 * @link https:// secure.php.net/manual/en/function.gzinflate.php#77336965 * @link https://www.php.net/gzinflate#70875 966 * @link https://www.php.net/gzinflate#77336 878 967 * 879 968 * @param string $gz_data String to decompress. 880 969 * @return string|bool False on failure. 970 * 971 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not a string. 881 972 */ 882 973 public static function compatible_gzinflate($gz_data) { 974 if (is_string($gz_data) === false) { 975 throw InvalidArgument::create(1, '$gz_data', 'string', gettype($gz_data)); 976 } 977 978 if (trim($gz_data) === '') { 979 return false; 980 } 981 883 982 // Compressed data might contain a full zlib header, if so strip it for 884 983 // gzinflate() … … 910 1009 // byte Huffman marker for gzinflate() 911 1010 // The response is Huffman coded by many compressors such as 912 // java.util.zip.Deflater, Ruby ’s Zlib::Deflate, and .NET's1011 // java.util.zip.Deflater, Ruby's Zlib::Deflate, and .NET's 913 1012 // System.IO.Compression.DeflateStream. 914 1013 // … … 978 1077 return false; 979 1078 } 980 981 public static function match_domain($host, $reference) {982 // Check for a direct match983 if ($host === $reference) {984 return true;985 }986 987 // Calculate the valid wildcard match if the host is not an IP address988 // Also validates that the host has 3 parts or more, as per Firefox's989 // ruleset.990 $parts = explode('.', $host);991 if (ip2long($host) === false && count($parts) >= 3) {992 $parts[0] = '*';993 $wildcard = implode('.', $parts);994 if ($wildcard === $reference) {995 return true;996 }997 }998 999 return false;1000 }1001 1079 } -
trunk/src/wp-includes/Requests/Response.php
r50842 r52244 3 3 * HTTP response class 4 4 * 5 * Contains a response from Requests::request() 5 * Contains a response from \WpOrg\Requests\Requests::request() 6 * 6 7 * @package Requests 7 8 */ 9 10 namespace WpOrg\Requests; 11 12 use WpOrg\Requests\Cookie\Jar; 13 use WpOrg\Requests\Exception; 14 use WpOrg\Requests\Exception\Http; 15 use WpOrg\Requests\Response\Headers; 8 16 9 17 /** 10 18 * HTTP response class 11 19 * 12 * Contains a response from Requests::request() 20 * Contains a response from \WpOrg\Requests\Requests::request() 21 * 13 22 * @package Requests 14 23 */ 15 class Requests_Response { 16 /** 17 * Constructor 18 */ 19 public function __construct() { 20 $this->headers = new Requests_Response_Headers(); 21 $this->cookies = new Requests_Cookie_Jar(); 22 } 24 class Response { 23 25 24 26 /** … … 39 41 * Headers, as an associative array 40 42 * 41 * @var Requests_Response_Headers Array-like object representing headers43 * @var \WpOrg\Requests\Response\Headers Array-like object representing headers 42 44 */ 43 public $headers = array();45 public $headers = []; 44 46 45 47 /** … … 81 83 * Previous requests (from redirects) 82 84 * 83 * @var array Array of Requests_Response objects85 * @var array Array of \WpOrg\Requests\Response objects 84 86 */ 85 public $history = array();87 public $history = []; 86 88 87 89 /** 88 90 * Cookies from the request 89 91 * 90 * @var Requests_Cookie_Jar Array-like object representing a cookie jar92 * @var \WpOrg\Requests\Cookie\Jar Array-like object representing a cookie jar 91 93 */ 92 public $cookies = array(); 94 public $cookies = []; 95 96 /** 97 * Constructor 98 */ 99 public function __construct() { 100 $this->headers = new Headers(); 101 $this->cookies = new Jar(); 102 } 93 103 94 104 /** … … 99 109 public function is_redirect() { 100 110 $code = $this->status_code; 101 return in_array($code, array(300, 301, 302, 303, 307), true) || $code > 307 && $code < 400;111 return in_array($code, [300, 301, 302, 303, 307], true) || $code > 307 && $code < 400; 102 112 } 103 113 … … 105 115 * Throws an exception if the request was not successful 106 116 * 107 * @throws Requests_Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`)108 * @throws Requests_Exception_HTTP On non-successful status code. Exception class corresponds to code (e.g. {@see Requests_Exception_HTTP_404})109 117 * @param boolean $allow_redirects Set to false to throw on a 3xx as well 118 * 119 * @throws \WpOrg\Requests\Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`) 120 * @throws \WpOrg\Requests\Exception\Http On non-successful status code. Exception class corresponds to "Status" + code (e.g. {@see \WpOrg\Requests\Exception\Http\Status404}) 110 121 */ 111 122 public function throw_for_status($allow_redirects = true) { 112 123 if ($this->is_redirect()) { 113 if ( !$allow_redirects) {114 throw new Requests_Exception('Redirection not allowed', 'response.no_redirects', $this);124 if ($allow_redirects !== true) { 125 throw new Exception('Redirection not allowed', 'response.no_redirects', $this); 115 126 } 116 127 } 117 128 elseif (!$this->success) { 118 $exception = Requests_Exception_HTTP::get_class($this->status_code);129 $exception = Http::get_class($this->status_code); 119 130 throw new $exception(null, $this); 120 131 } 121 132 } 133 134 /** 135 * JSON decode the response body. 136 * 137 * The method parameters are the same as those for the PHP native `json_decode()` function. 138 * 139 * @link https://php.net/json-decode 140 * 141 * @param ?bool $associative Optional. When `true`, JSON objects will be returned as associative arrays; 142 * When `false`, JSON objects will be returned as objects. 143 * When `null`, JSON objects will be returned as associative arrays 144 * or objects depending on whether `JSON_OBJECT_AS_ARRAY` is set in the flags. 145 * Defaults to `true` (in contrast to the PHP native default of `null`). 146 * @param int $depth Optional. Maximum nesting depth of the structure being decoded. 147 * Defaults to `512`. 148 * @param int $options Optional. Bitmask of JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, 149 * JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR. 150 * Defaults to `0` (no options set). 151 * 152 * @return array 153 * 154 * @throws \WpOrg\Requests\Exception If `$this->body` is not valid json. 155 */ 156 public function decode_body($associative = true, $depth = 512, $options = 0) { 157 $data = json_decode($this->body, $associative, $depth, $options); 158 159 if (json_last_error() !== JSON_ERROR_NONE) { 160 $last_error = json_last_error_msg(); 161 throw new Exception('Unable to parse JSON data: ' . $last_error, 'response.invalid', $this); 162 } 163 164 return $data; 165 } 122 166 } -
trunk/src/wp-includes/Requests/Response/Headers.php
r50842 r52244 6 6 */ 7 7 8 namespace WpOrg\Requests\Response; 9 10 use WpOrg\Requests\Exception; 11 use WpOrg\Requests\Exception\InvalidArgument; 12 use WpOrg\Requests\Utility\CaseInsensitiveDictionary; 13 use WpOrg\Requests\Utility\FilteredIterator; 14 8 15 /** 9 16 * Case-insensitive dictionary, suitable for HTTP headers … … 11 18 * @package Requests 12 19 */ 13 class Requests_Response_Headers extends Requests_Utility_CaseInsensitiveDictionary {20 class Headers extends CaseInsensitiveDictionary { 14 21 /** 15 22 * Get the given header 16 23 * 17 * Unlike {@see self::getValues()}, this returns a string. If there are24 * Unlike {@see \WpOrg\Requests\Response\Headers::getValues()}, this returns a string. If there are 18 25 * multiple values, it concatenates them with a comma as per RFC2616. 19 26 * … … 21 28 * Set-Cookie headers. 22 29 * 23 * @param string $ key30 * @param string $offset 24 31 * @return string|null Header value 25 32 */ 26 public function offsetGet($key) { 27 $key = strtolower($key); 28 if (!isset($this->data[$key])) { 33 public function offsetGet($offset) { 34 if (is_string($offset)) { 35 $offset = strtolower($offset); 36 } 37 38 if (!isset($this->data[$offset])) { 29 39 return null; 30 40 } 31 41 32 return $this->flatten($this->data[$ key]);42 return $this->flatten($this->data[$offset]); 33 43 } 34 44 … … 36 46 * Set the given item 37 47 * 38 * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`) 48 * @param string $offset Item name 49 * @param string $value Item value 39 50 * 40 * @param string $key Item name 41 * @param string $value Item value 51 * @throws \WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`) 42 52 */ 43 public function offsetSet($ key, $value) {44 if ($ key=== null) {45 throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');53 public function offsetSet($offset, $value) { 54 if ($offset === null) { 55 throw new Exception('Object is a dictionary, not a list', 'invalidset'); 46 56 } 47 57 48 $key = strtolower($key); 49 50 if (!isset($this->data[$key])) { 51 $this->data[$key] = array(); 58 if (is_string($offset)) { 59 $offset = strtolower($offset); 52 60 } 53 61 54 $this->data[$key][] = $value; 62 if (!isset($this->data[$offset])) { 63 $this->data[$offset] = []; 64 } 65 66 $this->data[$offset][] = $value; 55 67 } 56 68 … … 58 70 * Get all values for a given header 59 71 * 60 * @param string $ key72 * @param string $offset 61 73 * @return array|null Header values 74 * 75 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not valid as an array key. 62 76 */ 63 public function getValues($key) { 64 $key = strtolower($key); 65 if (!isset($this->data[$key])) { 77 public function getValues($offset) { 78 if (!is_string($offset) && !is_int($offset)) { 79 throw InvalidArgument::create(1, '$offset', 'string|int', gettype($offset)); 80 } 81 82 $offset = strtolower($offset); 83 if (!isset($this->data[$offset])) { 66 84 return null; 67 85 } 68 86 69 return $this->data[$ key];87 return $this->data[$offset]; 70 88 } 71 89 … … 78 96 * @param string|array $value Value to flatten 79 97 * @return string Flattened value 98 * 99 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not a string or an array. 80 100 */ 81 101 public function flatten($value) { 82 if (is_ array($value)) {83 $value = implode(',', $value);102 if (is_string($value)) { 103 return $value; 84 104 } 85 105 86 return $value; 106 if (is_array($value)) { 107 return implode(',', $value); 108 } 109 110 throw InvalidArgument::create(1, '$value', 'string|array', gettype($value)); 87 111 } 88 112 … … 90 114 * Get an iterator for the data 91 115 * 92 * Converts the internal 93 * @return ArrayIterator 116 * Converts the internally stored values to a comma-separated string if there is more 117 * than one value for a key. 118 * 119 * @return \ArrayIterator 94 120 */ 95 121 public function getIterator() { 96 return new Requests_Utility_FilteredIterator($this->data, array($this, 'flatten'));122 return new FilteredIterator($this->data, [$this, 'flatten']); 97 123 } 98 124 } -
trunk/src/wp-includes/Requests/Session.php
r50842 r52244 3 3 * Session handler for persistent requests and default parameters 4 4 * 5 * @package Requests 6 * @subpackage Session Handler 5 * @package Requests\SessionHandler 7 6 */ 7 8 namespace WpOrg\Requests; 9 10 use WpOrg\Requests\Cookie\Jar; 11 use WpOrg\Requests\Exception\InvalidArgument; 12 use WpOrg\Requests\Iri; 13 use WpOrg\Requests\Requests; 14 use WpOrg\Requests\Utility\InputValidator; 8 15 9 16 /** … … 15 22 * a shared cookie jar), then overridden for individual requests. 16 23 * 17 * @package Requests 18 * @subpackage Session Handler 24 * @package Requests\SessionHandler 19 25 */ 20 class Requests_Session {26 class Session { 21 27 /** 22 28 * Base URL for requests … … 33 39 * @var array 34 40 */ 35 public $headers = array();41 public $headers = []; 36 42 37 43 /** … … 43 49 * @var array 44 50 */ 45 public $data = array();51 public $data = []; 46 52 47 53 /** … … 56 62 * @var array 57 63 */ 58 public $options = array();64 public $options = []; 59 65 60 66 /** 61 67 * Create a new session 62 68 * 63 * @param string| null $url Base URL for requests69 * @param string|Stringable|null $url Base URL for requests 64 70 * @param array $headers Default headers for requests 65 71 * @param array $data Default data for requests 66 72 * @param array $options Default options for requests 67 */ 68 public function __construct($url = null, $headers = array(), $data = array(), $options = array()) { 73 * 74 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $url argument is not a string, Stringable or null. 75 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $headers argument is not an array. 76 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $data argument is not an array. 77 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array. 78 */ 79 public function __construct($url = null, $headers = [], $data = [], $options = []) { 80 if ($url !== null && InputValidator::is_string_or_stringable($url) === false) { 81 throw InvalidArgument::create(1, '$url', 'string|Stringable|null', gettype($url)); 82 } 83 84 if (is_array($headers) === false) { 85 throw InvalidArgument::create(2, '$headers', 'array', gettype($headers)); 86 } 87 88 if (is_array($data) === false) { 89 throw InvalidArgument::create(3, '$data', 'array', gettype($data)); 90 } 91 92 if (is_array($options) === false) { 93 throw InvalidArgument::create(4, '$options', 'array', gettype($options)); 94 } 95 69 96 $this->url = $url; 70 97 $this->headers = $headers; … … 73 100 74 101 if (empty($this->options['cookies'])) { 75 $this->options['cookies'] = new Requests_Cookie_Jar();102 $this->options['cookies'] = new Jar(); 76 103 } 77 104 } … … 80 107 * Get a property's value 81 108 * 82 * @param string $ key Property key109 * @param string $name Property name. 83 110 * @return mixed|null Property value, null if none found 84 111 */ 85 public function __get($ key) {86 if (isset($this->options[$ key])) {87 return $this->options[$ key];112 public function __get($name) { 113 if (isset($this->options[$name])) { 114 return $this->options[$name]; 88 115 } 89 116 … … 94 121 * Set a property's value 95 122 * 96 * @param string $ key Property key123 * @param string $name Property name. 97 124 * @param mixed $value Property value 98 125 */ 99 public function __set($ key, $value) {100 $this->options[$ key] = $value;126 public function __set($name, $value) { 127 $this->options[$name] = $value; 101 128 } 102 129 … … 104 131 * Remove a property's value 105 132 * 106 * @param string $ key Property key107 */ 108 public function __isset($ key) {109 return isset($this->options[$ key]);133 * @param string $name Property name. 134 */ 135 public function __isset($name) { 136 return isset($this->options[$name]); 110 137 } 111 138 … … 113 140 * Remove a property's value 114 141 * 115 * @param string $key Property key 116 */ 117 public function __unset($key) { 118 if (isset($this->options[$key])) { 119 unset($this->options[$key]); 120 } 142 * @param string $name Property name. 143 */ 144 public function __unset($name) { 145 unset($this->options[$name]); 121 146 } 122 147 123 148 /**#@+ 124 * @see request()149 * @see \WpOrg\Requests\Session::request() 125 150 * @param string $url 126 151 * @param array $headers 127 152 * @param array $options 128 * @return Requests_Response153 * @return \WpOrg\Requests\Response 129 154 */ 130 155 /** 131 156 * Send a GET request 132 157 */ 133 public function get($url, $headers = array(), $options = array()) {158 public function get($url, $headers = [], $options = []) { 134 159 return $this->request($url, $headers, null, Requests::GET, $options); 135 160 } … … 138 163 * Send a HEAD request 139 164 */ 140 public function head($url, $headers = array(), $options = array()) {165 public function head($url, $headers = [], $options = []) { 141 166 return $this->request($url, $headers, null, Requests::HEAD, $options); 142 167 } … … 145 170 * Send a DELETE request 146 171 */ 147 public function delete($url, $headers = array(), $options = array()) {172 public function delete($url, $headers = [], $options = []) { 148 173 return $this->request($url, $headers, null, Requests::DELETE, $options); 149 174 } … … 151 176 152 177 /**#@+ 153 * @see request()178 * @see \WpOrg\Requests\Session::request() 154 179 * @param string $url 155 180 * @param array $headers 156 181 * @param array $data 157 182 * @param array $options 158 * @return Requests_Response183 * @return \WpOrg\Requests\Response 159 184 */ 160 185 /** 161 186 * Send a POST request 162 187 */ 163 public function post($url, $headers = array(), $data = array(), $options = array()) {188 public function post($url, $headers = [], $data = [], $options = []) { 164 189 return $this->request($url, $headers, $data, Requests::POST, $options); 165 190 } … … 168 193 * Send a PUT request 169 194 */ 170 public function put($url, $headers = array(), $data = array(), $options = array()) {195 public function put($url, $headers = [], $data = [], $options = []) { 171 196 return $this->request($url, $headers, $data, Requests::PUT, $options); 172 197 } … … 175 200 * Send a PATCH request 176 201 * 177 * Note: Unlike {@see post} and {@see put}, `$headers` is required, as the178 * specification recommends that should send an ETag202 * Note: Unlike {@see \WpOrg\Requests\Session::post()} and {@see \WpOrg\Requests\Session::put()}, 203 * `$headers` is required, as the specification recommends that should send an ETag 179 204 * 180 205 * @link https://tools.ietf.org/html/rfc5789 181 206 */ 182 public function patch($url, $headers, $data = array(), $options = array()) {207 public function patch($url, $headers, $data = [], $options = []) { 183 208 return $this->request($url, $headers, $data, Requests::PATCH, $options); 184 209 } … … 191 216 * parsing. 192 217 * 193 * @see Requests::request() 194 * 195 * @throws Requests_Exception On invalid URLs (`nonhttp`) 218 * @see \WpOrg\Requests\Requests::request() 196 219 * 197 220 * @param string $url URL to request 198 221 * @param array $headers Extra headers to send with the request 199 222 * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests 200 * @param string $type HTTP request type (use Requests constants) 201 * @param array $options Options for the request (see {@see Requests::request}) 202 * @return Requests_Response 203 */ 204 public function request($url, $headers = array(), $data = array(), $type = Requests::GET, $options = array()) { 223 * @param string $type HTTP request type (use \WpOrg\Requests\Requests constants) 224 * @param array $options Options for the request (see {@see \WpOrg\Requests\Requests::request()}) 225 * @return \WpOrg\Requests\Response 226 * 227 * @throws \WpOrg\Requests\Exception On invalid URLs (`nonhttp`) 228 */ 229 public function request($url, $headers = [], $data = [], $type = Requests::GET, $options = []) { 205 230 $request = $this->merge_request(compact('url', 'headers', 'data', 'options')); 206 231 … … 211 236 * Send multiple HTTP requests simultaneously 212 237 * 213 * @see Requests::request_multiple() 214 * 215 * @param array $requests Requests data (see {@see Requests::request_multiple}) 216 * @param array $options Global and default options (see {@see Requests::request}) 217 * @return array Responses (either Requests_Response or a Requests_Exception object) 218 */ 219 public function request_multiple($requests, $options = array()) { 238 * @see \WpOrg\Requests\Requests::request_multiple() 239 * 240 * @param array $requests Requests data (see {@see \WpOrg\Requests\Requests::request_multiple()}) 241 * @param array $options Global and default options (see {@see \WpOrg\Requests\Requests::request()}) 242 * @return array Responses (either \WpOrg\Requests\Response or a \WpOrg\Requests\Exception object) 243 * 244 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $requests argument is not an array or iterable object with array access. 245 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array. 246 */ 247 public function request_multiple($requests, $options = []) { 248 if (InputValidator::has_array_access($requests) === false || InputValidator::is_iterable($requests) === false) { 249 throw InvalidArgument::create(1, '$requests', 'array|ArrayAccess&Traversable', gettype($requests)); 250 } 251 252 if (is_array($options) === false) { 253 throw InvalidArgument::create(2, '$options', 'array', gettype($options)); 254 } 255 220 256 foreach ($requests as $key => $request) { 221 257 $requests[$key] = $this->merge_request($request, false); … … 233 269 * Merge a request's data with the default data 234 270 * 235 * @param array $request Request data (same form as {@see request_multiple})271 * @param array $request Request data (same form as {@see \WpOrg\Requests\Session::request_multiple()}) 236 272 * @param boolean $merge_options Should we merge options as well? 237 273 * @return array Request data … … 239 275 protected function merge_request($request, $merge_options = true) { 240 276 if ($this->url !== null) { 241 $request['url'] = Requests_IRI::absolutize($this->url, $request['url']);277 $request['url'] = Iri::absolutize($this->url, $request['url']); 242 278 $request['url'] = $request['url']->uri; 243 279 } 244 280 245 281 if (empty($request['headers'])) { 246 $request['headers'] = array();282 $request['headers'] = []; 247 283 } 248 284 $request['headers'] = array_merge($this->headers, $request['headers']); … … 257 293 } 258 294 259 if ($merge_options !== false) {295 if ($merge_options === true) { 260 296 $request['options'] = array_merge($this->options, $request['options']); 261 297 -
trunk/src/wp-includes/Requests/Ssl.php
r52243 r52244 3 3 * SSL utilities for Requests 4 4 * 5 * @package Requests 6 * @subpackage Utilities 5 * @package Requests\Utilities 7 6 */ 7 8 namespace WpOrg\Requests; 9 10 use WpOrg\Requests\Exception\InvalidArgument; 11 use WpOrg\Requests\Utility\InputValidator; 8 12 9 13 /** … … 12 16 * Collection of utilities for working with and verifying SSL certificates. 13 17 * 14 * @package Requests 15 * @subpackage Utilities 18 * @package Requests\Utilities 16 19 */ 17 class Requests_SSL{20 final class Ssl { 18 21 /** 19 22 * Verify the certificate against common name and subject alternative names … … 22 25 * names, leading things like 'https://www.github.com/' to be invalid. 23 26 * 24 * @ seehttps://tools.ietf.org/html/rfc2818#section-3.1 RFC2818, Section 3.127 * @link https://tools.ietf.org/html/rfc2818#section-3.1 RFC2818, Section 3.1 25 28 * 26 * @throws Requests_Exception On not obtaining a match for the host (`fsockopen.ssl.no_match`) 27 * @param string $host Host name to verify against 29 * @param string|Stringable $host Host name to verify against 28 30 * @param array $cert Certificate data from openssl_x509_parse() 29 31 * @return bool 32 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $host argument is not a string or a stringable object. 33 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $cert argument is not an array or array accessible. 30 34 */ 31 35 public static function verify_certificate($host, $cert) { 36 if (InputValidator::is_string_or_stringable($host) === false) { 37 throw InvalidArgument::create(1, '$host', 'string|Stringable', gettype($host)); 38 } 39 40 if (InputValidator::has_array_access($cert) === false) { 41 throw InvalidArgument::create(2, '$cert', 'array|ArrayAccess', gettype($cert)); 42 } 43 32 44 $has_dns_alt = false; 33 45 34 46 // Check the subjectAltName 35 if (!empty($cert['extensions'] ) && !empty($cert['extensions']['subjectAltName'])) {47 if (!empty($cert['extensions']['subjectAltName'])) { 36 48 $altnames = explode(',', $cert['extensions']['subjectAltName']); 37 49 foreach ($altnames as $altname) { … … 51 63 } 52 64 } 65 66 if ($has_dns_alt === true) { 67 return false; 68 } 53 69 } 54 70 55 71 // Fall back to checking the common name if we didn't get any dNSName 56 72 // alt names, as per RFC2818 57 if (! $has_dns_alt && !empty($cert['subject']['CN'])) {73 if (!empty($cert['subject']['CN'])) { 58 74 // Check for a match 59 if (self::match_domain($host, $cert['subject']['CN']) === true) { 60 return true; 61 } 75 return (self::match_domain($host, $cert['subject']['CN']) === true); 62 76 } 63 77 … … 78 92 * the third rule. 79 93 * 80 * @param string $reference Reference dNSName94 * @param string|Stringable $reference Reference dNSName 81 95 * @return boolean Is the name valid? 96 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not a string or a stringable object. 82 97 */ 83 98 public static function verify_reference_name($reference) { 99 if (InputValidator::is_string_or_stringable($reference) === false) { 100 throw InvalidArgument::create(1, '$reference', 'string|Stringable', gettype($reference)); 101 } 102 103 if ($reference === '') { 104 return false; 105 } 106 107 if (preg_match('`\s`', $reference) > 0) { 108 // Whitespace detected. This can never be a dNSName. 109 return false; 110 } 111 84 112 $parts = explode('.', $reference); 113 if ($parts !== array_filter($parts)) { 114 // DNSName cannot contain two dots next to each other. 115 return false; 116 } 85 117 86 118 // Check the first part of the name … … 113 145 * Match a hostname against a dNSName reference 114 146 * 115 * @param string $host Requested host116 * @param string $reference dNSName to match against147 * @param string|Stringable $host Requested host 148 * @param string|Stringable $reference dNSName to match against 117 149 * @return boolean Does the domain match? 150 * @throws \WpOrg\Requests\Exception\InvalidArgument When either of the passed arguments is not a string or a stringable object. 118 151 */ 119 152 public static function match_domain($host, $reference) { 153 if (InputValidator::is_string_or_stringable($host) === false) { 154 throw InvalidArgument::create(1, '$host', 'string|Stringable', gettype($host)); 155 } 156 120 157 // Check if the reference is blocklisted first 121 158 if (self::verify_reference_name($reference) !== true) { … … 124 161 125 162 // Check for a direct match 126 if ( $host ===$reference) {163 if ((string) $host === (string) $reference) { 127 164 return true; 128 165 } 129 166 130 167 // Calculate the valid wildcard match if the host is not an IP address 131 // Also validates that the host has 3 parts or more, as per Firefox's 132 // ruleset. 168 // Also validates that the host has 3 parts or more, as per Firefox's ruleset, 169 // as a wildcard reference is only allowed with 3 parts or more, so the 170 // comparison will never match if host doesn't contain 3 parts or more as well. 133 171 if (ip2long($host) === false) { 134 172 $parts = explode('.', $host); 135 173 $parts[0] = '*'; 136 174 $wildcard = implode('.', $parts); 137 if ($wildcard === $reference) {175 if ($wildcard === (string) $reference) { 138 176 return true; 139 177 } -
trunk/src/wp-includes/Requests/Transport.php
r50842 r52244 3 3 * Base HTTP transport 4 4 * 5 * @package Requests 6 * @subpackage Transport 5 * @package Requests\Transport 7 6 */ 7 8 namespace WpOrg\Requests; 8 9 9 10 /** 10 11 * Base HTTP transport 11 12 * 12 * @package Requests 13 * @subpackage Transport 13 * @package Requests\Transport 14 14 */ 15 interface Requests_Transport {15 interface Transport { 16 16 /** 17 17 * Perform a request … … 20 20 * @param array $headers Associative array of request headers 21 21 * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD 22 * @param array $options Request options, see {@see Requests::response()} for documentation22 * @param array $options Request options, see {@see \WpOrg\Requests\Requests::response()} for documentation 23 23 * @return string Raw HTTP result 24 24 */ 25 public function request($url, $headers = array(), $data = array(), $options = array());25 public function request($url, $headers = [], $data = [], $options = []); 26 26 27 27 /** 28 28 * Send multiple requests simultaneously 29 29 * 30 * @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per {@see Requests_Transport::request}31 * @param array $options Global options, see {@see Requests::response()} for documentation32 * @return array Array of Requests_Response objects (may contain Requests_Exception or string responses as well)30 * @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per {@see \WpOrg\Requests\Transport::request()} 31 * @param array $options Global options, see {@see \WpOrg\Requests\Requests::response()} for documentation 32 * @return array Array of \WpOrg\Requests\Response objects (may contain \WpOrg\Requests\Exception or string responses as well) 33 33 */ 34 34 public function request_multiple($requests, $options); 35 35 36 36 /** 37 * Self-test whether the transport can be used 38 * @return bool 37 * Self-test whether the transport can be used. 38 * 39 * The available capabilities to test for can be found in {@see \WpOrg\Requests\Capability}. 40 * 41 * @param array<string, bool> $capabilities Optional. Associative array of capabilities to test against, i.e. `['<capability>' => true]`. 42 * @return bool Whether the transport can be used. 39 43 */ 40 public static function test( );44 public static function test($capabilities = []); 41 45 } -
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