From ba894a4a61a5c62fdf0f9f87d483b8566f8bc3c0 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 22 Aug 2021 06:05:50 +0200 Subject: [PATCH 01/45] PHPCS: few tweaks * Allow for files in the `src` directory to be scanned based on the same rules as before. * Allow for the `WpOrg\Requests` namespace prefix. --- .phpcs.xml.dist | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index bca2cd08f..a65c1296d 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -186,11 +186,12 @@ + - - /library/*\.php$ + */examples/*\.php$ + */tests/bootstrap\.php$ From 417c63d950bf53fdb6871c552409b56bf2f78f4f Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 26 Aug 2021 04:04:21 +0200 Subject: [PATCH 02/45] Requests: move autoload functionality to separate class Includes: * Adding a safeguard against the autoloader being registered multiple times. * Registering the autoloader via prepending it to the autoload queue to prevent potential conflicts with a Requests 1.x autoloader. * Deprecating the old methods in the original `Requests` class, while still safeguarding that they will continue to work for now. * Minor adjustments to the code to allow for the change in directory. --- library/Requests.php | 23 +++++++-------- src/Autoload.php | 68 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 src/Autoload.php diff --git a/library/Requests.php b/library/Requests.php index 2400cb18e..f4d35c122 100644 --- a/library/Requests.php +++ b/library/Requests.php @@ -124,36 +124,33 @@ class Requests { private function __construct() {} /** - * Autoloader for Requests + * Deprecated autoloader for Requests. * - * Register this with {@see register_autoloader()} if you'd like to avoid - * having to create your own. - * - * (You can also use `spl_autoload_register` directly if you'd prefer.) + * @deprecated 2.0.0 Use the `WpOrg\Requests\Autoload::load()` method instead. * * @codeCoverageIgnore * * @param string $class Class name to load */ public static function autoloader($class) { - // Check that the class starts with "Requests" - if (strpos($class, 'Requests') !== 0) { - return; + if (class_exists('WpOrg\Requests\Autoload') === false) { + require_once dirname(__DIR__) . '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/src/Autoload.php'; } - $file = str_replace('_', '/', $class); - if (file_exists(__DIR__ . '/' . $file . '.php')) { - require_once __DIR__ . '/' . $file . '.php'; - } + return WpOrg\Requests\Autoload::load($class); } /** * Register the built-in autoloader * + * @deprecated 2.0.0 Include the `WpOrg\Requests\Autoload` class and + * call `WpOrg\Requests\Autoload::register()` instead. + * * @codeCoverageIgnore */ public static function register_autoloader() { - spl_autoload_register(array(static::class, 'autoloader')); + require_once dirname(__DIR__) . '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/src/Autoload.php'; + WpOrg\Requests\Autoload::register(); } /** diff --git a/src/Autoload.php b/src/Autoload.php new file mode 100644 index 000000000..db6362a2b --- /dev/null +++ b/src/Autoload.php @@ -0,0 +1,68 @@ + Date: Thu, 26 Aug 2021 04:04:21 +0200 Subject: [PATCH 03/45] Update autoloading to allow for PSR-4 class names This commit: * Adds a PSR-4 directive for the `src` directory to the `composer.json` file to support autoloading of the new classes when users use the `vendor/autoload.php` file to handle the class loading. * Adds support for PSR-4 based class loading to the `WpOrg\Requests\Autoload` class for users who use the custom autoload functionality provided by Requests. Notes: * The autoloader will now always return a boolean value, which improves compatibility with autoloader chaining. * The old `Requests` class is special cased. While it is probably rare for this class to be requested for autoloading via the new autoloader, it should still be handled correctly if it is. As the class in the new structure will be split into two classes: `WpOrg\Requests\Requests` and this `WpOrg\Requests\Autoload` class, the file which will be loaded will be the old `Requests` class which will extend the `WpOrg\Requests\Requests` class and will use the `WpOrg\Requests\Autoload` class for the autoloading, maintaining the old functionality without BC break. * When PSR-0 class names which are no longer in use are requested, the new PSR-4 class name will be aliased to the PSR-0 class name for backward compatibility. - The first time during a "page load" such an alias is created, a deprecation notice will be thrown. - Only one deprecation notice per "page load" will be thrown. - Users of this library, which need to support both Requests 1.x as well as 2.x, can declare a `REQUESTS_SILENCE_PSR0_DEPRECATIONS` constant (before the autoloader is invoked for the first time) to silence these deprecation notices. This constant will be supported throughout the lifetime of Requests v 2.x and will be removed in Requests 3.0.0. * As quite a few directory names and class names will have changes in the letter case they use between PSR-0 and PSR-4 and there have also been changes in the class names for the HTTP status exceptions, I'm choosing to implement the matching of the PSR-0 classes to the PSR-4 class names via a case-insensitive lookup table. While the logic behind the renames is transparent and can be encoded in a function, using a lookup table provides optimal speed, especially as the list of classes for which aliasing is provided is limited and will not change. --- composer.json | 3 ++ src/Autoload.php | 85 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 614a3337d..12909af7e 100644 --- a/composer.json +++ b/composer.json @@ -53,6 +53,9 @@ "autoload": { "psr-0": { "Requests": "library/" + }, + "psr-4": { + "WpOrg\\Requests\\": "src/" } }, "autoload-dev": { diff --git a/src/Autoload.php b/src/Autoload.php index db6362a2b..e8a95add7 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -27,6 +27,27 @@ */ class Autoload { + /** + * List of the old PSR-0 class names in lowercase as keys with their PSR-4 case-sensitive name as a value. + * + * @var array + */ + private static $deprecated_classes = array(); + + /** + * Whether or not a deprecation notice has been thrown for calling a deprecated class. + * + * @var bool + */ + private static $deprecation_thrown = false; + + /** + * Whether or not a deprecation notice should be thrown for calling a deprecated class. + * + * @var bool + */ + private static $throw_notice = true; + /** * Register the autoloader. * @@ -52,17 +73,69 @@ public static function register() { * Autoloader. * * @param string $class_name Name of the class name to load. + * + * @return bool Whether a class was loaded or not. */ public static function load($class_name) { - // Check that the class starts with "Requests". - if (strpos($class_name, 'Requests') !== 0) { - return; + // Check that the class starts with "Requests" (PSR-0) or "WpOrg\Requests" (PSR-4). + $psr_0_prefix_pos = stripos($class_name, 'Requests'); + $psr_4_prefix_pos = stripos($class_name, 'WpOrg\\Requests\\'); + + if ($psr_0_prefix_pos !== 0 && $psr_4_prefix_pos !== 0) { + return false; } - $file = str_replace('_', '/', $class_name); - if (file_exists(dirname(__DIR__) . '/library/' . $file . '.php')) { - require_once dirname(__DIR__) . '/library/' . $file . '.php'; + if ($class_name === 'Requests') { + // Original PSR-0 Requests class. + $file = dirname(__DIR__) . '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/library/Requests.php'; + } elseif ($psr_0_prefix_pos === 0) { + // PSR-0 classname. + $file = dirname(__DIR__) . '/library/' . str_replace('_', '/', $class_name) . '.php'; + } elseif ($psr_4_prefix_pos === 0) { + // PSR-4 classname. + $file = __DIR__ . '/' . strtr(substr($class_name, 15), '\\', '/') . '.php'; } + + if (isset($file) && file_exists($file)) { + include $file; + return true; + } + + /* + * Okay, so the class starts with "Requests", but we couldn't find the file. + * If this is one of the deprecated/renamed PSR-0 classes being requested, + * let's alias it to the new name and throw a deprecation notice. + */ + $class_lower = strtolower($class_name); + + if (isset(self::$deprecated_classes[$class_lower])) { + if (self::$throw_notice === true) { + /* + * Integrators who cannot yet upgrade to the PSR-4 class names can silence deprecations + * by defining a `REQUESTS_SILENCE_PSR0_DEPRECATIONS` constant and setting it to `true`. + * The constant needs to be defined before the first deprecated class is requested + * via this autoloader. + */ + if (defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS') && REQUESTS_SILENCE_PSR0_DEPRECATIONS === true) { + self::$throw_notice = false; + } + + if (self::$throw_notice === true && self::$deprecation_thrown === false) { + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error + trigger_error( + 'The PSR-0 `Requests_...` class names in the Request library are deprecated.' + . ' Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience.', + E_USER_DEPRECATED + ); + self::$deprecation_thrown = true; + } + } + + // Create an alias and let the autoloader recursively kick in to load the PSR-4 class. + return class_alias(self::$deprecated_classes[$class_lower], $class_name, true); + } + + return false; } } } From 689c6f13b6adcda0c53b0b16f68ae0bd96701695 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 23 Aug 2021 06:46:48 +0200 Subject: [PATCH 04/45] Requests: update all code samples using the custom autoloader ... to use the new `WpOrg\Requests\Autoload` class instead of the old `Requests` class and use the `Autoload::register()` method instead of the `Requests::register_autoloader()` method. --- README.md | 8 ++++---- examples/basic-auth.php | 8 ++++---- examples/cookie.php | 8 ++++---- examples/cookie_jar.php | 8 ++++---- examples/get.php | 8 ++++---- examples/multiple.php | 8 ++++---- examples/post.php | 8 ++++---- examples/proxy.php | 8 ++++---- examples/session.php | 8 ++++---- examples/timeout.php | 8 ++++---- 10 files changed, 40 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 62f4c1a09..f743eebeb 100644 --- a/README.md +++ b/README.md @@ -79,14 +79,14 @@ To install the source code: $ git clone git://github.com/WordPress/Requests.git ``` -And include it in your scripts: +Next, include the autoloader in your scripts: ```php -require_once '/path/to/Requests/library/Requests.php'; +require_once '/path/to/Requests/src/Autoload.php'; ``` -You'll probably also want to register an autoloader: +You'll probably also want to register the autoloader: ```php -Requests::register_autoloader(); +WpOrg\Requests\Autoload::register(); ``` ### Install source from zip/tarball diff --git a/examples/basic-auth.php b/examples/basic-auth.php index dfabb399f..746cd944a 100644 --- a/examples/basic-auth.php +++ b/examples/basic-auth.php @@ -1,10 +1,10 @@ 'something']); diff --git a/examples/get.php b/examples/get.php index 953bf1694..71e3da8ea 100644 --- a/examples/get.php +++ b/examples/get.php @@ -1,10 +1,10 @@ 'application/json')); diff --git a/examples/multiple.php b/examples/multiple.php index 0de0d8f93..b4ab09c37 100644 --- a/examples/multiple.php +++ b/examples/multiple.php @@ -1,10 +1,10 @@ 'something')); diff --git a/examples/proxy.php b/examples/proxy.php index a9037eebd..4c97a3b07 100644 --- a/examples/proxy.php +++ b/examples/proxy.php @@ -1,10 +1,10 @@ Date: Sat, 14 Aug 2021 01:52:20 +0200 Subject: [PATCH 05/45] Tests: temporarily silence deprecation warnings ... while the classes are being namespaced. --- tests/bootstrap.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3137c883a..c2a9c74ca 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -21,6 +21,8 @@ function define_from_env($name, $default = false) { define_from_env('REQUESTS_HTTP_PROXY_AUTH_USER'); define_from_env('REQUESTS_HTTP_PROXY_AUTH_PASS'); +// Temporarily silence the PSR-0 deprecations while the code is being switched to PSR-4. +define('REQUESTS_SILENCE_PSR0_DEPRECATIONS', true); if (is_dir(dirname(__DIR__) . '/vendor') && file_exists(dirname(__DIR__) . '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/vendor/autoload.php') From 9e4c7c5bcf3db55f3e17559741857175c76844ee Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 13 Aug 2021 08:53:56 +0200 Subject: [PATCH 06/45] Requests: move the real functionality to a namespaced Requests class Move the _real_ `Requests` class - with the exception of the autoload functionality - to the `src` directory and namespace it. Includes updating the reference to this class in the release checklist. --- .github/release-checklist.md | 4 +- library/Requests.php | 937 +-------------------------------- src/Requests.php | 968 +++++++++++++++++++++++++++++++++++ 3 files changed, 973 insertions(+), 936 deletions(-) create mode 100644 src/Requests.php diff --git a/.github/release-checklist.md b/.github/release-checklist.md index 8def7d988..93e0d91ce 100644 --- a/.github/release-checklist.md +++ b/.github/release-checklist.md @@ -5,8 +5,8 @@ Template to use for release PRs from `develop` to `stable` PR for tracking changes for the x.x.x release. Target release date: **DOW MONTH DAY YEAR**. - [ ] Check if any dependencies need updating. -- [ ] Check if the `cacert.pem` file needs updating. -- [ ] Update the version constant in `library/Requests.php`. +- [ ] Check if the `src/Transports/cacert.pem` file needs updating. +- [ ] Update the version constant in `src/Requests.php`. - [ ] Add changelog for the release - PR #xxx - [ ] Merge this PR. - [ ] Make sure all CI builds are green. diff --git a/library/Requests.php b/library/Requests.php index f4d35c122..b8635e0ee 100644 --- a/library/Requests.php +++ b/library/Requests.php @@ -9,6 +9,8 @@ * @package Requests */ +require_once dirname(__DIR__) . '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/src/Requests.php'; + /** * Requests for PHP * @@ -18,110 +20,7 @@ * * @package Requests */ -class Requests { - /** - * POST method - * - * @var string - */ - const POST = 'POST'; - - /** - * PUT method - * - * @var string - */ - const PUT = 'PUT'; - - /** - * GET method - * - * @var string - */ - const GET = 'GET'; - - /** - * HEAD method - * - * @var string - */ - const HEAD = 'HEAD'; - - /** - * DELETE method - * - * @var string - */ - const DELETE = 'DELETE'; - - /** - * OPTIONS method - * - * @var string - */ - const OPTIONS = 'OPTIONS'; - - /** - * TRACE method - * - * @var string - */ - const TRACE = 'TRACE'; - - /** - * PATCH method - * - * @link https://tools.ietf.org/html/rfc5789 - * @var string - */ - const PATCH = 'PATCH'; - - /** - * Default size of buffer size to read streams - * - * @var integer - */ - const BUFFER_SIZE = 1160; - - /** - * Current version of Requests - * - * @var string - */ - const VERSION = '1.8.1'; - - /** - * Registered transport classes - * - * @var array - */ - protected static $transports = array(); - - /** - * Selected transport name - * - * Use {@see get_transport()} instead - * - * @var array - */ - public static $transport = array(); - - /** - * Default certificate path. - * - * @see Requests::get_certificate_path() - * @see Requests::set_certificate_path() - * - * @var string - */ - protected static $certificate_path; - - /** - * This is a static class, do not instantiate it - * - * @codeCoverageIgnore - */ - private function __construct() {} +class Requests extends WpOrg\Requests\Requests { /** * Deprecated autoloader for Requests. @@ -152,834 +51,4 @@ public static function register_autoloader() { require_once dirname(__DIR__) . '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/src/Autoload.php'; WpOrg\Requests\Autoload::register(); } - - /** - * Register a transport - * - * @param string $transport Transport class to add, must support the Requests_Transport interface - */ - public static function add_transport($transport) { - if (empty(self::$transports)) { - self::$transports = array( - Requests_Transport_cURL::class, - Requests_Transport_fsockopen::class, - ); - } - - self::$transports = array_merge(self::$transports, array($transport)); - } - - /** - * Get a working transport - * - * @throws Requests_Exception If no valid transport is found (`notransport`) - * @return Requests_Transport - */ - protected static function get_transport($capabilities = array()) { - // Caching code, don't bother testing coverage - // @codeCoverageIgnoreStart - // array of capabilities as a string to be used as an array key - ksort($capabilities); - $cap_string = serialize($capabilities); - - // Don't search for a transport if it's already been done for these $capabilities - if (isset(self::$transport[$cap_string]) && self::$transport[$cap_string] !== null) { - $class = self::$transport[$cap_string]; - return new $class(); - } - // @codeCoverageIgnoreEnd - - if (empty(self::$transports)) { - self::$transports = array( - Requests_Transport_cURL::class, - Requests_Transport_fsockopen::class, - ); - } - - // Find us a working transport - foreach (self::$transports as $class) { - if (!class_exists($class)) { - continue; - } - - $result = call_user_func(array($class, 'test'), $capabilities); - if ($result) { - self::$transport[$cap_string] = $class; - break; - } - } - if (self::$transport[$cap_string] === null) { - throw new Requests_Exception('No working transports found', 'notransport', self::$transports); - } - - $class = self::$transport[$cap_string]; - return new $class(); - } - - /**#@+ - * @see request() - * @param string $url - * @param array $headers - * @param array $options - * @return Requests_Response - */ - /** - * Send a GET request - */ - public static function get($url, $headers = array(), $options = array()) { - return self::request($url, $headers, null, self::GET, $options); - } - - /** - * Send a HEAD request - */ - public static function head($url, $headers = array(), $options = array()) { - return self::request($url, $headers, null, self::HEAD, $options); - } - - /** - * Send a DELETE request - */ - public static function delete($url, $headers = array(), $options = array()) { - return self::request($url, $headers, null, self::DELETE, $options); - } - - /** - * Send a TRACE request - */ - public static function trace($url, $headers = array(), $options = array()) { - return self::request($url, $headers, null, self::TRACE, $options); - } - /**#@-*/ - - /**#@+ - * @see request() - * @param string $url - * @param array $headers - * @param array $data - * @param array $options - * @return Requests_Response - */ - /** - * Send a POST request - */ - public static function post($url, $headers = array(), $data = array(), $options = array()) { - return self::request($url, $headers, $data, self::POST, $options); - } - /** - * Send a PUT request - */ - public static function put($url, $headers = array(), $data = array(), $options = array()) { - return self::request($url, $headers, $data, self::PUT, $options); - } - - /** - * Send an OPTIONS request - */ - public static function options($url, $headers = array(), $data = array(), $options = array()) { - return self::request($url, $headers, $data, self::OPTIONS, $options); - } - - /** - * Send a PATCH request - * - * Note: Unlike {@see post} and {@see put}, `$headers` is required, as the - * specification recommends that should send an ETag - * - * @link https://tools.ietf.org/html/rfc5789 - */ - public static function patch($url, $headers, $data = array(), $options = array()) { - return self::request($url, $headers, $data, self::PATCH, $options); - } - /**#@-*/ - - /** - * Main interface for HTTP requests - * - * This method initiates a request and sends it via a transport before - * parsing. - * - * The `$options` parameter takes an associative array with the following - * options: - * - * - `timeout`: How long should we wait for a response? - * Note: for cURL, a minimum of 1 second applies, as DNS resolution - * operates at second-resolution only. - * (float, seconds with a millisecond precision, default: 10, example: 0.01) - * - `connect_timeout`: How long should we wait while trying to connect? - * (float, seconds with a millisecond precision, default: 10, example: 0.01) - * - `useragent`: Useragent to send to the server - * (string, default: php-requests/$version) - * - `follow_redirects`: Should we follow 3xx redirects? - * (boolean, default: true) - * - `redirects`: How many times should we redirect before erroring? - * (integer, default: 10) - * - `blocking`: Should we block processing on this request? - * (boolean, default: true) - * - `filename`: File to stream the body to instead. - * (string|boolean, default: false) - * - `auth`: Authentication handler or array of user/password details to use - * for Basic authentication - * (Requests_Auth|array|boolean, default: false) - * - `proxy`: Proxy details to use for proxy by-passing and authentication - * (Requests_Proxy|array|string|boolean, default: false) - * - `max_bytes`: Limit for the response body size. - * (integer|boolean, default: false) - * - `idn`: Enable IDN parsing - * (boolean, default: true) - * - `transport`: Custom transport. Either a class name, or a - * transport object. Defaults to the first working transport from - * {@see getTransport()} - * (string|Requests_Transport, default: {@see getTransport()}) - * - `hooks`: Hooks handler. - * (Requests_Hooker, default: new Requests_Hooks()) - * - `verify`: Should we verify SSL certificates? Allows passing in a custom - * certificate file as a string. (Using true uses the system-wide root - * certificate store instead, but this may have different behaviour - * across transports.) - * (string|boolean, default: library/Requests/Transport/cacert.pem) - * - `verifyname`: Should we verify the common name in the SSL certificate? - * (boolean, default: true) - * - `data_format`: How should we send the `$data` parameter? - * (string, one of 'query' or 'body', default: 'query' for - * HEAD/GET/DELETE, 'body' for POST/PUT/OPTIONS/PATCH) - * - * @throws Requests_Exception On invalid URLs (`nonhttp`) - * - * @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 - * @param string $type HTTP request type (use Requests constants) - * @param array $options Options for the request (see description for more information) - * @return Requests_Response - */ - public static function request($url, $headers = array(), $data = array(), $type = self::GET, $options = array()) { - if (empty($options['type'])) { - $options['type'] = $type; - } - $options = array_merge(self::get_default_options(), $options); - - self::set_defaults($url, $headers, $data, $type, $options); - - $options['hooks']->dispatch('requests.before_request', array(&$url, &$headers, &$data, &$type, &$options)); - - if (!empty($options['transport'])) { - $transport = $options['transport']; - - if (is_string($options['transport'])) { - $transport = new $transport(); - } - } - else { - $need_ssl = (stripos($url, 'https://') === 0); - $capabilities = array('ssl' => $need_ssl); - $transport = self::get_transport($capabilities); - } - $response = $transport->request($url, $headers, $data, $options); - - $options['hooks']->dispatch('requests.before_parse', array(&$response, $url, $headers, $data, $type, $options)); - - return self::parse_response($response, $url, $headers, $data, $options); - } - - /** - * Send multiple HTTP requests simultaneously - * - * The `$requests` parameter takes an associative or indexed array of - * request fields. The key of each request can be used to match up the - * request with the returned data, or with the request passed into your - * `multiple.request.complete` callback. - * - * The request fields value is an associative array with the following keys: - * - * - `url`: Request URL Same as the `$url` parameter to - * {@see Requests::request} - * (string, required) - * - `headers`: Associative array of header fields. Same as the `$headers` - * parameter to {@see Requests::request} - * (array, default: `array()`) - * - `data`: Associative array of data fields or a string. Same as the - * `$data` parameter to {@see Requests::request} - * (array|string, default: `array()`) - * - `type`: HTTP request type (use Requests constants). Same as the `$type` - * parameter to {@see Requests::request} - * (string, default: `Requests::GET`) - * - `cookies`: Associative array of cookie name to value, or cookie jar. - * (array|Requests_Cookie_Jar) - * - * If the `$options` parameter is specified, individual requests will - * inherit options from it. This can be used to use a single hooking system, - * or set all the types to `Requests::POST`, for example. - * - * In addition, the `$options` parameter takes the following global options: - * - * - `complete`: A callback for when a request is complete. Takes two - * parameters, a Requests_Response/Requests_Exception reference, and the - * ID from the request array (Note: this can also be overridden on a - * per-request basis, although that's a little silly) - * (callback) - * - * @param array $requests Requests data (see description for more information) - * @param array $options Global and default options (see {@see Requests::request}) - * @return array Responses (either Requests_Response or a Requests_Exception object) - */ - public static function request_multiple($requests, $options = array()) { - $options = array_merge(self::get_default_options(true), $options); - - if (!empty($options['hooks'])) { - $options['hooks']->register('transport.internal.parse_response', array(static::class, 'parse_multiple')); - if (!empty($options['complete'])) { - $options['hooks']->register('multiple.request.complete', $options['complete']); - } - } - - foreach ($requests as $id => &$request) { - if (!isset($request['headers'])) { - $request['headers'] = array(); - } - if (!isset($request['data'])) { - $request['data'] = array(); - } - if (!isset($request['type'])) { - $request['type'] = self::GET; - } - if (!isset($request['options'])) { - $request['options'] = $options; - $request['options']['type'] = $request['type']; - } - else { - if (empty($request['options']['type'])) { - $request['options']['type'] = $request['type']; - } - $request['options'] = array_merge($options, $request['options']); - } - - self::set_defaults($request['url'], $request['headers'], $request['data'], $request['type'], $request['options']); - - // Ensure we only hook in once - if ($request['options']['hooks'] !== $options['hooks']) { - $request['options']['hooks']->register('transport.internal.parse_response', array(static::class, 'parse_multiple')); - if (!empty($request['options']['complete'])) { - $request['options']['hooks']->register('multiple.request.complete', $request['options']['complete']); - } - } - } - unset($request); - - if (!empty($options['transport'])) { - $transport = $options['transport']; - - if (is_string($options['transport'])) { - $transport = new $transport(); - } - } - else { - $transport = self::get_transport(); - } - $responses = $transport->request_multiple($requests, $options); - - foreach ($responses as $id => &$response) { - // If our hook got messed with somehow, ensure we end up with the - // correct response - if (is_string($response)) { - $request = $requests[$id]; - self::parse_multiple($response, $request); - $request['options']['hooks']->dispatch('multiple.request.complete', array(&$response, $id)); - } - } - - return $responses; - } - - /** - * Get the default options - * - * @see Requests::request() for values returned by this method - * @param boolean $multirequest Is this a multirequest? - * @return array Default option values - */ - protected static function get_default_options($multirequest = false) { - $defaults = array( - 'timeout' => 10, - 'connect_timeout' => 10, - 'useragent' => 'php-requests/' . self::VERSION, - 'protocol_version' => 1.1, - 'redirected' => 0, - 'redirects' => 10, - 'follow_redirects' => true, - 'blocking' => true, - 'type' => self::GET, - 'filename' => false, - 'auth' => false, - 'proxy' => false, - 'cookies' => false, - 'max_bytes' => false, - 'idn' => true, - 'hooks' => null, - 'transport' => null, - 'verify' => self::get_certificate_path(), - 'verifyname' => true, - ); - if ($multirequest !== false) { - $defaults['complete'] = null; - } - return $defaults; - } - - /** - * Get default certificate path. - * - * @return string Default certificate path. - */ - public static function get_certificate_path() { - if (!empty(self::$certificate_path)) { - return self::$certificate_path; - } - - return __DIR__ . '/Requests/Transport/cacert.pem'; - } - - /** - * Set default certificate path. - * - * @param string $path Certificate path, pointing to a PEM file. - */ - public static function set_certificate_path($path) { - self::$certificate_path = $path; - } - - /** - * Set the default values - * - * @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 - * @param string $type HTTP request type - * @param array $options Options for the request - * @return array $options - */ - protected static function set_defaults(&$url, &$headers, &$data, &$type, &$options) { - if (!preg_match('/^http(s)?:\/\//i', $url, $matches)) { - throw new Requests_Exception('Only HTTP(S) requests are handled.', 'nonhttp', $url); - } - - if (empty($options['hooks'])) { - $options['hooks'] = new Requests_Hooks(); - } - - if (is_array($options['auth'])) { - $options['auth'] = new Requests_Auth_Basic($options['auth']); - } - if ($options['auth'] !== false) { - $options['auth']->register($options['hooks']); - } - - if (is_string($options['proxy']) || is_array($options['proxy'])) { - $options['proxy'] = new Requests_Proxy_HTTP($options['proxy']); - } - if ($options['proxy'] !== false) { - $options['proxy']->register($options['hooks']); - } - - if (is_array($options['cookies'])) { - $options['cookies'] = new Requests_Cookie_Jar($options['cookies']); - } - elseif (empty($options['cookies'])) { - $options['cookies'] = new Requests_Cookie_Jar(); - } - if ($options['cookies'] !== false) { - $options['cookies']->register($options['hooks']); - } - - if ($options['idn'] !== false) { - $iri = new Requests_IRI($url); - $iri->host = Requests_IDNAEncoder::encode($iri->ihost); - $url = $iri->uri; - } - - // Massage the type to ensure we support it. - $type = strtoupper($type); - - if (!isset($options['data_format'])) { - if (in_array($type, array(self::HEAD, self::GET, self::DELETE), true)) { - $options['data_format'] = 'query'; - } - else { - $options['data_format'] = 'body'; - } - } - } - - /** - * HTTP response parser - * - * @throws Requests_Exception On missing head/body separator (`requests.no_crlf_separator`) - * @throws Requests_Exception On missing head/body separator (`noversion`) - * @throws Requests_Exception On missing head/body separator (`toomanyredirects`) - * - * @param string $headers Full response text including headers and body - * @param string $url Original request URL - * @param array $req_headers Original $headers array passed to {@link request()}, in case we need to follow redirects - * @param array $req_data Original $data array passed to {@link request()}, in case we need to follow redirects - * @param array $options Original $options array passed to {@link request()}, in case we need to follow redirects - * @return Requests_Response - */ - protected static function parse_response($headers, $url, $req_headers, $req_data, $options) { - $return = new Requests_Response(); - if (!$options['blocking']) { - return $return; - } - - $return->raw = $headers; - $return->url = (string) $url; - $return->body = ''; - - if (!$options['filename']) { - $pos = strpos($headers, "\r\n\r\n"); - if ($pos === false) { - // Crap! - throw new Requests_Exception('Missing header/body separator', 'requests.no_crlf_separator'); - } - - $headers = substr($return->raw, 0, $pos); - // Headers will always be separated from the body by two new lines - `\n\r\n\r`. - $body = substr($return->raw, $pos + 4); - if (!empty($body)) { - $return->body = $body; - } - } - // Pretend CRLF = LF for compatibility (RFC 2616, section 19.3) - $headers = str_replace("\r\n", "\n", $headers); - // Unfold headers (replace [CRLF] 1*( SP | HT ) with SP) as per RFC 2616 (section 2.2) - $headers = preg_replace('/\n[ \t]/', ' ', $headers); - $headers = explode("\n", $headers); - preg_match('#^HTTP/(1\.\d)[ \t]+(\d+)#i', array_shift($headers), $matches); - if (empty($matches)) { - throw new Requests_Exception('Response could not be parsed', 'noversion', $headers); - } - $return->protocol_version = (float) $matches[1]; - $return->status_code = (int) $matches[2]; - if ($return->status_code >= 200 && $return->status_code < 300) { - $return->success = true; - } - - foreach ($headers as $header) { - list($key, $value) = explode(':', $header, 2); - $value = trim($value); - preg_replace('#(\s+)#i', ' ', $value); - $return->headers[$key] = $value; - } - if (isset($return->headers['transfer-encoding'])) { - $return->body = self::decode_chunked($return->body); - unset($return->headers['transfer-encoding']); - } - if (isset($return->headers['content-encoding'])) { - $return->body = self::decompress($return->body); - } - - //fsockopen and cURL compatibility - if (isset($return->headers['connection'])) { - unset($return->headers['connection']); - } - - $options['hooks']->dispatch('requests.before_redirect_check', array(&$return, $req_headers, $req_data, $options)); - - if ($return->is_redirect() && $options['follow_redirects'] === true) { - if (isset($return->headers['location']) && $options['redirected'] < $options['redirects']) { - if ($return->status_code === 303) { - $options['type'] = self::GET; - } - $options['redirected']++; - $location = $return->headers['location']; - if (strpos($location, 'http://') !== 0 && strpos($location, 'https://') !== 0) { - // relative redirect, for compatibility make it absolute - $location = Requests_IRI::absolutize($url, $location); - $location = $location->uri; - } - - $hook_args = array( - &$location, - &$req_headers, - &$req_data, - &$options, - $return, - ); - $options['hooks']->dispatch('requests.before_redirect', $hook_args); - $redirected = self::request($location, $req_headers, $req_data, $options['type'], $options); - $redirected->history[] = $return; - return $redirected; - } - elseif ($options['redirected'] >= $options['redirects']) { - throw new Requests_Exception('Too many redirects', 'toomanyredirects', $return); - } - } - - $return->redirects = $options['redirected']; - - $options['hooks']->dispatch('requests.after_request', array(&$return, $req_headers, $req_data, $options)); - return $return; - } - - /** - * Callback for `transport.internal.parse_response` - * - * Internal use only. Converts a raw HTTP response to a Requests_Response - * while still executing a multiple request. - * - * @param string $response Full response text including headers and body (will be overwritten with Response instance) - * @param array $request Request data as passed into {@see Requests::request_multiple()} - * @return null `$response` is either set to a Requests_Response instance, or a Requests_Exception object - */ - public static function parse_multiple(&$response, $request) { - try { - $url = $request['url']; - $headers = $request['headers']; - $data = $request['data']; - $options = $request['options']; - $response = self::parse_response($response, $url, $headers, $data, $options); - } - catch (Requests_Exception $e) { - $response = $e; - } - } - - /** - * Decoded a chunked body as per RFC 2616 - * - * @see https://tools.ietf.org/html/rfc2616#section-3.6.1 - * @param string $data Chunked body - * @return string Decoded body - */ - protected static function decode_chunked($data) { - if (!preg_match('/^([0-9a-f]+)(?:;(?:[\w-]*)(?:=(?:(?:[\w-]*)*|"(?:[^\r\n])*"))?)*\r\n/i', trim($data))) { - return $data; - } - - $decoded = ''; - $encoded = $data; - - while (true) { - $is_chunked = (bool) preg_match('/^([0-9a-f]+)(?:;(?:[\w-]*)(?:=(?:(?:[\w-]*)*|"(?:[^\r\n])*"))?)*\r\n/i', $encoded, $matches); - if (!$is_chunked) { - // Looks like it's not chunked after all - return $data; - } - - $length = hexdec(trim($matches[1])); - if ($length === 0) { - // Ignore trailer headers - return $decoded; - } - - $chunk_length = strlen($matches[0]); - $decoded .= substr($encoded, $chunk_length, $length); - $encoded = substr($encoded, $chunk_length + $length + 2); - - if (trim($encoded) === '0' || empty($encoded)) { - return $decoded; - } - } - - // We'll never actually get down here - // @codeCoverageIgnoreStart - } - // @codeCoverageIgnoreEnd - - /** - * Convert a key => value array to a 'key: value' array for headers - * - * @param array $array Dictionary of header values - * @return array List of headers - */ - public static function flatten($array) { - $return = array(); - foreach ($array as $key => $value) { - $return[] = sprintf('%s: %s', $key, $value); - } - return $return; - } - - /** - * Decompress an encoded body - * - * Implements gzip, compress and deflate. Guesses which it is by attempting - * to decode. - * - * @param string $data Compressed data in one of the above formats - * @return string Decompressed string - */ - public static function decompress($data) { - if (substr($data, 0, 2) !== "\x1f\x8b" && substr($data, 0, 2) !== "\x78\x9c") { - // Not actually compressed. Probably cURL ruining this for us. - return $data; - } - - if (function_exists('gzdecode')) { - $decoded = @gzdecode($data); - if ($decoded !== false) { - return $decoded; - } - } - - if (function_exists('gzinflate')) { - $decoded = @gzinflate($data); - if ($decoded !== false) { - return $decoded; - } - } - - $decoded = self::compatible_gzinflate($data); - if ($decoded !== false) { - return $decoded; - } - - if (function_exists('gzuncompress')) { - $decoded = @gzuncompress($data); - if ($decoded !== false) { - return $decoded; - } - } - - return $data; - } - - /** - * Decompression of deflated string while staying compatible with the majority of servers. - * - * Certain Servers will return deflated data with headers which PHP's gzinflate() - * function cannot handle out of the box. The following function has been created from - * various snippets on the gzinflate() PHP documentation. - * - * Warning: Magic numbers within. Due to the potential different formats that the compressed - * data may be returned in, some "magic offsets" are needed to ensure proper decompression - * takes place. For a simple progmatic way to determine the magic offset in use, see: - * https://core.trac.wordpress.org/ticket/18273 - * - * @since 2.8.1 - * @link https://core.trac.wordpress.org/ticket/18273 - * @link https://www.php.net/gzinflate#70875 - * @link https://www.php.net/gzinflate#77336 - * - * @param string $gz_data String to decompress. - * @return string|bool False on failure. - */ - public static function compatible_gzinflate($gz_data) { - // Compressed data might contain a full zlib header, if so strip it for - // gzinflate() - if (substr($gz_data, 0, 3) === "\x1f\x8b\x08") { - $i = 10; - $flg = ord(substr($gz_data, 3, 1)); - if ($flg > 0) { - if ($flg & 4) { - list($xlen) = unpack('v', substr($gz_data, $i, 2)); - $i += 2 + $xlen; - } - if ($flg & 8) { - $i = strpos($gz_data, "\0", $i) + 1; - } - if ($flg & 16) { - $i = strpos($gz_data, "\0", $i) + 1; - } - if ($flg & 2) { - $i += 2; - } - } - $decompressed = self::compatible_gzinflate(substr($gz_data, $i)); - if ($decompressed !== false) { - return $decompressed; - } - } - - // If the data is Huffman Encoded, we must first strip the leading 2 - // byte Huffman marker for gzinflate() - // The response is Huffman coded by many compressors such as - // java.util.zip.Deflater, Ruby’s Zlib::Deflate, and .NET's - // System.IO.Compression.DeflateStream. - // - // See https://decompres.blogspot.com/ for a quick explanation of this - // data type - $huffman_encoded = false; - - // low nibble of first byte should be 0x08 - list(, $first_nibble) = unpack('h', $gz_data); - - // First 2 bytes should be divisible by 0x1F - list(, $first_two_bytes) = unpack('n', $gz_data); - - if ($first_nibble === 0x08 && ($first_two_bytes % 0x1F) === 0) { - $huffman_encoded = true; - } - - if ($huffman_encoded) { - $decompressed = @gzinflate(substr($gz_data, 2)); - if ($decompressed !== false) { - return $decompressed; - } - } - - if (substr($gz_data, 0, 4) === "\x50\x4b\x03\x04") { - // ZIP file format header - // Offset 6: 2 bytes, General-purpose field - // Offset 26: 2 bytes, filename length - // Offset 28: 2 bytes, optional field length - // Offset 30: Filename field, followed by optional field, followed - // immediately by data - list(, $general_purpose_flag) = unpack('v', substr($gz_data, 6, 2)); - - // If the file has been compressed on the fly, 0x08 bit is set of - // the general purpose field. We can use this to differentiate - // between a compressed document, and a ZIP file - $zip_compressed_on_the_fly = ((0x08 & $general_purpose_flag) === 0x08); - - if (!$zip_compressed_on_the_fly) { - // Don't attempt to decode a compressed zip file - return $gz_data; - } - - // Determine the first byte of data, based on the above ZIP header - // offsets: - $first_file_start = array_sum(unpack('v2', substr($gz_data, 26, 4))); - $decompressed = @gzinflate(substr($gz_data, 30 + $first_file_start)); - if ($decompressed !== false) { - return $decompressed; - } - return false; - } - - // Finally fall back to straight gzinflate - $decompressed = @gzinflate($gz_data); - if ($decompressed !== false) { - return $decompressed; - } - - // Fallback for all above failing, not expected, but included for - // debugging and preventing regressions and to track stats - $decompressed = @gzinflate(substr($gz_data, 2)); - if ($decompressed !== false) { - return $decompressed; - } - - return false; - } - - public static function match_domain($host, $reference) { - // Check for a direct match - if ($host === $reference) { - return true; - } - - // Calculate the valid wildcard match if the host is not an IP address - // Also validates that the host has 3 parts or more, as per Firefox's - // ruleset. - $parts = explode('.', $host); - if (ip2long($host) === false && count($parts) >= 3) { - $parts[0] = '*'; - $wildcard = implode('.', $parts); - if ($wildcard === $reference) { - return true; - } - } - - return false; - } } diff --git a/src/Requests.php b/src/Requests.php new file mode 100644 index 000000000..3a2499cb5 --- /dev/null +++ b/src/Requests.php @@ -0,0 +1,968 @@ +dispatch('requests.before_request', array(&$url, &$headers, &$data, &$type, &$options)); + + if (!empty($options['transport'])) { + $transport = $options['transport']; + + if (is_string($options['transport'])) { + $transport = new $transport(); + } + } + else { + $need_ssl = (stripos($url, 'https://') === 0); + $capabilities = array('ssl' => $need_ssl); + $transport = self::get_transport($capabilities); + } + $response = $transport->request($url, $headers, $data, $options); + + $options['hooks']->dispatch('requests.before_parse', array(&$response, $url, $headers, $data, $type, $options)); + + return self::parse_response($response, $url, $headers, $data, $options); + } + + /** + * Send multiple HTTP requests simultaneously + * + * The `$requests` parameter takes an associative or indexed array of + * request fields. The key of each request can be used to match up the + * request with the returned data, or with the request passed into your + * `multiple.request.complete` callback. + * + * The request fields value is an associative array with the following keys: + * + * - `url`: Request URL Same as the `$url` parameter to + * {@see Requests::request} + * (string, required) + * - `headers`: Associative array of header fields. Same as the `$headers` + * parameter to {@see Requests::request} + * (array, default: `array()`) + * - `data`: Associative array of data fields or a string. Same as the + * `$data` parameter to {@see Requests::request} + * (array|string, default: `array()`) + * - `type`: HTTP request type (use Requests constants). Same as the `$type` + * parameter to {@see Requests::request} + * (string, default: `Requests::GET`) + * - `cookies`: Associative array of cookie name to value, or cookie jar. + * (array|Requests_Cookie_Jar) + * + * If the `$options` parameter is specified, individual requests will + * inherit options from it. This can be used to use a single hooking system, + * or set all the types to `Requests::POST`, for example. + * + * In addition, the `$options` parameter takes the following global options: + * + * - `complete`: A callback for when a request is complete. Takes two + * parameters, a Requests_Response/Requests_Exception reference, and the + * ID from the request array (Note: this can also be overridden on a + * per-request basis, although that's a little silly) + * (callback) + * + * @param array $requests Requests data (see description for more information) + * @param array $options Global and default options (see {@see Requests::request}) + * @return array Responses (either Requests_Response or a Requests_Exception object) + */ + public static function request_multiple($requests, $options = array()) { + $options = array_merge(self::get_default_options(true), $options); + + if (!empty($options['hooks'])) { + $options['hooks']->register('transport.internal.parse_response', array(static::class, 'parse_multiple')); + if (!empty($options['complete'])) { + $options['hooks']->register('multiple.request.complete', $options['complete']); + } + } + + foreach ($requests as $id => &$request) { + if (!isset($request['headers'])) { + $request['headers'] = array(); + } + if (!isset($request['data'])) { + $request['data'] = array(); + } + if (!isset($request['type'])) { + $request['type'] = self::GET; + } + if (!isset($request['options'])) { + $request['options'] = $options; + $request['options']['type'] = $request['type']; + } + else { + if (empty($request['options']['type'])) { + $request['options']['type'] = $request['type']; + } + $request['options'] = array_merge($options, $request['options']); + } + + self::set_defaults($request['url'], $request['headers'], $request['data'], $request['type'], $request['options']); + + // Ensure we only hook in once + if ($request['options']['hooks'] !== $options['hooks']) { + $request['options']['hooks']->register('transport.internal.parse_response', array(static::class, 'parse_multiple')); + if (!empty($request['options']['complete'])) { + $request['options']['hooks']->register('multiple.request.complete', $request['options']['complete']); + } + } + } + unset($request); + + if (!empty($options['transport'])) { + $transport = $options['transport']; + + if (is_string($options['transport'])) { + $transport = new $transport(); + } + } + else { + $transport = self::get_transport(); + } + $responses = $transport->request_multiple($requests, $options); + + foreach ($responses as $id => &$response) { + // If our hook got messed with somehow, ensure we end up with the + // correct response + if (is_string($response)) { + $request = $requests[$id]; + self::parse_multiple($response, $request); + $request['options']['hooks']->dispatch('multiple.request.complete', array(&$response, $id)); + } + } + + return $responses; + } + + /** + * Get the default options + * + * @see Requests::request() for values returned by this method + * @param boolean $multirequest Is this a multirequest? + * @return array Default option values + */ + protected static function get_default_options($multirequest = false) { + $defaults = array( + 'timeout' => 10, + 'connect_timeout' => 10, + 'useragent' => 'php-requests/' . self::VERSION, + 'protocol_version' => 1.1, + 'redirected' => 0, + 'redirects' => 10, + 'follow_redirects' => true, + 'blocking' => true, + 'type' => self::GET, + 'filename' => false, + 'auth' => false, + 'proxy' => false, + 'cookies' => false, + 'max_bytes' => false, + 'idn' => true, + 'hooks' => null, + 'transport' => null, + 'verify' => self::get_certificate_path(), + 'verifyname' => true, + ); + if ($multirequest !== false) { + $defaults['complete'] = null; + } + return $defaults; + } + + /** + * Get default certificate path. + * + * @return string Default certificate path. + */ + public static function get_certificate_path() { + if (!empty(self::$certificate_path)) { + return self::$certificate_path; + } + + return dirname(__DIR__) . '/library/Requests/Transport/cacert.pem'; + } + + /** + * Set default certificate path. + * + * @param string $path Certificate path, pointing to a PEM file. + */ + public static function set_certificate_path($path) { + self::$certificate_path = $path; + } + + /** + * Set the default values + * + * @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 + * @param string $type HTTP request type + * @param array $options Options for the request + * @return array $options + */ + protected static function set_defaults(&$url, &$headers, &$data, &$type, &$options) { + if (!preg_match('/^http(s)?:\/\//i', $url, $matches)) { + throw new Requests_Exception('Only HTTP(S) requests are handled.', 'nonhttp', $url); + } + + if (empty($options['hooks'])) { + $options['hooks'] = new Requests_Hooks(); + } + + if (is_array($options['auth'])) { + $options['auth'] = new Requests_Auth_Basic($options['auth']); + } + if ($options['auth'] !== false) { + $options['auth']->register($options['hooks']); + } + + if (is_string($options['proxy']) || is_array($options['proxy'])) { + $options['proxy'] = new Requests_Proxy_HTTP($options['proxy']); + } + if ($options['proxy'] !== false) { + $options['proxy']->register($options['hooks']); + } + + if (is_array($options['cookies'])) { + $options['cookies'] = new Requests_Cookie_Jar($options['cookies']); + } + elseif (empty($options['cookies'])) { + $options['cookies'] = new Requests_Cookie_Jar(); + } + if ($options['cookies'] !== false) { + $options['cookies']->register($options['hooks']); + } + + if ($options['idn'] !== false) { + $iri = new Requests_IRI($url); + $iri->host = Requests_IDNAEncoder::encode($iri->ihost); + $url = $iri->uri; + } + + // Massage the type to ensure we support it. + $type = strtoupper($type); + + if (!isset($options['data_format'])) { + if (in_array($type, array(self::HEAD, self::GET, self::DELETE), true)) { + $options['data_format'] = 'query'; + } + else { + $options['data_format'] = 'body'; + } + } + } + + /** + * HTTP response parser + * + * @throws Requests_Exception On missing head/body separator (`requests.no_crlf_separator`) + * @throws Requests_Exception On missing head/body separator (`noversion`) + * @throws Requests_Exception On missing head/body separator (`toomanyredirects`) + * + * @param string $headers Full response text including headers and body + * @param string $url Original request URL + * @param array $req_headers Original $headers array passed to {@link request()}, in case we need to follow redirects + * @param array $req_data Original $data array passed to {@link request()}, in case we need to follow redirects + * @param array $options Original $options array passed to {@link request()}, in case we need to follow redirects + * @return Requests_Response + */ + protected static function parse_response($headers, $url, $req_headers, $req_data, $options) { + $return = new Requests_Response(); + if (!$options['blocking']) { + return $return; + } + + $return->raw = $headers; + $return->url = (string) $url; + $return->body = ''; + + if (!$options['filename']) { + $pos = strpos($headers, "\r\n\r\n"); + if ($pos === false) { + // Crap! + throw new Requests_Exception('Missing header/body separator', 'requests.no_crlf_separator'); + } + + $headers = substr($return->raw, 0, $pos); + // Headers will always be separated from the body by two new lines - `\n\r\n\r`. + $body = substr($return->raw, $pos + 4); + if (!empty($body)) { + $return->body = $body; + } + } + // Pretend CRLF = LF for compatibility (RFC 2616, section 19.3) + $headers = str_replace("\r\n", "\n", $headers); + // Unfold headers (replace [CRLF] 1*( SP | HT ) with SP) as per RFC 2616 (section 2.2) + $headers = preg_replace('/\n[ \t]/', ' ', $headers); + $headers = explode("\n", $headers); + preg_match('#^HTTP/(1\.\d)[ \t]+(\d+)#i', array_shift($headers), $matches); + if (empty($matches)) { + throw new Requests_Exception('Response could not be parsed', 'noversion', $headers); + } + $return->protocol_version = (float) $matches[1]; + $return->status_code = (int) $matches[2]; + if ($return->status_code >= 200 && $return->status_code < 300) { + $return->success = true; + } + + foreach ($headers as $header) { + list($key, $value) = explode(':', $header, 2); + $value = trim($value); + preg_replace('#(\s+)#i', ' ', $value); + $return->headers[$key] = $value; + } + if (isset($return->headers['transfer-encoding'])) { + $return->body = self::decode_chunked($return->body); + unset($return->headers['transfer-encoding']); + } + if (isset($return->headers['content-encoding'])) { + $return->body = self::decompress($return->body); + } + + //fsockopen and cURL compatibility + if (isset($return->headers['connection'])) { + unset($return->headers['connection']); + } + + $options['hooks']->dispatch('requests.before_redirect_check', array(&$return, $req_headers, $req_data, $options)); + + if ($return->is_redirect() && $options['follow_redirects'] === true) { + if (isset($return->headers['location']) && $options['redirected'] < $options['redirects']) { + if ($return->status_code === 303) { + $options['type'] = self::GET; + } + $options['redirected']++; + $location = $return->headers['location']; + if (strpos($location, 'http://') !== 0 && strpos($location, 'https://') !== 0) { + // relative redirect, for compatibility make it absolute + $location = Requests_IRI::absolutize($url, $location); + $location = $location->uri; + } + + $hook_args = array( + &$location, + &$req_headers, + &$req_data, + &$options, + $return, + ); + $options['hooks']->dispatch('requests.before_redirect', $hook_args); + $redirected = self::request($location, $req_headers, $req_data, $options['type'], $options); + $redirected->history[] = $return; + return $redirected; + } + elseif ($options['redirected'] >= $options['redirects']) { + throw new Requests_Exception('Too many redirects', 'toomanyredirects', $return); + } + } + + $return->redirects = $options['redirected']; + + $options['hooks']->dispatch('requests.after_request', array(&$return, $req_headers, $req_data, $options)); + return $return; + } + + /** + * Callback for `transport.internal.parse_response` + * + * Internal use only. Converts a raw HTTP response to a Requests_Response + * while still executing a multiple request. + * + * @param string $response Full response text including headers and body (will be overwritten with Response instance) + * @param array $request Request data as passed into {@see Requests::request_multiple()} + * @return null `$response` is either set to a Requests_Response instance, or a Requests_Exception object + */ + public static function parse_multiple(&$response, $request) { + try { + $url = $request['url']; + $headers = $request['headers']; + $data = $request['data']; + $options = $request['options']; + $response = self::parse_response($response, $url, $headers, $data, $options); + } + catch (Requests_Exception $e) { + $response = $e; + } + } + + /** + * Decoded a chunked body as per RFC 2616 + * + * @see https://tools.ietf.org/html/rfc2616#section-3.6.1 + * @param string $data Chunked body + * @return string Decoded body + */ + protected static function decode_chunked($data) { + if (!preg_match('/^([0-9a-f]+)(?:;(?:[\w-]*)(?:=(?:(?:[\w-]*)*|"(?:[^\r\n])*"))?)*\r\n/i', trim($data))) { + return $data; + } + + $decoded = ''; + $encoded = $data; + + while (true) { + $is_chunked = (bool) preg_match('/^([0-9a-f]+)(?:;(?:[\w-]*)(?:=(?:(?:[\w-]*)*|"(?:[^\r\n])*"))?)*\r\n/i', $encoded, $matches); + if (!$is_chunked) { + // Looks like it's not chunked after all + return $data; + } + + $length = hexdec(trim($matches[1])); + if ($length === 0) { + // Ignore trailer headers + return $decoded; + } + + $chunk_length = strlen($matches[0]); + $decoded .= substr($encoded, $chunk_length, $length); + $encoded = substr($encoded, $chunk_length + $length + 2); + + if (trim($encoded) === '0' || empty($encoded)) { + return $decoded; + } + } + + // We'll never actually get down here + // @codeCoverageIgnoreStart + } + // @codeCoverageIgnoreEnd + + /** + * Convert a key => value array to a 'key: value' array for headers + * + * @param array $array Dictionary of header values + * @return array List of headers + */ + public static function flatten($array) { + $return = array(); + foreach ($array as $key => $value) { + $return[] = sprintf('%s: %s', $key, $value); + } + return $return; + } + + /** + * Decompress an encoded body + * + * Implements gzip, compress and deflate. Guesses which it is by attempting + * to decode. + * + * @param string $data Compressed data in one of the above formats + * @return string Decompressed string + */ + public static function decompress($data) { + if (substr($data, 0, 2) !== "\x1f\x8b" && substr($data, 0, 2) !== "\x78\x9c") { + // Not actually compressed. Probably cURL ruining this for us. + return $data; + } + + if (function_exists('gzdecode')) { + $decoded = @gzdecode($data); + if ($decoded !== false) { + return $decoded; + } + } + + if (function_exists('gzinflate')) { + $decoded = @gzinflate($data); + if ($decoded !== false) { + return $decoded; + } + } + + $decoded = self::compatible_gzinflate($data); + if ($decoded !== false) { + return $decoded; + } + + if (function_exists('gzuncompress')) { + $decoded = @gzuncompress($data); + if ($decoded !== false) { + return $decoded; + } + } + + return $data; + } + + /** + * Decompression of deflated string while staying compatible with the majority of servers. + * + * Certain Servers will return deflated data with headers which PHP's gzinflate() + * function cannot handle out of the box. The following function has been created from + * various snippets on the gzinflate() PHP documentation. + * + * Warning: Magic numbers within. Due to the potential different formats that the compressed + * data may be returned in, some "magic offsets" are needed to ensure proper decompression + * takes place. For a simple progmatic way to determine the magic offset in use, see: + * https://core.trac.wordpress.org/ticket/18273 + * + * @since 2.8.1 + * @link https://core.trac.wordpress.org/ticket/18273 + * @link https://www.php.net/gzinflate#70875 + * @link https://www.php.net/gzinflate#77336 + * + * @param string $gz_data String to decompress. + * @return string|bool False on failure. + */ + public static function compatible_gzinflate($gz_data) { + // Compressed data might contain a full zlib header, if so strip it for + // gzinflate() + if (substr($gz_data, 0, 3) === "\x1f\x8b\x08") { + $i = 10; + $flg = ord(substr($gz_data, 3, 1)); + if ($flg > 0) { + if ($flg & 4) { + list($xlen) = unpack('v', substr($gz_data, $i, 2)); + $i += 2 + $xlen; + } + if ($flg & 8) { + $i = strpos($gz_data, "\0", $i) + 1; + } + if ($flg & 16) { + $i = strpos($gz_data, "\0", $i) + 1; + } + if ($flg & 2) { + $i += 2; + } + } + $decompressed = self::compatible_gzinflate(substr($gz_data, $i)); + if ($decompressed !== false) { + return $decompressed; + } + } + + // If the data is Huffman Encoded, we must first strip the leading 2 + // byte Huffman marker for gzinflate() + // The response is Huffman coded by many compressors such as + // java.util.zip.Deflater, Ruby’s Zlib::Deflate, and .NET's + // System.IO.Compression.DeflateStream. + // + // See https://decompres.blogspot.com/ for a quick explanation of this + // data type + $huffman_encoded = false; + + // low nibble of first byte should be 0x08 + list(, $first_nibble) = unpack('h', $gz_data); + + // First 2 bytes should be divisible by 0x1F + list(, $first_two_bytes) = unpack('n', $gz_data); + + if ($first_nibble === 0x08 && ($first_two_bytes % 0x1F) === 0) { + $huffman_encoded = true; + } + + if ($huffman_encoded) { + $decompressed = @gzinflate(substr($gz_data, 2)); + if ($decompressed !== false) { + return $decompressed; + } + } + + if (substr($gz_data, 0, 4) === "\x50\x4b\x03\x04") { + // ZIP file format header + // Offset 6: 2 bytes, General-purpose field + // Offset 26: 2 bytes, filename length + // Offset 28: 2 bytes, optional field length + // Offset 30: Filename field, followed by optional field, followed + // immediately by data + list(, $general_purpose_flag) = unpack('v', substr($gz_data, 6, 2)); + + // If the file has been compressed on the fly, 0x08 bit is set of + // the general purpose field. We can use this to differentiate + // between a compressed document, and a ZIP file + $zip_compressed_on_the_fly = ((0x08 & $general_purpose_flag) === 0x08); + + if (!$zip_compressed_on_the_fly) { + // Don't attempt to decode a compressed zip file + return $gz_data; + } + + // Determine the first byte of data, based on the above ZIP header + // offsets: + $first_file_start = array_sum(unpack('v2', substr($gz_data, 26, 4))); + $decompressed = @gzinflate(substr($gz_data, 30 + $first_file_start)); + if ($decompressed !== false) { + return $decompressed; + } + return false; + } + + // Finally fall back to straight gzinflate + $decompressed = @gzinflate($gz_data); + if ($decompressed !== false) { + return $decompressed; + } + + // Fallback for all above failing, not expected, but included for + // debugging and preventing regressions and to track stats + $decompressed = @gzinflate(substr($gz_data, 2)); + if ($decompressed !== false) { + return $decompressed; + } + + return false; + } + + public static function match_domain($host, $reference) { + // Check for a direct match + if ($host === $reference) { + return true; + } + + // Calculate the valid wildcard match if the host is not an IP address + // Also validates that the host has 3 parts or more, as per Firefox's + // ruleset. + $parts = explode('.', $host); + if (ip2long($host) === false && count($parts) >= 3) { + $parts[0] = '*'; + $wildcard = implode('.', $parts); + if ($wildcard === $reference) { + return true; + } + } + + return false; + } +} From b5b5c6d70973d1d0ba566688e98b9b8790d5efa9 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 26 Aug 2021 03:50:50 +0200 Subject: [PATCH 07/45] Requests: update all references to the class * Update references and code samples in the documentation. * Update code samples in the example files. * Update existing `use` statements referencing the class. * Add `use` import statements to all non-namespaced classes using the `Requests` class. --- README.md | 4 ++-- docs/authentication-custom.md | 2 +- docs/authentication.md | 6 +++--- docs/hooks.md | 2 +- docs/proxy.md | 4 ++-- docs/usage-advanced.md | 6 +++--- docs/usage.md | 10 +++++----- docs/why-requests.md | 4 ++-- examples/basic-auth.php | 2 +- examples/cookie.php | 2 +- examples/cookie_jar.php | 2 +- examples/get.php | 2 +- examples/multiple.php | 2 +- examples/post.php | 2 +- examples/proxy.php | 2 +- examples/timeout.php | 2 +- library/Requests/Auth.php | 4 ++-- library/Requests/Proxy.php | 4 ++-- library/Requests/Response.php | 4 ++-- library/Requests/Session.php | 14 ++++++++------ library/Requests/Transport.php | 4 ++-- library/Requests/Transport/cURL.php | 8 +++++--- library/Requests/Transport/fsockopen.php | 6 ++++-- src/Requests.php | 24 ++++++++++++------------ tests/Auth/BasicTest.php | 2 +- tests/ChunkedEncodingTest.php | 2 +- tests/CookiesTest.php | 2 +- tests/EncodingTest.php | 2 +- tests/Proxy/HTTPTest.php | 2 +- tests/RequestsTest.php | 2 +- tests/Transport/BaseTestCase.php | 2 +- tests/Transport/CURLTest.php | 2 +- tests/Transport/FsockopenTest.php | 2 +- 33 files changed, 73 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index f743eebeb..28cfa6cd4 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ We all have better things to do. That's why Requests was born. ```php $headers = array('Accept' => 'application/json'); $options = array('auth' => array('user', 'pass')); -$request = Requests::get('https://api.github.com/gists', $headers, $options); +$request = WpOrg\Requests\Requests::get('https://api.github.com/gists', $headers, $options); var_dump($request->status_code); // int(200) @@ -119,7 +119,7 @@ The best place to start is our [prose-based documentation][], which will guide you through using Requests. After that, take a look at [the documentation for -`Requests::request()`][request_method], where all the parameters are fully +`\WpOrg\Requests\Requests::request()`][request_method], where all the parameters are fully documented. Requests is [100% documented with PHPDoc](https://requests.ryanmccue.info/api/). diff --git a/docs/authentication-custom.md b/docs/authentication-custom.md index e4f55854a..c562e2e74 100644 --- a/docs/authentication-custom.md +++ b/docs/authentication-custom.md @@ -35,7 +35,7 @@ We then use this in our request calls like this: $options = array( 'auth' => new MySoftware_Auth_Hotdog('yummy') ); -$response = Requests::get('http://hotdogbin.org/admin', array(), $options); +$response = WpOrg\Requests\Requests::get('http://hotdogbin.org/admin', array(), $options); ``` For more information on how to register and use hooks, see the [hooking diff --git a/docs/authentication.md b/docs/authentication.md index fd49391ef..8514741b5 100644 --- a/docs/authentication.md +++ b/docs/authentication.md @@ -10,7 +10,7 @@ A Basic authenticated call can be made like this: $options = array( 'auth' => new Requests_Auth_Basic(array('user', 'password')) ); -Requests::get('https://httpbin.org/basic-auth/user/password', array(), $options); +WpOrg\Requests\Requests::get('https://httpbin.org/basic-auth/user/password', array(), $options); ``` As Basic authentication is usually what you want when you specify a username @@ -20,14 +20,14 @@ and password, you can also just pass in an array as a shorthand: $options = array( 'auth' => array('user', 'password') ); -Requests::get('https://httpbin.org/basic-auth/user/password', array(), $options); +WpOrg\Requests\Requests::get('https://httpbin.org/basic-auth/user/password', array(), $options); ``` Note that `POST`/`PUT` requests take a `$data` parameter, so you need to pass that before `$options`: ```php -Requests::post('https://httpbin.org/basic-auth/user/password', array(), null, $options); +WpOrg\Requests\Requests::post('https://httpbin.org/basic-auth/user/password', array(), null, $options); ``` *** diff --git a/docs/hooks.md b/docs/hooks.md index 8772a0bfe..f59bc74d1 100644 --- a/docs/hooks.md +++ b/docs/hooks.md @@ -95,7 +95,7 @@ and pass the object in via the `'hooks'` option. $hooks = new Requests_Hooks(); $hooks->register('requests.after_request', 'mycallback'); -$request = Requests::get('https://httpbin.org/get', array(), array('hooks' => $hooks)); +$request = WpOrg\Requests\Requests::get('https://httpbin.org/get', array(), array('hooks' => $hooks)); ``` *** diff --git a/docs/proxy.md b/docs/proxy.md index 31e2a725c..4459c4a19 100644 --- a/docs/proxy.md +++ b/docs/proxy.md @@ -9,7 +9,7 @@ To make requests through an open proxy, specify the following options: $options = array( 'proxy' => '127.0.0.1:3128' ); -Requests::get('https://httpbin.org/ip', array(), $options); +WpOrg\Requests\Requests::get('https://httpbin.org/ip', array(), $options); ``` If your proxy needs you to authenticate, the option will become an array like @@ -19,7 +19,7 @@ in the following example: $options = array( 'proxy' => array( '127.0.0.1:3128', 'my_username', 'my_password' ) ); -Requests::get('https://httpbin.org/ip', array(), $options); +WpOrg\Requests\Requests::get('https://httpbin.org/ip', array(), $options); ``` *** diff --git a/docs/usage-advanced.md b/docs/usage-advanced.md index 114a3fa90..ea084dd95 100644 --- a/docs/usage-advanced.md +++ b/docs/usage-advanced.md @@ -19,7 +19,7 @@ $response = $session->get('/zen'); You can use the `url`, `headers`, `data` and `options` properties of the Session object to set the defaults for this session, and the constructor also takes -parameters in the same order as `Requests::request()`. Accessing any other +parameters in the same order as `WpOrg\Requests\Requests::request()`. Accessing any other properties will set the corresponding key in the options array; that is: ```php @@ -36,7 +36,7 @@ Secure Requests with SSL By default, HTTPS requests will use the most secure options available: ```php -$response = Requests::get('https://httpbin.org/'); +$response = WpOrg\Requests\Requests::get('https://httpbin.org/'); ``` Requests bundles certificates from the [Mozilla certificate authority list][], @@ -49,7 +49,7 @@ accepted by cURL and OpenSSL): $options = array( 'verify' => '/path/to/cacert.pem' ); -$response = Requests::get('https://httpbin.org/', array(), $options); +$response = WpOrg\Requests\Requests::get('https://httpbin.org/', array(), $options); ``` Alternatively, if you want to disable verification completely, this is possible diff --git a/docs/usage.md b/docs/usage.md index 95534005f..f16d78181 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -15,7 +15,7 @@ One of the most basic things you can do with HTTP is make a GET request. Let's grab GitHub's public events: ```php -$response = Requests::get('https://api.github.com/events'); +$response = WpOrg\Requests\Requests::get('https://api.github.com/events'); ``` `$response` is now a **Requests_Response** object. Response objects are what @@ -38,7 +38,7 @@ If you want to add custom headers to the request, simply pass them in as an associative array as the second parameter: ```php -$response = Requests::get('https://api.github.com/events', array('X-Requests' => 'Is Awesome!')); +$response = WpOrg\Requests\Requests::get('https://api.github.com/events', array('X-Requests' => 'Is Awesome!')); ``` @@ -47,7 +47,7 @@ Make a POST Request Making a POST request is very similar to making a GET: ```php -$response = Requests::post('https://httpbin.org/post'); +$response = WpOrg\Requests\Requests::post('https://httpbin.org/post'); ``` You'll probably also want to pass in some data. You can pass in either a @@ -58,7 +58,7 @@ internally) as the third parameter (after the URL and headers): ```php $data = array('key1' => 'value1', 'key2' => 'value2'); -$response = Requests::post('https://httpbin.org/post', array(), $data); +$response = WpOrg\Requests\Requests::post('https://httpbin.org/post', array(), $data); var_dump($response->body); ``` @@ -93,7 +93,7 @@ sending it: $url = 'https://api.github.com/some/endpoint'; $headers = array('Content-Type' => 'application/json'); $data = array('some' => 'data'); -$response = Requests::post($url, $headers, json_encode($data)); +$response = WpOrg\Requests\Requests::post($url, $headers, json_encode($data)); ``` Note that if you don't manually specify a Content-Type header, Requests has diff --git a/docs/why-requests.md b/docs/why-requests.md index 66fcc5c70..a5bcb05b6 100644 --- a/docs/why-requests.md +++ b/docs/why-requests.md @@ -89,8 +89,8 @@ instead of something else, in our opinion. Requests, on the other hand, exposes only a handful of classes to the global namespace, most of which are for internal use. You can learn to use - the `Requests::request()` method and the `Requests_Response` object in the - space of 10 minutes and you already know how to use Requests. + the `WpOrg\Requests\Requests::request()` method and the `Requests_Response` + object in the space of 10 minutes and you already know how to use Requests. ### Guzzle diff --git a/examples/basic-auth.php b/examples/basic-auth.php index 746cd944a..f512454f5 100644 --- a/examples/basic-auth.php +++ b/examples/basic-auth.php @@ -10,7 +10,7 @@ $options = array( 'auth' => array('someuser', 'password'), ); -$request = Requests::get('http://httpbin.org/basic-auth/someuser/password', array(), $options); +$request = WpOrg\Requests\Requests::get('http://httpbin.org/basic-auth/someuser/password', array(), $options); // Check what we received var_dump($request); diff --git a/examples/cookie.php b/examples/cookie.php index 306127a07..4c8024a79 100644 --- a/examples/cookie.php +++ b/examples/cookie.php @@ -10,7 +10,7 @@ $c = new Requests_Cookie('login_uid', 'something'); // Now let's make a request! -$request = Requests::get('http://httpbin.org/cookies', array('Cookie' => $c->format_for_header())); +$request = WpOrg\Requests\Requests::get('http://httpbin.org/cookies', array('Cookie' => $c->format_for_header())); // Check what we received var_dump($request); diff --git a/examples/cookie_jar.php b/examples/cookie_jar.php index 32e10d99c..1a5537697 100644 --- a/examples/cookie_jar.php +++ b/examples/cookie_jar.php @@ -10,7 +10,7 @@ $c = new Requests_Cookie_Jar(['login_uid' => 'something']); // Now let's make a request! -$request = Requests::get( +$request = WpOrg\Requests\Requests::get( 'http://httpbin.org/cookies', // Url [], // No need to set the headers the Jar does this for us ['cookies' => $c] // Pass in the Jar as an option diff --git a/examples/get.php b/examples/get.php index 71e3da8ea..f96d075cb 100644 --- a/examples/get.php +++ b/examples/get.php @@ -7,7 +7,7 @@ WpOrg\Requests\Autoload::register(); // Now let's make a request! -$request = Requests::get('http://httpbin.org/get', array('Accept' => 'application/json')); +$request = WpOrg\Requests\Requests::get('http://httpbin.org/get', array('Accept' => 'application/json')); // Check what we received var_dump($request); diff --git a/examples/multiple.php b/examples/multiple.php index b4ab09c37..7bdab33f1 100644 --- a/examples/multiple.php +++ b/examples/multiple.php @@ -35,7 +35,7 @@ function my_callback(&$request, $id) { ); // Send the request! -$responses = Requests::request_multiple($requests, $options); +$responses = WpOrg\Requests\Requests::request_multiple($requests, $options); // Note: the response from the above call will be an associative array matching // $requests with the response data, however we've already handled it in diff --git a/examples/post.php b/examples/post.php index 80e213fc7..47abd674d 100644 --- a/examples/post.php +++ b/examples/post.php @@ -7,7 +7,7 @@ WpOrg\Requests\Autoload::register(); // Now let's make a request! -$request = Requests::post('http://httpbin.org/post', array(), array('mydata' => 'something')); +$request = WpOrg\Requests\Requests::post('http://httpbin.org/post', array(), array('mydata' => 'something')); // Check what we received var_dump($request); diff --git a/examples/proxy.php b/examples/proxy.php index 4c97a3b07..226e29854 100644 --- a/examples/proxy.php +++ b/examples/proxy.php @@ -12,7 +12,7 @@ // If you need to authenticate, use the following syntax: // 'proxy' => array( '127.0.0.1:8080', 'username', 'password' ), ); -$request = Requests::get('http://httpbin.org/ip', array(), $options); +$request = WpOrg\Requests\Requests::get('http://httpbin.org/ip', array(), $options); // See result var_dump($request->body); diff --git a/examples/timeout.php b/examples/timeout.php index 4a00d1a6d..91d963353 100644 --- a/examples/timeout.php +++ b/examples/timeout.php @@ -12,6 +12,6 @@ ); // Now let's make a request to a page that will delay its response by 3 seconds -$request = Requests::get('http://httpbin.org/delay/3', array(), $options); +$request = WpOrg\Requests\Requests::get('http://httpbin.org/delay/3', array(), $options); // An exception will be thrown, stating a timeout of the request ! diff --git a/library/Requests/Auth.php b/library/Requests/Auth.php index 914c7449c..a376f4cc9 100644 --- a/library/Requests/Auth.php +++ b/library/Requests/Auth.php @@ -22,8 +22,8 @@ interface Requests_Auth { /** * Register hooks as needed * - * This method is called in {@see Requests::request} when the user has set - * an instance as the 'auth' option. Use this callback to register all the + * This method is called in {@see \WpOrg\Requests\Requests::request} when the user + * has set an instance as the 'auth' option. Use this callback to register all the * hooks you'll need. * * @see Requests_Hooks::register diff --git a/library/Requests/Proxy.php b/library/Requests/Proxy.php index 0a798dbe9..c72508348 100644 --- a/library/Requests/Proxy.php +++ b/library/Requests/Proxy.php @@ -24,8 +24,8 @@ interface Requests_Proxy { /** * Register hooks as needed * - * This method is called in {@see Requests::request} when the user has set - * an instance as the 'auth' option. Use this callback to register all the + * This method is called in {@see \WpOrg\Requests\Requests::request} when the user + * has set an instance as the 'auth' option. Use this callback to register all the * hooks you'll need. * * @see Requests_Hooks::register diff --git a/library/Requests/Response.php b/library/Requests/Response.php index 20861dfb8..a783f7329 100644 --- a/library/Requests/Response.php +++ b/library/Requests/Response.php @@ -2,14 +2,14 @@ /** * HTTP response class * - * Contains a response from Requests::request() + * Contains a response from \WpOrg\Requests\Requests::request() * @package Requests */ /** * HTTP response class * - * Contains a response from Requests::request() + * Contains a response from \WpOrg\Requests\Requests::request() * @package Requests */ class Requests_Response { diff --git a/library/Requests/Session.php b/library/Requests/Session.php index b2e10991d..d8d7fdf36 100644 --- a/library/Requests/Session.php +++ b/library/Requests/Session.php @@ -6,6 +6,8 @@ * @subpackage Session Handler */ +use WpOrg\Requests\Requests; + /** * Session handler for persistent requests and default parameters * @@ -190,15 +192,15 @@ public function patch($url, $headers, $data = array(), $options = array()) { * This method initiates a request and sends it via a transport before * parsing. * - * @see Requests::request() + * @see \WpOrg\Requests\Requests::request() * * @throws Requests_Exception On invalid URLs (`nonhttp`) * * @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 - * @param string $type HTTP request type (use Requests constants) - * @param array $options Options for the request (see {@see Requests::request}) + * @param string $type HTTP request type (use \WpOrg\Requests\Requests constants) + * @param array $options Options for the request (see {@see \WpOrg\Requests\Requests::request}) * @return Requests_Response */ public function request($url, $headers = array(), $data = array(), $type = Requests::GET, $options = array()) { @@ -210,10 +212,10 @@ public function request($url, $headers = array(), $data = array(), $type = Reque /** * Send multiple HTTP requests simultaneously * - * @see Requests::request_multiple() + * @see \WpOrg\Requests\Requests::request_multiple() * - * @param array $requests Requests data (see {@see Requests::request_multiple}) - * @param array $options Global and default options (see {@see Requests::request}) + * @param array $requests Requests data (see {@see \WpOrg\Requests\Requests::request_multiple}) + * @param array $options Global and default options (see {@see \WpOrg\Requests\Requests::request}) * @return array Responses (either Requests_Response or a Requests_Exception object) */ public function request_multiple($requests, $options = array()) { diff --git a/library/Requests/Transport.php b/library/Requests/Transport.php index 5146c01d8..076faa6db 100644 --- a/library/Requests/Transport.php +++ b/library/Requests/Transport.php @@ -19,7 +19,7 @@ interface Requests_Transport { * @param string $url URL to request * @param array $headers Associative array of request headers * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD - * @param array $options Request options, see {@see Requests::response()} for documentation + * @param array $options Request options, see {@see \WpOrg\Requests\Requests::response()} for documentation * @return string Raw HTTP result */ public function request($url, $headers = array(), $data = array(), $options = array()); @@ -28,7 +28,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar * Send multiple requests simultaneously * * @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per {@see Requests_Transport::request} - * @param array $options Global options, see {@see Requests::response()} for documentation + * @param array $options Global options, see {@see \WpOrg\Requests\Requests::response()} for documentation * @return array Array of Requests_Response objects (may contain Requests_Exception or string responses as well) */ public function request_multiple($requests, $options); diff --git a/library/Requests/Transport/cURL.php b/library/Requests/Transport/cURL.php index 757c0cfef..11d0e0d3c 100644 --- a/library/Requests/Transport/cURL.php +++ b/library/Requests/Transport/cURL.php @@ -6,6 +6,8 @@ * @subpackage Transport */ +use WpOrg\Requests\Requests; + /** * cURL HTTP transport * @@ -126,7 +128,7 @@ public function __destruct() { * @param string $url URL to request * @param array $headers Associative array of request headers * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD - * @param array $options Request options, see {@see Requests::response()} for documentation + * @param array $options Request options, see {@see \WpOrg\Requests\Requests::response()} for documentation * @return string Raw HTTP result */ public function request($url, $headers = array(), $data = array(), $options = array()) { @@ -304,7 +306,7 @@ public function request_multiple($requests, $options) { * @param string $url URL to request * @param array $headers Associative array of request headers * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD - * @param array $options Request options, see {@see Requests::response()} for documentation + * @param array $options Request options, see {@see \WpOrg\Requests\Requests::response()} for documentation * @return resource Subrequest's cURL handle */ public function &get_subrequest_handle($url, $headers, $data, $options) { @@ -331,7 +333,7 @@ public function &get_subrequest_handle($url, $headers, $data, $options) { * @param string $url URL to request * @param array $headers Associative array of request headers * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD - * @param array $options Request options, see {@see Requests::response()} for documentation + * @param array $options Request options, see {@see \WpOrg\Requests\Requests::response()} for documentation */ protected function setup_handle($url, $headers, $data, $options) { $options['hooks']->dispatch('curl.before_request', array(&$this->handle)); diff --git a/library/Requests/Transport/fsockopen.php b/library/Requests/Transport/fsockopen.php index 2927e5dc8..fd45fd0a4 100644 --- a/library/Requests/Transport/fsockopen.php +++ b/library/Requests/Transport/fsockopen.php @@ -6,6 +6,8 @@ * @subpackage Transport */ +use WpOrg\Requests\Requests; + /** * fsockopen HTTP transport * @@ -52,7 +54,7 @@ class Requests_Transport_fsockopen implements Requests_Transport { * @param string $url URL to request * @param array $headers Associative array of request headers * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD - * @param array $options Request options, see {@see Requests::response()} for documentation + * @param array $options Request options, see {@see \WpOrg\Requests\Requests::response()} for documentation * @return string Raw HTTP result */ public function request($url, $headers = array(), $data = array(), $options = array()) { @@ -326,7 +328,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar * Send multiple requests simultaneously * * @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per {@see Requests_Transport::request} - * @param array $options Global options, see {@see Requests::response()} for documentation + * @param array $options Global options, see {@see \WpOrg\Requests\Requests::response()} for documentation * @return array Array of Requests_Response objects (may contain Requests_Exception or string responses as well) */ public function request_multiple($requests, $options) { diff --git a/src/Requests.php b/src/Requests.php index 3a2499cb5..2a796798c 100644 --- a/src/Requests.php +++ b/src/Requests.php @@ -122,8 +122,8 @@ class Requests { /** * Default certificate path. * - * @see Requests::get_certificate_path() - * @see Requests::set_certificate_path() + * @see \WpOrg\Requests\Requests::get_certificate_path() + * @see \WpOrg\Requests\Requests::set_certificate_path() * * @var string */ @@ -376,23 +376,23 @@ public static function request($url, $headers = array(), $data = array(), $type * The request fields value is an associative array with the following keys: * * - `url`: Request URL Same as the `$url` parameter to - * {@see Requests::request} + * {@see \WpOrg\Requests\Requests::request} * (string, required) * - `headers`: Associative array of header fields. Same as the `$headers` - * parameter to {@see Requests::request} + * parameter to {@see \WpOrg\Requests\Requests::request} * (array, default: `array()`) * - `data`: Associative array of data fields or a string. Same as the - * `$data` parameter to {@see Requests::request} + * `$data` parameter to {@see \WpOrg\Requests\Requests::request} * (array|string, default: `array()`) - * - `type`: HTTP request type (use Requests constants). Same as the `$type` - * parameter to {@see Requests::request} - * (string, default: `Requests::GET`) + * - `type`: HTTP request type (use \WpOrg\Requests\Requests constants). Same as the `$type` + * parameter to {@see \WpOrg\Requests\Requests::request} + * (string, default: `\WpOrg\Requests\Requests::GET`) * - `cookies`: Associative array of cookie name to value, or cookie jar. * (array|Requests_Cookie_Jar) * * If the `$options` parameter is specified, individual requests will * inherit options from it. This can be used to use a single hooking system, - * or set all the types to `Requests::POST`, for example. + * or set all the types to `\WpOrg\Requests\Requests::POST`, for example. * * In addition, the `$options` parameter takes the following global options: * @@ -403,7 +403,7 @@ public static function request($url, $headers = array(), $data = array(), $type * (callback) * * @param array $requests Requests data (see description for more information) - * @param array $options Global and default options (see {@see Requests::request}) + * @param array $options Global and default options (see {@see \WpOrg\Requests\Requests::request}) * @return array Responses (either Requests_Response or a Requests_Exception object) */ public static function request_multiple($requests, $options = array()) { @@ -477,7 +477,7 @@ public static function request_multiple($requests, $options = array()) { /** * Get the default options * - * @see Requests::request() for values returned by this method + * @see \WpOrg\Requests\Requests::request() for values returned by this method * @param boolean $multirequest Is this a multirequest? * @return array Default option values */ @@ -710,7 +710,7 @@ protected static function parse_response($headers, $url, $req_headers, $req_data * while still executing a multiple request. * * @param string $response Full response text including headers and body (will be overwritten with Response instance) - * @param array $request Request data as passed into {@see Requests::request_multiple()} + * @param array $request Request data as passed into {@see \WpOrg\Requests\Requests::request_multiple()} * @return null `$response` is either set to a Requests_Response instance, or a Requests_Exception object */ public static function parse_multiple(&$response, $request) { diff --git a/tests/Auth/BasicTest.php b/tests/Auth/BasicTest.php index 80b315c9e..ba4ae8909 100644 --- a/tests/Auth/BasicTest.php +++ b/tests/Auth/BasicTest.php @@ -2,12 +2,12 @@ namespace Requests\Tests\Auth; -use Requests; use Requests\Tests\TestCase; use Requests_Auth_Basic; use Requests_Exception; use Requests_Transport_cURL; use Requests_Transport_fsockopen; +use WpOrg\Requests\Requests; class BasicTest extends TestCase { public static function transportProvider() { diff --git a/tests/ChunkedEncodingTest.php b/tests/ChunkedEncodingTest.php index f890c8b06..7b35648c3 100644 --- a/tests/ChunkedEncodingTest.php +++ b/tests/ChunkedEncodingTest.php @@ -2,9 +2,9 @@ namespace Requests\Tests; -use Requests; use Requests\Tests\Mock\TransportMock; use Requests\Tests\TestCase; +use WpOrg\Requests\Requests; class ChunkedDecodingTest extends TestCase { public static function chunkedProvider() { diff --git a/tests/CookiesTest.php b/tests/CookiesTest.php index 24655544a..b61128df1 100644 --- a/tests/CookiesTest.php +++ b/tests/CookiesTest.php @@ -2,7 +2,6 @@ namespace Requests\Tests; -use Requests; use Requests\Tests\TestCase; use Requests_Cookie; use Requests_Cookie_Jar; @@ -10,6 +9,7 @@ use Requests_IRI; use Requests_Response_Headers; use Requests_Utility_CaseInsensitiveDictionary; +use WpOrg\Requests\Requests; class CookiesTest extends TestCase { public function testBasicCookie() { diff --git a/tests/EncodingTest.php b/tests/EncodingTest.php index 74d2a65ac..38cc23f83 100644 --- a/tests/EncodingTest.php +++ b/tests/EncodingTest.php @@ -2,8 +2,8 @@ namespace Requests\Tests; -use Requests; use Requests\Tests\TestCase; +use WpOrg\Requests\Requests; class EncodingTest extends TestCase { protected static function mapData($type, $data) { diff --git a/tests/Proxy/HTTPTest.php b/tests/Proxy/HTTPTest.php index b25e81471..91efeaabd 100644 --- a/tests/Proxy/HTTPTest.php +++ b/tests/Proxy/HTTPTest.php @@ -2,12 +2,12 @@ namespace Requests\Tests\Proxy; -use Requests; use Requests\Tests\TestCase; use Requests_Exception; use Requests_Proxy_HTTP; use Requests_Transport_cURL; use Requests_Transport_fsockopen; +use WpOrg\Requests\Requests; class HTTPTest extends TestCase { protected function checkProxyAvailable($type = '') { diff --git a/tests/RequestsTest.php b/tests/RequestsTest.php index 849b7dac9..8b47256cf 100644 --- a/tests/RequestsTest.php +++ b/tests/RequestsTest.php @@ -2,12 +2,12 @@ namespace Requests\Tests; -use Requests; use Requests\Tests\Mock\RawTransportMock; use Requests\Tests\Mock\TransportMock; use Requests\Tests\TestCase; use Requests_Exception; use Requests_Response_Headers; +use WpOrg\Requests\Requests; class RequestsTest extends TestCase { diff --git a/tests/Transport/BaseTestCase.php b/tests/Transport/BaseTestCase.php index 1e41d6b81..f36a0911c 100644 --- a/tests/Transport/BaseTestCase.php +++ b/tests/Transport/BaseTestCase.php @@ -2,7 +2,6 @@ namespace Requests\Tests\Transport; -use Requests; use Requests\Tests\Mock\TransportMock; use Requests\Tests\TestCase; use Requests_Exception; @@ -10,6 +9,7 @@ use Requests_Hooks; use Requests_Response; use stdClass; +use WpOrg\Requests\Requests; abstract class BaseTestCase extends TestCase { public function set_up() { diff --git a/tests/Transport/CURLTest.php b/tests/Transport/CURLTest.php index 0125d0c80..7bbc9ddee 100644 --- a/tests/Transport/CURLTest.php +++ b/tests/Transport/CURLTest.php @@ -2,10 +2,10 @@ namespace Requests\Tests\Transport; -use Requests; use Requests\Tests\Transport\BaseTestCase; use Requests_Exception; use Requests_Transport_cURL; +use WpOrg\Requests\Requests; class CurlTest extends BaseTestCase { protected $transport = Requests_Transport_cURL::class; diff --git a/tests/Transport/FsockopenTest.php b/tests/Transport/FsockopenTest.php index ba0f03be2..54ca9152f 100644 --- a/tests/Transport/FsockopenTest.php +++ b/tests/Transport/FsockopenTest.php @@ -2,11 +2,11 @@ namespace Requests\Tests\Transport; -use Requests; use Requests\Tests\Transport\BaseTestCase; use Requests_Exception; use Requests_Hooks; use Requests_Transport_fsockopen; +use WpOrg\Requests\Requests; class FsockopenTest extends BaseTestCase { protected $transport = Requests_Transport_fsockopen::class; From a215268c6160c1ec5e99dd4dc95c6758b8241b43 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 24 Aug 2021 05:12:23 +0200 Subject: [PATCH 08/45] Requests: deprecate the remaining class Deprecate all functionality which remains in the old non-namespaced `Requests` class and add a deprecation notice for when people would still `require` the class directly. Includes: * Preventing the new `WpOrg\Requests\Autoload` file from throwing a duplicate deprecation notice during a page load, when the `Requests` class was loaded as well as a class which is being aliased. --- library/Requests.php | 19 +++++++++++++++++++ src/Autoload.php | 8 ++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/library/Requests.php b/library/Requests.php index b8635e0ee..c98a3da03 100644 --- a/library/Requests.php +++ b/library/Requests.php @@ -7,8 +7,24 @@ * Based on concepts from SimplePie_File, RequestCore and WP_Http. * * @package Requests + * + * @deprecated 2.0.0 */ +/* + * Integrators who cannot yet upgrade to the PSR-4 class names can silence deprecations + * by defining a `REQUESTS_SILENCE_PSR0_DEPRECATIONS` constant and setting it to `true`. + * The constant needs to be defined before this class is required. + */ +if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS') || REQUESTS_SILENCE_PSR0_DEPRECATIONS !== true) { + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error + trigger_error( + 'The PSR-0 `Requests_...` class names in the Request library are deprecated.' + . ' Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience.', + E_USER_DEPRECATED + ); +} + require_once dirname(__DIR__) . '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/src/Requests.php'; /** @@ -19,6 +35,9 @@ * Based on concepts from SimplePie_File, RequestCore and WP_Http. * * @package Requests + * + * @deprecated 2.0.0 Use `WpOrg\Requests\Requests` instead for the actual functionality and + * use `WpOrg\Requests\Autoload` for the autoloading. */ class Requests extends WpOrg\Requests\Requests { diff --git a/src/Autoload.php b/src/Autoload.php index e8a95add7..44c9dda7b 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -86,8 +86,12 @@ public static function load($class_name) { } if ($class_name === 'Requests') { - // Original PSR-0 Requests class. - $file = dirname(__DIR__) . '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/library/Requests.php'; + /* + * Reference to the original PSR-0 Requests class. + * Loading this class will always generate a deprecation notice. + */ + $file = dirname(__DIR__) . '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/library/Requests.php'; + self::$deprecation_thrown = true; } elseif ($psr_0_prefix_pos === 0) { // PSR-0 classname. $file = dirname(__DIR__) . '/library/' . str_replace('_', '/', $class_name) . '.php'; From eb65203723347ce4570a65b843008348c5efb367 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 22 Aug 2021 17:29:39 +0200 Subject: [PATCH 09/45] Move certificates file to `src` directory --- src/Requests.php | 4 ++-- {library/Requests => src}/Transport/cacert.pem | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename {library/Requests => src}/Transport/cacert.pem (100%) diff --git a/src/Requests.php b/src/Requests.php index 2a796798c..526f54c1e 100644 --- a/src/Requests.php +++ b/src/Requests.php @@ -320,7 +320,7 @@ public static function patch($url, $headers, $data = array(), $options = array() * certificate file as a string. (Using true uses the system-wide root * certificate store instead, but this may have different behaviour * across transports.) - * (string|boolean, default: library/Requests/Transport/cacert.pem) + * (string|boolean, default: src/Transport/cacert.pem) * - `verifyname`: Should we verify the common name in the SSL certificate? * (boolean, default: true) * - `data_format`: How should we send the `$data` parameter? @@ -519,7 +519,7 @@ public static function get_certificate_path() { return self::$certificate_path; } - return dirname(__DIR__) . '/library/Requests/Transport/cacert.pem'; + return __DIR__ . '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/Transport/cacert.pem'; } /** diff --git a/library/Requests/Transport/cacert.pem b/src/Transport/cacert.pem similarity index 100% rename from library/Requests/Transport/cacert.pem rename to src/Transport/cacert.pem From 3f213aeee01c089a5e8ec76946efb0d909614909 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 13 Aug 2021 09:17:50 +0200 Subject: [PATCH 10/45] Requests_Auth: change to namespaced interface Includes: * Moving the namespaced version of the interface to the `src` directory. * [Custom autoload] Adding the interface to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the interface to a new `Deprecated.php` file. Same as the custom autoloader when encountering deprecated classes, loading this file will also trigger a deprecation notice. And again, same as the custom autoloader, the deprecation notice can be silenced by setting a `REQUESTS_SILENCE_PSR0_DEPRECATIONS` constant. * Adding the new `Deprecated.php` file to the Composer `autoload` directive to be indexed to a classmap. * Updating all references to the interface. --- composer.json | 3 ++- docs/authentication-custom.md | 4 ++-- library/Deprecated.php | 30 ++++++++++++++++++++++++++++++ library/Requests/Auth/Basic.php | 4 +++- {library/Requests => src}/Auth.php | 6 +++++- src/Autoload.php | 5 ++++- src/Requests.php | 2 +- 7 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 library/Deprecated.php rename {library/Requests => src}/Auth.php (92%) diff --git a/composer.json b/composer.json index 12909af7e..9e40532e7 100644 --- a/composer.json +++ b/composer.json @@ -56,7 +56,8 @@ }, "psr-4": { "WpOrg\\Requests\\": "src/" - } + }, + "classmap": ["library/Deprecated.php"] }, "autoload-dev": { "psr-4": { diff --git a/docs/authentication-custom.md b/docs/authentication-custom.md index c562e2e74..7e913b733 100644 --- a/docs/authentication-custom.md +++ b/docs/authentication-custom.md @@ -1,7 +1,7 @@ Custom Authentication ===================== Custom authentication handlers are designed to be straight-forward to write. -In order to write a handler, you'll need to implement the `Requests_Auth` +In order to write a handler, you'll need to implement the `WpOrg\Requests\Auth` interface. An instance of this handler can then be passed to Requests via the `auth` @@ -12,7 +12,7 @@ authenticates the call if said header is set to `Yummy`. (I don't know of any services that do this; perhaps this is a market waiting to be tapped?) ```php -class MySoftware_Auth_Hotdog implements Requests_Auth { +class MySoftware_Auth_Hotdog implements WpOrg\Requests\Auth { protected $password; public function __construct($password) { diff --git a/library/Deprecated.php b/library/Deprecated.php new file mode 100644 index 000000000..f8ad2b81d --- /dev/null +++ b/library/Deprecated.php @@ -0,0 +1,30 @@ + '\WpOrg\Requests\Auth', + ); /** * Whether or not a deprecation notice has been thrown for calling a deprecated class. diff --git a/src/Requests.php b/src/Requests.php index 526f54c1e..8aae397d7 100644 --- a/src/Requests.php +++ b/src/Requests.php @@ -303,7 +303,7 @@ public static function patch($url, $headers, $data = array(), $options = array() * (string|boolean, default: false) * - `auth`: Authentication handler or array of user/password details to use * for Basic authentication - * (Requests_Auth|array|boolean, default: false) + * (\WpOrg\Requests\Auth|array|boolean, default: false) * - `proxy`: Proxy details to use for proxy by-passing and authentication * (Requests_Proxy|array|string|boolean, default: false) * - `max_bytes`: Limit for the response body size. From b96313d8c5b8e50150f436c044470108528457dc Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 13 Aug 2021 09:26:08 +0200 Subject: [PATCH 11/45] Requests_Hooker: change to namespaced interface Includes: * Moving the namespaced version of the interface to the `src` directory. * [Custom autoload] Adding the interface to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the interface to the `Deprecated.php` file. * Updating all references to the interface. * Adding an exclusion to the PHPCS file to allow the `Deprecated.php` file to contain multiple class declarations. --- .phpcs.xml.dist | 4 ++++ library/Deprecated.php | 1 + library/Requests/Cookie/Jar.php | 6 ++++-- library/Requests/Hooks.php | 4 +++- src/Autoload.php | 3 ++- {library/Requests => src}/Hooker.php | 4 +++- src/Requests.php | 2 +- 7 files changed, 18 insertions(+), 6 deletions(-) rename {library/Requests => src}/Hooker.php (93%) diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index a65c1296d..fd2b1458e 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -224,6 +224,10 @@ */Transport/fsockopen\.php$ + + */library/Deprecated\.php$ + + diff --git a/library/Deprecated.php b/library/Deprecated.php index f8ad2b81d..2492fbfa8 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -28,3 +28,4 @@ } interface Requests_Auth extends WpOrg\Requests\Auth {} +interface Requests_Hooker extends WpOrg\Requests\Hooker {} diff --git a/library/Requests/Cookie/Jar.php b/library/Requests/Cookie/Jar.php index 9fee6b741..e07682ff0 100644 --- a/library/Requests/Cookie/Jar.php +++ b/library/Requests/Cookie/Jar.php @@ -6,6 +6,8 @@ * @subpackage Cookies */ +use WpOrg\Requests\Hooker; + /** * Cookie holder object * @@ -109,9 +111,9 @@ public function getIterator() { /** * Register the cookie handler with the request's hooking system * - * @param Requests_Hooker $hooks Hooking system + * @param \WpOrg\Requests\Hooker $hooks Hooking system */ - public function register(Requests_Hooker $hooks) { + public function register(Hooker $hooks) { $hooks->register('requests.before_request', array($this, 'before_request')); $hooks->register('requests.before_redirect_check', array($this, 'before_redirect_check')); } diff --git a/library/Requests/Hooks.php b/library/Requests/Hooks.php index 2a5111b56..187cbd713 100644 --- a/library/Requests/Hooks.php +++ b/library/Requests/Hooks.php @@ -6,13 +6,15 @@ * @subpackage Utilities */ +use WpOrg\Requests\Hooker; + /** * Handles adding and dispatching events * * @package Requests * @subpackage Utilities */ -class Requests_Hooks implements Requests_Hooker { +class Requests_Hooks implements Hooker { /** * Registered callbacks for each hook * diff --git a/src/Autoload.php b/src/Autoload.php index 8beb6fcda..9e8584271 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -34,7 +34,8 @@ class Autoload { */ private static $deprecated_classes = array( // Interfaces. - 'requests_auth' => '\WpOrg\Requests\Auth', + 'requests_auth' => '\WpOrg\Requests\Auth', + 'requests_hooker' => '\WpOrg\Requests\Hooker', ); /** diff --git a/library/Requests/Hooker.php b/src/Hooker.php similarity index 93% rename from library/Requests/Hooker.php rename to src/Hooker.php index c852931f1..2ef5dd1ab 100644 --- a/library/Requests/Hooker.php +++ b/src/Hooker.php @@ -6,13 +6,15 @@ * @subpackage Utilities */ +namespace WpOrg\Requests; + /** * Event dispatcher * * @package Requests * @subpackage Utilities */ -interface Requests_Hooker { +interface Hooker { /** * Register a callback for a hook * diff --git a/src/Requests.php b/src/Requests.php index 8aae397d7..6c6b6bcbd 100644 --- a/src/Requests.php +++ b/src/Requests.php @@ -315,7 +315,7 @@ public static function patch($url, $headers, $data = array(), $options = array() * {@see getTransport()} * (string|Requests_Transport, default: {@see getTransport()}) * - `hooks`: Hooks handler. - * (Requests_Hooker, default: new Requests_Hooks()) + * (\WpOrg\Requests\Hooker, default: new Requests_Hooks()) * - `verify`: Should we verify SSL certificates? Allows passing in a custom * certificate file as a string. (Using true uses the system-wide root * certificate store instead, but this may have different behaviour From 77f194e7ef0ff467fef5e9b0b081fad0f5104e15 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 13 Aug 2021 09:42:37 +0200 Subject: [PATCH 12/45] Requests_Proxy: change to namespaced interface Includes: * Moving the namespaced version of the interface to the `src` directory. * [Custom autoload] Adding the interface to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the interface to the `Deprecated.php` file. * Updating all references to the interface. --- library/Deprecated.php | 1 + library/Requests/Proxy/HTTP.php | 4 +++- src/Autoload.php | 1 + {library/Requests => src}/Proxy.php | 6 +++++- src/Requests.php | 2 +- 5 files changed, 11 insertions(+), 3 deletions(-) rename {library/Requests => src}/Proxy.php (92%) diff --git a/library/Deprecated.php b/library/Deprecated.php index 2492fbfa8..09ff27d13 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -29,3 +29,4 @@ interface Requests_Auth extends WpOrg\Requests\Auth {} interface Requests_Hooker extends WpOrg\Requests\Hooker {} +interface Requests_Proxy extends WpOrg\Requests\Proxy {} diff --git a/library/Requests/Proxy/HTTP.php b/library/Requests/Proxy/HTTP.php index 25bb95a94..7a5c2bb8d 100644 --- a/library/Requests/Proxy/HTTP.php +++ b/library/Requests/Proxy/HTTP.php @@ -7,6 +7,8 @@ * @since 1.6 */ +use WpOrg\Requests\Proxy; + /** * HTTP Proxy connection interface * @@ -16,7 +18,7 @@ * @subpackage Proxy * @since 1.6 */ -class Requests_Proxy_HTTP implements Requests_Proxy { +class Requests_Proxy_HTTP implements Proxy { /** * Proxy host and port * diff --git a/src/Autoload.php b/src/Autoload.php index 9e8584271..d62965578 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -36,6 +36,7 @@ class Autoload { // Interfaces. 'requests_auth' => '\WpOrg\Requests\Auth', 'requests_hooker' => '\WpOrg\Requests\Hooker', + 'requests_proxy' => '\WpOrg\Requests\Proxy', ); /** diff --git a/library/Requests/Proxy.php b/src/Proxy.php similarity index 92% rename from library/Requests/Proxy.php rename to src/Proxy.php index c72508348..9b8fb41cc 100644 --- a/library/Requests/Proxy.php +++ b/src/Proxy.php @@ -7,6 +7,10 @@ * @since 1.6 */ +namespace WpOrg\Requests; + +use Requests_Hooks; + /** * Proxy connection interface * @@ -20,7 +24,7 @@ * @subpackage Proxy * @since 1.6 */ -interface Requests_Proxy { +interface Proxy { /** * Register hooks as needed * diff --git a/src/Requests.php b/src/Requests.php index 6c6b6bcbd..bf2293411 100644 --- a/src/Requests.php +++ b/src/Requests.php @@ -305,7 +305,7 @@ public static function patch($url, $headers, $data = array(), $options = array() * for Basic authentication * (\WpOrg\Requests\Auth|array|boolean, default: false) * - `proxy`: Proxy details to use for proxy by-passing and authentication - * (Requests_Proxy|array|string|boolean, default: false) + * (\WpOrg\Requests\Proxy|array|string|boolean, default: false) * - `max_bytes`: Limit for the response body size. * (integer|boolean, default: false) * - `idn`: Enable IDN parsing From 94fad44a2b1c2e453a155b3dd3e101d446e40b7e Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 13 Aug 2021 09:51:40 +0200 Subject: [PATCH 13/45] Requests_Transport: change to namespaced interface Includes: * Moving the namespaced version of the interface to the `src` directory. * [Custom autoload] Adding the interface to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the interface to the `Deprecated.php` file. * Updating all references to the interface. --- library/Deprecated.php | 1 + library/Requests/Transport/cURL.php | 3 ++- library/Requests/Transport/fsockopen.php | 5 +++-- src/Autoload.php | 7 ++++--- src/Requests.php | 6 +++--- {library/Requests => src}/Transport.php | 6 ++++-- tests/Mock/RawTransportMock.php | 4 ++-- tests/Mock/TransportMock.php | 4 ++-- 8 files changed, 21 insertions(+), 15 deletions(-) rename {library/Requests => src}/Transport.php (90%) diff --git a/library/Deprecated.php b/library/Deprecated.php index 09ff27d13..3eb060f5c 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -30,3 +30,4 @@ interface Requests_Auth extends WpOrg\Requests\Auth {} interface Requests_Hooker extends WpOrg\Requests\Hooker {} interface Requests_Proxy extends WpOrg\Requests\Proxy {} +interface Requests_Transport extends WpOrg\Requests\Transport {} diff --git a/library/Requests/Transport/cURL.php b/library/Requests/Transport/cURL.php index 11d0e0d3c..40e392c33 100644 --- a/library/Requests/Transport/cURL.php +++ b/library/Requests/Transport/cURL.php @@ -7,6 +7,7 @@ */ use WpOrg\Requests\Requests; +use WpOrg\Requests\Transport; /** * cURL HTTP transport @@ -14,7 +15,7 @@ * @package Requests * @subpackage Transport */ -class Requests_Transport_cURL implements Requests_Transport { +class Requests_Transport_cURL implements Transport { const CURL_7_10_5 = 0x070A05; const CURL_7_16_2 = 0x071002; diff --git a/library/Requests/Transport/fsockopen.php b/library/Requests/Transport/fsockopen.php index fd45fd0a4..d1166f385 100644 --- a/library/Requests/Transport/fsockopen.php +++ b/library/Requests/Transport/fsockopen.php @@ -7,6 +7,7 @@ */ use WpOrg\Requests\Requests; +use WpOrg\Requests\Transport; /** * fsockopen HTTP transport @@ -14,7 +15,7 @@ * @package Requests * @subpackage Transport */ -class Requests_Transport_fsockopen implements Requests_Transport { +class Requests_Transport_fsockopen implements Transport { /** * Second to microsecond conversion * @@ -327,7 +328,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar /** * Send multiple requests simultaneously * - * @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per {@see Requests_Transport::request} + * @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per {@see \WpOrg\Requests\Transport::request} * @param array $options Global options, see {@see \WpOrg\Requests\Requests::response()} for documentation * @return array Array of Requests_Response objects (may contain Requests_Exception or string responses as well) */ diff --git a/src/Autoload.php b/src/Autoload.php index d62965578..9bec8c73d 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -34,9 +34,10 @@ class Autoload { */ private static $deprecated_classes = array( // Interfaces. - 'requests_auth' => '\WpOrg\Requests\Auth', - 'requests_hooker' => '\WpOrg\Requests\Hooker', - 'requests_proxy' => '\WpOrg\Requests\Proxy', + 'requests_auth' => '\WpOrg\Requests\Auth', + 'requests_hooker' => '\WpOrg\Requests\Hooker', + 'requests_proxy' => '\WpOrg\Requests\Proxy', + 'requests_transport' => '\WpOrg\Requests\Transport', ); /** diff --git a/src/Requests.php b/src/Requests.php index bf2293411..97f028961 100644 --- a/src/Requests.php +++ b/src/Requests.php @@ -139,7 +139,7 @@ private function __construct() {} /** * Register a transport * - * @param string $transport Transport class to add, must support the Requests_Transport interface + * @param string $transport Transport class to add, must support the \WpOrg\Requests\Transport interface */ public static function add_transport($transport) { if (empty(self::$transports)) { @@ -156,7 +156,7 @@ public static function add_transport($transport) { * Get a working transport * * @throws Requests_Exception If no valid transport is found (`notransport`) - * @return Requests_Transport + * @return \WpOrg\Requests\Transport */ protected static function get_transport($capabilities = array()) { // Caching code, don't bother testing coverage @@ -313,7 +313,7 @@ public static function patch($url, $headers, $data = array(), $options = array() * - `transport`: Custom transport. Either a class name, or a * transport object. Defaults to the first working transport from * {@see getTransport()} - * (string|Requests_Transport, default: {@see getTransport()}) + * (string|\WpOrg\Requests\Transport, default: {@see getTransport()}) * - `hooks`: Hooks handler. * (\WpOrg\Requests\Hooker, default: new Requests_Hooks()) * - `verify`: Should we verify SSL certificates? Allows passing in a custom diff --git a/library/Requests/Transport.php b/src/Transport.php similarity index 90% rename from library/Requests/Transport.php rename to src/Transport.php index 076faa6db..08175a38d 100644 --- a/library/Requests/Transport.php +++ b/src/Transport.php @@ -6,13 +6,15 @@ * @subpackage Transport */ +namespace WpOrg\Requests; + /** * Base HTTP transport * * @package Requests * @subpackage Transport */ -interface Requests_Transport { +interface Transport { /** * Perform a request * @@ -27,7 +29,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar /** * Send multiple requests simultaneously * - * @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per {@see Requests_Transport::request} + * @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per {@see \WpOrg\Requests\Transport::request} * @param array $options Global options, see {@see \WpOrg\Requests\Requests::response()} for documentation * @return array Array of Requests_Response objects (may contain Requests_Exception or string responses as well) */ diff --git a/tests/Mock/RawTransportMock.php b/tests/Mock/RawTransportMock.php index c807e01c9..a9150bafa 100644 --- a/tests/Mock/RawTransportMock.php +++ b/tests/Mock/RawTransportMock.php @@ -2,9 +2,9 @@ namespace Requests\Tests\Mock; -use Requests_Transport; +use WpOrg\Requests\Transport; -class RawTransportMock implements Requests_Transport { +class RawTransportMock implements Transport { public $data = ''; public function request($url, $headers = array(), $data = array(), $options = array()) { return $this->data; diff --git a/tests/Mock/TransportMock.php b/tests/Mock/TransportMock.php index a0af0a186..b3c2dc406 100644 --- a/tests/Mock/TransportMock.php +++ b/tests/Mock/TransportMock.php @@ -2,9 +2,9 @@ namespace Requests\Tests\Mock; -use Requests_Transport; +use WpOrg\Requests\Transport; -class TransportMock implements Requests_Transport { +class TransportMock implements Transport { public $code = 200; public $chunked = false; public $body = 'Test Body'; From 930e1d07d4b6f6847fd7ca15a24262ab6dcf831d Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 13 Aug 2021 10:06:09 +0200 Subject: [PATCH 14/45] Requests_Cookie: change to namespaced class Includes: * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. --- examples/cookie.php | 2 +- library/Deprecated.php | 2 ++ library/Requests/Cookie/Jar.php | 13 ++++++----- src/Autoload.php | 3 +++ {library/Requests => src}/Cookie.php | 12 +++++++--- tests/CookiesTest.php | 34 ++++++++++++++-------------- 6 files changed, 39 insertions(+), 27 deletions(-) rename {library/Requests => src}/Cookie.php (97%) diff --git a/examples/cookie.php b/examples/cookie.php index 4c8024a79..943f6d6eb 100644 --- a/examples/cookie.php +++ b/examples/cookie.php @@ -7,7 +7,7 @@ WpOrg\Requests\Autoload::register(); // Say you need to fake a login cookie -$c = new Requests_Cookie('login_uid', 'something'); +$c = new WpOrg\Requests\Cookie('login_uid', 'something'); // Now let's make a request! $request = WpOrg\Requests\Requests::get('http://httpbin.org/cookies', array('Cookie' => $c->format_for_header())); diff --git a/library/Deprecated.php b/library/Deprecated.php index 3eb060f5c..804d6fd8a 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -31,3 +31,5 @@ interface Requests_Auth extends WpOrg\Requests\Auth {} interface Requests_Hooker extends WpOrg\Requests\Hooker {} interface Requests_Proxy extends WpOrg\Requests\Proxy {} interface Requests_Transport extends WpOrg\Requests\Transport {} + +class Requests_Cookie extends WpOrg\Requests\Cookie {} diff --git a/library/Requests/Cookie/Jar.php b/library/Requests/Cookie/Jar.php index e07682ff0..26098a1b3 100644 --- a/library/Requests/Cookie/Jar.php +++ b/library/Requests/Cookie/Jar.php @@ -6,6 +6,7 @@ * @subpackage Cookies */ +use WpOrg\Requests\Cookie; use WpOrg\Requests\Hooker; /** @@ -32,17 +33,17 @@ public function __construct($cookies = array()) { } /** - * Normalise cookie data into a Requests_Cookie + * Normalise cookie data into a \WpOrg\Requests\Cookie * - * @param string|Requests_Cookie $cookie - * @return Requests_Cookie + * @param string|\WpOrg\Requests\Cookie $cookie + * @return \WpOrg\Requests\Cookie */ public function normalize_cookie($cookie, $key = null) { - if ($cookie instanceof Requests_Cookie) { + if ($cookie instanceof Cookie) { return $cookie; } - return Requests_Cookie::parse($cookie, $key); + return Cookie::parse($cookie, $key); } /** @@ -164,7 +165,7 @@ public function before_redirect_check(Requests_Response $return) { $url = new Requests_IRI($url); } - $cookies = Requests_Cookie::parse_from_headers($return->headers, $url); + $cookies = Cookie::parse_from_headers($return->headers, $url); $this->cookies = array_merge($this->cookies, $cookies); $return->cookies = $this; } diff --git a/src/Autoload.php b/src/Autoload.php index 9bec8c73d..5b39c0e29 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -38,6 +38,9 @@ class Autoload { 'requests_hooker' => '\WpOrg\Requests\Hooker', 'requests_proxy' => '\WpOrg\Requests\Proxy', 'requests_transport' => '\WpOrg\Requests\Transport', + + // Classes. + 'requests_cookie' => '\WpOrg\Requests\Cookie', ); /** diff --git a/library/Requests/Cookie.php b/src/Cookie.php similarity index 97% rename from library/Requests/Cookie.php rename to src/Cookie.php index d46956753..e88bddb47 100644 --- a/library/Requests/Cookie.php +++ b/src/Cookie.php @@ -6,13 +6,19 @@ * @subpackage Cookies */ +namespace WpOrg\Requests; + +use Requests_IRI; +use Requests_Response_Headers; +use Requests_Utility_CaseInsensitiveDictionary; + /** * Cookie storage object * * @package Requests * @subpackage Cookies */ -class Requests_Cookie { +class Cookie { /** * Cookie name. * @@ -360,7 +366,7 @@ public function __toString() { * specifies some of this handling, but not in a thorough manner. * * @param string Cookie header value (from a Set-Cookie header) - * @return Requests_Cookie Parsed cookie object + * @return \WpOrg\Requests\Cookie Parsed cookie object */ public static function parse($string, $name = '', $reference_time = null) { $parts = explode(';', $string); @@ -403,7 +409,7 @@ public static function parse($string, $name = '', $reference_time = null) { } } - return new Requests_Cookie($name, $value, $attributes, array(), $reference_time); + return new static($name, $value, $attributes, array(), $reference_time); } /** diff --git a/tests/CookiesTest.php b/tests/CookiesTest.php index b61128df1..f3be72f3b 100644 --- a/tests/CookiesTest.php +++ b/tests/CookiesTest.php @@ -3,17 +3,17 @@ namespace Requests\Tests; use Requests\Tests\TestCase; -use Requests_Cookie; use Requests_Cookie_Jar; use Requests_Exception; use Requests_IRI; use Requests_Response_Headers; use Requests_Utility_CaseInsensitiveDictionary; +use WpOrg\Requests\Cookie; use WpOrg\Requests\Requests; class CookiesTest extends TestCase { public function testBasicCookie() { - $cookie = new Requests_Cookie('requests-testcookie', 'testvalue'); + $cookie = new Cookie('requests-testcookie', 'testvalue'); $this->assertSame('requests-testcookie', $cookie->name); $this->assertSame('testvalue', $cookie->value); @@ -28,20 +28,20 @@ public function testCookieWithAttributes() { 'httponly', 'path' => '/', ); - $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes); + $cookie = new Cookie('requests-testcookie', 'testvalue', $attributes); $this->assertSame('requests-testcookie=testvalue', $cookie->format_for_header()); $this->assertSame('requests-testcookie=testvalue; httponly; path=/', $cookie->format_for_set_cookie()); } public function testEmptyCookieName() { - $cookie = Requests_Cookie::parse('test'); + $cookie = Cookie::parse('test'); $this->assertSame('', $cookie->name); $this->assertSame('test', $cookie->value); } public function testEmptyAttributes() { - $cookie = Requests_Cookie::parse('foo=bar; HttpOnly'); + $cookie = Cookie::parse('foo=bar; HttpOnly'); $this->assertTrue($cookie->attributes['httponly']); } @@ -198,7 +198,7 @@ public function testSendingMultipleCookiesWithJar() { public function testSendingPrebakedCookie() { $cookies = new Requests_Cookie_Jar( array( - new Requests_Cookie('requests-testcookie', 'testvalue'), + new Cookie('requests-testcookie', 'testvalue'), ) ); $data = $this->setCookieRequest($cookies); @@ -238,7 +238,7 @@ public function domainMatchProvider() { public function testDomainExactMatch($original, $check, $matches, $domain_matches) { $attributes = new Requests_Utility_CaseInsensitiveDictionary(); $attributes['domain'] = $original; - $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes); + $cookie = new Cookie('requests-testcookie', 'testvalue', $attributes); $this->assertSame($matches, $cookie->domain_matches($check)); } @@ -251,7 +251,7 @@ public function testDomainMatch($original, $check, $matches, $domain_matches) { $flags = array( 'host-only' => false, ); - $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes, $flags); + $cookie = new Cookie('requests-testcookie', 'testvalue', $attributes, $flags); $this->assertSame($domain_matches, $cookie->domain_matches($check)); } @@ -281,7 +281,7 @@ public function pathMatchProvider() { public function testPathMatch($original, $check, $matches) { $attributes = new Requests_Utility_CaseInsensitiveDictionary(); $attributes['path'] = $original; - $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes); + $cookie = new Cookie('requests-testcookie', 'testvalue', $attributes); $this->assertSame($matches, $cookie->path_matches($check)); } @@ -322,7 +322,7 @@ public function testUrlExactMatch($domain, $path, $check, $matches, $domain_matc $attributes['domain'] = $domain; $attributes['path'] = $path; $check = new Requests_IRI($check); - $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes); + $cookie = new Cookie('requests-testcookie', 'testvalue', $attributes); $this->assertSame($matches, $cookie->uri_matches($check)); } @@ -339,7 +339,7 @@ public function testUrlMatch($domain, $path, $check, $matches, $domain_matches) 'host-only' => false, ); $check = new Requests_IRI($check); - $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes, $flags); + $cookie = new Cookie('requests-testcookie', 'testvalue', $attributes, $flags); $this->assertSame($domain_matches, $cookie->uri_matches($check)); } @@ -351,7 +351,7 @@ public function testUrlMatchSecure() { $flags = array( 'host-only' => false, ); - $cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes, $flags); + $cookie = new Cookie('requests-testcookie', 'testvalue', $attributes, $flags); $this->assertTrue($cookie->uri_matches(new Requests_IRI('https://example.com/'))); $this->assertFalse($cookie->uri_matches(new Requests_IRI('http://example.com/'))); @@ -369,7 +369,7 @@ public function testUrlMatchSecure() { * should be regarded as "global" cookies (that is, set for `.`) */ public function testUrlMatchManuallySet() { - $cookie = new Requests_Cookie('requests-testcookie', 'testvalue'); + $cookie = new Cookie('requests-testcookie', 'testvalue'); $this->assertTrue($cookie->domain_matches('example.com')); $this->assertTrue($cookie->domain_matches('example.net')); $this->assertTrue($cookie->path_matches('/')); @@ -495,7 +495,7 @@ public function testParsingHeader($header, $expected, $expected_attributes = arr // Set the reference time to 2014-01-01 00:00:00 $reference_time = gmmktime(0, 0, 0, 1, 1, 2014); - $cookie = Requests_Cookie::parse($header, null, $reference_time); + $cookie = Cookie::parse($header, null, $reference_time); $this->check_parsed_cookie($cookie, $expected, $expected_attributes); } @@ -508,7 +508,7 @@ public function testParsingHeaderDouble($header, $expected, $expected_attributes // Set the reference time to 2014-01-01 00:00:00 $reference_time = gmmktime(0, 0, 0, 1, 1, 2014); - $cookie = Requests_Cookie::parse($header, null, $reference_time); + $cookie = Cookie::parse($header, null, $reference_time); // Normalize the value again $cookie->normalize(); @@ -526,7 +526,7 @@ public function testParsingHeaderObject($header, $expected, $expected_attributes // Set the reference time to 2014-01-01 00:00:00 $reference_time = gmmktime(0, 0, 0, 1, 1, 2014); - $parsed = Requests_Cookie::parse_from_headers($headers, null, $reference_time); + $parsed = Cookie::parse_from_headers($headers, null, $reference_time); $this->assertCount(1, $parsed); $cookie = reset($parsed); @@ -654,7 +654,7 @@ public function testParsingHeaderWithOrigin($header, $origin, $expected, $expect // Set the reference time to 2014-01-01 00:00:00 $reference_time = gmmktime(0, 0, 0, 1, 1, 2014); - $parsed = Requests_Cookie::parse_from_headers($headers, $origin, $reference_time); + $parsed = Cookie::parse_from_headers($headers, $origin, $reference_time); if (isset($expected['invalid'])) { $this->assertCount(0, $parsed); return; From c198a683cc77d4f286f155cc646a13161b7f5a3f Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 13 Aug 2021 10:10:36 +0200 Subject: [PATCH 15/45] Requests_Exception: change to namespaced class Includes: * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. --- library/Deprecated.php | 1 + library/Requests/Auth/Basic.php | 5 +-- library/Requests/Cookie/Jar.php | 5 +-- library/Requests/Exception/HTTP.php | 4 ++- library/Requests/Exception/Transport.php | 4 ++- library/Requests/IDNAEncoder.php | 36 ++++++++++--------- library/Requests/IRI.php | 4 ++- library/Requests/Proxy/HTTP.php | 5 +-- library/Requests/Response.php | 6 ++-- library/Requests/Response/Headers.php | 6 ++-- library/Requests/SSL.php | 2 +- library/Requests/Session.php | 4 +-- library/Requests/Transport/cURL.php | 11 +++--- library/Requests/Transport/fsockopen.php | 27 +++++++------- .../Utility/CaseInsensitiveDictionary.php | 6 ++-- src/Autoload.php | 1 + {library/Requests => src}/Exception.php | 6 +++- src/Requests.php | 30 ++++++++-------- src/Transport.php | 2 +- tests/Auth/BasicTest.php | 4 +-- tests/CookiesTest.php | 4 +-- tests/IDNAEncoderTest.php | 18 +++++----- tests/Proxy/HTTPTest.php | 6 ++-- tests/RequestsTest.php | 12 +++---- tests/Response/HeadersTest.php | 4 +-- tests/Transport/BaseTestCase.php | 26 +++++++------- tests/Transport/CURLTest.php | 10 +++--- tests/Transport/FsockopenTest.php | 10 +++--- 28 files changed, 142 insertions(+), 117 deletions(-) rename {library/Requests => src}/Exception.php (90%) diff --git a/library/Deprecated.php b/library/Deprecated.php index 804d6fd8a..554d2be98 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -33,3 +33,4 @@ interface Requests_Proxy extends WpOrg\Requests\Proxy {} interface Requests_Transport extends WpOrg\Requests\Transport {} class Requests_Cookie extends WpOrg\Requests\Cookie {} +class Requests_Exception extends WpOrg\Requests\Exception {} diff --git a/library/Requests/Auth/Basic.php b/library/Requests/Auth/Basic.php index ac100784c..508211a10 100644 --- a/library/Requests/Auth/Basic.php +++ b/library/Requests/Auth/Basic.php @@ -7,6 +7,7 @@ */ use WpOrg\Requests\Auth; +use WpOrg\Requests\Exception; /** * Basic Authentication provider @@ -35,13 +36,13 @@ class Requests_Auth_Basic implements Auth { /** * Constructor * - * @throws Requests_Exception On incorrect number of arguments (`authbasicbadargs`) + * @throws \WpOrg\Requests\Exception On incorrect number of arguments (`authbasicbadargs`) * @param array|null $args Array of user and password. Must have exactly two elements */ public function __construct($args = null) { if (is_array($args)) { if (count($args) !== 2) { - throw new Requests_Exception('Invalid number of arguments', 'authbasicbadargs'); + throw new Exception('Invalid number of arguments', 'authbasicbadargs'); } list($this->user, $this->pass) = $args; diff --git a/library/Requests/Cookie/Jar.php b/library/Requests/Cookie/Jar.php index 26098a1b3..3c4cce214 100644 --- a/library/Requests/Cookie/Jar.php +++ b/library/Requests/Cookie/Jar.php @@ -7,6 +7,7 @@ */ use WpOrg\Requests\Cookie; +use WpOrg\Requests\Exception; use WpOrg\Requests\Hooker; /** @@ -75,7 +76,7 @@ public function offsetGet($key) { /** * Set the given item * - * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`) + * @throws \WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`) * * @param string $key Item name * @param string $value Item value @@ -83,7 +84,7 @@ public function offsetGet($key) { #[ReturnTypeWillChange] public function offsetSet($key, $value) { if ($key === null) { - throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset'); + throw new Exception('Object is a dictionary, not a list', 'invalidset'); } $this->cookies[$key] = $value; diff --git a/library/Requests/Exception/HTTP.php b/library/Requests/Exception/HTTP.php index be9388ef9..174e40b1e 100644 --- a/library/Requests/Exception/HTTP.php +++ b/library/Requests/Exception/HTTP.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception; + /** * Exception based on HTTP response * * @package Requests */ -class Requests_Exception_HTTP extends Requests_Exception { +class Requests_Exception_HTTP extends Exception { /** * HTTP status code * diff --git a/library/Requests/Exception/Transport.php b/library/Requests/Exception/Transport.php index e60b4884c..a592a9c2d 100644 --- a/library/Requests/Exception/Transport.php +++ b/library/Requests/Exception/Transport.php @@ -1,5 +1,7 @@ 0) { if ($position + $length > $strlen) { - throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); + throw new Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); } for ($position++; $remaining > 0; $position++) { $value = ord($input[$position]); // If it is invalid, count the sequence as invalid and reprocess the current byte: if (($value & 0xC0) !== 0x80) { - throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); + throw new Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); } --$remaining; @@ -208,7 +210,7 @@ protected static function utf8_to_codepoints($input) { || $character > 0xEFFFD ) ) { - throw new Requests_Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); + throw new Exception('Invalid Unicode codepoint', 'idna.invalidcodepoint', $character); } $codepoints[] = $character; @@ -221,7 +223,7 @@ protected static function utf8_to_codepoints($input) { * RFC3492-compliant encoder * * @internal Pseudo-code from Section 6.3 is commented with "#" next to relevant code - * @throws Requests_Exception On character outside of the domain (never happens with Punycode) (`idna.character_outside_domain`) + * @throws \WpOrg\Requests\Exception On character outside of the domain (never happens with Punycode) (`idna.character_outside_domain`) * * @param string $input UTF-8 encoded string to encode * @return string Punycode-encoded string @@ -252,7 +254,7 @@ public static function punycode_encode($input) { // This never occurs for Punycode, so ignore in coverage // @codeCoverageIgnoreStart elseif ($char < $n) { - throw new Requests_Exception('Invalid character', 'idna.character_outside_domain', $char); + throw new Exception('Invalid character', 'idna.character_outside_domain', $char); } // @codeCoverageIgnoreEnd else { @@ -333,7 +335,7 @@ public static function punycode_encode($input) { * Convert a digit to its respective character * * @see https://tools.ietf.org/html/rfc3492#section-5 - * @throws Requests_Exception On invalid digit (`idna.invalid_digit`) + * @throws \WpOrg\Requests\Exception On invalid digit (`idna.invalid_digit`) * * @param int $digit Digit in the range 0-35 * @return string Single character corresponding to digit @@ -342,7 +344,7 @@ protected static function digit_to_char($digit) { // @codeCoverageIgnoreStart // As far as I know, this never happens, but still good to be sure. if ($digit < 0 || $digit > 35) { - throw new Requests_Exception(sprintf('Invalid digit %d', $digit), 'idna.invalid_digit', $digit); + throw new Exception(sprintf('Invalid digit %d', $digit), 'idna.invalid_digit', $digit); } // @codeCoverageIgnoreEnd $digits = 'abcdefghijklmnopqrstuvwxyz0123456789'; diff --git a/library/Requests/IRI.php b/library/Requests/IRI.php index 749b0bcf5..539c3f114 100644 --- a/library/Requests/IRI.php +++ b/library/Requests/IRI.php @@ -6,6 +6,8 @@ * @subpackage Utilities */ +use WpOrg\Requests\Exception; + /** * IRI parser/serialiser/normaliser * @@ -330,7 +332,7 @@ protected function parse_iri($iri) { $iri = trim($iri, "\x20\x09\x0A\x0C\x0D"); $has_match = preg_match('/^((?P[^:\/?#]+):)?(\/\/(?P[^\/?#]*))?(?P[^?#]*)(\?(?P[^#]*))?(#(?P.*))?$/', $iri, $match); if (!$has_match) { - throw new Requests_Exception('Cannot parse supplied IRI', 'iri.cannot_parse', $iri); + throw new Exception('Cannot parse supplied IRI', 'iri.cannot_parse', $iri); } if ($match[1] === '') { diff --git a/library/Requests/Proxy/HTTP.php b/library/Requests/Proxy/HTTP.php index 7a5c2bb8d..378a2430e 100644 --- a/library/Requests/Proxy/HTTP.php +++ b/library/Requests/Proxy/HTTP.php @@ -7,6 +7,7 @@ * @since 1.6 */ +use WpOrg\Requests\Exception; use WpOrg\Requests\Proxy; /** @@ -53,7 +54,7 @@ class Requests_Proxy_HTTP implements Proxy { * Constructor * * @since 1.6 - * @throws Requests_Exception On incorrect number of arguments (`authbasicbadargs`) + * @throws \WpOrg\Requests\Exception On incorrect number of arguments (`authbasicbadargs`) * @param array|null $args Array of user and password. Must have exactly two elements */ public function __construct($args = null) { @@ -69,7 +70,7 @@ public function __construct($args = null) { $this->use_authentication = true; } else { - throw new Requests_Exception('Invalid number of arguments', 'proxyhttpbadargs'); + throw new Exception('Invalid number of arguments', 'proxyhttpbadargs'); } } } diff --git a/library/Requests/Response.php b/library/Requests/Response.php index a783f7329..2e167b9e1 100644 --- a/library/Requests/Response.php +++ b/library/Requests/Response.php @@ -6,6 +6,8 @@ * @package Requests */ +use WpOrg\Requests\Exception; + /** * HTTP response class * @@ -104,14 +106,14 @@ public function is_redirect() { /** * Throws an exception if the request was not successful * - * @throws Requests_Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`) + * @throws \WpOrg\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}) * @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()) { if (!$allow_redirects) { - throw new Requests_Exception('Redirection not allowed', 'response.no_redirects', $this); + throw new Exception('Redirection not allowed', 'response.no_redirects', $this); } } elseif (!$this->success) { diff --git a/library/Requests/Response/Headers.php b/library/Requests/Response/Headers.php index 12db128ae..5a4918274 100644 --- a/library/Requests/Response/Headers.php +++ b/library/Requests/Response/Headers.php @@ -5,6 +5,8 @@ * @package Requests */ +use WpOrg\Requests\Exception; + /** * Case-insensitive dictionary, suitable for HTTP headers * @@ -35,14 +37,14 @@ public function offsetGet($key) { /** * Set the given item * - * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`) + * @throws \WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`) * * @param string $key Item name * @param string $value Item value */ public function offsetSet($key, $value) { if ($key === null) { - throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset'); + throw new Exception('Object is a dictionary, not a list', 'invalidset'); } $key = strtolower($key); diff --git a/library/Requests/SSL.php b/library/Requests/SSL.php index f7ecf3fbd..070b99d4a 100644 --- a/library/Requests/SSL.php +++ b/library/Requests/SSL.php @@ -23,7 +23,7 @@ class Requests_SSL { * * @see https://tools.ietf.org/html/rfc2818#section-3.1 RFC2818, Section 3.1 * - * @throws Requests_Exception On not obtaining a match for the host (`fsockopen.ssl.no_match`) + * @throws \WpOrg\Requests\Exception On not obtaining a match for the host (`fsockopen.ssl.no_match`) * @param string $host Host name to verify against * @param array $cert Certificate data from openssl_x509_parse() * @return bool diff --git a/library/Requests/Session.php b/library/Requests/Session.php index d8d7fdf36..0c5cbf667 100644 --- a/library/Requests/Session.php +++ b/library/Requests/Session.php @@ -194,7 +194,7 @@ public function patch($url, $headers, $data = array(), $options = array()) { * * @see \WpOrg\Requests\Requests::request() * - * @throws Requests_Exception On invalid URLs (`nonhttp`) + * @throws \WpOrg\Requests\Exception On invalid URLs (`nonhttp`) * * @param string $url URL to request * @param array $headers Extra headers to send with the request @@ -216,7 +216,7 @@ public function request($url, $headers = array(), $data = array(), $type = Reque * * @param array $requests Requests data (see {@see \WpOrg\Requests\Requests::request_multiple}) * @param array $options Global and default options (see {@see \WpOrg\Requests\Requests::request}) - * @return array Responses (either Requests_Response or a Requests_Exception object) + * @return array Responses (either Requests_Response or a \WpOrg\Requests\Exception object) */ public function request_multiple($requests, $options = array()) { foreach ($requests as $key => $request) { diff --git a/library/Requests/Transport/cURL.php b/library/Requests/Transport/cURL.php index 40e392c33..93587a08e 100644 --- a/library/Requests/Transport/cURL.php +++ b/library/Requests/Transport/cURL.php @@ -6,6 +6,7 @@ * @subpackage Transport */ +use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; use WpOrg\Requests\Transport; @@ -124,7 +125,7 @@ public function __destruct() { /** * Perform a request * - * @throws Requests_Exception On a cURL error (`curlerror`) + * @throws \WpOrg\Requests\Exception On a cURL error (`curlerror`) * * @param string $url URL to request * @param array $headers Associative array of request headers @@ -163,7 +164,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar $this->stream_handle = @fopen($options['filename'], 'wb'); if ($this->stream_handle === false) { $error = error_get_last(); - throw new Requests_Exception($error['message'], 'fopen'); + throw new Exception($error['message'], 'fopen'); } } @@ -218,7 +219,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar * * @param array $requests Request data * @param array $options Global options - * @return array Array of Requests_Response objects (may contain Requests_Exception or string responses as well) + * @return array Array of Requests_Response objects (may contain \WpOrg\Requests\Exception or string responses as well) */ public function request_multiple($requests, $options) { // If you're not requesting, we can't get any responses ¯\_(ツ)_/¯ @@ -444,7 +445,7 @@ protected function setup_handle($url, $headers, $data, $options) { * @param string $response Response data from the body * @param array $options Request options * @return string|false HTTP response data including headers. False if non-blocking. - * @throws Requests_Exception + * @throws \WpOrg\Requests\Exception */ public function process_response($response, $options) { if ($options['blocking'] === false) { @@ -466,7 +467,7 @@ public function process_response($response, $options) { curl_errno($this->handle), curl_error($this->handle) ); - throw new Requests_Exception($error, 'curlerror', $this->handle); + throw new Exception($error, 'curlerror', $this->handle); } $this->info = curl_getinfo($this->handle); diff --git a/library/Requests/Transport/fsockopen.php b/library/Requests/Transport/fsockopen.php index d1166f385..0987ed844 100644 --- a/library/Requests/Transport/fsockopen.php +++ b/library/Requests/Transport/fsockopen.php @@ -6,6 +6,7 @@ * @subpackage Transport */ +use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; use WpOrg\Requests\Transport; @@ -49,8 +50,8 @@ class Requests_Transport_fsockopen implements Transport { /** * Perform a request * - * @throws Requests_Exception On failure to connect to socket (`fsockopenerror`) - * @throws Requests_Exception On socket timeout (`timeout`) + * @throws \WpOrg\Requests\Exception On failure to connect to socket (`fsockopenerror`) + * @throws \WpOrg\Requests\Exception On socket timeout (`timeout`) * * @param string $url URL to request * @param array $headers Associative array of request headers @@ -82,7 +83,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar $url_parts = parse_url($url); if (empty($url_parts)) { - throw new Requests_Exception('Invalid URL.', 'invalidurl', $url); + throw new Exception('Invalid URL.', 'invalidurl', $url); } $host = $url_parts['host']; $context = stream_context_create(); @@ -150,16 +151,16 @@ public function request($url, $headers = array(), $data = array(), $options = ar restore_error_handler(); if ($verifyname && !$this->verify_certificate_from_context($host, $context)) { - throw new Requests_Exception('SSL certificate did not match the requested domain name', 'ssl.no_match'); + throw new Exception('SSL certificate did not match the requested domain name', 'ssl.no_match'); } if (!$socket) { if ($errno === 0) { // Connection issue - throw new Requests_Exception(rtrim($this->connect_error), 'fsockopen.connect_error'); + throw new Exception(rtrim($this->connect_error), 'fsockopen.connect_error'); } - throw new Requests_Exception($errstr, 'fsockopenerror', null, $errno); + throw new Exception($errstr, 'fsockopenerror', null, $errno); } $data_format = $options['data_format']; @@ -267,14 +268,14 @@ public function request($url, $headers = array(), $data = array(), $options = ar $download = @fopen($options['filename'], 'wb'); if ($download === false) { $error = error_get_last(); - throw new Requests_Exception($error['message'], 'fopen'); + throw new Exception($error['message'], 'fopen'); } } while (!feof($socket)) { $this->info = stream_get_meta_data($socket); if ($this->info['timed_out']) { - throw new Requests_Exception('fsocket timed out', 'timeout'); + throw new Exception('fsocket timed out', 'timeout'); } $block = fread($socket, Requests::BUFFER_SIZE); @@ -330,7 +331,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar * * @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per {@see \WpOrg\Requests\Transport::request} * @param array $options Global options, see {@see \WpOrg\Requests\Requests::response()} for documentation - * @return array Array of Requests_Response objects (may contain Requests_Exception or string responses as well) + * @return array Array of Requests_Response objects (may contain \WpOrg\Requests\Exception or string responses as well) */ public function request_multiple($requests, $options) { $responses = array(); @@ -342,7 +343,7 @@ public function request_multiple($requests, $options) { $request['options']['hooks']->dispatch('transport.internal.parse_response', array(&$responses[$id], $request)); } - catch (Requests_Exception $e) { + catch (Exception $e) { $responses[$id] = $e; } @@ -430,8 +431,8 @@ public function connect_error_handler($errno, $errstr) { * * @see https://tools.ietf.org/html/rfc2818#section-3.1 RFC2818, Section 3.1 * - * @throws Requests_Exception On failure to connect via TLS (`fsockopen.ssl.connect_error`) - * @throws Requests_Exception On not obtaining a match for the host (`fsockopen.ssl.no_match`) + * @throws \WpOrg\Requests\Exception On failure to connect via TLS (`fsockopen.ssl.connect_error`) + * @throws \WpOrg\Requests\Exception On not obtaining a match for the host (`fsockopen.ssl.no_match`) * @param string $host Host name to verify against * @param resource $context Stream context * @return bool @@ -442,7 +443,7 @@ public function verify_certificate_from_context($host, $context) { // If we don't have SSL options, then we couldn't make the connection at // all if (empty($meta) || empty($meta['ssl']) || empty($meta['ssl']['peer_certificate'])) { - throw new Requests_Exception(rtrim($this->connect_error), 'ssl.connect_error'); + throw new Exception(rtrim($this->connect_error), 'ssl.connect_error'); } $cert = openssl_x509_parse($meta['ssl']['peer_certificate']); diff --git a/library/Requests/Utility/CaseInsensitiveDictionary.php b/library/Requests/Utility/CaseInsensitiveDictionary.php index bf850c46c..8e69bee2c 100644 --- a/library/Requests/Utility/CaseInsensitiveDictionary.php +++ b/library/Requests/Utility/CaseInsensitiveDictionary.php @@ -6,6 +6,8 @@ * @subpackage Utilities */ +use WpOrg\Requests\Exception; + /** * Case-insensitive dictionary, suitable for HTTP headers * @@ -62,7 +64,7 @@ public function offsetGet($key) { /** * Set the given item * - * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`) + * @throws \WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`) * * @param string $key Item name * @param string $value Item value @@ -70,7 +72,7 @@ public function offsetGet($key) { #[ReturnTypeWillChange] public function offsetSet($key, $value) { if ($key === null) { - throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset'); + throw new Exception('Object is a dictionary, not a list', 'invalidset'); } $key = strtolower($key); diff --git a/src/Autoload.php b/src/Autoload.php index 5b39c0e29..b49d6f695 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -41,6 +41,7 @@ class Autoload { // Classes. 'requests_cookie' => '\WpOrg\Requests\Cookie', + 'requests_exception' => '\WpOrg\Requests\Exception', ); /** diff --git a/library/Requests/Exception.php b/src/Exception.php similarity index 90% rename from library/Requests/Exception.php rename to src/Exception.php index 1080efd97..543fcb6ab 100644 --- a/library/Requests/Exception.php +++ b/src/Exception.php @@ -5,12 +5,16 @@ * @package Requests */ +namespace WpOrg\Requests; + +use Exception as PHPException; + /** * Exception for HTTP requests * * @package Requests */ -class Requests_Exception extends Exception { +class Exception extends PHPException { /** * Type of exception * diff --git a/src/Requests.php b/src/Requests.php index 97f028961..d5140f307 100644 --- a/src/Requests.php +++ b/src/Requests.php @@ -13,7 +13,6 @@ use Requests_Auth_Basic; use Requests_Cookie_Jar; -use Requests_Exception; use Requests_Hooks; use Requests_IDNAEncoder; use Requests_IRI; @@ -21,6 +20,7 @@ use Requests_Response; use Requests_Transport_cURL; use Requests_Transport_fsockopen; +use WpOrg\Requests\Exception; /** * Requests for PHP @@ -155,7 +155,7 @@ public static function add_transport($transport) { /** * Get a working transport * - * @throws Requests_Exception If no valid transport is found (`notransport`) + * @throws \WpOrg\Requests\Exception If no valid transport is found (`notransport`) * @return \WpOrg\Requests\Transport */ protected static function get_transport($capabilities = array()) { @@ -192,7 +192,7 @@ protected static function get_transport($capabilities = array()) { } } if (self::$transport[$cap_string] === null) { - throw new Requests_Exception('No working transports found', 'notransport', self::$transports); + throw new Exception('No working transports found', 'notransport', self::$transports); } $class = self::$transport[$cap_string]; @@ -327,7 +327,7 @@ public static function patch($url, $headers, $data = array(), $options = array() * (string, one of 'query' or 'body', default: 'query' for * HEAD/GET/DELETE, 'body' for POST/PUT/OPTIONS/PATCH) * - * @throws Requests_Exception On invalid URLs (`nonhttp`) + * @throws \WpOrg\Requests\Exception On invalid URLs (`nonhttp`) * * @param string $url URL to request * @param array $headers Extra headers to send with the request @@ -397,14 +397,14 @@ public static function request($url, $headers = array(), $data = array(), $type * In addition, the `$options` parameter takes the following global options: * * - `complete`: A callback for when a request is complete. Takes two - * parameters, a Requests_Response/Requests_Exception reference, and the + * parameters, a Requests_Response/\WpOrg\Requests\Exception reference, and the * ID from the request array (Note: this can also be overridden on a * per-request basis, although that's a little silly) * (callback) * * @param array $requests Requests data (see description for more information) * @param array $options Global and default options (see {@see \WpOrg\Requests\Requests::request}) - * @return array Responses (either Requests_Response or a Requests_Exception object) + * @return array Responses (either Requests_Response or a \WpOrg\Requests\Exception object) */ public static function request_multiple($requests, $options = array()) { $options = array_merge(self::get_default_options(true), $options); @@ -543,7 +543,7 @@ public static function set_certificate_path($path) { */ protected static function set_defaults(&$url, &$headers, &$data, &$type, &$options) { if (!preg_match('/^http(s)?:\/\//i', $url, $matches)) { - throw new Requests_Exception('Only HTTP(S) requests are handled.', 'nonhttp', $url); + throw new Exception('Only HTTP(S) requests are handled.', 'nonhttp', $url); } if (empty($options['hooks'])) { @@ -596,9 +596,9 @@ protected static function set_defaults(&$url, &$headers, &$data, &$type, &$optio /** * HTTP response parser * - * @throws Requests_Exception On missing head/body separator (`requests.no_crlf_separator`) - * @throws Requests_Exception On missing head/body separator (`noversion`) - * @throws Requests_Exception On missing head/body separator (`toomanyredirects`) + * @throws \WpOrg\Requests\Exception On missing head/body separator (`requests.no_crlf_separator`) + * @throws \WpOrg\Requests\Exception On missing head/body separator (`noversion`) + * @throws \WpOrg\Requests\Exception On missing head/body separator (`toomanyredirects`) * * @param string $headers Full response text including headers and body * @param string $url Original request URL @@ -621,7 +621,7 @@ protected static function parse_response($headers, $url, $req_headers, $req_data $pos = strpos($headers, "\r\n\r\n"); if ($pos === false) { // Crap! - throw new Requests_Exception('Missing header/body separator', 'requests.no_crlf_separator'); + throw new Exception('Missing header/body separator', 'requests.no_crlf_separator'); } $headers = substr($return->raw, 0, $pos); @@ -638,7 +638,7 @@ protected static function parse_response($headers, $url, $req_headers, $req_data $headers = explode("\n", $headers); preg_match('#^HTTP/(1\.\d)[ \t]+(\d+)#i', array_shift($headers), $matches); if (empty($matches)) { - throw new Requests_Exception('Response could not be parsed', 'noversion', $headers); + throw new Exception('Response could not be parsed', 'noversion', $headers); } $return->protocol_version = (float) $matches[1]; $return->status_code = (int) $matches[2]; @@ -693,7 +693,7 @@ protected static function parse_response($headers, $url, $req_headers, $req_data return $redirected; } elseif ($options['redirected'] >= $options['redirects']) { - throw new Requests_Exception('Too many redirects', 'toomanyredirects', $return); + throw new Exception('Too many redirects', 'toomanyredirects', $return); } } @@ -711,7 +711,7 @@ protected static function parse_response($headers, $url, $req_headers, $req_data * * @param string $response Full response text including headers and body (will be overwritten with Response instance) * @param array $request Request data as passed into {@see \WpOrg\Requests\Requests::request_multiple()} - * @return null `$response` is either set to a Requests_Response instance, or a Requests_Exception object + * @return null `$response` is either set to a Requests_Response instance, or a \WpOrg\Requests\Exception object */ public static function parse_multiple(&$response, $request) { try { @@ -721,7 +721,7 @@ public static function parse_multiple(&$response, $request) { $options = $request['options']; $response = self::parse_response($response, $url, $headers, $data, $options); } - catch (Requests_Exception $e) { + catch (Exception $e) { $response = $e; } } diff --git a/src/Transport.php b/src/Transport.php index 08175a38d..94f362d1e 100644 --- a/src/Transport.php +++ b/src/Transport.php @@ -31,7 +31,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar * * @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per {@see \WpOrg\Requests\Transport::request} * @param array $options Global options, see {@see \WpOrg\Requests\Requests::response()} for documentation - * @return array Array of Requests_Response objects (may contain Requests_Exception or string responses as well) + * @return array Array of Requests_Response objects (may contain \WpOrg\Requests\Exception or string responses as well) */ public function request_multiple($requests, $options); diff --git a/tests/Auth/BasicTest.php b/tests/Auth/BasicTest.php index ba4ae8909..fe1a50b30 100644 --- a/tests/Auth/BasicTest.php +++ b/tests/Auth/BasicTest.php @@ -4,9 +4,9 @@ use Requests\Tests\TestCase; use Requests_Auth_Basic; -use Requests_Exception; use Requests_Transport_cURL; use Requests_Transport_fsockopen; +use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; class BasicTest extends TestCase { @@ -86,7 +86,7 @@ public function testPOSTUsingInstantiation($transport) { } public function testMissingPassword() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid number of arguments'); new Requests_Auth_Basic(array('user')); } diff --git a/tests/CookiesTest.php b/tests/CookiesTest.php index f3be72f3b..e55bf9752 100644 --- a/tests/CookiesTest.php +++ b/tests/CookiesTest.php @@ -4,11 +4,11 @@ use Requests\Tests\TestCase; use Requests_Cookie_Jar; -use Requests_Exception; use Requests_IRI; use Requests_Response_Headers; use Requests_Utility_CaseInsensitiveDictionary; use WpOrg\Requests\Cookie; +use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; class CookiesTest extends TestCase { @@ -69,7 +69,7 @@ public function testCookieJarUnsetter() { } public function testCookieJarAsList() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Object is a dictionary, not a list'); $cookies = new Requests_Cookie_Jar(); $cookies[] = 'requests-testcookie1=testvalue1'; diff --git a/tests/IDNAEncoderTest.php b/tests/IDNAEncoderTest.php index 5dd851d99..fb8aa63b5 100644 --- a/tests/IDNAEncoderTest.php +++ b/tests/IDNAEncoderTest.php @@ -3,8 +3,8 @@ namespace Requests\Tests; use Requests\Tests\TestCase; -use Requests_Exception; use Requests_IDNAEncoder; +use WpOrg\Requests\Exception; class IDNAEncoderTest extends TestCase { public static function specExamples() { @@ -29,21 +29,21 @@ public function testEncoding($data, $expected) { } public function testASCIITooLong() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Provided string is too long'); $data = str_repeat('abcd', 20); Requests_IDNAEncoder::encode($data); } public function testEncodedTooLong() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Encoded string is too long'); $data = str_repeat("\xe4\xbb\x96", 60); Requests_IDNAEncoder::encode($data); } public function testAlreadyPrefixed() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Provided string begins with ACE prefix'); Requests_IDNAEncoder::encode("xn--\xe4\xbb\x96"); } @@ -69,31 +69,31 @@ public function testFourByteCharacter() { } public function testFiveByteCharacter() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid Unicode codepoint'); Requests_IDNAEncoder::encode("\xfb\xb6\xb6\xb6\xb6"); } public function testSixByteCharacter() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid Unicode codepoint'); Requests_IDNAEncoder::encode("\xfd\xb6\xb6\xb6\xb6\xb6"); } public function testInvalidASCIICharacterWithMultibyte() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid Unicode codepoint'); Requests_IDNAEncoder::encode("\0\xc2\xb6"); } public function testUnfinishedMultibyte() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid Unicode codepoint'); Requests_IDNAEncoder::encode("\xc2"); } public function testPartialMultibyte() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid Unicode codepoint'); Requests_IDNAEncoder::encode("\xc2\xc2\xb6"); } diff --git a/tests/Proxy/HTTPTest.php b/tests/Proxy/HTTPTest.php index 91efeaabd..e9583722b 100644 --- a/tests/Proxy/HTTPTest.php +++ b/tests/Proxy/HTTPTest.php @@ -3,10 +3,10 @@ namespace Requests\Tests\Proxy; use Requests\Tests\TestCase; -use Requests_Exception; use Requests_Proxy_HTTP; use Requests_Transport_cURL; use Requests_Transport_fsockopen; +use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; class HTTPTest extends TestCase { @@ -77,7 +77,7 @@ public function testConnectInvalidParameters($transport) { 'proxy' => array(REQUESTS_HTTP_PROXY, 'testuser', 'password', 'something'), 'transport' => $transport, ); - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid number of arguments'); Requests::get(httpbin('/get'), array(), $options); } @@ -140,7 +140,7 @@ public function testConnectWithInvalidAuth($transport) { && $transport === Requests_Transport_fsockopen::class ) { // @TODO fsockopen connection times out on invalid auth instead of returning 407. - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('fsocket timed out'); } diff --git a/tests/RequestsTest.php b/tests/RequestsTest.php index 8b47256cf..815eb6e6b 100644 --- a/tests/RequestsTest.php +++ b/tests/RequestsTest.php @@ -5,14 +5,14 @@ use Requests\Tests\Mock\RawTransportMock; use Requests\Tests\Mock\TransportMock; use Requests\Tests\TestCase; -use Requests_Exception; use Requests_Response_Headers; +use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; class RequestsTest extends TestCase { public function testInvalidProtocol() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Only HTTP(S) requests are handled'); Requests::request('ftp://128.0.0.1/'); } @@ -117,7 +117,7 @@ public function testInvalidProtocolVersion() { 'transport' => $transport, ); - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Response could not be parsed'); Requests::get('http://example.com/', array(), $options); } @@ -133,7 +133,7 @@ public function testSingleCRLFSeparator() { 'transport' => $transport, ); - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Missing header/body separator'); Requests::get('http://example.com/', array(), $options); } @@ -146,7 +146,7 @@ public function testInvalidStatus() { 'transport' => $transport, ); - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Response could not be parsed'); Requests::get('http://example.com/', array(), $options); } @@ -165,7 +165,7 @@ public function test30xWithoutLocation() { public function testTimeoutException() { $options = array('timeout' => 0.5); - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('timed out'); Requests::get(httpbin('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/delay/3'), array(), $options); } diff --git a/tests/Response/HeadersTest.php b/tests/Response/HeadersTest.php index 2e69e0803..81651e939 100644 --- a/tests/Response/HeadersTest.php +++ b/tests/Response/HeadersTest.php @@ -3,8 +3,8 @@ namespace Requests\Tests\Response; use Requests\Tests\TestCase; -use Requests_Exception; use Requests_Response_Headers; +use WpOrg\Requests\Exception; class HeadersTest extends TestCase { public function testArrayAccess() { @@ -44,7 +44,7 @@ public function testIteration() { } public function testInvalidKey() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Object is a dictionary, not a list'); $headers = new Requests_Response_Headers(); $headers[] = 'text/plain'; diff --git a/tests/Transport/BaseTestCase.php b/tests/Transport/BaseTestCase.php index f36a0911c..ecee3afc2 100644 --- a/tests/Transport/BaseTestCase.php +++ b/tests/Transport/BaseTestCase.php @@ -4,11 +4,11 @@ use Requests\Tests\Mock\TransportMock; use Requests\Tests\TestCase; -use Requests_Exception; use Requests_Exception_HTTP_Unknown; use Requests_Hooks; use Requests_Response; use stdClass; +use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; abstract class BaseTestCase extends TestCase { @@ -406,7 +406,7 @@ public function testTooManyRedirects() { $options = array( 'redirects' => 10, // default, but force just in case ); - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('Too many redirects'); Requests::get(httpbin('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/redirect/11'), array(), $this->getOptions($options)); } @@ -497,7 +497,7 @@ public function testStatusCodeThrow($code, $success) { $this->expectExceptionCode($code); } elseif ($code >= 300 && $code < 400) { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); } } @@ -604,7 +604,7 @@ public function testStreamToNonWritableFile() { Requests::get(httpbin('/get'), array(), $this->getOptions($options)); // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch - } catch (Requests_Exception $e) { + } catch (Exception $e) { // This "Failed to open stream" exception is expected. } @@ -623,7 +623,7 @@ public function testStreamToInvalidFile() { 'filename' => tempnam(sys_get_temp_dir(), 'RLT') . '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/missing/directory', // RequestsLibraryTest ); - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); // First character (F) can be upper or lowercase depending on PHP version. $this->expectExceptionMessage('ailed to open stream'); @@ -640,7 +640,7 @@ public function testNonblocking() { } public function testBadIP() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); Requests::get('http://256.256.256.0/', array(), $this->getOptions()); } @@ -663,7 +663,7 @@ public function testExpiredHTTPS() { return; } - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); Requests::get('https://testssl-expire.disig.sk/index.en.html', array(), $this->getOptions()); } @@ -673,7 +673,7 @@ public function testRevokedHTTPS() { return; } - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); Requests::get('https://testssl-revoked.disig.sk/index.en.html', array(), $this->getOptions()); } @@ -686,7 +686,7 @@ public function testBadDomain() { return; } - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); Requests::head('https://wrong.host.badssl.com/', array(), $this->getOptions()); } @@ -737,7 +737,7 @@ public function testTimeout() { $options = array( 'timeout' => 1, ); - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('timed out'); Requests::get(httpbin('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/delay/10'), array(), $this->getOptions($options)); } @@ -811,7 +811,7 @@ public function testMultipleWithFailure() { ); $responses = Requests::request_multiple($requests, $this->getOptions()); $this->assertSame(200, $responses['success']->status_code); - $this->assertInstanceOf(Requests_Exception::class, $responses['timeout']); + $this->assertInstanceOf(Exception::class, $responses['timeout']); } public function testMultipleUsingCallback() { @@ -899,12 +899,12 @@ public function testAlternatePort() { try { $request = Requests::get('http://portquiz.net:8080/', array(), $this->getOptions()); } - catch (Requests_Exception $e) { + catch (Exception $e) { // Retry the request as it often times-out. try { $request = Requests::get('http://portquiz.net:8080/', array(), $this->getOptions()); } - catch (Requests_Exception $e) { + catch (Exception $e) { // If it still times out, mark the test as skipped. $this->markTestSkipped( $e->getMessage() diff --git a/tests/Transport/CURLTest.php b/tests/Transport/CURLTest.php index 7bbc9ddee..d84a18e45 100644 --- a/tests/Transport/CURLTest.php +++ b/tests/Transport/CURLTest.php @@ -3,27 +3,27 @@ namespace Requests\Tests\Transport; use Requests\Tests\Transport\BaseTestCase; -use Requests_Exception; use Requests_Transport_cURL; +use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; class CurlTest extends BaseTestCase { protected $transport = Requests_Transport_cURL::class; public function testBadIP() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('t resolve host'); parent::testBadIP(); } public function testExpiredHTTPS() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('certificate subject name'); parent::testExpiredHTTPS(); } public function testRevokedHTTPS() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('certificate subject name'); parent::testRevokedHTTPS(); } @@ -32,7 +32,7 @@ public function testRevokedHTTPS() { * Test that SSL fails with a bad certificate */ public function testBadDomain() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('certificate subject name'); parent::testBadDomain(); } diff --git a/tests/Transport/FsockopenTest.php b/tests/Transport/FsockopenTest.php index 54ca9152f..b902abccc 100644 --- a/tests/Transport/FsockopenTest.php +++ b/tests/Transport/FsockopenTest.php @@ -3,27 +3,27 @@ namespace Requests\Tests\Transport; use Requests\Tests\Transport\BaseTestCase; -use Requests_Exception; use Requests_Hooks; use Requests_Transport_fsockopen; +use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; class FsockopenTest extends BaseTestCase { protected $transport = Requests_Transport_fsockopen::class; public function testBadIP() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); parent::testBadIP(); } public function testExpiredHTTPS() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('SSL certificate did not match the requested domain name'); parent::testExpiredHTTPS(); } public function testRevokedHTTPS() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('SSL certificate did not match the requested domain name'); parent::testRevokedHTTPS(); } @@ -32,7 +32,7 @@ public function testRevokedHTTPS() { * Test that SSL fails with a bad certificate */ public function testBadDomain() { - $this->expectException(Requests_Exception::class); + $this->expectException(Exception::class); $this->expectExceptionMessage('SSL certificate did not match the requested domain name'); parent::testBadDomain(); } From 293ae014429d1f70ff898cad9e8a229257854b67 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 13 Aug 2021 10:49:12 +0200 Subject: [PATCH 16/45] Requests_Hooks: change to namespaced class Includes: * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. --- docs/authentication-custom.md | 2 +- docs/hooks.md | 6 +++--- library/Deprecated.php | 1 + library/Requests/Auth/Basic.php | 5 +++-- library/Requests/Proxy/HTTP.php | 5 +++-- library/Requests/Transport/cURL.php | 2 +- src/Auth.php | 10 +++++----- src/Autoload.php | 1 + {library/Requests => src}/Hooks.php | 4 +++- src/Proxy.php | 10 +++++----- src/Requests.php | 6 +++--- tests/Transport/BaseTestCase.php | 6 +++--- tests/Transport/FsockopenTest.php | 6 +++--- 13 files changed, 35 insertions(+), 29 deletions(-) rename {library/Requests => src}/Hooks.php (95%) diff --git a/docs/authentication-custom.md b/docs/authentication-custom.md index 7e913b733..07d4e3831 100644 --- a/docs/authentication-custom.md +++ b/docs/authentication-custom.md @@ -19,7 +19,7 @@ class MySoftware_Auth_Hotdog implements WpOrg\Requests\Auth { $this->password = $password; } - public function register(Requests_Hooks &$hooks) { + public function register(WpOrg\Requests\Hooks &$hooks) { $hooks->register('requests.before_request', array($this, 'before_request')); } diff --git a/docs/hooks.md b/docs/hooks.md index f59bc74d1..3390be5dd 100644 --- a/docs/hooks.md +++ b/docs/hooks.md @@ -3,7 +3,7 @@ Hooks Requests has a hook system that you can use to manipulate parts of the request process along with internal transport hooks. -Check out the [API documentation for `Requests_Hooks`][requests_hooks] for more +Check out the [API documentation for `WpOrg\Requests\Hooks`][requests_hooks] for more information on how to use the hook system. [requests_hooks]: {{ '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/api/class-Requests_Hooks.html' | prepend: site.baseurl }} @@ -88,11 +88,11 @@ Authentication guide][authentication-custom] instead. [authentication-custom]: authentication-custom.md -In order to register your own hooks, you need to instantiate `Requests_Hooks` +In order to register your own hooks, you need to instantiate `WpOrg\Requests\Hooks` and pass the object in via the `'hooks'` option. ```php -$hooks = new Requests_Hooks(); +$hooks = new WpOrg\Requests\Hooks(); $hooks->register('requests.after_request', 'mycallback'); $request = WpOrg\Requests\Requests::get('https://httpbin.org/get', array(), array('hooks' => $hooks)); diff --git a/library/Deprecated.php b/library/Deprecated.php index 554d2be98..d632e29ec 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -34,3 +34,4 @@ interface Requests_Transport extends WpOrg\Requests\Transport {} class Requests_Cookie extends WpOrg\Requests\Cookie {} class Requests_Exception extends WpOrg\Requests\Exception {} +class Requests_Hooks extends WpOrg\Requests\Hooks {} diff --git a/library/Requests/Auth/Basic.php b/library/Requests/Auth/Basic.php index 508211a10..38e1df59e 100644 --- a/library/Requests/Auth/Basic.php +++ b/library/Requests/Auth/Basic.php @@ -8,6 +8,7 @@ use WpOrg\Requests\Auth; use WpOrg\Requests\Exception; +use WpOrg\Requests\Hooks; /** * Basic Authentication provider @@ -54,9 +55,9 @@ public function __construct($args = null) { * * @see curl_before_send * @see fsockopen_header - * @param Requests_Hooks $hooks Hook system + * @param \WpOrg\Requests\Hooks $hooks Hook system */ - public function register(Requests_Hooks $hooks) { + public function register(Hooks $hooks) { $hooks->register('curl.before_send', array($this, 'curl_before_send')); $hooks->register('fsockopen.after_headers', array($this, 'fsockopen_header')); } diff --git a/library/Requests/Proxy/HTTP.php b/library/Requests/Proxy/HTTP.php index 378a2430e..775dddc58 100644 --- a/library/Requests/Proxy/HTTP.php +++ b/library/Requests/Proxy/HTTP.php @@ -8,6 +8,7 @@ */ use WpOrg\Requests\Exception; +use WpOrg\Requests\Hooks; use WpOrg\Requests\Proxy; /** @@ -83,9 +84,9 @@ public function __construct($args = null) { * @see fsockopen_remote_socket * @see fsockopen_remote_host_path * @see fsockopen_header - * @param Requests_Hooks $hooks Hook system + * @param \WpOrg\Requests\Hooks $hooks Hook system */ - public function register(Requests_Hooks $hooks) { + public function register(Hooks $hooks) { $hooks->register('curl.before_send', array($this, 'curl_before_send')); $hooks->register('fsockopen.remote_socket', array($this, 'fsockopen_remote_socket')); diff --git a/library/Requests/Transport/cURL.php b/library/Requests/Transport/cURL.php index 93587a08e..5aab934ee 100644 --- a/library/Requests/Transport/cURL.php +++ b/library/Requests/Transport/cURL.php @@ -58,7 +58,7 @@ class Requests_Transport_cURL implements Transport { /** * Hook dispatcher instance * - * @var Requests_Hooks + * @var \WpOrg\Requests\Hooks */ protected $hooks; diff --git a/src/Auth.php b/src/Auth.php index e3a18062c..a3599b5a2 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -8,7 +8,7 @@ namespace WpOrg\Requests; -use Requests_Hooks; +use WpOrg\Requests\Hooks; /** * Authentication provider interface @@ -18,7 +18,7 @@ * Parameters should be passed via the constructor where possible, as this * makes it much easier for users to use your provider. * - * @see Requests_Hooks + * @see \WpOrg\Requests\Hooks * @package Requests * @subpackage Authentication */ @@ -30,8 +30,8 @@ interface Auth { * has set an instance as the 'auth' option. Use this callback to register all the * hooks you'll need. * - * @see Requests_Hooks::register - * @param Requests_Hooks $hooks Hook system + * @see \WpOrg\Requests\Hooks::register + * @param \WpOrg\Requests\Hooks $hooks Hook system */ - public function register(Requests_Hooks $hooks); + public function register(Hooks $hooks); } diff --git a/src/Autoload.php b/src/Autoload.php index b49d6f695..a5e554044 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -42,6 +42,7 @@ class Autoload { // Classes. 'requests_cookie' => '\WpOrg\Requests\Cookie', 'requests_exception' => '\WpOrg\Requests\Exception', + 'requests_hooks' => '\WpOrg\Requests\Hooks', ); /** diff --git a/library/Requests/Hooks.php b/src/Hooks.php similarity index 95% rename from library/Requests/Hooks.php rename to src/Hooks.php index 187cbd713..86d5b22ca 100644 --- a/library/Requests/Hooks.php +++ b/src/Hooks.php @@ -6,6 +6,8 @@ * @subpackage Utilities */ +namespace WpOrg\Requests; + use WpOrg\Requests\Hooker; /** @@ -14,7 +16,7 @@ * @package Requests * @subpackage Utilities */ -class Requests_Hooks implements Hooker { +class Hooks implements Hooker { /** * Registered callbacks for each hook * diff --git a/src/Proxy.php b/src/Proxy.php index 9b8fb41cc..d7ca0148d 100644 --- a/src/Proxy.php +++ b/src/Proxy.php @@ -9,7 +9,7 @@ namespace WpOrg\Requests; -use Requests_Hooks; +use WpOrg\Requests\Hooks; /** * Proxy connection interface @@ -19,7 +19,7 @@ * Parameters should be passed via the constructor where possible, as this * makes it much easier for users to use your provider. * - * @see Requests_Hooks + * @see \WpOrg\Requests\Hooks * @package Requests * @subpackage Proxy * @since 1.6 @@ -32,8 +32,8 @@ interface Proxy { * has set an instance as the 'auth' option. Use this callback to register all the * hooks you'll need. * - * @see Requests_Hooks::register - * @param Requests_Hooks $hooks Hook system + * @see \WpOrg\Requests\Hooks::register + * @param \WpOrg\Requests\Hooks $hooks Hook system */ - public function register(Requests_Hooks $hooks); + public function register(Hooks $hooks); } diff --git a/src/Requests.php b/src/Requests.php index d5140f307..e30d5aac5 100644 --- a/src/Requests.php +++ b/src/Requests.php @@ -13,7 +13,6 @@ use Requests_Auth_Basic; use Requests_Cookie_Jar; -use Requests_Hooks; use Requests_IDNAEncoder; use Requests_IRI; use Requests_Proxy_HTTP; @@ -21,6 +20,7 @@ use Requests_Transport_cURL; use Requests_Transport_fsockopen; use WpOrg\Requests\Exception; +use WpOrg\Requests\Hooks; /** * Requests for PHP @@ -315,7 +315,7 @@ public static function patch($url, $headers, $data = array(), $options = array() * {@see getTransport()} * (string|\WpOrg\Requests\Transport, default: {@see getTransport()}) * - `hooks`: Hooks handler. - * (\WpOrg\Requests\Hooker, default: new Requests_Hooks()) + * (\WpOrg\Requests\Hooker, default: new WpOrg\Requests\Hooks()) * - `verify`: Should we verify SSL certificates? Allows passing in a custom * certificate file as a string. (Using true uses the system-wide root * certificate store instead, but this may have different behaviour @@ -547,7 +547,7 @@ protected static function set_defaults(&$url, &$headers, &$data, &$type, &$optio } if (empty($options['hooks'])) { - $options['hooks'] = new Requests_Hooks(); + $options['hooks'] = new Hooks(); } if (is_array($options['auth'])) { diff --git a/tests/Transport/BaseTestCase.php b/tests/Transport/BaseTestCase.php index ecee3afc2..1d377cd0c 100644 --- a/tests/Transport/BaseTestCase.php +++ b/tests/Transport/BaseTestCase.php @@ -5,10 +5,10 @@ use Requests\Tests\Mock\TransportMock; use Requests\Tests\TestCase; use Requests_Exception_HTTP_Unknown; -use Requests_Hooks; use Requests_Response; use stdClass; use WpOrg\Requests\Exception; +use WpOrg\Requests\Hooks; use WpOrg\Requests\Requests; abstract class BaseTestCase extends TestCase { @@ -921,7 +921,7 @@ public function testAlternatePort() { public function testProgressCallback() { $mock = $this->getMockBuilder(stdClass::class)->setMethods(array('progress'))->getMock(); $mock->expects($this->atLeastOnce())->method('progress'); - $hooks = new Requests_Hooks(); + $hooks = new Hooks(); $hooks->register('request.progress', array($mock, 'progress')); $options = array( 'hooks' => $hooks, @@ -942,7 +942,7 @@ public function testAfterRequestCallback() { $this->isType('string'), $this->logicalAnd($this->isType('array'), $this->logicalNot($this->isEmpty())) ); - $hooks = new Requests_Hooks(); + $hooks = new Hooks(); $hooks->register('curl.after_request', array($mock, 'after_request')); $hooks->register('fsockopen.after_request', array($mock, 'after_request')); $options = array( diff --git a/tests/Transport/FsockopenTest.php b/tests/Transport/FsockopenTest.php index b902abccc..38cf53cd2 100644 --- a/tests/Transport/FsockopenTest.php +++ b/tests/Transport/FsockopenTest.php @@ -3,9 +3,9 @@ namespace Requests\Tests\Transport; use Requests\Tests\Transport\BaseTestCase; -use Requests_Hooks; use Requests_Transport_fsockopen; use WpOrg\Requests\Exception; +use WpOrg\Requests\Hooks; use WpOrg\Requests\Requests; class FsockopenTest extends BaseTestCase { @@ -41,7 +41,7 @@ public function testBadDomain() { * Issue #248. */ public function testContentLengthHeader() { - $hooks = new Requests_Hooks(); + $hooks = new Hooks(); $hooks->register('fsockopen.after_headers', array($this, 'checkContentLengthHeader')); Requests::post(httpbin('/post'), array(), array(), $this->getOptions(array('hooks' => $hooks))); @@ -67,7 +67,7 @@ public function testHTTPVersionHeader() { // Make sure the locale was changed. $this->assertNotSame($locale, setlocale(LC_NUMERIC, 0), 'Changing the locale failed'); - $hooks = new Requests_Hooks(); + $hooks = new Hooks(); $hooks->register('fsockopen.after_headers', array($this, 'checkHTTPVersionHeader')); Requests::post(httpbin('/post'), array(), array(), $this->getOptions(array('hooks' => $hooks))); From db2619dbfd71fc52eee549f53662a9518d087dee Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 13 Aug 2021 11:09:36 +0200 Subject: [PATCH 17/45] Requests_IDNAEncoder: change to namespaced class Includes: * Using CamelCaps for the class name, as well as the test class. * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. --- library/Deprecated.php | 1 + src/Autoload.php | 15 +++++----- .../IDNAEncoder.php => src/IdnaEncoder.php | 4 ++- src/Requests.php | 4 +-- ...DNAEncoderTest.php => IdnaEncoderTest.php} | 30 +++++++++---------- 5 files changed, 29 insertions(+), 25 deletions(-) rename library/Requests/IDNAEncoder.php => src/IdnaEncoder.php (99%) rename tests/{IDNAEncoderTest.php => IdnaEncoderTest.php} (74%) diff --git a/library/Deprecated.php b/library/Deprecated.php index d632e29ec..93db0022d 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -35,3 +35,4 @@ interface Requests_Transport extends WpOrg\Requests\Transport {} class Requests_Cookie extends WpOrg\Requests\Cookie {} class Requests_Exception extends WpOrg\Requests\Exception {} class Requests_Hooks extends WpOrg\Requests\Hooks {} +class Requests_IDNAEncoder extends WpOrg\Requests\IdnaEncoder {} diff --git a/src/Autoload.php b/src/Autoload.php index a5e554044..7ead57f5b 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -34,15 +34,16 @@ class Autoload { */ private static $deprecated_classes = array( // Interfaces. - 'requests_auth' => '\WpOrg\Requests\Auth', - 'requests_hooker' => '\WpOrg\Requests\Hooker', - 'requests_proxy' => '\WpOrg\Requests\Proxy', - 'requests_transport' => '\WpOrg\Requests\Transport', + 'requests_auth' => '\WpOrg\Requests\Auth', + 'requests_hooker' => '\WpOrg\Requests\Hooker', + 'requests_proxy' => '\WpOrg\Requests\Proxy', + 'requests_transport' => '\WpOrg\Requests\Transport', // Classes. - 'requests_cookie' => '\WpOrg\Requests\Cookie', - 'requests_exception' => '\WpOrg\Requests\Exception', - 'requests_hooks' => '\WpOrg\Requests\Hooks', + 'requests_cookie' => '\WpOrg\Requests\Cookie', + 'requests_exception' => '\WpOrg\Requests\Exception', + 'requests_hooks' => '\WpOrg\Requests\Hooks', + 'requests_idnaencoder' => '\WpOrg\Requests\IdnaEncoder', ); /** diff --git a/library/Requests/IDNAEncoder.php b/src/IdnaEncoder.php similarity index 99% rename from library/Requests/IDNAEncoder.php rename to src/IdnaEncoder.php index 383cb26c2..0ec61d3a6 100644 --- a/library/Requests/IDNAEncoder.php +++ b/src/IdnaEncoder.php @@ -1,5 +1,7 @@ host = Requests_IDNAEncoder::encode($iri->ihost); + $iri->host = IdnaEncoder::encode($iri->ihost); $url = $iri->uri; } diff --git a/tests/IDNAEncoderTest.php b/tests/IdnaEncoderTest.php similarity index 74% rename from tests/IDNAEncoderTest.php rename to tests/IdnaEncoderTest.php index fb8aa63b5..061b2446d 100644 --- a/tests/IDNAEncoderTest.php +++ b/tests/IdnaEncoderTest.php @@ -3,10 +3,10 @@ namespace Requests\Tests; use Requests\Tests\TestCase; -use Requests_IDNAEncoder; use WpOrg\Requests\Exception; +use WpOrg\Requests\IdnaEncoder; -class IDNAEncoderTest extends TestCase { +class IdnaEncoderTest extends TestCase { public static function specExamples() { return array( array( @@ -24,7 +24,7 @@ public static function specExamples() { * @dataProvider specExamples */ public function testEncoding($data, $expected) { - $result = Requests_IDNAEncoder::encode($data); + $result = IdnaEncoder::encode($data); $this->assertSame($expected, $result); } @@ -32,69 +32,69 @@ public function testASCIITooLong() { $this->expectException(Exception::class); $this->expectExceptionMessage('Provided string is too long'); $data = str_repeat('abcd', 20); - Requests_IDNAEncoder::encode($data); + IdnaEncoder::encode($data); } public function testEncodedTooLong() { $this->expectException(Exception::class); $this->expectExceptionMessage('Encoded string is too long'); $data = str_repeat("\xe4\xbb\x96", 60); - Requests_IDNAEncoder::encode($data); + IdnaEncoder::encode($data); } public function testAlreadyPrefixed() { $this->expectException(Exception::class); $this->expectExceptionMessage('Provided string begins with ACE prefix'); - Requests_IDNAEncoder::encode("xn--\xe4\xbb\x96"); + IdnaEncoder::encode("xn--\xe4\xbb\x96"); } public function testASCIICharacter() { - $result = Requests_IDNAEncoder::encode('a'); + $result = IdnaEncoder::encode('a'); $this->assertSame('a', $result); } public function testTwoByteCharacter() { - $result = Requests_IDNAEncoder::encode("\xc2\xb6"); // Pilcrow character + $result = IdnaEncoder::encode("\xc2\xb6"); // Pilcrow character $this->assertSame('xn--tba', $result); } public function testThreeByteCharacter() { - $result = Requests_IDNAEncoder::encode("\xe2\x82\xac"); // Euro symbol + $result = IdnaEncoder::encode("\xe2\x82\xac"); // Euro symbol $this->assertSame('xn--lzg', $result); } public function testFourByteCharacter() { - $result = Requests_IDNAEncoder::encode("\xf0\xa4\xad\xa2"); // Chinese symbol? + $result = IdnaEncoder::encode("\xf0\xa4\xad\xa2"); // Chinese symbol? $this->assertSame('xn--ww6j', $result); } public function testFiveByteCharacter() { $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid Unicode codepoint'); - Requests_IDNAEncoder::encode("\xfb\xb6\xb6\xb6\xb6"); + IdnaEncoder::encode("\xfb\xb6\xb6\xb6\xb6"); } public function testSixByteCharacter() { $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid Unicode codepoint'); - Requests_IDNAEncoder::encode("\xfd\xb6\xb6\xb6\xb6\xb6"); + IdnaEncoder::encode("\xfd\xb6\xb6\xb6\xb6\xb6"); } public function testInvalidASCIICharacterWithMultibyte() { $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid Unicode codepoint'); - Requests_IDNAEncoder::encode("\0\xc2\xb6"); + IdnaEncoder::encode("\0\xc2\xb6"); } public function testUnfinishedMultibyte() { $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid Unicode codepoint'); - Requests_IDNAEncoder::encode("\xc2"); + IdnaEncoder::encode("\xc2"); } public function testPartialMultibyte() { $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid Unicode codepoint'); - Requests_IDNAEncoder::encode("\xc2\xc2\xb6"); + IdnaEncoder::encode("\xc2\xc2\xb6"); } } From e34d7da309b99d30c8c149ae08561d5a04a03089 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 13 Aug 2021 11:17:32 +0200 Subject: [PATCH 18/45] Requests_IPv6: change to namespaced class Includes: * Using CamelCaps for the class name. * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. --- library/Deprecated.php | 1 + library/Requests/IRI.php | 5 +++-- src/Autoload.php | 1 + library/Requests/IPv6.php => src/Ipv6.php | 4 +++- 4 files changed, 8 insertions(+), 3 deletions(-) rename library/Requests/IPv6.php => src/Ipv6.php (99%) diff --git a/library/Deprecated.php b/library/Deprecated.php index 93db0022d..1c63db76d 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -36,3 +36,4 @@ class Requests_Cookie extends WpOrg\Requests\Cookie {} class Requests_Exception extends WpOrg\Requests\Exception {} class Requests_Hooks extends WpOrg\Requests\Hooks {} class Requests_IDNAEncoder extends WpOrg\Requests\IdnaEncoder {} +class Requests_IPv6 extends WpOrg\Requests\Ipv6 {} diff --git a/library/Requests/IRI.php b/library/Requests/IRI.php index 539c3f114..239a433dc 100644 --- a/library/Requests/IRI.php +++ b/library/Requests/IRI.php @@ -7,6 +7,7 @@ */ use WpOrg\Requests\Exception; +use WpOrg\Requests\Ipv6; /** * IRI parser/serialiser/normaliser @@ -865,8 +866,8 @@ protected function set_host($ihost) { return true; } if (substr($ihost, 0, 1) === '[' && substr($ihost, -1) === ']') { - if (Requests_IPv6::check_ipv6(substr($ihost, 1, -1))) { - $this->ihost = '[' . Requests_IPv6::compress(substr($ihost, 1, -1)) . ']'; + if (Ipv6::check_ipv6(substr($ihost, 1, -1))) { + $this->ihost = '[' . Ipv6::compress(substr($ihost, 1, -1)) . ']'; } else { $this->ihost = null; diff --git a/src/Autoload.php b/src/Autoload.php index 7ead57f5b..53c64612f 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -44,6 +44,7 @@ class Autoload { 'requests_exception' => '\WpOrg\Requests\Exception', 'requests_hooks' => '\WpOrg\Requests\Hooks', 'requests_idnaencoder' => '\WpOrg\Requests\IdnaEncoder', + 'requests_ipv6' => '\WpOrg\Requests\Ipv6', ); /** diff --git a/library/Requests/IPv6.php b/src/Ipv6.php similarity index 99% rename from library/Requests/IPv6.php rename to src/Ipv6.php index 41ab89cca..83e6f2aec 100644 --- a/library/Requests/IPv6.php +++ b/src/Ipv6.php @@ -6,6 +6,8 @@ * @subpackage Utilities */ +namespace WpOrg\Requests; + /** * Class to validate and to work with IPv6 addresses * @@ -15,7 +17,7 @@ * @package Requests * @subpackage Utilities */ -class Requests_IPv6 { +class Ipv6 { /** * Uncompresses an IPv6 address * From 4a0fca628175b5c0bbbe2deff45ef1cfc7ca2108 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 16 Aug 2021 02:15:58 +0200 Subject: [PATCH 19/45] Requests_IRI: change to namespaced class Includes: * Using CamelCaps for the class name, as well as the test class. * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. * Updating references to this class and the test file in the PHPCS config file. --- .phpcs.xml.dist | 4 +- docs/why-requests.md | 2 +- library/Deprecated.php | 1 + library/Requests/Cookie/Jar.php | 9 ++-- library/Requests/Session.php | 3 +- src/Autoload.php | 1 + src/Cookie.php | 10 ++-- src/IdnaEncoder.php | 2 +- library/Requests/IRI.php => src/Iri.php | 20 +++---- src/Requests.php | 6 +-- tests/CookiesTest.php | 28 +++++----- tests/{IRITest.php => IriTest.php} | 72 ++++++++++++------------- 12 files changed, 82 insertions(+), 76 deletions(-) rename library/Requests/IRI.php => src/Iri.php (98%) rename tests/{IRITest.php => IriTest.php} (87%) diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index fd2b1458e..f3db29a7b 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -16,8 +16,8 @@ . - */library/Requests/IRI\.php$ - */tests/IRITest\.php$ + */src/Iri\.php$ + */tests/IriTest\.php$ */vendor/* */tests/coverage/ diff --git a/docs/why-requests.md b/docs/why-requests.md index a5bcb05b6..31389c2c8 100644 --- a/docs/why-requests.md +++ b/docs/why-requests.md @@ -162,7 +162,7 @@ instead of something else, in our opinion. using PEAR for your project. Requests is entirely self-contained, and includes all the libraries it needs - (for example, `Requests_IRI` is based on `ComplexPie_IRI` by Geoffrey Sneddon). + (for example, `WpOrg\Requests\Iri` is based on `ComplexPie_IRI` by Geoffrey Sneddon). ### Zend Framework's Zend\_Http\_Client diff --git a/library/Deprecated.php b/library/Deprecated.php index 1c63db76d..f487dc4c0 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -37,3 +37,4 @@ class Requests_Exception extends WpOrg\Requests\Exception {} class Requests_Hooks extends WpOrg\Requests\Hooks {} class Requests_IDNAEncoder extends WpOrg\Requests\IdnaEncoder {} class Requests_IPv6 extends WpOrg\Requests\Ipv6 {} +class Requests_IRI extends WpOrg\Requests\Iri {} diff --git a/library/Requests/Cookie/Jar.php b/library/Requests/Cookie/Jar.php index 3c4cce214..b593c662d 100644 --- a/library/Requests/Cookie/Jar.php +++ b/library/Requests/Cookie/Jar.php @@ -9,6 +9,7 @@ use WpOrg\Requests\Cookie; use WpOrg\Requests\Exception; use WpOrg\Requests\Hooker; +use WpOrg\Requests\Iri; /** * Cookie holder object @@ -132,8 +133,8 @@ public function register(Hooker $hooks) { * @param array $options */ public function before_request($url, &$headers, &$data, &$type, &$options) { - if (!$url instanceof Requests_IRI) { - $url = new Requests_IRI($url); + if (!$url instanceof Iri) { + $url = new Iri($url); } if (!empty($this->cookies)) { @@ -162,8 +163,8 @@ public function before_request($url, &$headers, &$data, &$type, &$options) { */ public function before_redirect_check(Requests_Response $return) { $url = $return->url; - if (!$url instanceof Requests_IRI) { - $url = new Requests_IRI($url); + if (!$url instanceof Iri) { + $url = new Iri($url); } $cookies = Cookie::parse_from_headers($return->headers, $url); diff --git a/library/Requests/Session.php b/library/Requests/Session.php index 0c5cbf667..27eb2fe98 100644 --- a/library/Requests/Session.php +++ b/library/Requests/Session.php @@ -6,6 +6,7 @@ * @subpackage Session Handler */ +use WpOrg\Requests\Iri; use WpOrg\Requests\Requests; /** @@ -240,7 +241,7 @@ public function request_multiple($requests, $options = array()) { */ protected function merge_request($request, $merge_options = true) { if ($this->url !== null) { - $request['url'] = Requests_IRI::absolutize($this->url, $request['url']); + $request['url'] = Iri::absolutize($this->url, $request['url']); $request['url'] = $request['url']->uri; } diff --git a/src/Autoload.php b/src/Autoload.php index 53c64612f..990888dad 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -45,6 +45,7 @@ class Autoload { 'requests_hooks' => '\WpOrg\Requests\Hooks', 'requests_idnaencoder' => '\WpOrg\Requests\IdnaEncoder', 'requests_ipv6' => '\WpOrg\Requests\Ipv6', + 'requests_iri' => '\WpOrg\Requests\Iri', ); /** diff --git a/src/Cookie.php b/src/Cookie.php index e88bddb47..82ce9809d 100644 --- a/src/Cookie.php +++ b/src/Cookie.php @@ -8,9 +8,9 @@ namespace WpOrg\Requests; -use Requests_IRI; use Requests_Response_Headers; use Requests_Utility_CaseInsensitiveDictionary; +use WpOrg\Requests\Iri; /** * Cookie storage object @@ -119,10 +119,10 @@ public function is_expired() { /** * Check if a cookie is valid for a given URI * - * @param Requests_IRI $uri URI to check + * @param \WpOrg\Requests\Iri $uri URI to check * @return boolean Whether the cookie is valid for the given URI */ - public function uri_matches(Requests_IRI $uri) { + public function uri_matches(Iri $uri) { if (!$this->domain_matches($uri->host)) { return false; } @@ -416,11 +416,11 @@ public static function parse($string, $name = '', $reference_time = null) { * Parse all Set-Cookie headers from request headers * * @param Requests_Response_Headers $headers Headers to parse from - * @param Requests_IRI|null $origin URI for comparing cookie origins + * @param \WpOrg\Requests\Iri|null $origin URI for comparing cookie origins * @param int|null $time Reference time for expiration calculation * @return array */ - public static function parse_from_headers(Requests_Response_Headers $headers, Requests_IRI $origin = null, $time = null) { + public static function parse_from_headers(Requests_Response_Headers $headers, Iri $origin = null, $time = null) { $cookie_headers = $headers->getValues('Set-Cookie'); if (empty($cookie_headers)) { return array(); diff --git a/src/IdnaEncoder.php b/src/IdnaEncoder.php index 0ec61d3a6..9a4751f53 100644 --- a/src/IdnaEncoder.php +++ b/src/IdnaEncoder.php @@ -133,7 +133,7 @@ protected static function nameprep($string) { /** * Convert a UTF-8 string to a UCS-4 codepoint array * - * Based on Requests_IRI::replace_invalid_with_pct_encoding() + * Based on \WpOrg\Requests\Iri::replace_invalid_with_pct_encoding() * * @throws \WpOrg\Requests\Exception Invalid UTF-8 codepoint (`idna.invalidcodepoint`) * @param string $input diff --git a/library/Requests/IRI.php b/src/Iri.php similarity index 98% rename from library/Requests/IRI.php rename to src/Iri.php index 239a433dc..a52fb20e1 100644 --- a/library/Requests/IRI.php +++ b/src/Iri.php @@ -6,6 +6,8 @@ * @subpackage Utilities */ +namespace WpOrg\Requests; + use WpOrg\Requests\Exception; use WpOrg\Requests\Ipv6; @@ -66,7 +68,7 @@ * @property string $fragment Fragment, formatted for a URI (after '#') * @property string $ifragment Fragment part of the IRI (after '#') */ -class Requests_IRI { +class Iri { /** * Scheme * @@ -254,13 +256,13 @@ public function __construct($iri = null) { * * Returns false if $base is not absolute, otherwise an IRI. * - * @param Requests_IRI|string $base (Absolute) Base IRI - * @param Requests_IRI|string $relative Relative IRI - * @return Requests_IRI|false + * @param \WpOrg\Requests\Iri|string $base (Absolute) Base IRI + * @param \WpOrg\Requests\Iri|string $relative Relative IRI + * @return \WpOrg\Requests\Iri|false */ public static function absolutize($base, $relative) { - if (!($relative instanceof Requests_IRI)) { - $relative = new Requests_IRI($relative); + if (!($relative instanceof self)) { + $relative = new self($relative); } if (!$relative->is_valid()) { return false; @@ -269,8 +271,8 @@ public static function absolutize($base, $relative) { return clone $relative; } - if (!($base instanceof Requests_IRI)) { - $base = new Requests_IRI($base); + if (!($base instanceof self)) { + $base = new self($base); } if ($base->scheme === null || !$base->is_valid()) { return false; @@ -282,7 +284,7 @@ public static function absolutize($base, $relative) { $target->scheme = $base->scheme; } else { - $target = new Requests_IRI; + $target = new self; $target->scheme = $base->scheme; $target->iuserinfo = $base->iuserinfo; $target->ihost = $base->ihost; diff --git a/src/Requests.php b/src/Requests.php index 7af913fdd..cafa6f305 100644 --- a/src/Requests.php +++ b/src/Requests.php @@ -13,7 +13,6 @@ use Requests_Auth_Basic; use Requests_Cookie_Jar; -use Requests_IRI; use Requests_Proxy_HTTP; use Requests_Response; use Requests_Transport_cURL; @@ -21,6 +20,7 @@ use WpOrg\Requests\Exception; use WpOrg\Requests\Hooks; use WpOrg\Requests\IdnaEncoder; +use WpOrg\Requests\Iri; /** * Requests for PHP @@ -575,7 +575,7 @@ protected static function set_defaults(&$url, &$headers, &$data, &$type, &$optio } if ($options['idn'] !== false) { - $iri = new Requests_IRI($url); + $iri = new Iri($url); $iri->host = IdnaEncoder::encode($iri->ihost); $url = $iri->uri; } @@ -676,7 +676,7 @@ protected static function parse_response($headers, $url, $req_headers, $req_data $location = $return->headers['location']; if (strpos($location, 'http://') !== 0 && strpos($location, 'https://') !== 0) { // relative redirect, for compatibility make it absolute - $location = Requests_IRI::absolutize($url, $location); + $location = Iri::absolutize($url, $location); $location = $location->uri; } diff --git a/tests/CookiesTest.php b/tests/CookiesTest.php index e55bf9752..3d9f21969 100644 --- a/tests/CookiesTest.php +++ b/tests/CookiesTest.php @@ -4,11 +4,11 @@ use Requests\Tests\TestCase; use Requests_Cookie_Jar; -use Requests_IRI; use Requests_Response_Headers; use Requests_Utility_CaseInsensitiveDictionary; use WpOrg\Requests\Cookie; use WpOrg\Requests\Exception; +use WpOrg\Requests\Iri; use WpOrg\Requests\Requests; class CookiesTest extends TestCase { @@ -321,7 +321,7 @@ public function testUrlExactMatch($domain, $path, $check, $matches, $domain_matc $attributes = new Requests_Utility_CaseInsensitiveDictionary(); $attributes['domain'] = $domain; $attributes['path'] = $path; - $check = new Requests_IRI($check); + $check = new Iri($check); $cookie = new Cookie('requests-testcookie', 'testvalue', $attributes); $this->assertSame($matches, $cookie->uri_matches($check)); } @@ -338,7 +338,7 @@ public function testUrlMatch($domain, $path, $check, $matches, $domain_matches) $flags = array( 'host-only' => false, ); - $check = new Requests_IRI($check); + $check = new Iri($check); $cookie = new Cookie('requests-testcookie', 'testvalue', $attributes, $flags); $this->assertSame($domain_matches, $cookie->uri_matches($check)); } @@ -353,12 +353,12 @@ public function testUrlMatchSecure() { ); $cookie = new Cookie('requests-testcookie', 'testvalue', $attributes, $flags); - $this->assertTrue($cookie->uri_matches(new Requests_IRI('https://example.com/'))); - $this->assertFalse($cookie->uri_matches(new Requests_IRI('http://example.com/'))); + $this->assertTrue($cookie->uri_matches(new Iri('https://example.com/'))); + $this->assertFalse($cookie->uri_matches(new Iri('http://example.com/'))); // Double-check host-only - $this->assertTrue($cookie->uri_matches(new Requests_IRI('https://www.example.com/'))); - $this->assertFalse($cookie->uri_matches(new Requests_IRI('http://www.example.com/'))); + $this->assertTrue($cookie->uri_matches(new Iri('https://www.example.com/'))); + $this->assertFalse($cookie->uri_matches(new Iri('http://www.example.com/'))); } /** @@ -375,12 +375,12 @@ public function testUrlMatchManuallySet() { $this->assertTrue($cookie->path_matches('/')); $this->assertTrue($cookie->path_matches('/test')); $this->assertTrue($cookie->path_matches('/test/')); - $this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.com/'))); - $this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.com/test'))); - $this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.com/test/'))); - $this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.net/'))); - $this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.net/test'))); - $this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.net/test/'))); + $this->assertTrue($cookie->uri_matches(new Iri('http://example.com/'))); + $this->assertTrue($cookie->uri_matches(new Iri('http://example.com/test'))); + $this->assertTrue($cookie->uri_matches(new Iri('http://example.com/test/'))); + $this->assertTrue($cookie->uri_matches(new Iri('http://example.net/'))); + $this->assertTrue($cookie->uri_matches(new Iri('http://example.net/test'))); + $this->assertTrue($cookie->uri_matches(new Iri('http://example.net/test/'))); } public static function parseResultProvider() { @@ -647,7 +647,7 @@ public function parseFromHeadersProvider() { * @dataProvider parseFromHeadersProvider */ public function testParsingHeaderWithOrigin($header, $origin, $expected, $expected_attributes = array(), $expected_flags = array()) { - $origin = new Requests_IRI($origin); + $origin = new Iri($origin); $headers = new Requests_Response_Headers(); $headers['Set-Cookie'] = $header; diff --git a/tests/IRITest.php b/tests/IriTest.php similarity index 87% rename from tests/IRITest.php rename to tests/IriTest.php index a0662929f..61a9e8fcb 100644 --- a/tests/IRITest.php +++ b/tests/IriTest.php @@ -42,10 +42,10 @@ namespace Requests\Tests; -use Requests_IRI; use Requests\Tests\TestCase; +use WpOrg\Requests\Iri; -class IRITest extends TestCase +class IriTest extends TestCase { public static function rfc3986_tests() { @@ -102,9 +102,9 @@ public static function rfc3986_tests() */ public function testStringRFC3986($relative, $expected) { - $base = new Requests_IRI('http://a/b/c/d;p?q'); - $this->assertSame($expected, Requests_IRI::absolutize($base, $relative)->iri); - $this->assertSame($expected, (string) Requests_IRI::absolutize($base, $relative)); + $base = new Iri('http://a/b/c/d;p?q'); + $this->assertSame($expected, Iri::absolutize($base, $relative)->iri); + $this->assertSame($expected, (string) Iri::absolutize($base, $relative)); } /** @@ -113,8 +113,8 @@ public function testStringRFC3986($relative, $expected) public function testBothStringRFC3986($relative, $expected) { $base = 'http://a/b/c/d;p?q'; - $this->assertSame($expected, Requests_IRI::absolutize($base, $relative)->iri); - $this->assertSame($expected, (string) Requests_IRI::absolutize($base, $relative)); + $this->assertSame($expected, Iri::absolutize($base, $relative)->iri); + $this->assertSame($expected, (string) Iri::absolutize($base, $relative)); } /** @@ -122,9 +122,9 @@ public function testBothStringRFC3986($relative, $expected) */ public function testObjectRFC3986($relative, $expected) { - $base = new Requests_IRI('http://a/b/c/d;p?q'); - $expected = new Requests_IRI($expected); - $this->assertEquals($expected, Requests_IRI::absolutize($base, $relative)); + $base = new Iri('http://a/b/c/d;p?q'); + $expected = new Iri($expected); + $this->assertEquals($expected, Iri::absolutize($base, $relative)); } public static function sp_tests() @@ -154,9 +154,9 @@ public static function sp_tests() */ public function testStringSP($base, $relative, $expected) { - $base = new Requests_IRI($base); - $this->assertSame($expected, Requests_IRI::absolutize($base, $relative)->iri); - $this->assertSame($expected, (string) Requests_IRI::absolutize($base, $relative)); + $base = new Iri($base); + $this->assertSame($expected, Iri::absolutize($base, $relative)->iri); + $this->assertSame($expected, (string) Iri::absolutize($base, $relative)); } /** @@ -164,9 +164,9 @@ public function testStringSP($base, $relative, $expected) */ public function testObjectSP($base, $relative, $expected) { - $base = new Requests_IRI($base); - $expected = new Requests_IRI($expected); - $this->assertEquals($expected, Requests_IRI::absolutize($base, $relative)); + $base = new Iri($base); + $expected = new Iri($expected); + $this->assertEquals($expected, Iri::absolutize($base, $relative)); } public static function absolutize_tests() @@ -182,8 +182,8 @@ public static function absolutize_tests() */ public function testAbsolutizeString($base, $relative, $expected) { - $base = new Requests_IRI($base); - $this->assertSame($expected, Requests_IRI::absolutize($base, $relative)->iri); + $base = new Iri($base); + $this->assertSame($expected, Iri::absolutize($base, $relative)->iri); } /** @@ -191,9 +191,9 @@ public function testAbsolutizeString($base, $relative, $expected) */ public function testAbsolutizeObject($base, $relative, $expected) { - $base = new Requests_IRI($base); - $expected = new Requests_IRI($expected); - $this->assertEquals($expected, Requests_IRI::absolutize($base, $relative)); + $base = new Iri($base); + $expected = new Iri($expected); + $this->assertEquals($expected, Iri::absolutize($base, $relative)); } public static function normalization_tests() @@ -284,7 +284,7 @@ public static function normalization_tests() */ public function testStringNormalization($input, $output) { - $input = new Requests_IRI($input); + $input = new Iri($input); $this->assertSame($output, $input->iri); $this->assertSame($output, (string) $input); } @@ -294,8 +294,8 @@ public function testStringNormalization($input, $output) */ public function testObjectNormalization($input, $output) { - $input = new Requests_IRI($input); - $output = new Requests_IRI($output); + $input = new Iri($input); + $output = new Iri($output); $this->assertEquals($output, $input); } @@ -311,8 +311,8 @@ public static function equivalence_tests() */ public function testObjectEquivalence($input, $output) { - $input = new Requests_IRI($input); - $output = new Requests_IRI($output); + $input = new Iri($input); + $output = new Iri($output); $this->assertEquals($output, $input); } @@ -328,19 +328,19 @@ public static function not_equivalence_tests() */ public function testObjectNotEquivalence($input, $output) { - $input = new Requests_IRI($input); - $output = new Requests_IRI($output); + $input = new Iri($input); + $output = new Iri($output); $this->assertNotEquals($output, $input); } public function testInvalidAbsolutizeBase() { - $this->assertFalse(Requests_IRI::absolutize('://not a URL', '../')); + $this->assertFalse(Iri::absolutize('://not a URL', '../')); } public function testFullGamut() { - $iri = new Requests_IRI(); + $iri = new Iri(); $iri->scheme = 'http'; $iri->userinfo = 'user:password'; $iri->host = 'example.com'; @@ -357,7 +357,7 @@ public function testFullGamut() public function testReadAliased() { - $iri = new Requests_IRI(); + $iri = new Iri(); $iri->scheme = 'http'; $iri->userinfo = 'user:password'; $iri->host = 'example.com'; @@ -374,7 +374,7 @@ public function testReadAliased() public function testWriteAliased() { - $iri = new Requests_IRI(); + $iri = new Iri(); $iri->scheme = 'http'; $iri->iuserinfo = 'user:password'; $iri->ihost = 'example.com'; @@ -391,15 +391,15 @@ public function testWriteAliased() public function testNonexistantProperty() { - $this->expectNotice('Undefined property: Requests_IRI::nonexistant_prop'); - $iri = new Requests_IRI(); + $this->expectNotice('Undefined property: WpOrg\Requests\Iri::nonexistant_prop'); + $iri = new Iri(); $this->assertFalse(isset($iri->nonexistant_prop)); $should_fail = $iri->nonexistant_prop; } public function testBlankHost() { - $iri = new Requests_IRI('http://example.com/a/?b=c#d'); + $iri = new Iri('http://example.com/a/?b=c#d'); $iri->host = null; $this->assertNull($iri->host); @@ -408,7 +408,7 @@ public function testBlankHost() public function testBadPort() { - $iri = new Requests_IRI(); + $iri = new Iri(); $iri->port = 'example'; $this->assertNull($iri->port); From 894c3d12bb4327abda58b7d743cae0d0fb45c2d0 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 16 Aug 2021 02:50:03 +0200 Subject: [PATCH 20/45] Requests_Response: change to namespaced class Includes: * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. --- docs/hooks.md | 2 +- docs/usage.md | 2 +- docs/why-requests.md | 2 +- library/Deprecated.php | 1 + library/Requests/Cookie/Jar.php | 5 +++-- library/Requests/Exception/HTTP/Unknown.php | 6 ++++-- library/Requests/Session.php | 8 ++++---- library/Requests/Transport/cURL.php | 2 +- library/Requests/Transport/fsockopen.php | 2 +- src/Autoload.php | 1 + src/Requests.php | 20 ++++++++++---------- {library/Requests => src}/Response.php | 9 +++++++-- src/Transport.php | 2 +- tests/SessionTest.php | 6 +++--- tests/Transport/BaseTestCase.php | 8 ++++---- 15 files changed, 43 insertions(+), 33 deletions(-) rename {library/Requests => src}/Response.php (93%) diff --git a/docs/hooks.md b/docs/hooks.md index 3390be5dd..1e0cc455a 100644 --- a/docs/hooks.md +++ b/docs/hooks.md @@ -28,7 +28,7 @@ Available Hooks Alter the response object before it is returned to the user. - Parameters: `Requests_Response &$return` + Parameters: `WpOrg\Requests\Response &$return` * **`curl.before_request`** diff --git a/docs/usage.md b/docs/usage.md index f16d78181..f1e0074b7 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -18,7 +18,7 @@ Let's grab GitHub's public events: $response = WpOrg\Requests\Requests::get('https://api.github.com/events'); ``` -`$response` is now a **Requests_Response** object. Response objects are what +`$response` is now a **WpOrg\Requests\Response** object. Response objects are what you'll be working with whenever you want to get data back from your request. diff --git a/docs/why-requests.md b/docs/why-requests.md index 31389c2c8..e239759ce 100644 --- a/docs/why-requests.md +++ b/docs/why-requests.md @@ -89,7 +89,7 @@ instead of something else, in our opinion. Requests, on the other hand, exposes only a handful of classes to the global namespace, most of which are for internal use. You can learn to use - the `WpOrg\Requests\Requests::request()` method and the `Requests_Response` + the `WpOrg\Requests\Requests::request()` method and the `WpOrg\Requests\Response` object in the space of 10 minutes and you already know how to use Requests. diff --git a/library/Deprecated.php b/library/Deprecated.php index f487dc4c0..71ec395bd 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -38,3 +38,4 @@ class Requests_Hooks extends WpOrg\Requests\Hooks {} class Requests_IDNAEncoder extends WpOrg\Requests\IdnaEncoder {} class Requests_IPv6 extends WpOrg\Requests\Ipv6 {} class Requests_IRI extends WpOrg\Requests\Iri {} +class Requests_Response extends WpOrg\Requests\Response {} diff --git a/library/Requests/Cookie/Jar.php b/library/Requests/Cookie/Jar.php index b593c662d..0f76ae7ab 100644 --- a/library/Requests/Cookie/Jar.php +++ b/library/Requests/Cookie/Jar.php @@ -10,6 +10,7 @@ use WpOrg\Requests\Exception; use WpOrg\Requests\Hooker; use WpOrg\Requests\Iri; +use WpOrg\Requests\Response; /** * Cookie holder object @@ -159,9 +160,9 @@ public function before_request($url, &$headers, &$data, &$type, &$options) { /** * Parse all cookies from a response and attach them to the response * - * @var Requests_Response $response + * @var \WpOrg\Requests\Response $response */ - public function before_redirect_check(Requests_Response $return) { + public function before_redirect_check(Response $return) { $url = $return->url; if (!$url instanceof Iri) { $url = new Iri($url); diff --git a/library/Requests/Exception/HTTP/Unknown.php b/library/Requests/Exception/HTTP/Unknown.php index b7eeb7953..a1e76a6ac 100644 --- a/library/Requests/Exception/HTTP/Unknown.php +++ b/library/Requests/Exception/HTTP/Unknown.php @@ -5,6 +5,8 @@ * @package Requests */ +use WpOrg\Requests\Response; + /** * Exception for unknown status responses * @@ -28,14 +30,14 @@ class Requests_Exception_HTTP_Unknown extends Requests_Exception_HTTP { /** * Create a new exception * - * If `$data` is an instance of {@see Requests_Response}, uses the status + * If `$data` is an instance of {@see \WpOrg\Requests\Response}, uses the status * code from it. Otherwise, sets as 0 * * @param string|null $reason Reason phrase * @param mixed $data Associated data */ public function __construct($reason = null, $data = null) { - if ($data instanceof Requests_Response) { + if ($data instanceof Response) { $this->code = $data->status_code; } diff --git a/library/Requests/Session.php b/library/Requests/Session.php index 27eb2fe98..45de5b89d 100644 --- a/library/Requests/Session.php +++ b/library/Requests/Session.php @@ -128,7 +128,7 @@ public function __unset($key) { * @param string $url * @param array $headers * @param array $options - * @return Requests_Response + * @return \WpOrg\Requests\Response */ /** * Send a GET request @@ -158,7 +158,7 @@ public function delete($url, $headers = array(), $options = array()) { * @param array $headers * @param array $data * @param array $options - * @return Requests_Response + * @return \WpOrg\Requests\Response */ /** * Send a POST request @@ -202,7 +202,7 @@ public function patch($url, $headers, $data = array(), $options = array()) { * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests * @param string $type HTTP request type (use \WpOrg\Requests\Requests constants) * @param array $options Options for the request (see {@see \WpOrg\Requests\Requests::request}) - * @return Requests_Response + * @return \WpOrg\Requests\Response */ public function request($url, $headers = array(), $data = array(), $type = Requests::GET, $options = array()) { $request = $this->merge_request(compact('url', 'headers', 'data', 'options')); @@ -217,7 +217,7 @@ public function request($url, $headers = array(), $data = array(), $type = Reque * * @param array $requests Requests data (see {@see \WpOrg\Requests\Requests::request_multiple}) * @param array $options Global and default options (see {@see \WpOrg\Requests\Requests::request}) - * @return array Responses (either Requests_Response or a \WpOrg\Requests\Exception object) + * @return array Responses (either \WpOrg\Requests\Response or a \WpOrg\Requests\Exception object) */ public function request_multiple($requests, $options = array()) { foreach ($requests as $key => $request) { diff --git a/library/Requests/Transport/cURL.php b/library/Requests/Transport/cURL.php index 5aab934ee..5f3d58506 100644 --- a/library/Requests/Transport/cURL.php +++ b/library/Requests/Transport/cURL.php @@ -219,7 +219,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar * * @param array $requests Request data * @param array $options Global options - * @return array Array of Requests_Response objects (may contain \WpOrg\Requests\Exception or string responses as well) + * @return array Array of \WpOrg\Requests\Response objects (may contain \WpOrg\Requests\Exception or string responses as well) */ public function request_multiple($requests, $options) { // If you're not requesting, we can't get any responses ¯\_(ツ)_/¯ diff --git a/library/Requests/Transport/fsockopen.php b/library/Requests/Transport/fsockopen.php index 0987ed844..85c2515ab 100644 --- a/library/Requests/Transport/fsockopen.php +++ b/library/Requests/Transport/fsockopen.php @@ -331,7 +331,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar * * @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per {@see \WpOrg\Requests\Transport::request} * @param array $options Global options, see {@see \WpOrg\Requests\Requests::response()} for documentation - * @return array Array of Requests_Response objects (may contain \WpOrg\Requests\Exception or string responses as well) + * @return array Array of \WpOrg\Requests\Response objects (may contain \WpOrg\Requests\Exception or string responses as well) */ public function request_multiple($requests, $options) { $responses = array(); diff --git a/src/Autoload.php b/src/Autoload.php index 990888dad..df74c2096 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -46,6 +46,7 @@ class Autoload { 'requests_idnaencoder' => '\WpOrg\Requests\IdnaEncoder', 'requests_ipv6' => '\WpOrg\Requests\Ipv6', 'requests_iri' => '\WpOrg\Requests\Iri', + 'requests_response' => '\WpOrg\Requests\Response', ); /** diff --git a/src/Requests.php b/src/Requests.php index cafa6f305..39fe2924e 100644 --- a/src/Requests.php +++ b/src/Requests.php @@ -14,13 +14,13 @@ use Requests_Auth_Basic; use Requests_Cookie_Jar; use Requests_Proxy_HTTP; -use Requests_Response; use Requests_Transport_cURL; use Requests_Transport_fsockopen; use WpOrg\Requests\Exception; use WpOrg\Requests\Hooks; use WpOrg\Requests\IdnaEncoder; use WpOrg\Requests\Iri; +use WpOrg\Requests\Response; /** * Requests for PHP @@ -204,7 +204,7 @@ protected static function get_transport($capabilities = array()) { * @param string $url * @param array $headers * @param array $options - * @return Requests_Response + * @return \WpOrg\Requests\Response */ /** * Send a GET request @@ -241,7 +241,7 @@ public static function trace($url, $headers = array(), $options = array()) { * @param array $headers * @param array $data * @param array $options - * @return Requests_Response + * @return \WpOrg\Requests\Response */ /** * Send a POST request @@ -334,7 +334,7 @@ public static function patch($url, $headers, $data = array(), $options = array() * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests * @param string $type HTTP request type (use Requests constants) * @param array $options Options for the request (see description for more information) - * @return Requests_Response + * @return \WpOrg\Requests\Response */ public static function request($url, $headers = array(), $data = array(), $type = self::GET, $options = array()) { if (empty($options['type'])) { @@ -397,14 +397,14 @@ public static function request($url, $headers = array(), $data = array(), $type * In addition, the `$options` parameter takes the following global options: * * - `complete`: A callback for when a request is complete. Takes two - * parameters, a Requests_Response/\WpOrg\Requests\Exception reference, and the + * parameters, a \WpOrg\Requests\Response/\WpOrg\Requests\Exception reference, and the * ID from the request array (Note: this can also be overridden on a * per-request basis, although that's a little silly) * (callback) * * @param array $requests Requests data (see description for more information) * @param array $options Global and default options (see {@see \WpOrg\Requests\Requests::request}) - * @return array Responses (either Requests_Response or a \WpOrg\Requests\Exception object) + * @return array Responses (either \WpOrg\Requests\Response or a \WpOrg\Requests\Exception object) */ public static function request_multiple($requests, $options = array()) { $options = array_merge(self::get_default_options(true), $options); @@ -605,10 +605,10 @@ protected static function set_defaults(&$url, &$headers, &$data, &$type, &$optio * @param array $req_headers Original $headers array passed to {@link request()}, in case we need to follow redirects * @param array $req_data Original $data array passed to {@link request()}, in case we need to follow redirects * @param array $options Original $options array passed to {@link request()}, in case we need to follow redirects - * @return Requests_Response + * @return \WpOrg\Requests\Response */ protected static function parse_response($headers, $url, $req_headers, $req_data, $options) { - $return = new Requests_Response(); + $return = new Response(); if (!$options['blocking']) { return $return; } @@ -706,12 +706,12 @@ protected static function parse_response($headers, $url, $req_headers, $req_data /** * Callback for `transport.internal.parse_response` * - * Internal use only. Converts a raw HTTP response to a Requests_Response + * Internal use only. Converts a raw HTTP response to a \WpOrg\Requests\Response * while still executing a multiple request. * * @param string $response Full response text including headers and body (will be overwritten with Response instance) * @param array $request Request data as passed into {@see \WpOrg\Requests\Requests::request_multiple()} - * @return null `$response` is either set to a Requests_Response instance, or a \WpOrg\Requests\Exception object + * @return null `$response` is either set to a \WpOrg\Requests\Response instance, or a \WpOrg\Requests\Exception object */ public static function parse_multiple(&$response, $request) { try { diff --git a/library/Requests/Response.php b/src/Response.php similarity index 93% rename from library/Requests/Response.php rename to src/Response.php index 2e167b9e1..c8f80277e 100644 --- a/library/Requests/Response.php +++ b/src/Response.php @@ -6,6 +6,11 @@ * @package Requests */ +namespace WpOrg\Requests; + +use Requests_Cookie_Jar; +use Requests_Exception_HTTP; +use Requests_Response_Headers; use WpOrg\Requests\Exception; /** @@ -14,7 +19,7 @@ * Contains a response from \WpOrg\Requests\Requests::request() * @package Requests */ -class Requests_Response { +class Response { /** * Constructor */ @@ -82,7 +87,7 @@ public function __construct() { /** * Previous requests (from redirects) * - * @var array Array of Requests_Response objects + * @var array Array of \WpOrg\Requests\Response objects */ public $history = array(); diff --git a/src/Transport.php b/src/Transport.php index 94f362d1e..313d7c83a 100644 --- a/src/Transport.php +++ b/src/Transport.php @@ -31,7 +31,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar * * @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per {@see \WpOrg\Requests\Transport::request} * @param array $options Global options, see {@see \WpOrg\Requests\Requests::response()} for documentation - * @return array Array of Requests_Response objects (may contain \WpOrg\Requests\Exception or string responses as well) + * @return array Array of \WpOrg\Requests\Response objects (may contain \WpOrg\Requests\Exception or string responses as well) */ public function request_multiple($requests, $options); diff --git a/tests/SessionTest.php b/tests/SessionTest.php index ecd637744..a0fe419ef 100644 --- a/tests/SessionTest.php +++ b/tests/SessionTest.php @@ -3,8 +3,8 @@ namespace Requests\Tests; use Requests\Tests\TestCase; -use Requests_Response; use Requests_Session; +use WpOrg\Requests\Response; class SessionTest extends TestCase { public function testURLResolution() { @@ -139,7 +139,7 @@ public function testMultiple() { // test1 $this->assertNotEmpty($responses['test1']); - $this->assertInstanceOf(Requests_Response::class, $responses['test1']); + $this->assertInstanceOf(Response::class, $responses['test1']); $this->assertSame(200, $responses['test1']->status_code); $result = json_decode($responses['test1']->body, true); @@ -148,7 +148,7 @@ public function testMultiple() { // test2 $this->assertNotEmpty($responses['test2']); - $this->assertInstanceOf(Requests_Response::class, $responses['test2']); + $this->assertInstanceOf(Response::class, $responses['test2']); $this->assertSame(200, $responses['test2']->status_code); $result = json_decode($responses['test2']->body, true); diff --git a/tests/Transport/BaseTestCase.php b/tests/Transport/BaseTestCase.php index 1d377cd0c..147af96cc 100644 --- a/tests/Transport/BaseTestCase.php +++ b/tests/Transport/BaseTestCase.php @@ -5,11 +5,11 @@ use Requests\Tests\Mock\TransportMock; use Requests\Tests\TestCase; use Requests_Exception_HTTP_Unknown; -use Requests_Response; use stdClass; use WpOrg\Requests\Exception; use WpOrg\Requests\Hooks; use WpOrg\Requests\Requests; +use WpOrg\Requests\Response; abstract class BaseTestCase extends TestCase { public function set_up() { @@ -635,7 +635,7 @@ public function testNonblocking() { 'blocking' => false, ); $request = Requests::get(httpbin('/get'), array(), $this->getOptions($options)); - $empty = new Requests_Response(); + $empty = new Response(); $this->assertEquals($empty, $request); } @@ -755,7 +755,7 @@ public function testMultiple() { // test1 $this->assertNotEmpty($responses['test1']); - $this->assertInstanceOf(Requests_Response::class, $responses['test1']); + $this->assertInstanceOf(Response::class, $responses['test1']); $this->assertSame(200, $responses['test1']->status_code); $result = json_decode($responses['test1']->body, true); @@ -764,7 +764,7 @@ public function testMultiple() { // test2 $this->assertNotEmpty($responses['test2']); - $this->assertInstanceOf(Requests_Response::class, $responses['test2']); + $this->assertInstanceOf(Response::class, $responses['test2']); $this->assertSame(200, $responses['test2']->status_code); $result = json_decode($responses['test2']->body, true); From e06e605c3425df5530641bfdb10130160ba0ecaf Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 16 Aug 2021 03:01:03 +0200 Subject: [PATCH 21/45] Requests_Session: change to namespaced class Includes: * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. --- docs/usage-advanced.md | 4 ++-- examples/session.php | 2 +- library/Deprecated.php | 1 + src/Autoload.php | 1 + {library/Requests => src}/Session.php | 5 ++++- tests/SessionTest.php | 22 +++++++++++----------- 6 files changed, 20 insertions(+), 15 deletions(-) rename {library/Requests => src}/Session.php (99%) diff --git a/docs/usage-advanced.md b/docs/usage-advanced.md index ea084dd95..0f0319535 100644 --- a/docs/usage-advanced.md +++ b/docs/usage-advanced.md @@ -10,14 +10,14 @@ default parameters for these. Let's simulate communicating with GitHub. ```php -$session = new Requests_Session('https://api.github.com/'); +$session = new WpOrg\Requests\Session('https://api.github.com/'); $session->headers['X-ContactAuthor'] = 'rmccue'; $session->useragent = 'My-Awesome-App'; $response = $session->get('/zen'); ``` -You can use the `url`, `headers`, `data` and `options` properties of the Session +You can use the `url`, `headers`, `data` and `options` properties of the `WpOrg\Requests\Session` object to set the defaults for this session, and the constructor also takes parameters in the same order as `WpOrg\Requests\Requests::request()`. Accessing any other properties will set the corresponding key in the options array; that is: diff --git a/examples/session.php b/examples/session.php index e0ef67f16..600afdbac 100644 --- a/examples/session.php +++ b/examples/session.php @@ -7,7 +7,7 @@ WpOrg\Requests\Autoload::register(); // Set up our session -$session = new Requests_Session('http://httpbin.org/'); +$session = new WpOrg\Requests\Session('http://httpbin.org/'); $session->headers['Accept'] = 'application/json'; $session->useragent = 'Awesomesauce'; diff --git a/library/Deprecated.php b/library/Deprecated.php index 71ec395bd..d646b8123 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -39,3 +39,4 @@ class Requests_IDNAEncoder extends WpOrg\Requests\IdnaEncoder {} class Requests_IPv6 extends WpOrg\Requests\Ipv6 {} class Requests_IRI extends WpOrg\Requests\Iri {} class Requests_Response extends WpOrg\Requests\Response {} +class Requests_Session extends WpOrg\Requests\Session {} diff --git a/src/Autoload.php b/src/Autoload.php index df74c2096..43c16fffb 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -47,6 +47,7 @@ class Autoload { 'requests_ipv6' => '\WpOrg\Requests\Ipv6', 'requests_iri' => '\WpOrg\Requests\Iri', 'requests_response' => '\WpOrg\Requests\Response', + 'requests_session' => '\WpOrg\Requests\Session', ); /** diff --git a/library/Requests/Session.php b/src/Session.php similarity index 99% rename from library/Requests/Session.php rename to src/Session.php index 45de5b89d..c5c2942e7 100644 --- a/library/Requests/Session.php +++ b/src/Session.php @@ -6,6 +6,9 @@ * @subpackage Session Handler */ +namespace WpOrg\Requests; + +use Requests_Cookie_Jar; use WpOrg\Requests\Iri; use WpOrg\Requests\Requests; @@ -20,7 +23,7 @@ * @package Requests * @subpackage Session Handler */ -class Requests_Session { +class Session { /** * Base URL for requests * diff --git a/tests/SessionTest.php b/tests/SessionTest.php index a0fe419ef..a2c44ed11 100644 --- a/tests/SessionTest.php +++ b/tests/SessionTest.php @@ -3,12 +3,12 @@ namespace Requests\Tests; use Requests\Tests\TestCase; -use Requests_Session; use WpOrg\Requests\Response; +use WpOrg\Requests\Session; class SessionTest extends TestCase { public function testURLResolution() { - $session = new Requests_Session(httpbin('/')); + $session = new Session(httpbin('/')); // Set the cookies up $response = $session->get('/get'); @@ -34,7 +34,7 @@ public function testBasicGET() { 'X-Requests-Session' => 'BasicGET', 'X-Requests-Request' => 'notset', ); - $session = new Requests_Session(httpbin('/'), $session_headers); + $session = new Session(httpbin('/'), $session_headers); $response = $session->get('/get', array('X-Requests-Request' => 'GET')); $response->throw_for_status(false); $this->assertSame(200, $response->status_code); @@ -51,7 +51,7 @@ public function testBasicHEAD() { 'X-Requests-Session' => 'BasicHEAD', 'X-Requests-Request' => 'notset', ); - $session = new Requests_Session(httpbin('/'), $session_headers); + $session = new Session(httpbin('/'), $session_headers); $response = $session->head('/get', array('X-Requests-Request' => 'HEAD')); $response->throw_for_status(false); $this->assertSame(200, $response->status_code); @@ -62,7 +62,7 @@ public function testBasicDELETE() { 'X-Requests-Session' => 'BasicDELETE', 'X-Requests-Request' => 'notset', ); - $session = new Requests_Session(httpbin('/'), $session_headers); + $session = new Session(httpbin('/'), $session_headers); $response = $session->delete('/delete', array('X-Requests-Request' => 'DELETE')); $response->throw_for_status(false); $this->assertSame(200, $response->status_code); @@ -79,7 +79,7 @@ public function testBasicPOST() { 'X-Requests-Session' => 'BasicPOST', 'X-Requests-Request' => 'notset', ); - $session = new Requests_Session(httpbin('/'), $session_headers); + $session = new Session(httpbin('/'), $session_headers); $response = $session->post('/post', array('X-Requests-Request' => 'POST'), array('postdata' => 'exists')); $response->throw_for_status(false); $this->assertSame(200, $response->status_code); @@ -96,7 +96,7 @@ public function testBasicPUT() { 'X-Requests-Session' => 'BasicPUT', 'X-Requests-Request' => 'notset', ); - $session = new Requests_Session(httpbin('/'), $session_headers); + $session = new Session(httpbin('/'), $session_headers); $response = $session->put('/put', array('X-Requests-Request' => 'PUT'), array('postdata' => 'exists')); $response->throw_for_status(false); $this->assertSame(200, $response->status_code); @@ -113,7 +113,7 @@ public function testBasicPATCH() { 'X-Requests-Session' => 'BasicPATCH', 'X-Requests-Request' => 'notset', ); - $session = new Requests_Session(httpbin('/'), $session_headers); + $session = new Session(httpbin('/'), $session_headers); $response = $session->patch('/patch', array('X-Requests-Request' => 'PATCH'), array('postdata' => 'exists')); $response->throw_for_status(false); $this->assertSame(200, $response->status_code); @@ -126,7 +126,7 @@ public function testBasicPATCH() { } public function testMultiple() { - $session = new Requests_Session(httpbin('/'), array('X-Requests-Session' => 'Multiple')); + $session = new Session(httpbin('/'), array('X-Requests-Session' => 'Multiple')); $requests = array( 'test1' => array( 'url' => httpbin('/get'), @@ -174,7 +174,7 @@ public function testPropertyUsage() { 'foo' => 'bar', ); - $session = new Requests_Session('http://example.com/', $headers, $data, $options); + $session = new Session('http://example.com/', $headers, $data, $options); $this->assertSame('http://example.com/', $session->url); $this->assertSame($headers, $session->headers); $this->assertSame($data, $session->data); @@ -202,7 +202,7 @@ public function testPropertyUsage() { } public function testSharedCookies() { - $session = new Requests_Session(httpbin('/')); + $session = new Session(httpbin('/')); $options = array( 'follow_redirects' => false, From bb5ea241fa295a0d23826afc884978255f38dfee Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 16 Aug 2021 03:10:31 +0200 Subject: [PATCH 22/45] Requests_SSL: change to namespaced class Includes: * Using CamelCaps for the class name, as well as the test class. * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. --- docs/why-requests.md | 2 +- library/Deprecated.php | 1 + library/Requests/Transport/fsockopen.php | 3 ++- src/Autoload.php | 1 + library/Requests/SSL.php => src/Ssl.php | 4 +++- tests/{SSLTest.php => SslTest.php} | 20 ++++++++++---------- 6 files changed, 18 insertions(+), 13 deletions(-) rename library/Requests/SSL.php => src/Ssl.php (98%) rename tests/{SSLTest.php => SslTest.php} (77%) diff --git a/docs/why-requests.md b/docs/why-requests.md index e239759ce..ca1253e11 100644 --- a/docs/why-requests.md +++ b/docs/why-requests.md @@ -60,7 +60,7 @@ Why should I use Requests? [codecov]: https://app.codecov.io/gh/WordPress/Requests/branch/stable [hooking system]: hooks.md -[requests_ssl]: https://github.com/WordPress/Requests/blob/stable/library/Requests/SSL.php +[requests_ssl]: https://github.com/WordPress/Requests/blob/stable/src/Ssl.php [ghactions]: https://github.com/WordPress/Requests/actions [wpssl]: https://core.trac.wordpress.org/ticket/25007 diff --git a/library/Deprecated.php b/library/Deprecated.php index d646b8123..2b2d5bbd0 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -40,3 +40,4 @@ class Requests_IPv6 extends WpOrg\Requests\Ipv6 {} class Requests_IRI extends WpOrg\Requests\Iri {} class Requests_Response extends WpOrg\Requests\Response {} class Requests_Session extends WpOrg\Requests\Session {} +class Requests_SSL extends WpOrg\Requests\Ssl {} diff --git a/library/Requests/Transport/fsockopen.php b/library/Requests/Transport/fsockopen.php index 85c2515ab..c9aeb1c68 100644 --- a/library/Requests/Transport/fsockopen.php +++ b/library/Requests/Transport/fsockopen.php @@ -8,6 +8,7 @@ use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; +use WpOrg\Requests\Ssl; use WpOrg\Requests\Transport; /** @@ -448,7 +449,7 @@ public function verify_certificate_from_context($host, $context) { $cert = openssl_x509_parse($meta['ssl']['peer_certificate']); - return Requests_SSL::verify_certificate($host, $cert); + return Ssl::verify_certificate($host, $cert); } /** diff --git a/src/Autoload.php b/src/Autoload.php index 43c16fffb..3a68cfd82 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -48,6 +48,7 @@ class Autoload { 'requests_iri' => '\WpOrg\Requests\Iri', 'requests_response' => '\WpOrg\Requests\Response', 'requests_session' => '\WpOrg\Requests\Session', + 'requests_ssl' => '\WpOrg\Requests\Ssl', ); /** diff --git a/library/Requests/SSL.php b/src/Ssl.php similarity index 98% rename from library/Requests/SSL.php rename to src/Ssl.php index 070b99d4a..1ac84fdf8 100644 --- a/library/Requests/SSL.php +++ b/src/Ssl.php @@ -6,6 +6,8 @@ * @subpackage Utilities */ +namespace WpOrg\Requests; + /** * SSL utilities for Requests * @@ -14,7 +16,7 @@ * @package Requests * @subpackage Utilities */ -class Requests_SSL { +class Ssl { /** * Verify the certificate against common name and subject alternative names * diff --git a/tests/SSLTest.php b/tests/SslTest.php similarity index 77% rename from tests/SSLTest.php rename to tests/SslTest.php index 7f3c462cd..caa3b8f29 100644 --- a/tests/SSLTest.php +++ b/tests/SslTest.php @@ -3,9 +3,9 @@ namespace Requests\Tests; use Requests\Tests\TestCase; -use Requests_SSL; +use WpOrg\Requests\Ssl; -class SSLTest extends TestCase { +class SslTest extends TestCase { public static function domainMatchProvider() { return array( array('example.com', 'example.com'), @@ -40,14 +40,14 @@ public static function domainNoMatchProvider() { * @dataProvider domainMatchProvider */ public function testMatch($base, $dnsname) { - $this->assertTrue(Requests_SSL::match_domain($base, $dnsname)); + $this->assertTrue(Ssl::match_domain($base, $dnsname)); } /** * @dataProvider domainNoMatchProvider */ public function testNoMatch($base, $dnsname) { - $this->assertFalse(Requests_SSL::match_domain($base, $dnsname)); + $this->assertFalse(Ssl::match_domain($base, $dnsname)); } protected function fakeCertificate($dnsname, $with_san = true) { @@ -75,7 +75,7 @@ protected function fakeCertificate($dnsname, $with_san = true) { */ public function testMatchViaCertificate($base, $dnsname) { $certificate = $this->fakeCertificate($dnsname); - $this->assertTrue(Requests_SSL::verify_certificate($base, $certificate)); + $this->assertTrue(Ssl::verify_certificate($base, $certificate)); } /** @@ -83,17 +83,17 @@ public function testMatchViaCertificate($base, $dnsname) { */ public function testNoMatchViaCertificate($base, $dnsname) { $certificate = $this->fakeCertificate($dnsname); - $this->assertFalse(Requests_SSL::verify_certificate($base, $certificate)); + $this->assertFalse(Ssl::verify_certificate($base, $certificate)); } public function testCNFallback() { $certificate = $this->fakeCertificate('example.com', false); - $this->assertTrue(Requests_SSL::verify_certificate('example.com', $certificate)); + $this->assertTrue(Ssl::verify_certificate('example.com', $certificate)); } public function testInvalidCNFallback() { $certificate = $this->fakeCertificate('example.com', false); - $this->assertFalse(Requests_SSL::verify_certificate('example.net', $certificate)); + $this->assertFalse(Ssl::verify_certificate('example.net', $certificate)); } /** @@ -107,7 +107,7 @@ public function testInvalidCNFallback() { public function testIgnoreCNWithSAN() { $certificate = $this->fakeCertificate('example.net', 'example.com'); - $this->assertTrue(Requests_SSL::verify_certificate('example.com', $certificate), 'Checking SAN validation'); - $this->assertFalse(Requests_SSL::verify_certificate('example.net', $certificate), 'Checking CN non-validation'); + $this->assertTrue(Ssl::verify_certificate('example.com', $certificate), 'Checking SAN validation'); + $this->assertFalse(Ssl::verify_certificate('example.net', $certificate), 'Checking CN non-validation'); } } From 53c92042e7d4f9d1baf8988d67d82d3bf9a428bc Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 16 Aug 2021 03:15:44 +0200 Subject: [PATCH 23/45] Requests_Auth_Basic: change to namespaced class Includes: * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. --- docs/authentication.md | 2 +- library/Deprecated.php | 2 ++ {library/Requests => src}/Auth/Basic.php | 4 +++- src/Autoload.php | 1 + src/Requests.php | 4 ++-- tests/Auth/BasicTest.php | 8 ++++---- 6 files changed, 13 insertions(+), 8 deletions(-) rename {library/Requests => src}/Auth/Basic.php (96%) diff --git a/docs/authentication.md b/docs/authentication.md index 8514741b5..b394002b1 100644 --- a/docs/authentication.md +++ b/docs/authentication.md @@ -8,7 +8,7 @@ A Basic authenticated call can be made like this: ```php $options = array( - 'auth' => new Requests_Auth_Basic(array('user', 'password')) + 'auth' => new WpOrg\Requests\Auth\Basic(array('user', 'password')) ); WpOrg\Requests\Requests::get('https://httpbin.org/basic-auth/user/password', array(), $options); ``` diff --git a/library/Deprecated.php b/library/Deprecated.php index 2b2d5bbd0..7c5efa92a 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -41,3 +41,5 @@ class Requests_IRI extends WpOrg\Requests\Iri {} class Requests_Response extends WpOrg\Requests\Response {} class Requests_Session extends WpOrg\Requests\Session {} class Requests_SSL extends WpOrg\Requests\Ssl {} + +class Requests_Auth_Basic extends WpOrg\Requests\Auth\Basic {} diff --git a/library/Requests/Auth/Basic.php b/src/Auth/Basic.php similarity index 96% rename from library/Requests/Auth/Basic.php rename to src/Auth/Basic.php index 38e1df59e..83bd94979 100644 --- a/library/Requests/Auth/Basic.php +++ b/src/Auth/Basic.php @@ -6,6 +6,8 @@ * @subpackage Authentication */ +namespace WpOrg\Requests\Auth; + use WpOrg\Requests\Auth; use WpOrg\Requests\Exception; use WpOrg\Requests\Hooks; @@ -19,7 +21,7 @@ * @package Requests * @subpackage Authentication */ -class Requests_Auth_Basic implements Auth { +class Basic implements Auth { /** * Username * diff --git a/src/Autoload.php b/src/Autoload.php index 3a68cfd82..5f3db7be3 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -49,6 +49,7 @@ class Autoload { 'requests_response' => '\WpOrg\Requests\Response', 'requests_session' => '\WpOrg\Requests\Session', 'requests_ssl' => '\WpOrg\Requests\Ssl', + 'requests_auth_basic' => '\WpOrg\Requests\Auth\Basic', ); /** diff --git a/src/Requests.php b/src/Requests.php index 39fe2924e..67fdc5e48 100644 --- a/src/Requests.php +++ b/src/Requests.php @@ -11,11 +11,11 @@ namespace WpOrg\Requests; -use Requests_Auth_Basic; use Requests_Cookie_Jar; use Requests_Proxy_HTTP; use Requests_Transport_cURL; use Requests_Transport_fsockopen; +use WpOrg\Requests\Auth\Basic; use WpOrg\Requests\Exception; use WpOrg\Requests\Hooks; use WpOrg\Requests\IdnaEncoder; @@ -551,7 +551,7 @@ protected static function set_defaults(&$url, &$headers, &$data, &$type, &$optio } if (is_array($options['auth'])) { - $options['auth'] = new Requests_Auth_Basic($options['auth']); + $options['auth'] = new Basic($options['auth']); } if ($options['auth'] !== false) { $options['auth']->register($options['hooks']); diff --git a/tests/Auth/BasicTest.php b/tests/Auth/BasicTest.php index fe1a50b30..941d89400 100644 --- a/tests/Auth/BasicTest.php +++ b/tests/Auth/BasicTest.php @@ -3,9 +3,9 @@ namespace Requests\Tests\Auth; use Requests\Tests\TestCase; -use Requests_Auth_Basic; use Requests_Transport_cURL; use Requests_Transport_fsockopen; +use WpOrg\Requests\Auth\Basic; use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; @@ -48,7 +48,7 @@ public function testUsingInstantiation($transport) { } $options = array( - 'auth' => new Requests_Auth_Basic(array('user', 'passwd')), + 'auth' => new Basic(array('user', 'passwd')), 'transport' => $transport, ); $request = Requests::get(httpbin('/basic-auth/user/passwd'), array(), $options); @@ -69,7 +69,7 @@ public function testPOSTUsingInstantiation($transport) { } $options = array( - 'auth' => new Requests_Auth_Basic(array('user', 'passwd')), + 'auth' => new Basic(array('user', 'passwd')), 'transport' => $transport, ); $data = 'test'; @@ -88,7 +88,7 @@ public function testPOSTUsingInstantiation($transport) { public function testMissingPassword() { $this->expectException(Exception::class); $this->expectExceptionMessage('Invalid number of arguments'); - new Requests_Auth_Basic(array('user')); + new Basic(array('user')); } } From d0b8c0688ff5ee40ea3ddbeb3ab05b7de02eab72 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 16 Aug 2021 03:22:11 +0200 Subject: [PATCH 24/45] Requests_Cookie_Jar: change to namespaced class Includes: * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. --- examples/cookie_jar.php | 2 +- library/Deprecated.php | 1 + src/Autoload.php | 1 + {library/Requests => src}/Cookie/Jar.php | 10 ++++++++-- src/Requests.php | 8 ++++---- src/Response.php | 6 +++--- src/Session.php | 4 ++-- tests/CookiesTest.php | 18 +++++++++--------- 8 files changed, 29 insertions(+), 21 deletions(-) rename {library/Requests => src}/Cookie/Jar.php (94%) diff --git a/examples/cookie_jar.php b/examples/cookie_jar.php index 1a5537697..546d8f275 100644 --- a/examples/cookie_jar.php +++ b/examples/cookie_jar.php @@ -7,7 +7,7 @@ WpOrg\Requests\Autoload::register(); // Say you need to fake a login cookie -$c = new Requests_Cookie_Jar(['login_uid' => 'something']); +$c = new WpOrg\Requests\Cookie\Jar(['login_uid' => 'something']); // Now let's make a request! $request = WpOrg\Requests\Requests::get( diff --git a/library/Deprecated.php b/library/Deprecated.php index 7c5efa92a..be4decf8f 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -43,3 +43,4 @@ class Requests_Session extends WpOrg\Requests\Session {} class Requests_SSL extends WpOrg\Requests\Ssl {} class Requests_Auth_Basic extends WpOrg\Requests\Auth\Basic {} +class Requests_Cookie_Jar extends WpOrg\Requests\Cookie\Jar {} diff --git a/src/Autoload.php b/src/Autoload.php index 5f3db7be3..3266fa860 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -50,6 +50,7 @@ class Autoload { 'requests_session' => '\WpOrg\Requests\Session', 'requests_ssl' => '\WpOrg\Requests\Ssl', 'requests_auth_basic' => '\WpOrg\Requests\Auth\Basic', + 'requests_cookie_jar' => '\WpOrg\Requests\Cookie\Jar', ); /** diff --git a/library/Requests/Cookie/Jar.php b/src/Cookie/Jar.php similarity index 94% rename from library/Requests/Cookie/Jar.php rename to src/Cookie/Jar.php index 0f76ae7ab..b33bf5c08 100644 --- a/library/Requests/Cookie/Jar.php +++ b/src/Cookie/Jar.php @@ -6,6 +6,12 @@ * @subpackage Cookies */ +namespace WpOrg\Requests\Cookie; + +use ArrayAccess; +use ArrayIterator; +use IteratorAggregate; +use ReturnTypeWillChange; use WpOrg\Requests\Cookie; use WpOrg\Requests\Exception; use WpOrg\Requests\Hooker; @@ -18,7 +24,7 @@ * @package Requests * @subpackage Cookies */ -class Requests_Cookie_Jar implements ArrayAccess, IteratorAggregate { +class Jar implements ArrayAccess, IteratorAggregate { /** * Actual item data * @@ -105,7 +111,7 @@ public function offsetUnset($key) { /** * Get an iterator for the data * - * @return ArrayIterator + * @return \ArrayIterator */ #[ReturnTypeWillChange] public function getIterator() { diff --git a/src/Requests.php b/src/Requests.php index 67fdc5e48..0e1ecd94e 100644 --- a/src/Requests.php +++ b/src/Requests.php @@ -11,11 +11,11 @@ namespace WpOrg\Requests; -use Requests_Cookie_Jar; use Requests_Proxy_HTTP; use Requests_Transport_cURL; use Requests_Transport_fsockopen; use WpOrg\Requests\Auth\Basic; +use WpOrg\Requests\Cookie\Jar; use WpOrg\Requests\Exception; use WpOrg\Requests\Hooks; use WpOrg\Requests\IdnaEncoder; @@ -388,7 +388,7 @@ public static function request($url, $headers = array(), $data = array(), $type * parameter to {@see \WpOrg\Requests\Requests::request} * (string, default: `\WpOrg\Requests\Requests::GET`) * - `cookies`: Associative array of cookie name to value, or cookie jar. - * (array|Requests_Cookie_Jar) + * (array|\WpOrg\Requests\Cookie\Jar) * * If the `$options` parameter is specified, individual requests will * inherit options from it. This can be used to use a single hooking system, @@ -565,10 +565,10 @@ protected static function set_defaults(&$url, &$headers, &$data, &$type, &$optio } if (is_array($options['cookies'])) { - $options['cookies'] = new Requests_Cookie_Jar($options['cookies']); + $options['cookies'] = new Jar($options['cookies']); } elseif (empty($options['cookies'])) { - $options['cookies'] = new Requests_Cookie_Jar(); + $options['cookies'] = new Jar(); } if ($options['cookies'] !== false) { $options['cookies']->register($options['hooks']); diff --git a/src/Response.php b/src/Response.php index c8f80277e..4ec13e6ca 100644 --- a/src/Response.php +++ b/src/Response.php @@ -8,9 +8,9 @@ namespace WpOrg\Requests; -use Requests_Cookie_Jar; use Requests_Exception_HTTP; use Requests_Response_Headers; +use WpOrg\Requests\Cookie\Jar; use WpOrg\Requests\Exception; /** @@ -25,7 +25,7 @@ class Response { */ public function __construct() { $this->headers = new Requests_Response_Headers(); - $this->cookies = new Requests_Cookie_Jar(); + $this->cookies = new Jar(); } /** @@ -94,7 +94,7 @@ public function __construct() { /** * Cookies from the request * - * @var Requests_Cookie_Jar Array-like object representing a cookie jar + * @var \WpOrg\Requests\Cookie\Jar Array-like object representing a cookie jar */ public $cookies = array(); diff --git a/src/Session.php b/src/Session.php index c5c2942e7..1e5753199 100644 --- a/src/Session.php +++ b/src/Session.php @@ -8,7 +8,7 @@ namespace WpOrg\Requests; -use Requests_Cookie_Jar; +use WpOrg\Requests\Cookie\Jar; use WpOrg\Requests\Iri; use WpOrg\Requests\Requests; @@ -78,7 +78,7 @@ public function __construct($url = null, $headers = array(), $data = array(), $o $this->options = $options; if (empty($this->options['cookies'])) { - $this->options['cookies'] = new Requests_Cookie_Jar(); + $this->options['cookies'] = new Jar(); } } diff --git a/tests/CookiesTest.php b/tests/CookiesTest.php index 3d9f21969..a6c78b0d2 100644 --- a/tests/CookiesTest.php +++ b/tests/CookiesTest.php @@ -3,10 +3,10 @@ namespace Requests\Tests; use Requests\Tests\TestCase; -use Requests_Cookie_Jar; use Requests_Response_Headers; use Requests_Utility_CaseInsensitiveDictionary; use WpOrg\Requests\Cookie; +use WpOrg\Requests\Cookie\Jar; use WpOrg\Requests\Exception; use WpOrg\Requests\Iri; use WpOrg\Requests\Requests; @@ -46,10 +46,10 @@ public function testEmptyAttributes() { } public function testCookieJarSetter() { - $jar1 = new Requests_Cookie_Jar(); + $jar1 = new Jar(); $jar1['requests-testcookie'] = 'testvalue'; - $jar2 = new Requests_Cookie_Jar( + $jar2 = new Jar( array( 'requests-testcookie' => 'testvalue', ) @@ -58,7 +58,7 @@ public function testCookieJarSetter() { } public function testCookieJarUnsetter() { - $jar = new Requests_Cookie_Jar(); + $jar = new Jar(); $jar['requests-testcookie'] = 'testvalue'; $this->assertSame('testvalue', $jar['requests-testcookie']); @@ -71,7 +71,7 @@ public function testCookieJarUnsetter() { public function testCookieJarAsList() { $this->expectException(Exception::class); $this->expectExceptionMessage('Object is a dictionary, not a list'); - $cookies = new Requests_Cookie_Jar(); + $cookies = new Jar(); $cookies[] = 'requests-testcookie1=testvalue1'; } @@ -80,7 +80,7 @@ public function testCookieJarIterator() { 'requests-testcookie1' => 'testvalue1', 'requests-testcookie2' => 'testvalue2', ); - $jar = new Requests_Cookie_Jar($cookies); + $jar = new Jar($cookies); foreach ($jar as $key => $value) { $this->assertSame($cookies[$key], $value); @@ -154,7 +154,7 @@ public function testCookieExpiration() { } public function testSendingCookieWithJar() { - $cookies = new Requests_Cookie_Jar( + $cookies = new Jar( array( 'requests-testcookie1' => 'testvalue1', ) @@ -180,7 +180,7 @@ public function testSendingMultipleCookies() { } public function testSendingMultipleCookiesWithJar() { - $cookies = new Requests_Cookie_Jar( + $cookies = new Jar( array( 'requests-testcookie1' => 'testvalue1', 'requests-testcookie2' => 'testvalue2', @@ -196,7 +196,7 @@ public function testSendingMultipleCookiesWithJar() { } public function testSendingPrebakedCookie() { - $cookies = new Requests_Cookie_Jar( + $cookies = new Jar( array( new Cookie('requests-testcookie', 'testvalue'), ) From 6b2d102b36660458829faeb2312ebc883da4227b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 16 Aug 2021 03:32:56 +0200 Subject: [PATCH 25/45] Requests_Proxy_HTTP: change to namespaced class Includes: * Using CamelCaps for the class name, as well as the test class. * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. --- library/Deprecated.php | 1 + src/Autoload.php | 1 + library/Requests/Proxy/HTTP.php => src/Proxy/Http.php | 4 +++- src/Requests.php | 4 ++-- tests/Proxy/{HTTPTest.php => HttpTest.php} | 6 +++--- 5 files changed, 10 insertions(+), 6 deletions(-) rename library/Requests/Proxy/HTTP.php => src/Proxy/Http.php (98%) rename tests/Proxy/{HTTPTest.php => HttpTest.php} (96%) diff --git a/library/Deprecated.php b/library/Deprecated.php index be4decf8f..fcfc29b95 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -44,3 +44,4 @@ class Requests_SSL extends WpOrg\Requests\Ssl {} class Requests_Auth_Basic extends WpOrg\Requests\Auth\Basic {} class Requests_Cookie_Jar extends WpOrg\Requests\Cookie\Jar {} +class Requests_Proxy_HTTP extends WpOrg\Requests\Proxy\Http {} diff --git a/src/Autoload.php b/src/Autoload.php index 3266fa860..ad91c6a3d 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -51,6 +51,7 @@ class Autoload { 'requests_ssl' => '\WpOrg\Requests\Ssl', 'requests_auth_basic' => '\WpOrg\Requests\Auth\Basic', 'requests_cookie_jar' => '\WpOrg\Requests\Cookie\Jar', + 'requests_proxy_http' => '\WpOrg\Requests\Proxy\Http', ); /** diff --git a/library/Requests/Proxy/HTTP.php b/src/Proxy/Http.php similarity index 98% rename from library/Requests/Proxy/HTTP.php rename to src/Proxy/Http.php index 775dddc58..330342a6f 100644 --- a/library/Requests/Proxy/HTTP.php +++ b/src/Proxy/Http.php @@ -7,6 +7,8 @@ * @since 1.6 */ +namespace WpOrg\Requests\Proxy; + use WpOrg\Requests\Exception; use WpOrg\Requests\Hooks; use WpOrg\Requests\Proxy; @@ -20,7 +22,7 @@ * @subpackage Proxy * @since 1.6 */ -class Requests_Proxy_HTTP implements Proxy { +class Http implements Proxy { /** * Proxy host and port * diff --git a/src/Requests.php b/src/Requests.php index 0e1ecd94e..ab4bad9b6 100644 --- a/src/Requests.php +++ b/src/Requests.php @@ -11,7 +11,6 @@ namespace WpOrg\Requests; -use Requests_Proxy_HTTP; use Requests_Transport_cURL; use Requests_Transport_fsockopen; use WpOrg\Requests\Auth\Basic; @@ -20,6 +19,7 @@ use WpOrg\Requests\Hooks; use WpOrg\Requests\IdnaEncoder; use WpOrg\Requests\Iri; +use WpOrg\Requests\Proxy\Http; use WpOrg\Requests\Response; /** @@ -558,7 +558,7 @@ protected static function set_defaults(&$url, &$headers, &$data, &$type, &$optio } if (is_string($options['proxy']) || is_array($options['proxy'])) { - $options['proxy'] = new Requests_Proxy_HTTP($options['proxy']); + $options['proxy'] = new Http($options['proxy']); } if ($options['proxy'] !== false) { $options['proxy']->register($options['hooks']); diff --git a/tests/Proxy/HTTPTest.php b/tests/Proxy/HttpTest.php similarity index 96% rename from tests/Proxy/HTTPTest.php rename to tests/Proxy/HttpTest.php index e9583722b..455a37c46 100644 --- a/tests/Proxy/HTTPTest.php +++ b/tests/Proxy/HttpTest.php @@ -3,13 +3,13 @@ namespace Requests\Tests\Proxy; use Requests\Tests\TestCase; -use Requests_Proxy_HTTP; use Requests_Transport_cURL; use Requests_Transport_fsockopen; use WpOrg\Requests\Exception; +use WpOrg\Requests\Proxy\Http; use WpOrg\Requests\Requests; -class HTTPTest extends TestCase { +class HttpTest extends TestCase { protected function checkProxyAvailable($type = '') { switch ($type) { case 'auth': @@ -89,7 +89,7 @@ public function testConnectWithInstance($transport) { $this->checkProxyAvailable(); $options = array( - 'proxy' => new Requests_Proxy_HTTP(REQUESTS_HTTP_PROXY), + 'proxy' => new Http(REQUESTS_HTTP_PROXY), 'transport' => $transport, ); $response = Requests::get(httpbin('/get'), array(), $options); From e71c125591b23d5a859d1f2c68d054dfb7c9b3c1 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 16 Aug 2021 03:38:38 +0200 Subject: [PATCH 26/45] Requests_Response_Headers: change to namespaced class Includes: * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. --- library/Deprecated.php | 1 + src/Autoload.php | 33 ++++++++++--------- src/Cookie.php | 6 ++-- src/Response.php | 6 ++-- .../Requests => src}/Response/Headers.php | 8 +++-- tests/CookiesTest.php | 6 ++-- tests/RequestsTest.php | 4 +-- tests/Response/HeadersTest.php | 12 +++---- 8 files changed, 41 insertions(+), 35 deletions(-) rename {library/Requests => src}/Response/Headers.php (90%) diff --git a/library/Deprecated.php b/library/Deprecated.php index fcfc29b95..9a3b2c212 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -45,3 +45,4 @@ class Requests_SSL extends WpOrg\Requests\Ssl {} class Requests_Auth_Basic extends WpOrg\Requests\Auth\Basic {} class Requests_Cookie_Jar extends WpOrg\Requests\Cookie\Jar {} class Requests_Proxy_HTTP extends WpOrg\Requests\Proxy\Http {} +class Requests_Response_Headers extends WpOrg\Requests\Response\Headers {} diff --git a/src/Autoload.php b/src/Autoload.php index ad91c6a3d..72218030b 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -34,24 +34,25 @@ class Autoload { */ private static $deprecated_classes = array( // Interfaces. - 'requests_auth' => '\WpOrg\Requests\Auth', - 'requests_hooker' => '\WpOrg\Requests\Hooker', - 'requests_proxy' => '\WpOrg\Requests\Proxy', - 'requests_transport' => '\WpOrg\Requests\Transport', + 'requests_auth' => '\WpOrg\Requests\Auth', + 'requests_hooker' => '\WpOrg\Requests\Hooker', + 'requests_proxy' => '\WpOrg\Requests\Proxy', + 'requests_transport' => '\WpOrg\Requests\Transport', // Classes. - 'requests_cookie' => '\WpOrg\Requests\Cookie', - 'requests_exception' => '\WpOrg\Requests\Exception', - 'requests_hooks' => '\WpOrg\Requests\Hooks', - 'requests_idnaencoder' => '\WpOrg\Requests\IdnaEncoder', - 'requests_ipv6' => '\WpOrg\Requests\Ipv6', - 'requests_iri' => '\WpOrg\Requests\Iri', - 'requests_response' => '\WpOrg\Requests\Response', - 'requests_session' => '\WpOrg\Requests\Session', - 'requests_ssl' => '\WpOrg\Requests\Ssl', - 'requests_auth_basic' => '\WpOrg\Requests\Auth\Basic', - 'requests_cookie_jar' => '\WpOrg\Requests\Cookie\Jar', - 'requests_proxy_http' => '\WpOrg\Requests\Proxy\Http', + 'requests_cookie' => '\WpOrg\Requests\Cookie', + 'requests_exception' => '\WpOrg\Requests\Exception', + 'requests_hooks' => '\WpOrg\Requests\Hooks', + 'requests_idnaencoder' => '\WpOrg\Requests\IdnaEncoder', + 'requests_ipv6' => '\WpOrg\Requests\Ipv6', + 'requests_iri' => '\WpOrg\Requests\Iri', + 'requests_response' => '\WpOrg\Requests\Response', + 'requests_session' => '\WpOrg\Requests\Session', + 'requests_ssl' => '\WpOrg\Requests\Ssl', + 'requests_auth_basic' => '\WpOrg\Requests\Auth\Basic', + 'requests_cookie_jar' => '\WpOrg\Requests\Cookie\Jar', + 'requests_proxy_http' => '\WpOrg\Requests\Proxy\Http', + 'requests_response_headers' => '\WpOrg\Requests\Response\Headers', ); /** diff --git a/src/Cookie.php b/src/Cookie.php index 82ce9809d..3e19ec4c7 100644 --- a/src/Cookie.php +++ b/src/Cookie.php @@ -8,9 +8,9 @@ namespace WpOrg\Requests; -use Requests_Response_Headers; use Requests_Utility_CaseInsensitiveDictionary; use WpOrg\Requests\Iri; +use WpOrg\Requests\Response\Headers; /** * Cookie storage object @@ -415,12 +415,12 @@ public static function parse($string, $name = '', $reference_time = null) { /** * Parse all Set-Cookie headers from request headers * - * @param Requests_Response_Headers $headers Headers to parse from + * @param \WpOrg\Requests\Response\Headers $headers Headers to parse from * @param \WpOrg\Requests\Iri|null $origin URI for comparing cookie origins * @param int|null $time Reference time for expiration calculation * @return array */ - public static function parse_from_headers(Requests_Response_Headers $headers, Iri $origin = null, $time = null) { + public static function parse_from_headers(Headers $headers, Iri $origin = null, $time = null) { $cookie_headers = $headers->getValues('Set-Cookie'); if (empty($cookie_headers)) { return array(); diff --git a/src/Response.php b/src/Response.php index 4ec13e6ca..fad46ceed 100644 --- a/src/Response.php +++ b/src/Response.php @@ -9,9 +9,9 @@ namespace WpOrg\Requests; use Requests_Exception_HTTP; -use Requests_Response_Headers; use WpOrg\Requests\Cookie\Jar; use WpOrg\Requests\Exception; +use WpOrg\Requests\Response\Headers; /** * HTTP response class @@ -24,7 +24,7 @@ class Response { * Constructor */ public function __construct() { - $this->headers = new Requests_Response_Headers(); + $this->headers = new Headers(); $this->cookies = new Jar(); } @@ -45,7 +45,7 @@ public function __construct() { /** * Headers, as an associative array * - * @var Requests_Response_Headers Array-like object representing headers + * @var \WpOrg\Requests\Response\Headers Array-like object representing headers */ public $headers = array(); diff --git a/library/Requests/Response/Headers.php b/src/Response/Headers.php similarity index 90% rename from library/Requests/Response/Headers.php rename to src/Response/Headers.php index 5a4918274..d35736add 100644 --- a/library/Requests/Response/Headers.php +++ b/src/Response/Headers.php @@ -5,6 +5,10 @@ * @package Requests */ +namespace WpOrg\Requests\Response; + +use Requests_Utility_CaseInsensitiveDictionary; +use Requests_Utility_FilteredIterator; use WpOrg\Requests\Exception; /** @@ -12,7 +16,7 @@ * * @package Requests */ -class Requests_Response_Headers extends Requests_Utility_CaseInsensitiveDictionary { +class Headers extends Requests_Utility_CaseInsensitiveDictionary { /** * Get the given header * @@ -92,7 +96,7 @@ public function flatten($value) { * Get an iterator for the data * * Converts the internal - * @return ArrayIterator + * @return \ArrayIterator */ public function getIterator() { return new Requests_Utility_FilteredIterator($this->data, array($this, 'flatten')); diff --git a/tests/CookiesTest.php b/tests/CookiesTest.php index a6c78b0d2..2fbd27570 100644 --- a/tests/CookiesTest.php +++ b/tests/CookiesTest.php @@ -3,13 +3,13 @@ namespace Requests\Tests; use Requests\Tests\TestCase; -use Requests_Response_Headers; use Requests_Utility_CaseInsensitiveDictionary; use WpOrg\Requests\Cookie; use WpOrg\Requests\Cookie\Jar; use WpOrg\Requests\Exception; use WpOrg\Requests\Iri; use WpOrg\Requests\Requests; +use WpOrg\Requests\Response\Headers; class CookiesTest extends TestCase { public function testBasicCookie() { @@ -520,7 +520,7 @@ public function testParsingHeaderDouble($header, $expected, $expected_attributes * @dataProvider parseResultProvider */ public function testParsingHeaderObject($header, $expected, $expected_attributes = array(), $expected_flags = array()) { - $headers = new Requests_Response_Headers(); + $headers = new Headers(); $headers['Set-Cookie'] = $header; // Set the reference time to 2014-01-01 00:00:00 @@ -648,7 +648,7 @@ public function parseFromHeadersProvider() { */ public function testParsingHeaderWithOrigin($header, $origin, $expected, $expected_attributes = array(), $expected_flags = array()) { $origin = new Iri($origin); - $headers = new Requests_Response_Headers(); + $headers = new Headers(); $headers['Set-Cookie'] = $header; // Set the reference time to 2014-01-01 00:00:00 diff --git a/tests/RequestsTest.php b/tests/RequestsTest.php index 815eb6e6b..82709a765 100644 --- a/tests/RequestsTest.php +++ b/tests/RequestsTest.php @@ -5,9 +5,9 @@ use Requests\Tests\Mock\RawTransportMock; use Requests\Tests\Mock\TransportMock; use Requests\Tests\TestCase; -use Requests_Response_Headers; use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; +use WpOrg\Requests\Response\Headers; class RequestsTest extends TestCase { @@ -44,7 +44,7 @@ public function testHeaderParsing() { 'transport' => $transport, ); $response = Requests::get('http://example.com/', array(), $options); - $expected = new Requests_Response_Headers(); + $expected = new Headers(); $expected['host'] = 'localhost,ambiguous'; $expected['nospace'] = 'here'; $expected['muchspace'] = 'there'; diff --git a/tests/Response/HeadersTest.php b/tests/Response/HeadersTest.php index 81651e939..29bc18c4e 100644 --- a/tests/Response/HeadersTest.php +++ b/tests/Response/HeadersTest.php @@ -3,18 +3,18 @@ namespace Requests\Tests\Response; use Requests\Tests\TestCase; -use Requests_Response_Headers; use WpOrg\Requests\Exception; +use WpOrg\Requests\Response\Headers; class HeadersTest extends TestCase { public function testArrayAccess() { - $headers = new Requests_Response_Headers(); + $headers = new Headers(); $headers['Content-Type'] = 'text/plain'; $this->assertSame('text/plain', $headers['Content-Type']); } public function testCaseInsensitiveArrayAccess() { - $headers = new Requests_Response_Headers(); + $headers = new Headers(); $headers['Content-Type'] = 'text/plain'; $this->assertSame('text/plain', $headers['CONTENT-TYPE']); @@ -25,7 +25,7 @@ public function testCaseInsensitiveArrayAccess() { * @depends testArrayAccess */ public function testIteration() { - $headers = new Requests_Response_Headers(); + $headers = new Headers(); $headers['Content-Type'] = 'text/plain'; $headers['Content-Length'] = 10; @@ -46,12 +46,12 @@ public function testIteration() { public function testInvalidKey() { $this->expectException(Exception::class); $this->expectExceptionMessage('Object is a dictionary, not a list'); - $headers = new Requests_Response_Headers(); + $headers = new Headers(); $headers[] = 'text/plain'; } public function testMultipleHeaders() { - $headers = new Requests_Response_Headers(); + $headers = new Headers(); $headers['Accept'] = 'text/html;q=1.0'; $headers['Accept'] = '*/*;q=0.1'; From 09becbc71dca4df32563a123f74d1b70317b5d74 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 16 Aug 2021 03:57:12 +0200 Subject: [PATCH 27/45] Requests_Transport_cURL: change to namespaced class Includes: * Using CamelCaps for the class name, as well as the test class. * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. * Minor PHPCS tweak. --- .phpcs.xml.dist | 1 + library/Deprecated.php | 1 + src/Autoload.php | 1 + src/Requests.php | 6 +++--- .../Transport/cURL.php => src/Transport/Curl.php | 10 ++++++++-- tests/Auth/BasicTest.php | 4 ++-- tests/Proxy/HttpTest.php | 4 ++-- tests/Transport/{CURLTest.php => CurlTest.php} | 4 ++-- 8 files changed, 20 insertions(+), 11 deletions(-) rename library/Requests/Transport/cURL.php => src/Transport/Curl.php (98%) rename tests/Transport/{CURLTest.php => CurlTest.php} (97%) diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index f3db29a7b..beda29837 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -220,6 +220,7 @@ + */library/Deprecated\.php$ */Transport/cURL\.php$ */Transport/fsockopen\.php$ diff --git a/library/Deprecated.php b/library/Deprecated.php index 9a3b2c212..64f43e032 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -46,3 +46,4 @@ class Requests_Auth_Basic extends WpOrg\Requests\Auth\Basic {} class Requests_Cookie_Jar extends WpOrg\Requests\Cookie\Jar {} class Requests_Proxy_HTTP extends WpOrg\Requests\Proxy\Http {} class Requests_Response_Headers extends WpOrg\Requests\Response\Headers {} +class Requests_Transport_cURL extends WpOrg\Requests\Transport\Curl {} diff --git a/src/Autoload.php b/src/Autoload.php index 72218030b..5858f59a6 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -53,6 +53,7 @@ class Autoload { 'requests_cookie_jar' => '\WpOrg\Requests\Cookie\Jar', 'requests_proxy_http' => '\WpOrg\Requests\Proxy\Http', 'requests_response_headers' => '\WpOrg\Requests\Response\Headers', + 'requests_transport_curl' => '\WpOrg\Requests\Transport\Curl', ); /** diff --git a/src/Requests.php b/src/Requests.php index ab4bad9b6..65a38b7f4 100644 --- a/src/Requests.php +++ b/src/Requests.php @@ -11,7 +11,6 @@ namespace WpOrg\Requests; -use Requests_Transport_cURL; use Requests_Transport_fsockopen; use WpOrg\Requests\Auth\Basic; use WpOrg\Requests\Cookie\Jar; @@ -21,6 +20,7 @@ use WpOrg\Requests\Iri; use WpOrg\Requests\Proxy\Http; use WpOrg\Requests\Response; +use WpOrg\Requests\Transport\Curl; /** * Requests for PHP @@ -144,7 +144,7 @@ private function __construct() {} public static function add_transport($transport) { if (empty(self::$transports)) { self::$transports = array( - Requests_Transport_cURL::class, + Curl::class, Requests_Transport_fsockopen::class, ); } @@ -174,7 +174,7 @@ protected static function get_transport($capabilities = array()) { if (empty(self::$transports)) { self::$transports = array( - Requests_Transport_cURL::class, + Curl::class, Requests_Transport_fsockopen::class, ); } diff --git a/library/Requests/Transport/cURL.php b/src/Transport/Curl.php similarity index 98% rename from library/Requests/Transport/cURL.php rename to src/Transport/Curl.php index 5f3d58506..0bebd444b 100644 --- a/library/Requests/Transport/cURL.php +++ b/src/Transport/Curl.php @@ -6,6 +6,12 @@ * @subpackage Transport */ +namespace WpOrg\Requests\Transport; + +use RecursiveArrayIterator; +use RecursiveIteratorIterator; +use Requests_Exception_InvalidArgument; +use Requests_Exception_Transport_cURL; use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; use WpOrg\Requests\Transport; @@ -16,7 +22,7 @@ * @package Requests * @subpackage Transport */ -class Requests_Transport_cURL implements Transport { +class Curl implements Transport { const CURL_7_10_5 = 0x070A05; const CURL_7_16_2 = 0x071002; @@ -207,7 +213,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar $this->process_response($response, $options); // Need to remove the $this reference from the curl handle. - // Otherwise Requests_Transport_cURL wont be garbage collected and the curl_close() will never be called. + // Otherwise \WpOrg\Requests\Transport\Curl wont be garbage collected and the curl_close() will never be called. curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, null); curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, null); diff --git a/tests/Auth/BasicTest.php b/tests/Auth/BasicTest.php index 941d89400..cb958e4f5 100644 --- a/tests/Auth/BasicTest.php +++ b/tests/Auth/BasicTest.php @@ -3,17 +3,17 @@ namespace Requests\Tests\Auth; use Requests\Tests\TestCase; -use Requests_Transport_cURL; use Requests_Transport_fsockopen; use WpOrg\Requests\Auth\Basic; use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; +use WpOrg\Requests\Transport\Curl; class BasicTest extends TestCase { public static function transportProvider() { return array( array(Requests_Transport_fsockopen::class), - array(Requests_Transport_cURL::class), + array(Curl::class), ); } diff --git a/tests/Proxy/HttpTest.php b/tests/Proxy/HttpTest.php index 455a37c46..35270b411 100644 --- a/tests/Proxy/HttpTest.php +++ b/tests/Proxy/HttpTest.php @@ -3,11 +3,11 @@ namespace Requests\Tests\Proxy; use Requests\Tests\TestCase; -use Requests_Transport_cURL; use Requests_Transport_fsockopen; use WpOrg\Requests\Exception; use WpOrg\Requests\Proxy\Http; use WpOrg\Requests\Requests; +use WpOrg\Requests\Transport\Curl; class HttpTest extends TestCase { protected function checkProxyAvailable($type = '') { @@ -28,7 +28,7 @@ protected function checkProxyAvailable($type = '') { public function transportProvider() { return array( - array(Requests_Transport_cURL::class), + array(Curl::class), array(Requests_Transport_fsockopen::class), ); } diff --git a/tests/Transport/CURLTest.php b/tests/Transport/CurlTest.php similarity index 97% rename from tests/Transport/CURLTest.php rename to tests/Transport/CurlTest.php index d84a18e45..d8923d5b0 100644 --- a/tests/Transport/CURLTest.php +++ b/tests/Transport/CurlTest.php @@ -3,12 +3,12 @@ namespace Requests\Tests\Transport; use Requests\Tests\Transport\BaseTestCase; -use Requests_Transport_cURL; use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; +use WpOrg\Requests\Transport\Curl; class CurlTest extends BaseTestCase { - protected $transport = Requests_Transport_cURL::class; + protected $transport = Curl::class; public function testBadIP() { $this->expectException(Exception::class); From 03ee83df66ca704cbb8b311bc89dfd8e9cf6fe1c Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 16 Aug 2021 04:15:23 +0200 Subject: [PATCH 28/45] Requests_Transport_fsockopen: change to namespaced class Includes: * Using CamelCaps for the class name. * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. * Minor PHPCS config tweak. --- .phpcs.xml.dist | 1 - library/Deprecated.php | 1 + src/Autoload.php | 37 ++++++++++--------- src/Requests.php | 6 +-- .../Transport/Fsockopen.php | 6 ++- tests/Auth/BasicTest.php | 4 +- tests/Proxy/HttpTest.php | 6 +-- tests/Transport/FsockopenTest.php | 4 +- 8 files changed, 35 insertions(+), 30 deletions(-) rename library/Requests/Transport/fsockopen.php => src/Transport/Fsockopen.php (98%) diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index beda29837..ab1d811a9 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -222,7 +222,6 @@ */library/Deprecated\.php$ */Transport/cURL\.php$ - */Transport/fsockopen\.php$ diff --git a/library/Deprecated.php b/library/Deprecated.php index 64f43e032..f02c08559 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -47,3 +47,4 @@ class Requests_Cookie_Jar extends WpOrg\Requests\Cookie\Jar {} class Requests_Proxy_HTTP extends WpOrg\Requests\Proxy\Http {} class Requests_Response_Headers extends WpOrg\Requests\Response\Headers {} class Requests_Transport_cURL extends WpOrg\Requests\Transport\Curl {} +class Requests_Transport_fsockopen extends WpOrg\Requests\Transport\Fsockopen {} diff --git a/src/Autoload.php b/src/Autoload.php index 5858f59a6..831d41476 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -34,26 +34,27 @@ class Autoload { */ private static $deprecated_classes = array( // Interfaces. - 'requests_auth' => '\WpOrg\Requests\Auth', - 'requests_hooker' => '\WpOrg\Requests\Hooker', - 'requests_proxy' => '\WpOrg\Requests\Proxy', - 'requests_transport' => '\WpOrg\Requests\Transport', + 'requests_auth' => '\WpOrg\Requests\Auth', + 'requests_hooker' => '\WpOrg\Requests\Hooker', + 'requests_proxy' => '\WpOrg\Requests\Proxy', + 'requests_transport' => '\WpOrg\Requests\Transport', // Classes. - 'requests_cookie' => '\WpOrg\Requests\Cookie', - 'requests_exception' => '\WpOrg\Requests\Exception', - 'requests_hooks' => '\WpOrg\Requests\Hooks', - 'requests_idnaencoder' => '\WpOrg\Requests\IdnaEncoder', - 'requests_ipv6' => '\WpOrg\Requests\Ipv6', - 'requests_iri' => '\WpOrg\Requests\Iri', - 'requests_response' => '\WpOrg\Requests\Response', - 'requests_session' => '\WpOrg\Requests\Session', - 'requests_ssl' => '\WpOrg\Requests\Ssl', - 'requests_auth_basic' => '\WpOrg\Requests\Auth\Basic', - 'requests_cookie_jar' => '\WpOrg\Requests\Cookie\Jar', - 'requests_proxy_http' => '\WpOrg\Requests\Proxy\Http', - 'requests_response_headers' => '\WpOrg\Requests\Response\Headers', - 'requests_transport_curl' => '\WpOrg\Requests\Transport\Curl', + 'requests_cookie' => '\WpOrg\Requests\Cookie', + 'requests_exception' => '\WpOrg\Requests\Exception', + 'requests_hooks' => '\WpOrg\Requests\Hooks', + 'requests_idnaencoder' => '\WpOrg\Requests\IdnaEncoder', + 'requests_ipv6' => '\WpOrg\Requests\Ipv6', + 'requests_iri' => '\WpOrg\Requests\Iri', + 'requests_response' => '\WpOrg\Requests\Response', + 'requests_session' => '\WpOrg\Requests\Session', + 'requests_ssl' => '\WpOrg\Requests\Ssl', + 'requests_auth_basic' => '\WpOrg\Requests\Auth\Basic', + 'requests_cookie_jar' => '\WpOrg\Requests\Cookie\Jar', + 'requests_proxy_http' => '\WpOrg\Requests\Proxy\Http', + 'requests_response_headers' => '\WpOrg\Requests\Response\Headers', + 'requests_transport_curl' => '\WpOrg\Requests\Transport\Curl', + 'requests_transport_fsockopen' => '\WpOrg\Requests\Transport\Fsockopen', ); /** diff --git a/src/Requests.php b/src/Requests.php index 65a38b7f4..19d52689d 100644 --- a/src/Requests.php +++ b/src/Requests.php @@ -11,7 +11,6 @@ namespace WpOrg\Requests; -use Requests_Transport_fsockopen; use WpOrg\Requests\Auth\Basic; use WpOrg\Requests\Cookie\Jar; use WpOrg\Requests\Exception; @@ -21,6 +20,7 @@ use WpOrg\Requests\Proxy\Http; use WpOrg\Requests\Response; use WpOrg\Requests\Transport\Curl; +use WpOrg\Requests\Transport\Fsockopen; /** * Requests for PHP @@ -145,7 +145,7 @@ public static function add_transport($transport) { if (empty(self::$transports)) { self::$transports = array( Curl::class, - Requests_Transport_fsockopen::class, + Fsockopen::class, ); } @@ -175,7 +175,7 @@ protected static function get_transport($capabilities = array()) { if (empty(self::$transports)) { self::$transports = array( Curl::class, - Requests_Transport_fsockopen::class, + Fsockopen::class, ); } diff --git a/library/Requests/Transport/fsockopen.php b/src/Transport/Fsockopen.php similarity index 98% rename from library/Requests/Transport/fsockopen.php rename to src/Transport/Fsockopen.php index c9aeb1c68..eea1c471c 100644 --- a/library/Requests/Transport/fsockopen.php +++ b/src/Transport/Fsockopen.php @@ -6,6 +6,10 @@ * @subpackage Transport */ +namespace WpOrg\Requests\Transport; + +use Requests_Exception_InvalidArgument; +use Requests_Utility_CaseInsensitiveDictionary; use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; use WpOrg\Requests\Ssl; @@ -17,7 +21,7 @@ * @package Requests * @subpackage Transport */ -class Requests_Transport_fsockopen implements Transport { +class Fsockopen implements Transport { /** * Second to microsecond conversion * diff --git a/tests/Auth/BasicTest.php b/tests/Auth/BasicTest.php index cb958e4f5..e91fa9552 100644 --- a/tests/Auth/BasicTest.php +++ b/tests/Auth/BasicTest.php @@ -3,16 +3,16 @@ namespace Requests\Tests\Auth; use Requests\Tests\TestCase; -use Requests_Transport_fsockopen; use WpOrg\Requests\Auth\Basic; use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; use WpOrg\Requests\Transport\Curl; +use WpOrg\Requests\Transport\Fsockopen; class BasicTest extends TestCase { public static function transportProvider() { return array( - array(Requests_Transport_fsockopen::class), + array(Fsockopen::class), array(Curl::class), ); } diff --git a/tests/Proxy/HttpTest.php b/tests/Proxy/HttpTest.php index 35270b411..6d447cbb1 100644 --- a/tests/Proxy/HttpTest.php +++ b/tests/Proxy/HttpTest.php @@ -3,11 +3,11 @@ namespace Requests\Tests\Proxy; use Requests\Tests\TestCase; -use Requests_Transport_fsockopen; use WpOrg\Requests\Exception; use WpOrg\Requests\Proxy\Http; use WpOrg\Requests\Requests; use WpOrg\Requests\Transport\Curl; +use WpOrg\Requests\Transport\Fsockopen; class HttpTest extends TestCase { protected function checkProxyAvailable($type = '') { @@ -29,7 +29,7 @@ protected function checkProxyAvailable($type = '') { public function transportProvider() { return array( array(Curl::class), - array(Requests_Transport_fsockopen::class), + array(Fsockopen::class), ); } @@ -137,7 +137,7 @@ public function testConnectWithInvalidAuth($transport) { ); if (version_compare(phpversion(), '5.5.0', '>=') === true - && $transport === Requests_Transport_fsockopen::class + && $transport === Fsockopen::class ) { // @TODO fsockopen connection times out on invalid auth instead of returning 407. $this->expectException(Exception::class); diff --git a/tests/Transport/FsockopenTest.php b/tests/Transport/FsockopenTest.php index 38cf53cd2..fcc19bc73 100644 --- a/tests/Transport/FsockopenTest.php +++ b/tests/Transport/FsockopenTest.php @@ -3,13 +3,13 @@ namespace Requests\Tests\Transport; use Requests\Tests\Transport\BaseTestCase; -use Requests_Transport_fsockopen; use WpOrg\Requests\Exception; use WpOrg\Requests\Hooks; use WpOrg\Requests\Requests; +use WpOrg\Requests\Transport\Fsockopen; class FsockopenTest extends BaseTestCase { - protected $transport = Requests_Transport_fsockopen::class; + protected $transport = Fsockopen::class; public function testBadIP() { $this->expectException(Exception::class); From 58493113167c88dd65954cd4486207aa0b26e404 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 16 Aug 2021 04:21:55 +0200 Subject: [PATCH 29/45] Requests_Utility_CaseInsensitiveDictionary: change to namespaced class Includes: * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. --- library/Deprecated.php | 1 + src/Autoload.php | 39 ++++++++++--------- src/Cookie.php | 8 ++-- src/Response/Headers.php | 4 +- src/Transport/Fsockopen.php | 4 +- .../Utility/CaseInsensitiveDictionary.php | 10 ++++- tests/CookiesTest.php | 14 +++---- 7 files changed, 44 insertions(+), 36 deletions(-) rename {library/Requests => src}/Utility/CaseInsensitiveDictionary.php (90%) diff --git a/library/Deprecated.php b/library/Deprecated.php index f02c08559..9abfe3b78 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -48,3 +48,4 @@ class Requests_Proxy_HTTP extends WpOrg\Requests\Proxy\Http {} class Requests_Response_Headers extends WpOrg\Requests\Response\Headers {} class Requests_Transport_cURL extends WpOrg\Requests\Transport\Curl {} class Requests_Transport_fsockopen extends WpOrg\Requests\Transport\Fsockopen {} +class Requests_Utility_CaseInsensitiveDictionary extends WpOrg\Requests\Utility\CaseInsensitiveDictionary {} diff --git a/src/Autoload.php b/src/Autoload.php index 831d41476..451b24e43 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -34,27 +34,28 @@ class Autoload { */ private static $deprecated_classes = array( // Interfaces. - 'requests_auth' => '\WpOrg\Requests\Auth', - 'requests_hooker' => '\WpOrg\Requests\Hooker', - 'requests_proxy' => '\WpOrg\Requests\Proxy', - 'requests_transport' => '\WpOrg\Requests\Transport', + 'requests_auth' => '\WpOrg\Requests\Auth', + 'requests_hooker' => '\WpOrg\Requests\Hooker', + 'requests_proxy' => '\WpOrg\Requests\Proxy', + 'requests_transport' => '\WpOrg\Requests\Transport', // Classes. - 'requests_cookie' => '\WpOrg\Requests\Cookie', - 'requests_exception' => '\WpOrg\Requests\Exception', - 'requests_hooks' => '\WpOrg\Requests\Hooks', - 'requests_idnaencoder' => '\WpOrg\Requests\IdnaEncoder', - 'requests_ipv6' => '\WpOrg\Requests\Ipv6', - 'requests_iri' => '\WpOrg\Requests\Iri', - 'requests_response' => '\WpOrg\Requests\Response', - 'requests_session' => '\WpOrg\Requests\Session', - 'requests_ssl' => '\WpOrg\Requests\Ssl', - 'requests_auth_basic' => '\WpOrg\Requests\Auth\Basic', - 'requests_cookie_jar' => '\WpOrg\Requests\Cookie\Jar', - 'requests_proxy_http' => '\WpOrg\Requests\Proxy\Http', - 'requests_response_headers' => '\WpOrg\Requests\Response\Headers', - 'requests_transport_curl' => '\WpOrg\Requests\Transport\Curl', - 'requests_transport_fsockopen' => '\WpOrg\Requests\Transport\Fsockopen', + 'requests_cookie' => '\WpOrg\Requests\Cookie', + 'requests_exception' => '\WpOrg\Requests\Exception', + 'requests_hooks' => '\WpOrg\Requests\Hooks', + 'requests_idnaencoder' => '\WpOrg\Requests\IdnaEncoder', + 'requests_ipv6' => '\WpOrg\Requests\Ipv6', + 'requests_iri' => '\WpOrg\Requests\Iri', + 'requests_response' => '\WpOrg\Requests\Response', + 'requests_session' => '\WpOrg\Requests\Session', + 'requests_ssl' => '\WpOrg\Requests\Ssl', + 'requests_auth_basic' => '\WpOrg\Requests\Auth\Basic', + 'requests_cookie_jar' => '\WpOrg\Requests\Cookie\Jar', + 'requests_proxy_http' => '\WpOrg\Requests\Proxy\Http', + 'requests_response_headers' => '\WpOrg\Requests\Response\Headers', + 'requests_transport_curl' => '\WpOrg\Requests\Transport\Curl', + 'requests_transport_fsockopen' => '\WpOrg\Requests\Transport\Fsockopen', + 'requests_utility_caseinsensitivedictionary' => '\WpOrg\Requests\Utility\CaseInsensitiveDictionary', ); /** diff --git a/src/Cookie.php b/src/Cookie.php index 3e19ec4c7..2e302ccef 100644 --- a/src/Cookie.php +++ b/src/Cookie.php @@ -8,9 +8,9 @@ namespace WpOrg\Requests; -use Requests_Utility_CaseInsensitiveDictionary; use WpOrg\Requests\Iri; use WpOrg\Requests\Response\Headers; +use WpOrg\Requests\Utility\CaseInsensitiveDictionary; /** * Cookie storage object @@ -39,7 +39,7 @@ class Cookie { * Valid keys are (currently) path, domain, expires, max-age, secure and * httponly. * - * @var Requests_Utility_CaseInsensitiveDictionary|array Array-like object + * @var \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array Array-like object */ public $attributes = array(); @@ -68,7 +68,7 @@ class Cookie { * * @param string $name * @param string $value - * @param array|Requests_Utility_CaseInsensitiveDictionary $attributes Associative array of attribute data + * @param array|\WpOrg\Requests\Utility\CaseInsensitiveDictionary $attributes Associative array of attribute data */ public function __construct($name, $value, $attributes = array(), $flags = array(), $reference_time = null) { $this->name = $name; @@ -391,7 +391,7 @@ public static function parse($string, $name = '', $reference_time = null) { $value = trim($value); // Attribute key are handled case-insensitively - $attributes = new Requests_Utility_CaseInsensitiveDictionary(); + $attributes = new CaseInsensitiveDictionary(); if (!empty($parts)) { foreach ($parts as $part) { diff --git a/src/Response/Headers.php b/src/Response/Headers.php index d35736add..3d90f11f6 100644 --- a/src/Response/Headers.php +++ b/src/Response/Headers.php @@ -7,16 +7,16 @@ namespace WpOrg\Requests\Response; -use Requests_Utility_CaseInsensitiveDictionary; use Requests_Utility_FilteredIterator; use WpOrg\Requests\Exception; +use WpOrg\Requests\Utility\CaseInsensitiveDictionary; /** * Case-insensitive dictionary, suitable for HTTP headers * * @package Requests */ -class Headers extends Requests_Utility_CaseInsensitiveDictionary { +class Headers extends CaseInsensitiveDictionary { /** * Get the given header * diff --git a/src/Transport/Fsockopen.php b/src/Transport/Fsockopen.php index eea1c471c..eb5df9dd6 100644 --- a/src/Transport/Fsockopen.php +++ b/src/Transport/Fsockopen.php @@ -9,11 +9,11 @@ namespace WpOrg\Requests\Transport; use Requests_Exception_InvalidArgument; -use Requests_Utility_CaseInsensitiveDictionary; use WpOrg\Requests\Exception; use WpOrg\Requests\Requests; use WpOrg\Requests\Ssl; use WpOrg\Requests\Transport; +use WpOrg\Requests\Utility\CaseInsensitiveDictionary; /** * fsockopen HTTP transport @@ -93,7 +93,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar $host = $url_parts['host']; $context = stream_context_create(); $verifyname = false; - $case_insensitive_headers = new Requests_Utility_CaseInsensitiveDictionary($headers); + $case_insensitive_headers = new CaseInsensitiveDictionary($headers); // HTTPS support if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) === 'https') { diff --git a/library/Requests/Utility/CaseInsensitiveDictionary.php b/src/Utility/CaseInsensitiveDictionary.php similarity index 90% rename from library/Requests/Utility/CaseInsensitiveDictionary.php rename to src/Utility/CaseInsensitiveDictionary.php index 8e69bee2c..738944d8c 100644 --- a/library/Requests/Utility/CaseInsensitiveDictionary.php +++ b/src/Utility/CaseInsensitiveDictionary.php @@ -6,6 +6,12 @@ * @subpackage Utilities */ +namespace WpOrg\Requests\Utility; + +use ArrayAccess; +use ArrayIterator; +use IteratorAggregate; +use ReturnTypeWillChange; use WpOrg\Requests\Exception; /** @@ -14,7 +20,7 @@ * @package Requests * @subpackage Utilities */ -class Requests_Utility_CaseInsensitiveDictionary implements ArrayAccess, IteratorAggregate { +class CaseInsensitiveDictionary implements ArrayAccess, IteratorAggregate { /** * Actual item data * @@ -92,7 +98,7 @@ public function offsetUnset($key) { /** * Get an iterator for the data * - * @return ArrayIterator + * @return \ArrayIterator */ #[ReturnTypeWillChange] public function getIterator() { diff --git a/tests/CookiesTest.php b/tests/CookiesTest.php index 2fbd27570..8e89ed8dc 100644 --- a/tests/CookiesTest.php +++ b/tests/CookiesTest.php @@ -3,13 +3,13 @@ namespace Requests\Tests; use Requests\Tests\TestCase; -use Requests_Utility_CaseInsensitiveDictionary; use WpOrg\Requests\Cookie; use WpOrg\Requests\Cookie\Jar; use WpOrg\Requests\Exception; use WpOrg\Requests\Iri; use WpOrg\Requests\Requests; use WpOrg\Requests\Response\Headers; +use WpOrg\Requests\Utility\CaseInsensitiveDictionary; class CookiesTest extends TestCase { public function testBasicCookie() { @@ -236,7 +236,7 @@ public function domainMatchProvider() { * @dataProvider domainMatchProvider */ public function testDomainExactMatch($original, $check, $matches, $domain_matches) { - $attributes = new Requests_Utility_CaseInsensitiveDictionary(); + $attributes = new CaseInsensitiveDictionary(); $attributes['domain'] = $original; $cookie = new Cookie('requests-testcookie', 'testvalue', $attributes); $this->assertSame($matches, $cookie->domain_matches($check)); @@ -246,7 +246,7 @@ public function testDomainExactMatch($original, $check, $matches, $domain_matche * @dataProvider domainMatchProvider */ public function testDomainMatch($original, $check, $matches, $domain_matches) { - $attributes = new Requests_Utility_CaseInsensitiveDictionary(); + $attributes = new CaseInsensitiveDictionary(); $attributes['domain'] = $original; $flags = array( 'host-only' => false, @@ -279,7 +279,7 @@ public function pathMatchProvider() { * @dataProvider pathMatchProvider */ public function testPathMatch($original, $check, $matches) { - $attributes = new Requests_Utility_CaseInsensitiveDictionary(); + $attributes = new CaseInsensitiveDictionary(); $attributes['path'] = $original; $cookie = new Cookie('requests-testcookie', 'testvalue', $attributes); $this->assertSame($matches, $cookie->path_matches($check)); @@ -318,7 +318,7 @@ public function urlMatchProvider() { * @dataProvider urlMatchProvider */ public function testUrlExactMatch($domain, $path, $check, $matches, $domain_matches) { - $attributes = new Requests_Utility_CaseInsensitiveDictionary(); + $attributes = new CaseInsensitiveDictionary(); $attributes['domain'] = $domain; $attributes['path'] = $path; $check = new Iri($check); @@ -332,7 +332,7 @@ public function testUrlExactMatch($domain, $path, $check, $matches, $domain_matc * @dataProvider urlMatchProvider */ public function testUrlMatch($domain, $path, $check, $matches, $domain_matches) { - $attributes = new Requests_Utility_CaseInsensitiveDictionary(); + $attributes = new CaseInsensitiveDictionary(); $attributes['domain'] = $domain; $attributes['path'] = $path; $flags = array( @@ -344,7 +344,7 @@ public function testUrlMatch($domain, $path, $check, $matches, $domain_matches) } public function testUrlMatchSecure() { - $attributes = new Requests_Utility_CaseInsensitiveDictionary(); + $attributes = new CaseInsensitiveDictionary(); $attributes['domain'] = 'example.com'; $attributes['path'] = '/'; $attributes['secure'] = true; From 40066115ee06ada013bd781d5c3b502308a8224e Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 16 Aug 2021 04:43:58 +0200 Subject: [PATCH 30/45] Requests_Utility_FilteredIterator: change to namespaced class Includes: * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. --- library/Deprecated.php | 1 + src/Autoload.php | 1 + src/Response/Headers.php | 4 ++-- .../Requests => src}/Utility/FilteredIterator.php | 7 ++++++- tests/Utility/FilteredIteratorTest.php | 12 ++++++------ 5 files changed, 16 insertions(+), 9 deletions(-) rename {library/Requests => src}/Utility/FilteredIterator.php (90%) diff --git a/library/Deprecated.php b/library/Deprecated.php index 9abfe3b78..f82885b15 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -49,3 +49,4 @@ class Requests_Response_Headers extends WpOrg\Requests\Response\Headers {} class Requests_Transport_cURL extends WpOrg\Requests\Transport\Curl {} class Requests_Transport_fsockopen extends WpOrg\Requests\Transport\Fsockopen {} class Requests_Utility_CaseInsensitiveDictionary extends WpOrg\Requests\Utility\CaseInsensitiveDictionary {} +class Requests_Utility_FilteredIterator extends WpOrg\Requests\Utility\FilteredIterator {} diff --git a/src/Autoload.php b/src/Autoload.php index 451b24e43..ddd64c35d 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -56,6 +56,7 @@ class Autoload { 'requests_transport_curl' => '\WpOrg\Requests\Transport\Curl', 'requests_transport_fsockopen' => '\WpOrg\Requests\Transport\Fsockopen', 'requests_utility_caseinsensitivedictionary' => '\WpOrg\Requests\Utility\CaseInsensitiveDictionary', + 'requests_utility_filterediterator' => '\WpOrg\Requests\Utility\FilteredIterator', ); /** diff --git a/src/Response/Headers.php b/src/Response/Headers.php index 3d90f11f6..682eb6c39 100644 --- a/src/Response/Headers.php +++ b/src/Response/Headers.php @@ -7,9 +7,9 @@ namespace WpOrg\Requests\Response; -use Requests_Utility_FilteredIterator; use WpOrg\Requests\Exception; use WpOrg\Requests\Utility\CaseInsensitiveDictionary; +use WpOrg\Requests\Utility\FilteredIterator; /** * Case-insensitive dictionary, suitable for HTTP headers @@ -99,6 +99,6 @@ public function flatten($value) { * @return \ArrayIterator */ public function getIterator() { - return new Requests_Utility_FilteredIterator($this->data, array($this, 'flatten')); + return new FilteredIterator($this->data, array($this, 'flatten')); } } diff --git a/library/Requests/Utility/FilteredIterator.php b/src/Utility/FilteredIterator.php similarity index 90% rename from library/Requests/Utility/FilteredIterator.php rename to src/Utility/FilteredIterator.php index 81a431b69..e34955767 100644 --- a/library/Requests/Utility/FilteredIterator.php +++ b/src/Utility/FilteredIterator.php @@ -6,13 +6,18 @@ * @subpackage Utilities */ +namespace WpOrg\Requests\Utility; + +use ArrayIterator; +use ReturnTypeWillChange; + /** * Iterator for arrays requiring filtered values * * @package Requests * @subpackage Utilities */ -class Requests_Utility_FilteredIterator extends ArrayIterator { +class FilteredIterator extends ArrayIterator { /** * Callback to run as a filter * diff --git a/tests/Utility/FilteredIteratorTest.php b/tests/Utility/FilteredIteratorTest.php index 4e37f5281..7019c2fe9 100644 --- a/tests/Utility/FilteredIteratorTest.php +++ b/tests/Utility/FilteredIteratorTest.php @@ -5,7 +5,7 @@ use ArrayIterator; use ReflectionClass; use Requests\Tests\TestCase; -use Requests_Utility_FilteredIterator; +use WpOrg\Requests\Utility\FilteredIterator; class FilteredIteratorTest extends TestCase { /** @@ -13,9 +13,9 @@ class FilteredIteratorTest extends TestCase { */ public function testDeserializeRequestUtilityFilteredIteratorObjects($value) { $serialized = serialize($value); - if (get_class($value) === Requests_Utility_FilteredIterator::class) { + if (get_class($value) === FilteredIterator::class) { $new_value = unserialize($serialized); - $reflection = new ReflectionClass(Requests_Utility_FilteredIterator::class); + $reflection = new ReflectionClass(FilteredIterator::class); $property = $reflection->getProperty('callback'); $property->setAccessible(true); $callback_value = $property->getValue($new_value); @@ -27,9 +27,9 @@ public function testDeserializeRequestUtilityFilteredIteratorObjects($value) { public function dataSerializeDeserializeObjects() { return array( - array(new Requests_Utility_FilteredIterator(array(1), 'md5')), - array(new Requests_Utility_FilteredIterator(array(1, 2), 'sha1')), - array(new Requests_Utility_FilteredIterator(array(1, 2, 3), 'doesnotexist')), + array(new FilteredIterator(array(1), 'md5')), + array(new FilteredIterator(array(1, 2), 'sha1')), + array(new FilteredIterator(array(1, 2, 3), 'doesnotexist')), array(new ArrayIterator(array(1, 2, 3))), ); } From b726ac6ef8478e99a82304fcc7f5c57269755ad8 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 19 Aug 2021 01:57:26 +0200 Subject: [PATCH 31/45] Requests_Exception_HTTP: change to namespaced class Includes: * Using CamelCaps for the class name. * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. --- library/Deprecated.php | 2 ++ library/Requests/Exception/HTTP/304.php | 4 +++- library/Requests/Exception/HTTP/305.php | 4 +++- library/Requests/Exception/HTTP/306.php | 4 +++- library/Requests/Exception/HTTP/400.php | 4 +++- library/Requests/Exception/HTTP/401.php | 4 +++- library/Requests/Exception/HTTP/402.php | 4 +++- library/Requests/Exception/HTTP/403.php | 4 +++- library/Requests/Exception/HTTP/404.php | 4 +++- library/Requests/Exception/HTTP/405.php | 4 +++- library/Requests/Exception/HTTP/406.php | 4 +++- library/Requests/Exception/HTTP/407.php | 4 +++- library/Requests/Exception/HTTP/408.php | 4 +++- library/Requests/Exception/HTTP/409.php | 4 +++- library/Requests/Exception/HTTP/410.php | 4 +++- library/Requests/Exception/HTTP/411.php | 4 +++- library/Requests/Exception/HTTP/412.php | 4 +++- library/Requests/Exception/HTTP/413.php | 4 +++- library/Requests/Exception/HTTP/414.php | 4 +++- library/Requests/Exception/HTTP/415.php | 4 +++- library/Requests/Exception/HTTP/416.php | 4 +++- library/Requests/Exception/HTTP/417.php | 4 +++- library/Requests/Exception/HTTP/418.php | 4 +++- library/Requests/Exception/HTTP/428.php | 4 +++- library/Requests/Exception/HTTP/429.php | 4 +++- library/Requests/Exception/HTTP/431.php | 4 +++- library/Requests/Exception/HTTP/500.php | 4 +++- library/Requests/Exception/HTTP/501.php | 4 +++- library/Requests/Exception/HTTP/502.php | 4 +++- library/Requests/Exception/HTTP/503.php | 4 +++- library/Requests/Exception/HTTP/504.php | 4 +++- library/Requests/Exception/HTTP/505.php | 4 +++- library/Requests/Exception/HTTP/511.php | 4 +++- library/Requests/Exception/HTTP/Unknown.php | 3 ++- src/Autoload.php | 1 + .../Requests/Exception/HTTP.php => src/Exception/Http.php | 5 ++++- src/Response.php | 6 +++--- 37 files changed, 108 insertions(+), 37 deletions(-) rename library/Requests/Exception/HTTP.php => src/Exception/Http.php (93%) diff --git a/library/Deprecated.php b/library/Deprecated.php index f82885b15..c97aa9f92 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -50,3 +50,5 @@ class Requests_Transport_cURL extends WpOrg\Requests\Transport\Curl {} class Requests_Transport_fsockopen extends WpOrg\Requests\Transport\Fsockopen {} class Requests_Utility_CaseInsensitiveDictionary extends WpOrg\Requests\Utility\CaseInsensitiveDictionary {} class Requests_Utility_FilteredIterator extends WpOrg\Requests\Utility\FilteredIterator {} + +class Requests_Exception_HTTP extends WpOrg\Requests\Exception\Http {} diff --git a/library/Requests/Exception/HTTP/304.php b/library/Requests/Exception/HTTP/304.php index 2636ba451..0e9be9f30 100644 --- a/library/Requests/Exception/HTTP/304.php +++ b/library/Requests/Exception/HTTP/304.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 304 Not Modified responses * * @package Requests */ -class Requests_Exception_HTTP_304 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_304 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/305.php b/library/Requests/Exception/HTTP/305.php index 37d115a81..fffab73da 100644 --- a/library/Requests/Exception/HTTP/305.php +++ b/library/Requests/Exception/HTTP/305.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 305 Use Proxy responses * * @package Requests */ -class Requests_Exception_HTTP_305 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_305 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/306.php b/library/Requests/Exception/HTTP/306.php index 743a4ed1d..a1799241f 100644 --- a/library/Requests/Exception/HTTP/306.php +++ b/library/Requests/Exception/HTTP/306.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 306 Switch Proxy responses * * @package Requests */ -class Requests_Exception_HTTP_306 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_306 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/400.php b/library/Requests/Exception/HTTP/400.php index 5bd5428c0..4f1064f51 100644 --- a/library/Requests/Exception/HTTP/400.php +++ b/library/Requests/Exception/HTTP/400.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 400 Bad Request responses * * @package Requests */ -class Requests_Exception_HTTP_400 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_400 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/401.php b/library/Requests/Exception/HTTP/401.php index 62a283ab0..8e883b70c 100644 --- a/library/Requests/Exception/HTTP/401.php +++ b/library/Requests/Exception/HTTP/401.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 401 Unauthorized responses * * @package Requests */ -class Requests_Exception_HTTP_401 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_401 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/402.php b/library/Requests/Exception/HTTP/402.php index f287fd4b2..870e8e469 100644 --- a/library/Requests/Exception/HTTP/402.php +++ b/library/Requests/Exception/HTTP/402.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 402 Payment Required responses * * @package Requests */ -class Requests_Exception_HTTP_402 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_402 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/403.php b/library/Requests/Exception/HTTP/403.php index 2ae1c4445..0887160c6 100644 --- a/library/Requests/Exception/HTTP/403.php +++ b/library/Requests/Exception/HTTP/403.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 403 Forbidden responses * * @package Requests */ -class Requests_Exception_HTTP_403 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_403 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/404.php b/library/Requests/Exception/HTTP/404.php index e6e28672c..724f58ee1 100644 --- a/library/Requests/Exception/HTTP/404.php +++ b/library/Requests/Exception/HTTP/404.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 404 Not Found responses * * @package Requests */ -class Requests_Exception_HTTP_404 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_404 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/405.php b/library/Requests/Exception/HTTP/405.php index 0461e6108..f9d20d3b4 100644 --- a/library/Requests/Exception/HTTP/405.php +++ b/library/Requests/Exception/HTTP/405.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 405 Method Not Allowed responses * * @package Requests */ -class Requests_Exception_HTTP_405 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_405 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/406.php b/library/Requests/Exception/HTTP/406.php index 980ef0efc..95d1f6a9a 100644 --- a/library/Requests/Exception/HTTP/406.php +++ b/library/Requests/Exception/HTTP/406.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 406 Not Acceptable responses * * @package Requests */ -class Requests_Exception_HTTP_406 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_406 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/407.php b/library/Requests/Exception/HTTP/407.php index d08c6af7c..efc66d7c1 100644 --- a/library/Requests/Exception/HTTP/407.php +++ b/library/Requests/Exception/HTTP/407.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 407 Proxy Authentication Required responses * * @package Requests */ -class Requests_Exception_HTTP_407 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_407 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/408.php b/library/Requests/Exception/HTTP/408.php index db15bc291..31ceb2daa 100644 --- a/library/Requests/Exception/HTTP/408.php +++ b/library/Requests/Exception/HTTP/408.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 408 Request Timeout responses * * @package Requests */ -class Requests_Exception_HTTP_408 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_408 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/409.php b/library/Requests/Exception/HTTP/409.php index 83002f936..4dab894aa 100644 --- a/library/Requests/Exception/HTTP/409.php +++ b/library/Requests/Exception/HTTP/409.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 409 Conflict responses * * @package Requests */ -class Requests_Exception_HTTP_409 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_409 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/410.php b/library/Requests/Exception/HTTP/410.php index 5bf7aa6d4..d977133c3 100644 --- a/library/Requests/Exception/HTTP/410.php +++ b/library/Requests/Exception/HTTP/410.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 410 Gone responses * * @package Requests */ -class Requests_Exception_HTTP_410 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_410 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/411.php b/library/Requests/Exception/HTTP/411.php index 25517b4f3..de8480054 100644 --- a/library/Requests/Exception/HTTP/411.php +++ b/library/Requests/Exception/HTTP/411.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 411 Length Required responses * * @package Requests */ -class Requests_Exception_HTTP_411 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_411 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/412.php b/library/Requests/Exception/HTTP/412.php index e89533a7e..1838c5b19 100644 --- a/library/Requests/Exception/HTTP/412.php +++ b/library/Requests/Exception/HTTP/412.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 412 Precondition Failed responses * * @package Requests */ -class Requests_Exception_HTTP_412 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_412 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/413.php b/library/Requests/Exception/HTTP/413.php index a7b38fce1..f69025afb 100644 --- a/library/Requests/Exception/HTTP/413.php +++ b/library/Requests/Exception/HTTP/413.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 413 Request Entity Too Large responses * * @package Requests */ -class Requests_Exception_HTTP_413 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_413 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/414.php b/library/Requests/Exception/HTTP/414.php index 54c8b8c9a..46909f757 100644 --- a/library/Requests/Exception/HTTP/414.php +++ b/library/Requests/Exception/HTTP/414.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 414 Request-URI Too Large responses * * @package Requests */ -class Requests_Exception_HTTP_414 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_414 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/415.php b/library/Requests/Exception/HTTP/415.php index 6b5f0785b..e70af9b86 100644 --- a/library/Requests/Exception/HTTP/415.php +++ b/library/Requests/Exception/HTTP/415.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 415 Unsupported Media Type responses * * @package Requests */ -class Requests_Exception_HTTP_415 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_415 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/416.php b/library/Requests/Exception/HTTP/416.php index 48a4ecdb8..8351511b4 100644 --- a/library/Requests/Exception/HTTP/416.php +++ b/library/Requests/Exception/HTTP/416.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 416 Requested Range Not Satisfiable responses * * @package Requests */ -class Requests_Exception_HTTP_416 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_416 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/417.php b/library/Requests/Exception/HTTP/417.php index 81570330a..339bc751a 100644 --- a/library/Requests/Exception/HTTP/417.php +++ b/library/Requests/Exception/HTTP/417.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 417 Expectation Failed responses * * @package Requests */ -class Requests_Exception_HTTP_417 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_417 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/418.php b/library/Requests/Exception/HTTP/418.php index 0fcc87d61..441c96243 100644 --- a/library/Requests/Exception/HTTP/418.php +++ b/library/Requests/Exception/HTTP/418.php @@ -6,13 +6,15 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 418 I'm A Teapot responses * * @see https://tools.ietf.org/html/rfc2324 * @package Requests */ -class Requests_Exception_HTTP_418 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_418 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/428.php b/library/Requests/Exception/HTTP/428.php index 799ddffd5..d6c31a177 100644 --- a/library/Requests/Exception/HTTP/428.php +++ b/library/Requests/Exception/HTTP/428.php @@ -6,13 +6,15 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 428 Precondition Required responses * * @see https://tools.ietf.org/html/rfc6585 * @package Requests */ -class Requests_Exception_HTTP_428 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_428 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/429.php b/library/Requests/Exception/HTTP/429.php index 78a3148ad..039c9a809 100644 --- a/library/Requests/Exception/HTTP/429.php +++ b/library/Requests/Exception/HTTP/429.php @@ -6,13 +6,15 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 429 Too Many Requests responses * * @see https://tools.ietf.org/html/draft-nottingham-http-new-status-04 * @package Requests */ -class Requests_Exception_HTTP_429 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_429 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/431.php b/library/Requests/Exception/HTTP/431.php index a92b48a4e..ee9fc6b83 100644 --- a/library/Requests/Exception/HTTP/431.php +++ b/library/Requests/Exception/HTTP/431.php @@ -6,13 +6,15 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 431 Request Header Fields Too Large responses * * @see https://tools.ietf.org/html/rfc6585 * @package Requests */ -class Requests_Exception_HTTP_431 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_431 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/500.php b/library/Requests/Exception/HTTP/500.php index 22a6406ab..4bcea6814 100644 --- a/library/Requests/Exception/HTTP/500.php +++ b/library/Requests/Exception/HTTP/500.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 500 Internal Server Error responses * * @package Requests */ -class Requests_Exception_HTTP_500 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_500 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/501.php b/library/Requests/Exception/HTTP/501.php index 11f395b20..f1a7cc4e5 100644 --- a/library/Requests/Exception/HTTP/501.php +++ b/library/Requests/Exception/HTTP/501.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 501 Not Implemented responses * * @package Requests */ -class Requests_Exception_HTTP_501 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_501 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/502.php b/library/Requests/Exception/HTTP/502.php index 43f576a62..8f1b35451 100644 --- a/library/Requests/Exception/HTTP/502.php +++ b/library/Requests/Exception/HTTP/502.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 502 Bad Gateway responses * * @package Requests */ -class Requests_Exception_HTTP_502 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_502 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/503.php b/library/Requests/Exception/HTTP/503.php index 4024a4f5c..1512daaa4 100644 --- a/library/Requests/Exception/HTTP/503.php +++ b/library/Requests/Exception/HTTP/503.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 503 Service Unavailable responses * * @package Requests */ -class Requests_Exception_HTTP_503 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_503 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/504.php b/library/Requests/Exception/HTTP/504.php index 25fd08631..e241316d2 100644 --- a/library/Requests/Exception/HTTP/504.php +++ b/library/Requests/Exception/HTTP/504.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 504 Gateway Timeout responses * * @package Requests */ -class Requests_Exception_HTTP_504 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_504 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/505.php b/library/Requests/Exception/HTTP/505.php index 1f7cfec9b..ce5dc7c56 100644 --- a/library/Requests/Exception/HTTP/505.php +++ b/library/Requests/Exception/HTTP/505.php @@ -5,12 +5,14 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 505 HTTP Version Not Supported responses * * @package Requests */ -class Requests_Exception_HTTP_505 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_505 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/511.php b/library/Requests/Exception/HTTP/511.php index 3332513b1..837a24569 100644 --- a/library/Requests/Exception/HTTP/511.php +++ b/library/Requests/Exception/HTTP/511.php @@ -6,13 +6,15 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; + /** * Exception for 511 Network Authentication Required responses * * @see https://tools.ietf.org/html/rfc6585 * @package Requests */ -class Requests_Exception_HTTP_511 extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_511 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/Unknown.php b/library/Requests/Exception/HTTP/Unknown.php index a1e76a6ac..ff51e8548 100644 --- a/library/Requests/Exception/HTTP/Unknown.php +++ b/library/Requests/Exception/HTTP/Unknown.php @@ -5,6 +5,7 @@ * @package Requests */ +use WpOrg\Requests\Exception\Http; use WpOrg\Requests\Response; /** @@ -12,7 +13,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_Unknown extends Requests_Exception_HTTP { +class Requests_Exception_HTTP_Unknown extends Http { /** * HTTP status code * diff --git a/src/Autoload.php b/src/Autoload.php index ddd64c35d..ab9d877c5 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -57,6 +57,7 @@ class Autoload { 'requests_transport_fsockopen' => '\WpOrg\Requests\Transport\Fsockopen', 'requests_utility_caseinsensitivedictionary' => '\WpOrg\Requests\Utility\CaseInsensitiveDictionary', 'requests_utility_filterediterator' => '\WpOrg\Requests\Utility\FilteredIterator', + 'requests_exception_http' => '\WpOrg\Requests\Exception\Http', ); /** diff --git a/library/Requests/Exception/HTTP.php b/src/Exception/Http.php similarity index 93% rename from library/Requests/Exception/HTTP.php rename to src/Exception/Http.php index 174e40b1e..fdfacd878 100644 --- a/library/Requests/Exception/HTTP.php +++ b/src/Exception/Http.php @@ -5,6 +5,9 @@ * @package Requests */ +namespace WpOrg\Requests\Exception; + +use Requests_Exception_HTTP_Unknown; use WpOrg\Requests\Exception; /** @@ -12,7 +15,7 @@ * * @package Requests */ -class Requests_Exception_HTTP extends Exception { +class Http extends Exception { /** * HTTP status code * diff --git a/src/Response.php b/src/Response.php index fad46ceed..4defe7bfc 100644 --- a/src/Response.php +++ b/src/Response.php @@ -8,9 +8,9 @@ namespace WpOrg\Requests; -use Requests_Exception_HTTP; use WpOrg\Requests\Cookie\Jar; use WpOrg\Requests\Exception; +use WpOrg\Requests\Exception\Http; use WpOrg\Requests\Response\Headers; /** @@ -112,7 +112,7 @@ public function is_redirect() { * Throws an exception if the request was not successful * * @throws \WpOrg\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}) + * @throws \WpOrg\Requests\Exception\Http On non-successful status code. Exception class corresponds to code (e.g. {@see Requests_Exception_HTTP_404}) * @param boolean $allow_redirects Set to false to throw on a 3xx as well */ public function throw_for_status($allow_redirects = true) { @@ -122,7 +122,7 @@ public function throw_for_status($allow_redirects = true) { } } elseif (!$this->success) { - $exception = Requests_Exception_HTTP::get_class($this->status_code); + $exception = Http::get_class($this->status_code); throw new $exception(null, $this); } } From d8eb589a511b256c79415a471c4c0efa696a96fd Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 22 Aug 2021 18:54:56 +0200 Subject: [PATCH 32/45] Requests_Exception_InvalidArgument: change to namespaced class Includes: * Moving the namespaced version of the class to the `src` directory. * Updating all references to the class. * Adding a missing `@since` tag. Note: as this class has only been introduced in Requests 2.0.0, the original class name has not been added to the class alias list or the deprecated classes file. --- library/Requests/Exception/InvalidArgument.php | 8 -------- src/Exception/InvalidArgument.php | 13 +++++++++++++ src/Transport/Curl.php | 4 ++-- src/Transport/Fsockopen.php | 4 ++-- tests/Transport/BaseTestCase.php | 3 ++- 5 files changed, 19 insertions(+), 13 deletions(-) delete mode 100644 library/Requests/Exception/InvalidArgument.php create mode 100644 src/Exception/InvalidArgument.php diff --git a/library/Requests/Exception/InvalidArgument.php b/library/Requests/Exception/InvalidArgument.php deleted file mode 100644 index 6553742d3..000000000 --- a/library/Requests/Exception/InvalidArgument.php +++ /dev/null @@ -1,8 +0,0 @@ -expectException('Requests_Exception_InvalidArgument'); + $this->expectException(InvalidArgument::class); $this->expectExceptionMessage('Argument #3 ($data) must be of type array|string'); $request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions()); From 82258854720ca25abfca48dc761c18b8ad5162f2 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 19 Aug 2021 02:06:36 +0200 Subject: [PATCH 33/45] Requests_Exception_Transport: change to namespaced class Includes: * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. --- library/Deprecated.php | 1 + library/Requests/Exception/Transport.php | 7 ------- library/Requests/Exception/Transport/cURL.php | 4 +++- src/Autoload.php | 1 + src/Exception/Transport.php | 7 +++++++ 5 files changed, 12 insertions(+), 8 deletions(-) delete mode 100644 library/Requests/Exception/Transport.php create mode 100644 src/Exception/Transport.php diff --git a/library/Deprecated.php b/library/Deprecated.php index c97aa9f92..6bde86760 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -52,3 +52,4 @@ class Requests_Utility_CaseInsensitiveDictionary extends WpOrg\Requests\Utility\ class Requests_Utility_FilteredIterator extends WpOrg\Requests\Utility\FilteredIterator {} class Requests_Exception_HTTP extends WpOrg\Requests\Exception\Http {} +class Requests_Exception_Transport extends WpOrg\Requests\Exception\Transport {} diff --git a/library/Requests/Exception/Transport.php b/library/Requests/Exception/Transport.php deleted file mode 100644 index a592a9c2d..000000000 --- a/library/Requests/Exception/Transport.php +++ /dev/null @@ -1,7 +0,0 @@ - '\WpOrg\Requests\Utility\CaseInsensitiveDictionary', 'requests_utility_filterediterator' => '\WpOrg\Requests\Utility\FilteredIterator', 'requests_exception_http' => '\WpOrg\Requests\Exception\Http', + 'requests_exception_transport' => '\WpOrg\Requests\Exception\Transport', ); /** diff --git a/src/Exception/Transport.php b/src/Exception/Transport.php new file mode 100644 index 000000000..5b112507e --- /dev/null +++ b/src/Exception/Transport.php @@ -0,0 +1,7 @@ + Date: Thu, 19 Aug 2021 02:09:42 +0200 Subject: [PATCH 34/45] Requests_Exception_Transport_cURL: change to namespaced class Includes: * Using CamelCaps for the class name. * Moving the namespaced version of the class to the `src` directory. * [Custom autoload] Adding the class to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the class to the `Deprecated.php` file. * Updating all references to the class. * Minor PHPCS config tweak. --- .phpcs.xml.dist | 1 - library/Deprecated.php | 1 + src/Autoload.php | 1 + .../Transport/cURL.php => src/Exception/Transport/Curl.php | 4 +++- src/Transport/Curl.php | 6 +++--- 5 files changed, 8 insertions(+), 5 deletions(-) rename library/Requests/Exception/Transport/cURL.php => src/Exception/Transport/Curl.php (91%) diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index ab1d811a9..68974d6a0 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -221,7 +221,6 @@ */library/Deprecated\.php$ - */Transport/cURL\.php$ diff --git a/library/Deprecated.php b/library/Deprecated.php index 6bde86760..a64479a9e 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -53,3 +53,4 @@ class Requests_Utility_FilteredIterator extends WpOrg\Requests\Utility\FilteredI class Requests_Exception_HTTP extends WpOrg\Requests\Exception\Http {} class Requests_Exception_Transport extends WpOrg\Requests\Exception\Transport {} +class Requests_Exception_Transport_cURL extends WpOrg\Requests\Exception\Transport\Curl {} diff --git a/src/Autoload.php b/src/Autoload.php index c6eb2d2db..470ade3e7 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -59,6 +59,7 @@ class Autoload { 'requests_utility_filterediterator' => '\WpOrg\Requests\Utility\FilteredIterator', 'requests_exception_http' => '\WpOrg\Requests\Exception\Http', 'requests_exception_transport' => '\WpOrg\Requests\Exception\Transport', + 'requests_exception_transport_curl' => '\WpOrg\Requests\Exception\Transport\Curl', ); /** diff --git a/library/Requests/Exception/Transport/cURL.php b/src/Exception/Transport/Curl.php similarity index 91% rename from library/Requests/Exception/Transport/cURL.php rename to src/Exception/Transport/Curl.php index 7c13860e6..d99a3fe35 100644 --- a/library/Requests/Exception/Transport/cURL.php +++ b/src/Exception/Transport/Curl.php @@ -1,8 +1,10 @@ Date: Thu, 19 Aug 2021 02:25:49 +0200 Subject: [PATCH 35/45] Requests_Exception_HTTP_*: change to namespaced classes Includes: * Prefix the class names with `Status` as purely numeric class names are not allowed in PHP. Note: while `Unknown` would have been allowed, I've elected to prefix this with `Status` as well for consistency. * Moving the namespaced version of the classes to the `src` directory. * [Custom autoload] Adding the classes to the `$deprecated_classes` array in the `WpOrg\Requests\Autoload` class. * [Composer autoload] Adding the classes to the `Deprecated.php` file. * Updating all references to the classes. --- library/Deprecated.php | 33 +++++++++++++++++++ src/Autoload.php | 33 +++++++++++++++++++ src/Exception/Http.php | 8 ++--- .../Exception/Http/Status304.php | 4 ++- .../Exception/Http/Status305.php | 4 ++- .../Exception/Http/Status306.php | 4 ++- .../Exception/Http/Status400.php | 4 ++- .../Exception/Http/Status401.php | 4 ++- .../Exception/Http/Status402.php | 4 ++- .../Exception/Http/Status403.php | 4 ++- .../Exception/Http/Status404.php | 4 ++- .../Exception/Http/Status405.php | 4 ++- .../Exception/Http/Status406.php | 4 ++- .../Exception/Http/Status407.php | 4 ++- .../Exception/Http/Status408.php | 4 ++- .../Exception/Http/Status409.php | 4 ++- .../Exception/Http/Status410.php | 4 ++- .../Exception/Http/Status411.php | 4 ++- .../Exception/Http/Status412.php | 4 ++- .../Exception/Http/Status413.php | 4 ++- .../Exception/Http/Status414.php | 4 ++- .../Exception/Http/Status415.php | 4 ++- .../Exception/Http/Status416.php | 4 ++- .../Exception/Http/Status417.php | 4 ++- .../Exception/Http/Status418.php | 4 ++- .../Exception/Http/Status428.php | 4 ++- .../Exception/Http/Status429.php | 4 ++- .../Exception/Http/Status431.php | 4 ++- .../Exception/Http/Status500.php | 4 ++- .../Exception/Http/Status501.php | 4 ++- .../Exception/Http/Status502.php | 4 ++- .../Exception/Http/Status503.php | 4 ++- .../Exception/Http/Status504.php | 4 ++- .../Exception/Http/Status505.php | 4 ++- .../Exception/Http/Status511.php | 4 ++- .../Exception/Http/StatusUnknown.php | 4 ++- src/Response.php | 2 +- tests/Transport/BaseTestCase.php | 8 ++--- 38 files changed, 174 insertions(+), 42 deletions(-) rename library/Requests/Exception/HTTP/304.php => src/Exception/Http/Status304.php (83%) rename library/Requests/Exception/HTTP/305.php => src/Exception/Http/Status305.php (82%) rename library/Requests/Exception/HTTP/306.php => src/Exception/Http/Status306.php (83%) rename library/Requests/Exception/HTTP/400.php => src/Exception/Http/Status400.php (82%) rename library/Requests/Exception/HTTP/401.php => src/Exception/Http/Status401.php (83%) rename library/Requests/Exception/HTTP/402.php => src/Exception/Http/Status402.php (83%) rename library/Requests/Exception/HTTP/403.php => src/Exception/Http/Status403.php (82%) rename library/Requests/Exception/HTTP/404.php => src/Exception/Http/Status404.php (82%) rename library/Requests/Exception/HTTP/405.php => src/Exception/Http/Status405.php (83%) rename library/Requests/Exception/HTTP/406.php => src/Exception/Http/Status406.php (83%) rename library/Requests/Exception/HTTP/407.php => src/Exception/Http/Status407.php (84%) rename library/Requests/Exception/HTTP/408.php => src/Exception/Http/Status408.php (83%) rename library/Requests/Exception/HTTP/409.php => src/Exception/Http/Status409.php (82%) rename library/Requests/Exception/HTTP/410.php => src/Exception/Http/Status410.php (82%) rename library/Requests/Exception/HTTP/411.php => src/Exception/Http/Status411.php (83%) rename library/Requests/Exception/HTTP/412.php => src/Exception/Http/Status412.php (83%) rename library/Requests/Exception/HTTP/413.php => src/Exception/Http/Status413.php (84%) rename library/Requests/Exception/HTTP/414.php => src/Exception/Http/Status414.php (84%) rename library/Requests/Exception/HTTP/415.php => src/Exception/Http/Status415.php (84%) rename library/Requests/Exception/HTTP/416.php => src/Exception/Http/Status416.php (85%) rename library/Requests/Exception/HTTP/417.php => src/Exception/Http/Status417.php (83%) rename library/Requests/Exception/HTTP/418.php => src/Exception/Http/Status418.php (85%) rename library/Requests/Exception/HTTP/428.php => src/Exception/Http/Status428.php (86%) rename library/Requests/Exception/HTTP/429.php => src/Exception/Http/Status429.php (87%) rename library/Requests/Exception/HTTP/431.php => src/Exception/Http/Status431.php (87%) rename library/Requests/Exception/HTTP/500.php => src/Exception/Http/Status500.php (84%) rename library/Requests/Exception/HTTP/501.php => src/Exception/Http/Status501.php (83%) rename library/Requests/Exception/HTTP/502.php => src/Exception/Http/Status502.php (82%) rename library/Requests/Exception/HTTP/503.php => src/Exception/Http/Status503.php (83%) rename library/Requests/Exception/HTTP/504.php => src/Exception/Http/Status504.php (83%) rename library/Requests/Exception/HTTP/505.php => src/Exception/Http/Status505.php (84%) rename library/Requests/Exception/HTTP/511.php => src/Exception/Http/Status511.php (87%) rename library/Requests/Exception/HTTP/Unknown.php => src/Exception/Http/StatusUnknown.php (91%) diff --git a/library/Deprecated.php b/library/Deprecated.php index a64479a9e..2530bb812 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -54,3 +54,36 @@ class Requests_Utility_FilteredIterator extends WpOrg\Requests\Utility\FilteredI class Requests_Exception_HTTP extends WpOrg\Requests\Exception\Http {} class Requests_Exception_Transport extends WpOrg\Requests\Exception\Transport {} class Requests_Exception_Transport_cURL extends WpOrg\Requests\Exception\Transport\Curl {} +class Requests_Exception_HTTP_304 extends WpOrg\Requests\Exception\Http\Status304 {} +class Requests_Exception_HTTP_305 extends WpOrg\Requests\Exception\Http\Status305 {} +class Requests_Exception_HTTP_306 extends WpOrg\Requests\Exception\Http\Status306 {} +class Requests_Exception_HTTP_400 extends WpOrg\Requests\Exception\Http\Status400 {} +class Requests_Exception_HTTP_401 extends WpOrg\Requests\Exception\Http\Status401 {} +class Requests_Exception_HTTP_402 extends WpOrg\Requests\Exception\Http\Status402 {} +class Requests_Exception_HTTP_403 extends WpOrg\Requests\Exception\Http\Status403 {} +class Requests_Exception_HTTP_404 extends WpOrg\Requests\Exception\Http\Status404 {} +class Requests_Exception_HTTP_405 extends WpOrg\Requests\Exception\Http\Status405 {} +class Requests_Exception_HTTP_406 extends WpOrg\Requests\Exception\Http\Status406 {} +class Requests_Exception_HTTP_407 extends WpOrg\Requests\Exception\Http\Status407 {} +class Requests_Exception_HTTP_408 extends WpOrg\Requests\Exception\Http\Status408 {} +class Requests_Exception_HTTP_409 extends WpOrg\Requests\Exception\Http\Status409 {} +class Requests_Exception_HTTP_410 extends WpOrg\Requests\Exception\Http\Status410 {} +class Requests_Exception_HTTP_411 extends WpOrg\Requests\Exception\Http\Status411 {} +class Requests_Exception_HTTP_412 extends WpOrg\Requests\Exception\Http\Status412 {} +class Requests_Exception_HTTP_413 extends WpOrg\Requests\Exception\Http\Status413 {} +class Requests_Exception_HTTP_414 extends WpOrg\Requests\Exception\Http\Status414 {} +class Requests_Exception_HTTP_415 extends WpOrg\Requests\Exception\Http\Status415 {} +class Requests_Exception_HTTP_416 extends WpOrg\Requests\Exception\Http\Status416 {} +class Requests_Exception_HTTP_417 extends WpOrg\Requests\Exception\Http\Status417 {} +class Requests_Exception_HTTP_418 extends WpOrg\Requests\Exception\Http\Status418 {} +class Requests_Exception_HTTP_428 extends WpOrg\Requests\Exception\Http\Status428 {} +class Requests_Exception_HTTP_429 extends WpOrg\Requests\Exception\Http\Status429 {} +class Requests_Exception_HTTP_431 extends WpOrg\Requests\Exception\Http\Status431 {} +class Requests_Exception_HTTP_500 extends WpOrg\Requests\Exception\Http\Status500 {} +class Requests_Exception_HTTP_501 extends WpOrg\Requests\Exception\Http\Status501 {} +class Requests_Exception_HTTP_502 extends WpOrg\Requests\Exception\Http\Status502 {} +class Requests_Exception_HTTP_503 extends WpOrg\Requests\Exception\Http\Status503 {} +class Requests_Exception_HTTP_504 extends WpOrg\Requests\Exception\Http\Status504 {} +class Requests_Exception_HTTP_505 extends WpOrg\Requests\Exception\Http\Status505 {} +class Requests_Exception_HTTP_511 extends WpOrg\Requests\Exception\Http\Status511 {} +class Requests_Exception_HTTP_Unknown extends WpOrg\Requests\Exception\Http\StatusUnknown {} diff --git a/src/Autoload.php b/src/Autoload.php index 470ade3e7..bee7de04d 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -60,6 +60,39 @@ class Autoload { 'requests_exception_http' => '\WpOrg\Requests\Exception\Http', 'requests_exception_transport' => '\WpOrg\Requests\Exception\Transport', 'requests_exception_transport_curl' => '\WpOrg\Requests\Exception\Transport\Curl', + 'requests_exception_http_304' => '\WpOrg\Requests\Exception\Http\Status304', + 'requests_exception_http_305' => '\WpOrg\Requests\Exception\Http\Status305', + 'requests_exception_http_306' => '\WpOrg\Requests\Exception\Http\Status306', + 'requests_exception_http_400' => '\WpOrg\Requests\Exception\Http\Status400', + 'requests_exception_http_401' => '\WpOrg\Requests\Exception\Http\Status401', + 'requests_exception_http_402' => '\WpOrg\Requests\Exception\Http\Status402', + 'requests_exception_http_403' => '\WpOrg\Requests\Exception\Http\Status403', + 'requests_exception_http_404' => '\WpOrg\Requests\Exception\Http\Status404', + 'requests_exception_http_405' => '\WpOrg\Requests\Exception\Http\Status405', + 'requests_exception_http_406' => '\WpOrg\Requests\Exception\Http\Status406', + 'requests_exception_http_407' => '\WpOrg\Requests\Exception\Http\Status407', + 'requests_exception_http_408' => '\WpOrg\Requests\Exception\Http\Status408', + 'requests_exception_http_409' => '\WpOrg\Requests\Exception\Http\Status409', + 'requests_exception_http_410' => '\WpOrg\Requests\Exception\Http\Status410', + 'requests_exception_http_411' => '\WpOrg\Requests\Exception\Http\Status411', + 'requests_exception_http_412' => '\WpOrg\Requests\Exception\Http\Status412', + 'requests_exception_http_413' => '\WpOrg\Requests\Exception\Http\Status413', + 'requests_exception_http_414' => '\WpOrg\Requests\Exception\Http\Status414', + 'requests_exception_http_415' => '\WpOrg\Requests\Exception\Http\Status415', + 'requests_exception_http_416' => '\WpOrg\Requests\Exception\Http\Status416', + 'requests_exception_http_417' => '\WpOrg\Requests\Exception\Http\Status417', + 'requests_exception_http_418' => '\WpOrg\Requests\Exception\Http\Status418', + 'requests_exception_http_428' => '\WpOrg\Requests\Exception\Http\Status428', + 'requests_exception_http_429' => '\WpOrg\Requests\Exception\Http\Status429', + 'requests_exception_http_431' => '\WpOrg\Requests\Exception\Http\Status431', + 'requests_exception_http_500' => '\WpOrg\Requests\Exception\Http\Status500', + 'requests_exception_http_501' => '\WpOrg\Requests\Exception\Http\Status501', + 'requests_exception_http_502' => '\WpOrg\Requests\Exception\Http\Status502', + 'requests_exception_http_503' => '\WpOrg\Requests\Exception\Http\Status503', + 'requests_exception_http_504' => '\WpOrg\Requests\Exception\Http\Status504', + 'requests_exception_http_505' => '\WpOrg\Requests\Exception\Http\Status505', + 'requests_exception_http_511' => '\WpOrg\Requests\Exception\Http\Status511', + 'requests_exception_http_unknown' => '\WpOrg\Requests\Exception\Http\StatusUnknown', ); /** diff --git a/src/Exception/Http.php b/src/Exception/Http.php index fdfacd878..3f90e1557 100644 --- a/src/Exception/Http.php +++ b/src/Exception/Http.php @@ -7,8 +7,8 @@ namespace WpOrg\Requests\Exception; -use Requests_Exception_HTTP_Unknown; use WpOrg\Requests\Exception; +use WpOrg\Requests\Exception\Http\StatusUnknown; /** * Exception based on HTTP response @@ -63,14 +63,14 @@ public function getReason() { */ public static function get_class($code) { if (!$code) { - return Requests_Exception_HTTP_Unknown::class; + return StatusUnknown::class; } - $class = sprintf('Requests_Exception_HTTP_%d', $code); + $class = sprintf('\WpOrg\Requests\Exception\Http\Status%d', $code); if (class_exists($class)) { return $class; } - return Requests_Exception_HTTP_Unknown::class; + return StatusUnknown::class; } } diff --git a/library/Requests/Exception/HTTP/304.php b/src/Exception/Http/Status304.php similarity index 83% rename from library/Requests/Exception/HTTP/304.php rename to src/Exception/Http/Status304.php index 0e9be9f30..d128678a4 100644 --- a/library/Requests/Exception/HTTP/304.php +++ b/src/Exception/Http/Status304.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_304 extends Http { +class Status304 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/305.php b/src/Exception/Http/Status305.php similarity index 82% rename from library/Requests/Exception/HTTP/305.php rename to src/Exception/Http/Status305.php index fffab73da..9fc70a44c 100644 --- a/library/Requests/Exception/HTTP/305.php +++ b/src/Exception/Http/Status305.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_305 extends Http { +class Status305 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/306.php b/src/Exception/Http/Status306.php similarity index 83% rename from library/Requests/Exception/HTTP/306.php rename to src/Exception/Http/Status306.php index a1799241f..7fb03f0a8 100644 --- a/library/Requests/Exception/HTTP/306.php +++ b/src/Exception/Http/Status306.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_306 extends Http { +class Status306 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/400.php b/src/Exception/Http/Status400.php similarity index 82% rename from library/Requests/Exception/HTTP/400.php rename to src/Exception/Http/Status400.php index 4f1064f51..f9b799ba2 100644 --- a/library/Requests/Exception/HTTP/400.php +++ b/src/Exception/Http/Status400.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_400 extends Http { +class Status400 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/401.php b/src/Exception/Http/Status401.php similarity index 83% rename from library/Requests/Exception/HTTP/401.php rename to src/Exception/Http/Status401.php index 8e883b70c..b991c8bc2 100644 --- a/library/Requests/Exception/HTTP/401.php +++ b/src/Exception/Http/Status401.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_401 extends Http { +class Status401 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/402.php b/src/Exception/Http/Status402.php similarity index 83% rename from library/Requests/Exception/HTTP/402.php rename to src/Exception/Http/Status402.php index 870e8e469..eaf6ef8de 100644 --- a/library/Requests/Exception/HTTP/402.php +++ b/src/Exception/Http/Status402.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_402 extends Http { +class Status402 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/403.php b/src/Exception/Http/Status403.php similarity index 82% rename from library/Requests/Exception/HTTP/403.php rename to src/Exception/Http/Status403.php index 0887160c6..b4e6706dc 100644 --- a/library/Requests/Exception/HTTP/403.php +++ b/src/Exception/Http/Status403.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_403 extends Http { +class Status403 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/404.php b/src/Exception/Http/Status404.php similarity index 82% rename from library/Requests/Exception/HTTP/404.php rename to src/Exception/Http/Status404.php index 724f58ee1..48b936233 100644 --- a/library/Requests/Exception/HTTP/404.php +++ b/src/Exception/Http/Status404.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_404 extends Http { +class Status404 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/405.php b/src/Exception/Http/Status405.php similarity index 83% rename from library/Requests/Exception/HTTP/405.php rename to src/Exception/Http/Status405.php index f9d20d3b4..680d64a4f 100644 --- a/library/Requests/Exception/HTTP/405.php +++ b/src/Exception/Http/Status405.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_405 extends Http { +class Status405 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/406.php b/src/Exception/Http/Status406.php similarity index 83% rename from library/Requests/Exception/HTTP/406.php rename to src/Exception/Http/Status406.php index 95d1f6a9a..60e5302e0 100644 --- a/library/Requests/Exception/HTTP/406.php +++ b/src/Exception/Http/Status406.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_406 extends Http { +class Status406 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/407.php b/src/Exception/Http/Status407.php similarity index 84% rename from library/Requests/Exception/HTTP/407.php rename to src/Exception/Http/Status407.php index efc66d7c1..7911c87a2 100644 --- a/library/Requests/Exception/HTTP/407.php +++ b/src/Exception/Http/Status407.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_407 extends Http { +class Status407 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/408.php b/src/Exception/Http/Status408.php similarity index 83% rename from library/Requests/Exception/HTTP/408.php rename to src/Exception/Http/Status408.php index 31ceb2daa..bff63610e 100644 --- a/library/Requests/Exception/HTTP/408.php +++ b/src/Exception/Http/Status408.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_408 extends Http { +class Status408 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/409.php b/src/Exception/Http/Status409.php similarity index 82% rename from library/Requests/Exception/HTTP/409.php rename to src/Exception/Http/Status409.php index 4dab894aa..db41d6c6e 100644 --- a/library/Requests/Exception/HTTP/409.php +++ b/src/Exception/Http/Status409.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_409 extends Http { +class Status409 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/410.php b/src/Exception/Http/Status410.php similarity index 82% rename from library/Requests/Exception/HTTP/410.php rename to src/Exception/Http/Status410.php index d977133c3..0bd4b28b9 100644 --- a/library/Requests/Exception/HTTP/410.php +++ b/src/Exception/Http/Status410.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_410 extends Http { +class Status410 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/411.php b/src/Exception/Http/Status411.php similarity index 83% rename from library/Requests/Exception/HTTP/411.php rename to src/Exception/Http/Status411.php index de8480054..037edccba 100644 --- a/library/Requests/Exception/HTTP/411.php +++ b/src/Exception/Http/Status411.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_411 extends Http { +class Status411 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/412.php b/src/Exception/Http/Status412.php similarity index 83% rename from library/Requests/Exception/HTTP/412.php rename to src/Exception/Http/Status412.php index 1838c5b19..12cf8574e 100644 --- a/library/Requests/Exception/HTTP/412.php +++ b/src/Exception/Http/Status412.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_412 extends Http { +class Status412 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/413.php b/src/Exception/Http/Status413.php similarity index 84% rename from library/Requests/Exception/HTTP/413.php rename to src/Exception/Http/Status413.php index f69025afb..a6d40474c 100644 --- a/library/Requests/Exception/HTTP/413.php +++ b/src/Exception/Http/Status413.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_413 extends Http { +class Status413 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/414.php b/src/Exception/Http/Status414.php similarity index 84% rename from library/Requests/Exception/HTTP/414.php rename to src/Exception/Http/Status414.php index 46909f757..8e4a6d7ee 100644 --- a/library/Requests/Exception/HTTP/414.php +++ b/src/Exception/Http/Status414.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_414 extends Http { +class Status414 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/415.php b/src/Exception/Http/Status415.php similarity index 84% rename from library/Requests/Exception/HTTP/415.php rename to src/Exception/Http/Status415.php index e70af9b86..713c59192 100644 --- a/library/Requests/Exception/HTTP/415.php +++ b/src/Exception/Http/Status415.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_415 extends Http { +class Status415 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/416.php b/src/Exception/Http/Status416.php similarity index 85% rename from library/Requests/Exception/HTTP/416.php rename to src/Exception/Http/Status416.php index 8351511b4..db27f24b9 100644 --- a/library/Requests/Exception/HTTP/416.php +++ b/src/Exception/Http/Status416.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_416 extends Http { +class Status416 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/417.php b/src/Exception/Http/Status417.php similarity index 83% rename from library/Requests/Exception/HTTP/417.php rename to src/Exception/Http/Status417.php index 339bc751a..9e1fce484 100644 --- a/library/Requests/Exception/HTTP/417.php +++ b/src/Exception/Http/Status417.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_417 extends Http { +class Status417 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/418.php b/src/Exception/Http/Status418.php similarity index 85% rename from library/Requests/Exception/HTTP/418.php rename to src/Exception/Http/Status418.php index 441c96243..f98055fe6 100644 --- a/library/Requests/Exception/HTTP/418.php +++ b/src/Exception/Http/Status418.php @@ -6,6 +6,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -14,7 +16,7 @@ * @see https://tools.ietf.org/html/rfc2324 * @package Requests */ -class Requests_Exception_HTTP_418 extends Http { +class Status418 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/428.php b/src/Exception/Http/Status428.php similarity index 86% rename from library/Requests/Exception/HTTP/428.php rename to src/Exception/Http/Status428.php index d6c31a177..8638c1e17 100644 --- a/library/Requests/Exception/HTTP/428.php +++ b/src/Exception/Http/Status428.php @@ -6,6 +6,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -14,7 +16,7 @@ * @see https://tools.ietf.org/html/rfc6585 * @package Requests */ -class Requests_Exception_HTTP_428 extends Http { +class Status428 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/429.php b/src/Exception/Http/Status429.php similarity index 87% rename from library/Requests/Exception/HTTP/429.php rename to src/Exception/Http/Status429.php index 039c9a809..5d35f062b 100644 --- a/library/Requests/Exception/HTTP/429.php +++ b/src/Exception/Http/Status429.php @@ -6,6 +6,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -14,7 +16,7 @@ * @see https://tools.ietf.org/html/draft-nottingham-http-new-status-04 * @package Requests */ -class Requests_Exception_HTTP_429 extends Http { +class Status429 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/431.php b/src/Exception/Http/Status431.php similarity index 87% rename from library/Requests/Exception/HTTP/431.php rename to src/Exception/Http/Status431.php index ee9fc6b83..4eebd55fa 100644 --- a/library/Requests/Exception/HTTP/431.php +++ b/src/Exception/Http/Status431.php @@ -6,6 +6,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -14,7 +16,7 @@ * @see https://tools.ietf.org/html/rfc6585 * @package Requests */ -class Requests_Exception_HTTP_431 extends Http { +class Status431 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/500.php b/src/Exception/Http/Status500.php similarity index 84% rename from library/Requests/Exception/HTTP/500.php rename to src/Exception/Http/Status500.php index 4bcea6814..8e12e5fc3 100644 --- a/library/Requests/Exception/HTTP/500.php +++ b/src/Exception/Http/Status500.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_500 extends Http { +class Status500 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/501.php b/src/Exception/Http/Status501.php similarity index 83% rename from library/Requests/Exception/HTTP/501.php rename to src/Exception/Http/Status501.php index f1a7cc4e5..89fe4a43b 100644 --- a/library/Requests/Exception/HTTP/501.php +++ b/src/Exception/Http/Status501.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_501 extends Http { +class Status501 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/502.php b/src/Exception/Http/Status502.php similarity index 82% rename from library/Requests/Exception/HTTP/502.php rename to src/Exception/Http/Status502.php index 8f1b35451..c4fb85e60 100644 --- a/library/Requests/Exception/HTTP/502.php +++ b/src/Exception/Http/Status502.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_502 extends Http { +class Status502 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/503.php b/src/Exception/Http/Status503.php similarity index 83% rename from library/Requests/Exception/HTTP/503.php rename to src/Exception/Http/Status503.php index 1512daaa4..264264189 100644 --- a/library/Requests/Exception/HTTP/503.php +++ b/src/Exception/Http/Status503.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_503 extends Http { +class Status503 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/504.php b/src/Exception/Http/Status504.php similarity index 83% rename from library/Requests/Exception/HTTP/504.php rename to src/Exception/Http/Status504.php index e241316d2..3c484febc 100644 --- a/library/Requests/Exception/HTTP/504.php +++ b/src/Exception/Http/Status504.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_504 extends Http { +class Status504 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/505.php b/src/Exception/Http/Status505.php similarity index 84% rename from library/Requests/Exception/HTTP/505.php rename to src/Exception/Http/Status505.php index ce5dc7c56..53e0489ee 100644 --- a/library/Requests/Exception/HTTP/505.php +++ b/src/Exception/Http/Status505.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -12,7 +14,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_505 extends Http { +class Status505 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/511.php b/src/Exception/Http/Status511.php similarity index 87% rename from library/Requests/Exception/HTTP/511.php rename to src/Exception/Http/Status511.php index 837a24569..6149bd9a8 100644 --- a/library/Requests/Exception/HTTP/511.php +++ b/src/Exception/Http/Status511.php @@ -6,6 +6,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; /** @@ -14,7 +16,7 @@ * @see https://tools.ietf.org/html/rfc6585 * @package Requests */ -class Requests_Exception_HTTP_511 extends Http { +class Status511 extends Http { /** * HTTP status code * diff --git a/library/Requests/Exception/HTTP/Unknown.php b/src/Exception/Http/StatusUnknown.php similarity index 91% rename from library/Requests/Exception/HTTP/Unknown.php rename to src/Exception/Http/StatusUnknown.php index ff51e8548..02e129665 100644 --- a/library/Requests/Exception/HTTP/Unknown.php +++ b/src/Exception/Http/StatusUnknown.php @@ -5,6 +5,8 @@ * @package Requests */ +namespace WpOrg\Requests\Exception\Http; + use WpOrg\Requests\Exception\Http; use WpOrg\Requests\Response; @@ -13,7 +15,7 @@ * * @package Requests */ -class Requests_Exception_HTTP_Unknown extends Http { +class StatusUnknown extends Http { /** * HTTP status code * diff --git a/src/Response.php b/src/Response.php index 4defe7bfc..b895c2bf7 100644 --- a/src/Response.php +++ b/src/Response.php @@ -112,7 +112,7 @@ public function is_redirect() { * Throws an exception if the request was not successful * * @throws \WpOrg\Requests\Exception If `$allow_redirects` is false, and code is 3xx (`response.no_redirects`) - * @throws \WpOrg\Requests\Exception\Http On non-successful status code. Exception class corresponds to code (e.g. {@see Requests_Exception_HTTP_404}) + * @throws \WpOrg\Requests\Exception\Http On non-successful status code. Exception class corresponds to "Status" + code (e.g. {@see \WpOrg\Requests\Exception\Http\Status404}) * @param boolean $allow_redirects Set to false to throw on a 3xx as well */ public function throw_for_status($allow_redirects = true) { diff --git a/tests/Transport/BaseTestCase.php b/tests/Transport/BaseTestCase.php index 1ed068c5f..c2f4c991f 100644 --- a/tests/Transport/BaseTestCase.php +++ b/tests/Transport/BaseTestCase.php @@ -4,9 +4,9 @@ use Requests\Tests\Mock\TransportMock; use Requests\Tests\TestCase; -use Requests_Exception_HTTP_Unknown; use stdClass; use WpOrg\Requests\Exception; +use WpOrg\Requests\Exception\Http\StatusUnknown; use WpOrg\Requests\Exception\InvalidArgument; use WpOrg\Requests\Hooks; use WpOrg\Requests\Requests; @@ -494,7 +494,7 @@ public function testStatusCodeThrow($code, $success) { if (!$success) { if ($code >= 400) { - $this->expectException('Requests_Exception_HTTP_' . $code); + $this->expectException('\WpOrg\Requests\Exception\Http\Status' . $code); $this->expectExceptionCode($code); } elseif ($code >= 300 && $code < 400) { @@ -524,7 +524,7 @@ public function testStatusCodeThrowAllowRedirects($code, $success) { if (!$success) { if ($code >= 400 || $code === 304 || $code === 305 || $code === 306) { - $this->expectException('Requests_Exception_HTTP_' . $code); + $this->expectException('\WpOrg\Requests\Exception\Http\Status' . $code); $this->expectExceptionCode($code); } } @@ -558,7 +558,7 @@ public function testStatusCodeThrowUnknown() { ); $request = Requests::get(httpbin('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/status/599'), array(), $options); - $this->expectException(Requests_Exception_HTTP_Unknown::class); + $this->expectException(StatusUnknown::class); $this->expectExceptionMessage('599 Unknown'); $request->throw_for_status(true); } From 7a2d2b3ab24886f8ee4106505b1631eee9e814a6 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 26 Aug 2021 15:38:51 +0200 Subject: [PATCH 36/45] Remove support for PSR-0 autoloading ... as Requests now no longer contains any PSR-0 classes. PSR-0 classes are still supported in the backward-compatibility layer, which means that: * When using Composer for the autoloading, the PSR-0 classes will be made available via a classmap. * When using the Requests native custom autoloading, the PSR-0 classes will be aliased to their PSR-4 equivalent. --- composer.json | 5 +---- src/Autoload.php | 6 +----- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 9e40532e7..f45964065 100644 --- a/composer.json +++ b/composer.json @@ -51,13 +51,10 @@ }, "type": "library", "autoload": { - "psr-0": { - "Requests": "library/" - }, "psr-4": { "WpOrg\\Requests\\": "src/" }, - "classmap": ["library/Deprecated.php"] + "classmap": ["library/Deprecated.php", "library/Requests.php"] }, "autoload-dev": { "psr-4": { diff --git a/src/Autoload.php b/src/Autoload.php index bee7de04d..85d339827 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -139,10 +139,9 @@ public static function register() { */ public static function load($class_name) { // Check that the class starts with "Requests" (PSR-0) or "WpOrg\Requests" (PSR-4). - $psr_0_prefix_pos = stripos($class_name, 'Requests'); $psr_4_prefix_pos = stripos($class_name, 'WpOrg\\Requests\\'); - if ($psr_0_prefix_pos !== 0 && $psr_4_prefix_pos !== 0) { + if (stripos($class_name, 'Requests') !== 0 && $psr_4_prefix_pos !== 0) { return false; } @@ -153,9 +152,6 @@ public static function load($class_name) { */ $file = dirname(__DIR__) . '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/library/Requests.php'; self::$deprecation_thrown = true; - } elseif ($psr_0_prefix_pos === 0) { - // PSR-0 classname. - $file = dirname(__DIR__) . '/library/' . str_replace('_', '/', $class_name) . '.php'; } elseif ($psr_4_prefix_pos === 0) { // PSR-4 classname. $file = __DIR__ . '/' . strtr(substr($class_name, 15), '\\', '/') . '.php'; From 82c1cda5f5f716a7d303943d41f6d4594758e9e1 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 23 Aug 2021 16:17:54 +0200 Subject: [PATCH 37/45] Tests: undo the temporarily silencing of deprecation warnings ... as all classes are now properly namespaced. Also: switch to the new `WpOrg\Requests\Autoload` class for the autoloading of the files when using a PHPUnit PHAR file to run the tests. --- tests/bootstrap.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index c2a9c74ca..81d4c0369 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -21,8 +21,6 @@ function define_from_env($name, $default = false) { define_from_env('REQUESTS_HTTP_PROXY_AUTH_USER'); define_from_env('REQUESTS_HTTP_PROXY_AUTH_PASS'); -// Temporarily silence the PSR-0 deprecations while the code is being switched to PSR-4. -define('REQUESTS_SILENCE_PSR0_DEPRECATIONS', true); if (is_dir(dirname(__DIR__) . '/vendor') && file_exists(dirname(__DIR__) . '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/vendor/autoload.php') @@ -43,8 +41,8 @@ function define_from_env($name, $default = false) { require_once $vendor_dir . '/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php'; // Load the library autoloader. - require_once dirname(__DIR__) . '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/library/Requests.php'; - Requests::register_autoloader(); + require_once dirname(__DIR__) . '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/src/Autoload.php'; + WpOrg\Requests\Autoload::register(); /* * Autoloader specifically for the test files. From 74fa7866be2fae9063f9faacd943b960d0a9289b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 22 Aug 2021 20:17:07 +0200 Subject: [PATCH 38/45] Document the library directory --- library/README.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 library/README.md diff --git a/library/README.md b/library/README.md new file mode 100644 index 000000000..d492c0f89 --- /dev/null +++ b/library/README.md @@ -0,0 +1,6 @@ +## The Library directory is deprecated. + +The files in this directory are only still in place to provide backward compatibility with Requests 1.x. +Please see the release notes of Requests 2.0.0 on how to upgrade. + +This directory will be removed in Requests v 4.0.0. From 8f8c4dc468276b545ddcc7435c4c02fd836e81c7 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 23 Aug 2021 06:29:49 +0200 Subject: [PATCH 39/45] Documentation: add page about upgrading to Requests 2.0 --- docs/README.md | 2 + docs/hooks.md | 2 + docs/upgrading.md | 110 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 docs/upgrading.md diff --git a/docs/README.md b/docs/README.md index f1a98344e..65975c52a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -17,6 +17,7 @@ here are prose; you might also want to check out the [API documentation][]. * [Custom authentication][authentication-custom] * [Requests through proxy][proxy] * [Hooking system][hooks] +* [Upgrading to Requests 2.0][upgrading] [goals]: goals.md [why-requests]: why-requests.md @@ -26,3 +27,4 @@ here are prose; you might also want to check out the [API documentation][]. [authentication-custom]: authentication-custom.md [hooks]: hooks.md [proxy]: proxy.md +[upgrading]: upgrading.md diff --git a/docs/hooks.md b/docs/hooks.md index 1e0cc455a..70585aa5e 100644 --- a/docs/hooks.md +++ b/docs/hooks.md @@ -101,3 +101,5 @@ $request = WpOrg\Requests\Requests::get('https://httpbin.org/get', array(), arra *** Previous: [Requests through proxy](proxy.md) + +Next: [Upgrading to Requests 2.0](upgrading.md) diff --git a/docs/upgrading.md b/docs/upgrading.md new file mode 100644 index 000000000..8361fb72e --- /dev/null +++ b/docs/upgrading.md @@ -0,0 +1,110 @@ +Upgrading to Requests 2.0 +========================= + +Autoloading the Requests library +-------------------------------- + +In Requests 1.x, both a custom autoloader native to Requests, as well as autoloading +via the Composer `vendor/autoload.php` file was supported. + +The same applies to Requests 2.x, though the way to initiate the Requests native +custom autoloader has changed. + +If you were autoloading the Requests library via the Composer autoload file, +no changes are needed. + +```php +// OLD: Using the custom autoloader in Requests 1.x. +require_once 'path/to/Requests/library/Requests.php'; +Requests::register_autoloader(); + +// NEW: Using the custom autoloader in Requests 2.x. +require_once 'path/to/Requests/src/Autoload.php'; +WpOrg\Requests\Autoload::register(); +``` + +For backward compatibility reasons, the Requests 1.x manner to call the autoloader +will still be supported for the time being, but it is recommended to update +your code to use the new autoloader. + + +New namespaced names +-------------------- + +As of Requests 2.0.0, all code in the Requests library is namespaced and +class/interface names have been normalized to CamelCaps. + +We recommend for all users to upgrade their code to use the new namespaced +class names. + +| Type | Request 1.x name | Requests >= 2.0 name | +|-----------|----------------------------------------------|-------------------------------------------------------------------------| +| class | `Requests` | `WpOrg\Requests\Requests` 1 | +| interface | `Requests_Auth` | `WpOrg\Requests\Auth` | +| interface | `Requests_Hooker` | `WpOrg\Requests\Hooker` | +| interface | `Requests_Transport` | `WpOrg\Requests\Transport` | +| interface | `Requests_Proxy` | `WpOrg\Requests\Proxy` | +| class | `Requests_Cookie` | `WpOrg\Requests\Cookie` | +| class | `Requests_Exception` | `WpOrg\Requests\Exception` | +| class | `Requests_Hooks` | `WpOrg\Requests\Hooks` | +| class | `Requests_IDNAEncoder` | `WpOrg\Requests\IdnaEncoder` | +| class | `Requests_IPv6` | `WpOrg\Requests\Ipv6` | +| class | `Requests_IRI` | `WpOrg\Requests\Iri` | +| class | `Requests_Response` | `WpOrg\Requests\Response` | +| class | `Requests_Session` | `WpOrg\Requests\Session` | +| class | `Requests_SSL` | `WpOrg\Requests\Ssl` | +| class | `Requests_Auth_Basic` | `WpOrg\Requests\Auth\Basic` | +| class | `Requests_Cookie_Jar` | `WpOrg\Requests\Cookie\Jar` | +| class | `Requests_Proxy_HTTP` | `WpOrg\Requests\Proxy\Http` | +| class | `Requests_Response_Headers` | `WpOrg\Requests\Response\Headers` | +| class | `Requests_Transport_cURL` | `WpOrg\Requests\Transport\Curl` | +| class | `Requests_Transport_fsockopen` | `WpOrg\Requests\Transport\Fsockopen` | +| class | `Requests_Utility_CaseInsensitiveDictionary` | `WpOrg\Requests\Utility\CaseInsensitiveDictionary` | +| class | `Requests_Utility_FilteredIterator` | `WpOrg\Requests\Utility\FilteredIterator` | +| class | `Requests_Exception_HTTP` | `WpOrg\Requests\Exception\Http` | +| class | `Requests_Exception_Transport` | `WpOrg\Requests\Exception\Transport` | +| class | `Requests_Exception_Transport_cURL` | `WpOrg\Requests\Exception\Transport\Curl` | +| class | `Requests_Exception_HTTP_###` | `WpOrg\Requests\Exception\Http\Status###` 2 | +| class | `Requests_Exception_HTTP_Unknown` | `WpOrg\Requests\Exception\Http\StatusUnknown` | + + +**Notes**: + +1 The original `Requests` class has been split into two classes: +* `WpOrg\Requests\Requests` contains the actual functionality; +* `WpOrg\Requests\Autoload` contains the custom autoloader. +* The original non-namespaced `Requests` class remains for backward-compatibility + reasons and extends `WpOrg\Requests\Requests` and uses `WpOrg\Requests\Autoload` + in the old autoload functions, `Requests::autoloader()` and + `Requests::register_autoloader()`. + +2 The `###` in the HTTP Exception classes represents a three character, +numeric HTTP status code. +Exception classes are available for HTTP status 304 - 306, 400 - 418, +428, 429, 431, 500 - 505 and 511. + + +Backward compatibility layer +---------------------------- + +If for some reason, you can not yet upgrade or have to support both Requests 1.x +as well as Requests >= 2.0, a backward compatibility layer is provided. +This backward compatibility layer will work with both the Requests native +custom autoloader, as well as when using the Composer `vendor/autoload.php` file. + +Using the old PSR-0 names will still work, but a deprecation notice will be +thrown the first time during a page load when a PSR-0 class name is referenced. + +For the lifetime of Requests 2.x, the deprecation notices can be disabled by +defining a global `REQUESTS_SILENCE_PSR0_DEPRECATIONS` constant and +setting the value of this constant to `true`. +The constant needs to be defined before the first PSR-0 class name is requested. + +As of Requests 3.0, support for the constant will be removed and +the deprecation notice will always be thrown. + +As of Requests 4.0, the backward compatibility layer will be removed. + +*** + +Previous: [Hooks](hooks.md) From 6580293945da676eff660d079b4f81d4860f58fa Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 27 Aug 2021 06:55:55 +0200 Subject: [PATCH 40/45] Code coverage: update configuration ... to point to the `src` instead of the `library` directory. --- .codecov.yml | 4 ++-- phpunit.xml.dist | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.codecov.yml b/.codecov.yml index 169e0d091..18782759f 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -11,11 +11,11 @@ coverage: default: threshold: 1% paths: - - "library" + - "src" patch: default: threshold: 3% paths: - - "library" + - "src" comment: false diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d14f496a5..16cba6d73 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -19,7 +19,7 @@ - library + src From 449dfb173a9236e61d7315d1e47c4cc7c2b43a80 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 27 Aug 2021 06:58:23 +0200 Subject: [PATCH 41/45] README: update external class loader example code Note: the Symfony Class Loader package is deprecated and was removed from Symfony in version 4.0, so we may want to replace this example with one using a still maintained external library. --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 28cfa6cd4..57257201e 100644 --- a/README.md +++ b/README.md @@ -104,13 +104,15 @@ $ wget https://github.com/WordPress/Requests/tarball/stable -O - | tar xzv ### Using a Class Loader If you're using a class loader (e.g., [Symfony Class Loader][]) for -[PSR-0][]-style class loading: +[PSR-4][]-style class loading: ```php -$loader->registerPrefix('Requests', 'path/to/vendor/Requests/library'); +$loader = new Psr4ClassLoader(); +$loader->addPrefix('WpOrg\\Requests\\', 'path/to/vendor/Requests/src'); +$loader->register(); ``` [Symfony Class Loader]: https://github.com/symfony/ClassLoader -[PSR-0]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md +[PSR-4]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4.md Documentation From 69494d13571dca7d6d7fe4f24f6ca4530ab6b793 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 30 Aug 2021 03:46:43 +0200 Subject: [PATCH 42/45] Deprecated classes classmap file: wrap each class/interface declaration in an "exists" check This fixes a potential fatal "class already exists" error when during one "page load": * Two projects both using Requests are used. * Neither of these projects uses Requests with a custom namespace/prefix as can be done via PHP_Scoper. * The first of these projects loaded is still using Requests 1.x and is using Composer autoload. * The second project is using Requests 2.x and is also using Composer autoload, In that case, depending on the order of the registered autoloaders, the `Deprecated.php` file _may_ be called while _some_ Requests 1.x have already been autoloaded, but not all. Bulk declaring the deprecated classes will then lead to a "Fatal error: Cannot declare interface/class Requests_..., because the name is already in use" error. Using `class_exists()` wrappers will prevent this. In addition to this, the conditions also check for the existance of the "new" classes to prevent potential fatal errors when only a selection of the classes from this library are used/available within an external project. --- library/Deprecated.php | 228 ++++++++++++++++++++++++++++++----------- 1 file changed, 171 insertions(+), 57 deletions(-) diff --git a/library/Deprecated.php b/library/Deprecated.php index 2530bb812..42a297047 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -27,63 +27,177 @@ ); } -interface Requests_Auth extends WpOrg\Requests\Auth {} -interface Requests_Hooker extends WpOrg\Requests\Hooker {} -interface Requests_Proxy extends WpOrg\Requests\Proxy {} -interface Requests_Transport extends WpOrg\Requests\Transport {} +if (interface_exists('Requests_Auth') === false && interface_exists('WpOrg\Requests\Auth') === true) { + interface Requests_Auth extends WpOrg\Requests\Auth {} +} +if (interface_exists('Requests_Hooker') === false && interface_exists('WpOrg\Requests\Hooker') === true) { + interface Requests_Hooker extends WpOrg\Requests\Hooker {} +} +if (interface_exists('Requests_Proxy') === false && interface_exists('WpOrg\Requests\Proxy') === true) { + interface Requests_Proxy extends WpOrg\Requests\Proxy {} +} +if (interface_exists('Requests_Transport') === false && interface_exists('WpOrg\Requests\Transport') === true) { + interface Requests_Transport extends WpOrg\Requests\Transport {} +} -class Requests_Cookie extends WpOrg\Requests\Cookie {} -class Requests_Exception extends WpOrg\Requests\Exception {} -class Requests_Hooks extends WpOrg\Requests\Hooks {} -class Requests_IDNAEncoder extends WpOrg\Requests\IdnaEncoder {} -class Requests_IPv6 extends WpOrg\Requests\Ipv6 {} -class Requests_IRI extends WpOrg\Requests\Iri {} -class Requests_Response extends WpOrg\Requests\Response {} -class Requests_Session extends WpOrg\Requests\Session {} -class Requests_SSL extends WpOrg\Requests\Ssl {} +if (class_exists('Requests_Cookie') === false && class_exists('WpOrg\Requests\Cookie') === true) { + class Requests_Cookie extends WpOrg\Requests\Cookie {} +} +if (class_exists('Requests_Exception') === false && class_exists('WpOrg\Requests\Exception') === true) { + class Requests_Exception extends WpOrg\Requests\Exception {} +} +if (class_exists('Requests_Hooks') === false && class_exists('WpOrg\Requests\Hooks') === true) { + class Requests_Hooks extends WpOrg\Requests\Hooks {} +} +if (class_exists('Requests_IDNAEncoder') === false && class_exists('WpOrg\Requests\IdnaEncoder') === true) { + class Requests_IDNAEncoder extends WpOrg\Requests\IdnaEncoder {} +} +if (class_exists('Requests_IPv6') === false && class_exists('WpOrg\Requests\Ipv6') === true) { + class Requests_IPv6 extends WpOrg\Requests\Ipv6 {} +} +if (class_exists('Requests_IRI') === false && class_exists('WpOrg\Requests\Iri') === true) { + class Requests_IRI extends WpOrg\Requests\Iri {} +} +if (class_exists('Requests_Response') === false && class_exists('WpOrg\Requests\Response') === true) { + class Requests_Response extends WpOrg\Requests\Response {} +} +if (class_exists('Requests_Session') === false && class_exists('WpOrg\Requests\Session') === true) { + class Requests_Session extends WpOrg\Requests\Session {} +} +if (class_exists('Requests_SSL') === false && class_exists('WpOrg\Requests\Ssl') === true) { + class Requests_SSL extends WpOrg\Requests\Ssl {} +} -class Requests_Auth_Basic extends WpOrg\Requests\Auth\Basic {} -class Requests_Cookie_Jar extends WpOrg\Requests\Cookie\Jar {} -class Requests_Proxy_HTTP extends WpOrg\Requests\Proxy\Http {} -class Requests_Response_Headers extends WpOrg\Requests\Response\Headers {} -class Requests_Transport_cURL extends WpOrg\Requests\Transport\Curl {} -class Requests_Transport_fsockopen extends WpOrg\Requests\Transport\Fsockopen {} -class Requests_Utility_CaseInsensitiveDictionary extends WpOrg\Requests\Utility\CaseInsensitiveDictionary {} -class Requests_Utility_FilteredIterator extends WpOrg\Requests\Utility\FilteredIterator {} +if (class_exists('Requests_Auth_Basic') === false && class_exists('WpOrg\Requests\Auth\Basic') === true) { + class Requests_Auth_Basic extends WpOrg\Requests\Auth\Basic {} +} +if (class_exists('Requests_Cookie_Jar') === false && class_exists('WpOrg\Requests\Cookie\Jar') === true) { + class Requests_Cookie_Jar extends WpOrg\Requests\Cookie\Jar {} +} +if (class_exists('Requests_Proxy_HTTP') === false && class_exists('WpOrg\Requests\Proxy\Http') === true) { + class Requests_Proxy_HTTP extends WpOrg\Requests\Proxy\Http {} +} +if (class_exists('Requests_Response_Headers') === false && class_exists('WpOrg\Requests\Response\Headers') === true) { + class Requests_Response_Headers extends WpOrg\Requests\Response\Headers {} +} +if (class_exists('Requests_Transport_cURL') === false && class_exists('WpOrg\Requests\Transport\Curl') === true) { + class Requests_Transport_cURL extends WpOrg\Requests\Transport\Curl {} +} +if (class_exists('Requests_Transport_fsockopen') === false && class_exists('WpOrg\Requests\Transport\Fsockopen') === true) { + class Requests_Transport_fsockopen extends WpOrg\Requests\Transport\Fsockopen {} +} +if (class_exists('Requests_Utility_CaseInsensitiveDictionary') === false && class_exists('WpOrg\Requests\Utility\CaseInsensitiveDictionary') === true) { + class Requests_Utility_CaseInsensitiveDictionary extends WpOrg\Requests\Utility\CaseInsensitiveDictionary {} +} +if (class_exists('Requests_Utility_FilteredIterator') === false && class_exists('WpOrg\Requests\Utility\FilteredIterator') === true) { + class Requests_Utility_FilteredIterator extends WpOrg\Requests\Utility\FilteredIterator {} +} -class Requests_Exception_HTTP extends WpOrg\Requests\Exception\Http {} -class Requests_Exception_Transport extends WpOrg\Requests\Exception\Transport {} -class Requests_Exception_Transport_cURL extends WpOrg\Requests\Exception\Transport\Curl {} -class Requests_Exception_HTTP_304 extends WpOrg\Requests\Exception\Http\Status304 {} -class Requests_Exception_HTTP_305 extends WpOrg\Requests\Exception\Http\Status305 {} -class Requests_Exception_HTTP_306 extends WpOrg\Requests\Exception\Http\Status306 {} -class Requests_Exception_HTTP_400 extends WpOrg\Requests\Exception\Http\Status400 {} -class Requests_Exception_HTTP_401 extends WpOrg\Requests\Exception\Http\Status401 {} -class Requests_Exception_HTTP_402 extends WpOrg\Requests\Exception\Http\Status402 {} -class Requests_Exception_HTTP_403 extends WpOrg\Requests\Exception\Http\Status403 {} -class Requests_Exception_HTTP_404 extends WpOrg\Requests\Exception\Http\Status404 {} -class Requests_Exception_HTTP_405 extends WpOrg\Requests\Exception\Http\Status405 {} -class Requests_Exception_HTTP_406 extends WpOrg\Requests\Exception\Http\Status406 {} -class Requests_Exception_HTTP_407 extends WpOrg\Requests\Exception\Http\Status407 {} -class Requests_Exception_HTTP_408 extends WpOrg\Requests\Exception\Http\Status408 {} -class Requests_Exception_HTTP_409 extends WpOrg\Requests\Exception\Http\Status409 {} -class Requests_Exception_HTTP_410 extends WpOrg\Requests\Exception\Http\Status410 {} -class Requests_Exception_HTTP_411 extends WpOrg\Requests\Exception\Http\Status411 {} -class Requests_Exception_HTTP_412 extends WpOrg\Requests\Exception\Http\Status412 {} -class Requests_Exception_HTTP_413 extends WpOrg\Requests\Exception\Http\Status413 {} -class Requests_Exception_HTTP_414 extends WpOrg\Requests\Exception\Http\Status414 {} -class Requests_Exception_HTTP_415 extends WpOrg\Requests\Exception\Http\Status415 {} -class Requests_Exception_HTTP_416 extends WpOrg\Requests\Exception\Http\Status416 {} -class Requests_Exception_HTTP_417 extends WpOrg\Requests\Exception\Http\Status417 {} -class Requests_Exception_HTTP_418 extends WpOrg\Requests\Exception\Http\Status418 {} -class Requests_Exception_HTTP_428 extends WpOrg\Requests\Exception\Http\Status428 {} -class Requests_Exception_HTTP_429 extends WpOrg\Requests\Exception\Http\Status429 {} -class Requests_Exception_HTTP_431 extends WpOrg\Requests\Exception\Http\Status431 {} -class Requests_Exception_HTTP_500 extends WpOrg\Requests\Exception\Http\Status500 {} -class Requests_Exception_HTTP_501 extends WpOrg\Requests\Exception\Http\Status501 {} -class Requests_Exception_HTTP_502 extends WpOrg\Requests\Exception\Http\Status502 {} -class Requests_Exception_HTTP_503 extends WpOrg\Requests\Exception\Http\Status503 {} -class Requests_Exception_HTTP_504 extends WpOrg\Requests\Exception\Http\Status504 {} -class Requests_Exception_HTTP_505 extends WpOrg\Requests\Exception\Http\Status505 {} -class Requests_Exception_HTTP_511 extends WpOrg\Requests\Exception\Http\Status511 {} -class Requests_Exception_HTTP_Unknown extends WpOrg\Requests\Exception\Http\StatusUnknown {} +if (class_exists('Requests_Exception_HTTP') === false && class_exists('WpOrg\Requests\Exception\Http') === true) { + class Requests_Exception_HTTP extends WpOrg\Requests\Exception\Http {} +} +if (class_exists('Requests_Exception_Transport') === false && class_exists('WpOrg\Requests\Exception\Transport') === true) { + class Requests_Exception_Transport extends WpOrg\Requests\Exception\Transport {} +} +if (class_exists('Requests_Exception_Transport_cURL') === false && class_exists('WpOrg\Requests\Exception\Transport\Curl') === true) { + class Requests_Exception_Transport_cURL extends WpOrg\Requests\Exception\Transport\Curl {} +} +if (class_exists('Requests_Exception_HTTP_304') === false && class_exists('WpOrg\Requests\Exception\Http\Status304') === true) { + class Requests_Exception_HTTP_304 extends WpOrg\Requests\Exception\Http\Status304 {} +} +if (class_exists('Requests_Exception_HTTP_305') === false && class_exists('WpOrg\Requests\Exception\Http\Status305') === true) { + class Requests_Exception_HTTP_305 extends WpOrg\Requests\Exception\Http\Status305 {} +} +if (class_exists('Requests_Exception_HTTP_306') === false && class_exists('WpOrg\Requests\Exception\Http\Status306') === true) { + class Requests_Exception_HTTP_306 extends WpOrg\Requests\Exception\Http\Status306 {} +} +if (class_exists('Requests_Exception_HTTP_400') === false && class_exists('WpOrg\Requests\Exception\Http\Status400') === true) { + class Requests_Exception_HTTP_400 extends WpOrg\Requests\Exception\Http\Status400 {} +} +if (class_exists('Requests_Exception_HTTP_401') === false && class_exists('WpOrg\Requests\Exception\Http\Status401') === true) { + class Requests_Exception_HTTP_401 extends WpOrg\Requests\Exception\Http\Status401 {} +} +if (class_exists('Requests_Exception_HTTP_402') === false && class_exists('WpOrg\Requests\Exception\Http\Status402') === true) { + class Requests_Exception_HTTP_402 extends WpOrg\Requests\Exception\Http\Status402 {} +} +if (class_exists('Requests_Exception_HTTP_403') === false && class_exists('WpOrg\Requests\Exception\Http\Status403') === true) { + class Requests_Exception_HTTP_403 extends WpOrg\Requests\Exception\Http\Status403 {} +} +if (class_exists('Requests_Exception_HTTP_404') === false && class_exists('WpOrg\Requests\Exception\Http\Status404') === true) { + class Requests_Exception_HTTP_404 extends WpOrg\Requests\Exception\Http\Status404 {} +} +if (class_exists('Requests_Exception_HTTP_405') === false && class_exists('WpOrg\Requests\Exception\Http\Status405') === true) { + class Requests_Exception_HTTP_405 extends WpOrg\Requests\Exception\Http\Status405 {} +} +if (class_exists('Requests_Exception_HTTP_406') === false && class_exists('WpOrg\Requests\Exception\Http\Status406') === true) { + class Requests_Exception_HTTP_406 extends WpOrg\Requests\Exception\Http\Status406 {} +} +if (class_exists('Requests_Exception_HTTP_407') === false && class_exists('WpOrg\Requests\Exception\Http\Status407') === true) { + class Requests_Exception_HTTP_407 extends WpOrg\Requests\Exception\Http\Status407 {} +} +if (class_exists('Requests_Exception_HTTP_408') === false && class_exists('WpOrg\Requests\Exception\Http\Status408') === true) { + class Requests_Exception_HTTP_408 extends WpOrg\Requests\Exception\Http\Status408 {} +} +if (class_exists('Requests_Exception_HTTP_409') === false && class_exists('WpOrg\Requests\Exception\Http\Status409') === true) { + class Requests_Exception_HTTP_409 extends WpOrg\Requests\Exception\Http\Status409 {} +} +if (class_exists('Requests_Exception_HTTP_410') === false && class_exists('WpOrg\Requests\Exception\Http\Status410') === true) { + class Requests_Exception_HTTP_410 extends WpOrg\Requests\Exception\Http\Status410 {} +} +if (class_exists('Requests_Exception_HTTP_411') === false && class_exists('WpOrg\Requests\Exception\Http\Status411') === true) { + class Requests_Exception_HTTP_411 extends WpOrg\Requests\Exception\Http\Status411 {} +} +if (class_exists('Requests_Exception_HTTP_412') === false && class_exists('WpOrg\Requests\Exception\Http\Status412') === true) { + class Requests_Exception_HTTP_412 extends WpOrg\Requests\Exception\Http\Status412 {} +} +if (class_exists('Requests_Exception_HTTP_413') === false && class_exists('WpOrg\Requests\Exception\Http\Status413') === true) { + class Requests_Exception_HTTP_413 extends WpOrg\Requests\Exception\Http\Status413 {} +} +if (class_exists('Requests_Exception_HTTP_414') === false && class_exists('WpOrg\Requests\Exception\Http\Status414') === true) { + class Requests_Exception_HTTP_414 extends WpOrg\Requests\Exception\Http\Status414 {} +} +if (class_exists('Requests_Exception_HTTP_415') === false && class_exists('WpOrg\Requests\Exception\Http\Status415') === true) { + class Requests_Exception_HTTP_415 extends WpOrg\Requests\Exception\Http\Status415 {} +} +if (class_exists('Requests_Exception_HTTP_416') === false && class_exists('WpOrg\Requests\Exception\Http\Status416') === true) { + class Requests_Exception_HTTP_416 extends WpOrg\Requests\Exception\Http\Status416 {} +} +if (class_exists('Requests_Exception_HTTP_417') === false && class_exists('WpOrg\Requests\Exception\Http\Status417') === true) { + class Requests_Exception_HTTP_417 extends WpOrg\Requests\Exception\Http\Status417 {} +} +if (class_exists('Requests_Exception_HTTP_418') === false && class_exists('WpOrg\Requests\Exception\Http\Status418') === true) { + class Requests_Exception_HTTP_418 extends WpOrg\Requests\Exception\Http\Status418 {} +} +if (class_exists('Requests_Exception_HTTP_428') === false && class_exists('WpOrg\Requests\Exception\Http\Status428') === true) { + class Requests_Exception_HTTP_428 extends WpOrg\Requests\Exception\Http\Status428 {} +} +if (class_exists('Requests_Exception_HTTP_429') === false && class_exists('WpOrg\Requests\Exception\Http\Status429') === true) { + class Requests_Exception_HTTP_429 extends WpOrg\Requests\Exception\Http\Status429 {} +} +if (class_exists('Requests_Exception_HTTP_431') === false && class_exists('WpOrg\Requests\Exception\Http\Status431') === true) { + class Requests_Exception_HTTP_431 extends WpOrg\Requests\Exception\Http\Status431 {} +} +if (class_exists('Requests_Exception_HTTP_500') === false && class_exists('WpOrg\Requests\Exception\Http\Status500') === true) { + class Requests_Exception_HTTP_500 extends WpOrg\Requests\Exception\Http\Status500 {} +} +if (class_exists('Requests_Exception_HTTP_501') === false && class_exists('WpOrg\Requests\Exception\Http\Status501') === true) { + class Requests_Exception_HTTP_501 extends WpOrg\Requests\Exception\Http\Status501 {} +} +if (class_exists('Requests_Exception_HTTP_502') === false && class_exists('WpOrg\Requests\Exception\Http\Status502') === true) { + class Requests_Exception_HTTP_502 extends WpOrg\Requests\Exception\Http\Status502 {} +} +if (class_exists('Requests_Exception_HTTP_503') === false && class_exists('WpOrg\Requests\Exception\Http\Status503') === true) { + class Requests_Exception_HTTP_503 extends WpOrg\Requests\Exception\Http\Status503 {} +} +if (class_exists('Requests_Exception_HTTP_504') === false && class_exists('WpOrg\Requests\Exception\Http\Status504') === true) { + class Requests_Exception_HTTP_504 extends WpOrg\Requests\Exception\Http\Status504 {} +} +if (class_exists('Requests_Exception_HTTP_505') === false && class_exists('WpOrg\Requests\Exception\Http\Status505') === true) { + class Requests_Exception_HTTP_505 extends WpOrg\Requests\Exception\Http\Status505 {} +} +if (class_exists('Requests_Exception_HTTP_511') === false && class_exists('WpOrg\Requests\Exception\Http\Status511') === true) { + class Requests_Exception_HTTP_511 extends WpOrg\Requests\Exception\Http\Status511 {} +} +if (class_exists('Requests_Exception_HTTP_Unknown') === false && class_exists('WpOrg\Requests\Exception\Http\StatusUnknown') === true) { + class Requests_Exception_HTTP_Unknown extends WpOrg\Requests\Exception\Http\StatusUnknown {} +} From aad891a85ee6a6bb101c6ecd2726e22f99dddcd3 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 30 Aug 2021 05:09:35 +0200 Subject: [PATCH 43/45] Tests: add minimal test to verify that deprecation notices are thrown ... when the old PSR-0 classes are being autoloaded. --- tests/AutoloadTest.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/AutoloadTest.php diff --git a/tests/AutoloadTest.php b/tests/AutoloadTest.php new file mode 100644 index 000000000..6636de917 --- /dev/null +++ b/tests/AutoloadTest.php @@ -0,0 +1,31 @@ +expectDeprecation(); + $this->expectDeprecationMessage(self::MSG); + + require_once dirname(__DIR__) . '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/library/Requests.php'; + } + + /** + * Verify that a deprecation notice is thrown when one of the other "old" Requests classes is autoloaded. + */ + public function testDeprecationNoticeThrownForOtherOldRequestsClass() { + $this->expectDeprecation(); + $this->expectDeprecationMessage(self::MSG); + + $this->assertSame('cURLEasy', Requests_Exception_Transport_cURL::EASY); + } +} From 84efc00ff800b62dcca0ebd3dad83347aa7f285b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 11 Sep 2021 04:24:04 +0200 Subject: [PATCH 44/45] Tests: rename the namespace ... from `Requests\Tests` to `WpOrg\Requests\Tests` for consistency. --- composer.json | 2 +- tests/Auth/BasicTest.php | 4 ++-- tests/AutoloadTest.php | 4 ++-- tests/ChunkedEncodingTest.php | 6 +++--- tests/CookiesTest.php | 4 ++-- tests/EncodingTest.php | 4 ++-- tests/IdnaEncoderTest.php | 4 ++-- tests/IriTest.php | 4 ++-- tests/Mock/RawTransportMock.php | 2 +- tests/Mock/TransportMock.php | 2 +- tests/Proxy/HttpTest.php | 4 ++-- tests/RequestsTest.php | 8 ++++---- tests/Response/HeadersTest.php | 4 ++-- tests/SessionTest.php | 4 ++-- tests/SslTest.php | 4 ++-- tests/TestCase.php | 2 +- tests/Transport/BaseTestCase.php | 6 +++--- tests/Transport/CurlTest.php | 4 ++-- tests/Transport/FsockopenTest.php | 4 ++-- tests/Utility/FilteredIteratorTest.php | 4 ++-- tests/bootstrap.php | 6 +++--- 21 files changed, 43 insertions(+), 43 deletions(-) diff --git a/composer.json b/composer.json index f45964065..48301f5b2 100644 --- a/composer.json +++ b/composer.json @@ -58,7 +58,7 @@ }, "autoload-dev": { "psr-4": { - "Requests\\Tests\\": "tests/" + "WpOrg\\Requests\\Tests\\": "tests/" } }, "scripts": { diff --git a/tests/Auth/BasicTest.php b/tests/Auth/BasicTest.php index e91fa9552..3714062e4 100644 --- a/tests/Auth/BasicTest.php +++ b/tests/Auth/BasicTest.php @@ -1,11 +1,11 @@ Date: Fri, 17 Sep 2021 09:43:35 +0200 Subject: [PATCH 45/45] Deprecation notices: declare the constant if thrown ... to prevent there ever being more than one deprecation notice in one page load. --- library/Deprecated.php | 5 ++++ library/Requests.php | 5 ++++ src/Autoload.php | 58 ++++++++++++++---------------------------- 3 files changed, 29 insertions(+), 39 deletions(-) diff --git a/library/Deprecated.php b/library/Deprecated.php index 42a297047..39e947aa0 100644 --- a/library/Deprecated.php +++ b/library/Deprecated.php @@ -25,6 +25,11 @@ . ' Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience.', E_USER_DEPRECATED ); + + // Prevent the deprecation notice from being thrown twice. + if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS')) { + define('REQUESTS_SILENCE_PSR0_DEPRECATIONS', true); + } } if (interface_exists('Requests_Auth') === false && interface_exists('WpOrg\Requests\Auth') === true) { diff --git a/library/Requests.php b/library/Requests.php index c98a3da03..05250640f 100644 --- a/library/Requests.php +++ b/library/Requests.php @@ -23,6 +23,11 @@ . ' Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience.', E_USER_DEPRECATED ); + + // Prevent the deprecation notice from being thrown twice. + if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS')) { + define('REQUESTS_SILENCE_PSR0_DEPRECATIONS', true); + } } require_once dirname(__DIR__) . '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/src/Requests.php'; diff --git a/src/Autoload.php b/src/Autoload.php index 85d339827..4c1417cf1 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -95,20 +95,6 @@ class Autoload { 'requests_exception_http_unknown' => '\WpOrg\Requests\Exception\Http\StatusUnknown', ); - /** - * Whether or not a deprecation notice has been thrown for calling a deprecated class. - * - * @var bool - */ - private static $deprecation_thrown = false; - - /** - * Whether or not a deprecation notice should be thrown for calling a deprecated class. - * - * @var bool - */ - private static $throw_notice = true; - /** * Register the autoloader. * @@ -146,12 +132,8 @@ public static function load($class_name) { } if ($class_name === 'Requests') { - /* - * Reference to the original PSR-0 Requests class. - * Loading this class will always generate a deprecation notice. - */ - $file = dirname(__DIR__) . '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/library/Requests.php'; - self::$deprecation_thrown = true; + // Reference to the original PSR-0 Requests class. + $file = dirname(__DIR__) . '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/library/Requests.php'; } elseif ($psr_4_prefix_pos === 0) { // PSR-4 classname. $file = __DIR__ . '/' . strtr(substr($class_name, 15), '\\', '/') . '.php'; @@ -170,25 +152,23 @@ public static function load($class_name) { $class_lower = strtolower($class_name); if (isset(self::$deprecated_classes[$class_lower])) { - if (self::$throw_notice === true) { - /* - * Integrators who cannot yet upgrade to the PSR-4 class names can silence deprecations - * by defining a `REQUESTS_SILENCE_PSR0_DEPRECATIONS` constant and setting it to `true`. - * The constant needs to be defined before the first deprecated class is requested - * via this autoloader. - */ - if (defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS') && REQUESTS_SILENCE_PSR0_DEPRECATIONS === true) { - self::$throw_notice = false; - } - - if (self::$throw_notice === true && self::$deprecation_thrown === false) { - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error - trigger_error( - 'The PSR-0 `Requests_...` class names in the Request library are deprecated.' - . ' Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience.', - E_USER_DEPRECATED - ); - self::$deprecation_thrown = true; + /* + * Integrators who cannot yet upgrade to the PSR-4 class names can silence deprecations + * by defining a `REQUESTS_SILENCE_PSR0_DEPRECATIONS` constant and setting it to `true`. + * The constant needs to be defined before the first deprecated class is requested + * via this autoloader. + */ + if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS') || REQUESTS_SILENCE_PSR0_DEPRECATIONS !== true) { + // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error + trigger_error( + 'The PSR-0 `Requests_...` class names in the Request library are deprecated.' + . ' Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience.', + E_USER_DEPRECATED + ); + + // Prevent the deprecation notice from being thrown twice. + if (!defined('REQUESTS_SILENCE_PSR0_DEPRECATIONS')) { + define('REQUESTS_SILENCE_PSR0_DEPRECATIONS', true); } }