2016-05-13 06:42:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Session handler for persistent requests and default parameters
|
|
|
|
*
|
2021-12-06 22:30:03 +01:00
|
|
|
* @package Requests
|
|
|
|
* @subpackage Session Handler
|
2016-05-13 06:42:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Session handler for persistent requests and default parameters
|
|
|
|
*
|
|
|
|
* Allows various options to be set as default values, and merges both the
|
|
|
|
* options and URL properties together. A base URL can be set for all requests,
|
|
|
|
* with all subrequests resolved from this. Base options can be set (including
|
|
|
|
* a shared cookie jar), then overridden for individual requests.
|
|
|
|
*
|
2021-12-06 22:30:03 +01:00
|
|
|
* @package Requests
|
|
|
|
* @subpackage Session Handler
|
2016-05-13 06:42:28 +02:00
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
class Requests_Session {
|
2016-05-13 06:42:28 +02:00
|
|
|
/**
|
|
|
|
* Base URL for requests
|
|
|
|
*
|
|
|
|
* URLs will be made absolute using this as the base
|
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 21:42:02 +02:00
|
|
|
*
|
2016-05-13 06:42:28 +02:00
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
public $url = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base headers for requests
|
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 21:42:02 +02:00
|
|
|
*
|
2016-05-13 06:42:28 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
public $headers = array();
|
2016-05-13 06:42:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base data for requests
|
|
|
|
*
|
|
|
|
* If both the base data and the per-request data are arrays, the data will
|
|
|
|
* be merged before sending the request.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
public $data = array();
|
2016-05-13 06:42:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base options for requests
|
|
|
|
*
|
|
|
|
* The base options are merged with the per-request data for each request.
|
|
|
|
* The only default option is a shared cookie jar between requests.
|
|
|
|
*
|
|
|
|
* Values here can also be set directly via properties on the Session
|
|
|
|
* object, e.g. `$session->useragent = 'X';`
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
public $options = array();
|
2016-05-13 06:42:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new session
|
|
|
|
*
|
2021-12-06 22:30:03 +01:00
|
|
|
* @param string|null $url Base URL for requests
|
2016-05-13 06:42:28 +02:00
|
|
|
* @param array $headers Default headers for requests
|
|
|
|
* @param array $data Default data for requests
|
|
|
|
* @param array $options Default options for requests
|
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
public function __construct($url = null, $headers = array(), $data = array(), $options = array()) {
|
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 21:42:02 +02:00
|
|
|
$this->url = $url;
|
2016-05-13 06:42:28 +02:00
|
|
|
$this->headers = $headers;
|
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 21:42:02 +02:00
|
|
|
$this->data = $data;
|
2016-05-13 06:42:28 +02:00
|
|
|
$this->options = $options;
|
|
|
|
|
|
|
|
if (empty($this->options['cookies'])) {
|
2021-12-06 22:30:03 +01:00
|
|
|
$this->options['cookies'] = new Requests_Cookie_Jar();
|
2016-05-13 06:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a property's value
|
|
|
|
*
|
2021-12-06 22:30:03 +01:00
|
|
|
* @param string $key Property key
|
2016-05-13 06:42:28 +02:00
|
|
|
* @return mixed|null Property value, null if none found
|
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
public function __get($key) {
|
|
|
|
if (isset($this->options[$key])) {
|
|
|
|
return $this->options[$key];
|
2016-05-13 06:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a property's value
|
|
|
|
*
|
2021-12-06 22:30:03 +01:00
|
|
|
* @param string $key Property key
|
2016-05-13 06:42:28 +02:00
|
|
|
* @param mixed $value Property value
|
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
public function __set($key, $value) {
|
|
|
|
$this->options[$key] = $value;
|
2016-05-13 06:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a property's value
|
|
|
|
*
|
2021-12-06 22:30:03 +01:00
|
|
|
* @param string $key Property key
|
2016-05-13 06:42:28 +02:00
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
public function __isset($key) {
|
|
|
|
return isset($this->options[$key]);
|
2016-05-13 06:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a property's value
|
|
|
|
*
|
2021-12-06 22:30:03 +01:00
|
|
|
* @param string $key Property key
|
2016-05-13 06:42:28 +02:00
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
public function __unset($key) {
|
|
|
|
if (isset($this->options[$key])) {
|
|
|
|
unset($this->options[$key]);
|
|
|
|
}
|
2016-05-13 06:42:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**#@+
|
2021-12-06 22:30:03 +01:00
|
|
|
* @see request()
|
2016-05-13 06:42:28 +02:00
|
|
|
* @param string $url
|
|
|
|
* @param array $headers
|
|
|
|
* @param array $options
|
2021-12-06 22:30:03 +01:00
|
|
|
* @return Requests_Response
|
2016-05-13 06:42:28 +02:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Send a GET request
|
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
public function get($url, $headers = array(), $options = array()) {
|
2016-05-13 06:42:28 +02:00
|
|
|
return $this->request($url, $headers, null, Requests::GET, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a HEAD request
|
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
public function head($url, $headers = array(), $options = array()) {
|
2016-05-13 06:42:28 +02:00
|
|
|
return $this->request($url, $headers, null, Requests::HEAD, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a DELETE request
|
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
public function delete($url, $headers = array(), $options = array()) {
|
2016-05-13 06:42:28 +02:00
|
|
|
return $this->request($url, $headers, null, Requests::DELETE, $options);
|
|
|
|
}
|
|
|
|
/**#@-*/
|
|
|
|
|
|
|
|
/**#@+
|
2021-12-06 22:30:03 +01:00
|
|
|
* @see request()
|
2016-05-13 06:42:28 +02:00
|
|
|
* @param string $url
|
|
|
|
* @param array $headers
|
|
|
|
* @param array $data
|
|
|
|
* @param array $options
|
2021-12-06 22:30:03 +01:00
|
|
|
* @return Requests_Response
|
2016-05-13 06:42:28 +02:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Send a POST request
|
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
public function post($url, $headers = array(), $data = array(), $options = array()) {
|
2016-05-13 06:42:28 +02:00
|
|
|
return $this->request($url, $headers, $data, Requests::POST, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a PUT request
|
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
public function put($url, $headers = array(), $data = array(), $options = array()) {
|
2016-05-13 06:42:28 +02:00
|
|
|
return $this->request($url, $headers, $data, Requests::PUT, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a PATCH request
|
|
|
|
*
|
2021-12-06 22:30:03 +01:00
|
|
|
* Note: Unlike {@see post} and {@see put}, `$headers` is required, as the
|
|
|
|
* specification recommends that should send an ETag
|
2016-05-13 06:42:28 +02:00
|
|
|
*
|
2016-06-10 06:50:33 +02:00
|
|
|
* @link https://tools.ietf.org/html/rfc5789
|
2016-05-13 06:42:28 +02:00
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
public function patch($url, $headers, $data = array(), $options = array()) {
|
2016-05-13 06:42:28 +02:00
|
|
|
return $this->request($url, $headers, $data, Requests::PATCH, $options);
|
|
|
|
}
|
|
|
|
/**#@-*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main interface for HTTP requests
|
|
|
|
*
|
|
|
|
* This method initiates a request and sends it via a transport before
|
|
|
|
* parsing.
|
|
|
|
*
|
2021-12-06 22:30:03 +01:00
|
|
|
* @see Requests::request()
|
|
|
|
*
|
|
|
|
* @throws Requests_Exception On invalid URLs (`nonhttp`)
|
2016-05-13 06:42:28 +02:00
|
|
|
*
|
|
|
|
* @param string $url URL to request
|
|
|
|
* @param array $headers Extra headers to send with the request
|
|
|
|
* @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests
|
2021-12-06 22:30:03 +01:00
|
|
|
* @param string $type HTTP request type (use Requests constants)
|
|
|
|
* @param array $options Options for the request (see {@see Requests::request})
|
|
|
|
* @return Requests_Response
|
2016-05-13 06:42:28 +02:00
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
public function request($url, $headers = array(), $data = array(), $type = Requests::GET, $options = array()) {
|
2016-05-13 06:42:28 +02:00
|
|
|
$request = $this->merge_request(compact('url', 'headers', 'data', 'options'));
|
|
|
|
|
|
|
|
return Requests::request($request['url'], $request['headers'], $request['data'], $type, $request['options']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send multiple HTTP requests simultaneously
|
|
|
|
*
|
2021-12-06 22:30:03 +01:00
|
|
|
* @see Requests::request_multiple()
|
2016-05-13 06:42:28 +02:00
|
|
|
*
|
2021-12-06 22:30:03 +01:00
|
|
|
* @param array $requests Requests data (see {@see Requests::request_multiple})
|
|
|
|
* @param array $options Global and default options (see {@see Requests::request})
|
|
|
|
* @return array Responses (either Requests_Response or a Requests_Exception object)
|
2016-05-13 06:42:28 +02:00
|
|
|
*/
|
2021-12-06 22:30:03 +01:00
|
|
|
public function request_multiple($requests, $options = array()) {
|
2016-05-13 06:42:28 +02:00
|
|
|
foreach ($requests as $key => $request) {
|
|
|
|
$requests[$key] = $this->merge_request($request, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = array_merge($this->options, $options);
|
|
|
|
|
|
|
|
// Disallow forcing the type, as that's a per request setting
|
|
|
|
unset($options['type']);
|
|
|
|
|
|
|
|
return Requests::request_multiple($requests, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge a request's data with the default data
|
|
|
|
*
|
2021-12-06 22:30:03 +01:00
|
|
|
* @param array $request Request data (same form as {@see request_multiple})
|
2016-05-13 06:42:28 +02:00
|
|
|
* @param boolean $merge_options Should we merge options as well?
|
|
|
|
* @return array Request data
|
|
|
|
*/
|
|
|
|
protected function merge_request($request, $merge_options = true) {
|
|
|
|
if ($this->url !== null) {
|
2021-12-06 22:30:03 +01:00
|
|
|
$request['url'] = Requests_IRI::absolutize($this->url, $request['url']);
|
2016-05-13 06:42:28 +02:00
|
|
|
$request['url'] = $request['url']->uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($request['headers'])) {
|
2021-12-06 22:30:03 +01:00
|
|
|
$request['headers'] = array();
|
2016-05-13 06:42:28 +02:00
|
|
|
}
|
|
|
|
$request['headers'] = array_merge($this->headers, $request['headers']);
|
|
|
|
|
|
|
|
if (empty($request['data'])) {
|
|
|
|
if (is_array($this->data)) {
|
|
|
|
$request['data'] = $this->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif (is_array($request['data']) && is_array($this->data)) {
|
|
|
|
$request['data'] = array_merge($this->data, $request['data']);
|
|
|
|
}
|
|
|
|
|
2021-12-06 22:30:03 +01:00
|
|
|
if ($merge_options !== false) {
|
2016-05-13 06:42:28 +02:00
|
|
|
$request['options'] = array_merge($this->options, $request['options']);
|
|
|
|
|
|
|
|
// Disallow forcing the type, as that's a per request setting
|
|
|
|
unset($request['options']['type']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $request;
|
|
|
|
}
|
|
|
|
}
|