2016-05-13 00:42:28 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* HTTP response class
|
|
|
|
*
|
2021-12-06 16:30:03 -05:00
|
|
|
* Contains a response from Requests::request()
|
2016-05-13 00:42:28 -04:00
|
|
|
* @package Requests
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP response class
|
|
|
|
*
|
2021-12-06 16:30:03 -05:00
|
|
|
* Contains a response from Requests::request()
|
2016-05-13 00:42:28 -04:00
|
|
|
* @package Requests
|
|
|
|
*/
|
2021-12-06 16:30:03 -05:00
|
|
|
class Requests_Response {
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$this->headers = new Requests_Response_Headers();
|
|
|
|
$this->cookies = new Requests_Cookie_Jar();
|
|
|
|
}
|
2016-05-13 00:42:28 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Response body
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $body = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Raw HTTP data from the transport
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $raw = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Headers, as an associative array
|
|
|
|
*
|
2021-12-06 16:30:03 -05:00
|
|
|
* @var Requests_Response_Headers Array-like object representing headers
|
2016-05-13 00:42:28 -04:00
|
|
|
*/
|
2021-12-06 16:30:03 -05:00
|
|
|
public $headers = array();
|
2016-05-13 00:42:28 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Status code, false if non-blocking
|
|
|
|
*
|
|
|
|
* @var integer|boolean
|
|
|
|
*/
|
|
|
|
public $status_code = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Protocol version, false if non-blocking
|
External Libraries: Update the Requests library to version `1.8.0`.
While some of the changes in the `1.8.0` release have already been copied to WordPress Core in earlier releases (see [38727], [46258], [47902] and [49382]), this release contains additional improvements, including:
- A significant performance fix when using cURL.
- Improved compliance with RFC2616.
The library has also been moved under the WordPress project’s GitHub organization and can now be found at https://github.com/WordPress/Requests.
Props jrf, dd32, rmccue, justinahinon, netweb, schlessera, TimothyBJacobs, soulseekah, ozh, skithund, carlalexander, travisnorthcutt, desrosj.
Fixes #53101.
Built from https://develop.svn.wordpress.org/trunk@50842
git-svn-id: http://core.svn.wordpress.org/trunk@50451 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-05-11 15:42:02 -04:00
|
|
|
*
|
2016-05-13 00:42:28 -04:00
|
|
|
* @var float|boolean
|
|
|
|
*/
|
|
|
|
public $protocol_version = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the request succeeded or not
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public $success = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of redirects the request used
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
public $redirects = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* URL requested
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $url = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Previous requests (from redirects)
|
|
|
|
*
|
2021-12-06 16:30:03 -05:00
|
|
|
* @var array Array of Requests_Response objects
|
2016-05-13 00:42:28 -04:00
|
|
|
*/
|
2021-12-06 16:30:03 -05:00
|
|
|
public $history = array();
|
2016-05-13 00:42:28 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cookies from the request
|
|
|
|
*
|
2021-12-06 16:30:03 -05:00
|
|
|
* @var Requests_Cookie_Jar Array-like object representing a cookie jar
|
2016-05-13 00:42:28 -04:00
|
|
|
*/
|
2021-12-06 16:30:03 -05:00
|
|
|
public $cookies = array();
|
2016-05-13 00:42:28 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the response a redirect?
|
|
|
|
*
|
|
|
|
* @return boolean True if redirect (3xx status), false if not.
|
|
|
|
*/
|
|
|
|
public function is_redirect() {
|
|
|
|
$code = $this->status_code;
|
2021-12-06 16:30:03 -05:00
|
|
|
return in_array($code, array(300, 301, 302, 303, 307), true) || $code > 307 && $code < 400;
|
2016-05-13 00:42:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws an exception if the request was not successful
|
|
|
|
*
|
2021-12-06 16:30:03 -05:00
|
|
|
* @throws Requests_Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`)
|
|
|
|
* @throws Requests_Exception_HTTP On non-successful status code. Exception class corresponds to code (e.g. {@see Requests_Exception_HTTP_404})
|
2016-05-13 00:42:28 -04:00
|
|
|
* @param boolean $allow_redirects Set to false to throw on a 3xx as well
|
|
|
|
*/
|
|
|
|
public function throw_for_status($allow_redirects = true) {
|
|
|
|
if ($this->is_redirect()) {
|
2021-12-06 16:30:03 -05:00
|
|
|
if (!$allow_redirects) {
|
|
|
|
throw new Requests_Exception('Redirection not allowed', 'response.no_redirects', $this);
|
2016-05-13 00:42:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif (!$this->success) {
|
2021-12-06 16:30:03 -05:00
|
|
|
$exception = Requests_Exception_HTTP::get_class($this->status_code);
|
2016-05-13 00:42:28 -04:00
|
|
|
throw new $exception(null, $this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|