From 612fa065adafeec6a1f5654e45caa0640ecb45e2 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Mon, 25 Jul 2022 16:22:25 -0400 Subject: [PATCH 1/6] Adding parallel HTTP request support --- src/wp-includes/class-wp-http-streams.php | 2 +- src/wp-includes/class-wp-http.php | 197 +++++++++++++++++++--- src/wp-includes/http.php | 26 +-- tests/phpunit/tests/http/base.php | 73 ++++++++ 4 files changed, 259 insertions(+), 39 deletions(-) diff --git a/src/wp-includes/class-wp-http-streams.php b/src/wp-includes/class-wp-http-streams.php index a5d0694da3f28..f258c9bf2b849 100644 --- a/src/wp-includes/class-wp-http-streams.php +++ b/src/wp-includes/class-wp-http-streams.php @@ -17,7 +17,7 @@ class WP_Http_Streams { /** * Send a HTTP request to a URI using PHP Streams. * - * @see WP_Http::request() For default options descriptions. + * @see WP_Http::format_request() For default options descriptions. * * @since 2.7.0 * @since 3.7.0 Combined with the fsockopen transport and switched to stream_socket_client(). diff --git a/src/wp-includes/class-wp-http.php b/src/wp-includes/class-wp-http.php index 3dca28536e5ac..a33ced3010b35 100644 --- a/src/wp-includes/class-wp-http.php +++ b/src/wp-includes/class-wp-http.php @@ -143,10 +143,104 @@ class WP_Http { * @type int $limit_response_size Size in bytes to limit the response to. Default null. * * } - * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. + * @return array|WP_Error Array or array of arrays containing 'headers', 'body', 'response', 'cookies', 'filename'. * A WP_Error instance upon error. */ public function request( $url, $args = array() ) { + if ( is_array( $url ) ) { + $pending_requests = array(); + $responses = array(); + + foreach ( $url as $index => $request ) { + // Support an array of string URLs. + if ( ! is_array( $request ) ) { + $request = array( $request, array() ); + } + + $request = $this->format_request( $request[0], isset( $request[1] ) ? $request[1] : array() ); + + // Handle an error in the pre-request step. Also allow the request to be + // circumvented if the response was short-circuit'd. + if ( is_wp_error( $request ) || isset( $request['response'] ) ) { + $responses[ $index ] = isset( $request['response'] ) ? $request['response'] : $request; + continue; + } + + $pending_requests[ $index ] = $request; + } + + if ( ! empty( $pending_requests ) ) { + // Avoid issues where mbstring.func_overload is enabled. + mbstring_binary_safe_encoding(); + + try { + $raw_responses = Requests::request_multiple( $pending_requests ); + } catch ( Requests_Exception $e ) { + $raw_responses = new WP_Error( 'http_request_failed', $e->getMessage() ); + } + + reset_mbstring_encoding(); + + if ( is_wp_error( $raw_responses ) ) { + return $raw_responses; + } + + foreach ( $pending_requests as $index => $request ) { + $responses[ $index ] = $this->format_response( + $raw_responses[ $index ], + $request['args'], + $request['url'] + ); + } + } + + return $responses; + } + + $formatted = $this->format_request( $url, $args ); + + // Handle an error in the pre-request step. Also allow the request to be + // circumvented if the response was short-circuit'd. + if ( is_wp_error( $formatted ) || isset( $formatted['response'] ) ) { + return isset( $formatted['response'] ) ? $formatted['response'] : $formatted; + } + + // Avoid issues where mbstring.func_overload is enabled. + mbstring_binary_safe_encoding(); + + try { + $response = Requests::request( + $formatted['url'], + $formatted['headers'], + $formatted['data'], + $formatted['type'], + $formatted['options'] + ); + } catch ( Requests_Exception $e ) { + $response = new WP_Error( 'http_request_failed', $e->getMessage() ); + } + + reset_mbstring_encoding(); + + return $this->format_response( $response, $formatted['args'], $formatted['url'] ); + } + + /** + * Format a request to be passed to Requests. + * + * @param string $url URL to request. + * @param array $args Arguments to send to request. + * @return array { + * Array of formatted arguments for the request. + * + * @type string $url URL to request. + * @type array $headers Array of headers for the request. + * @type string $data Data to send with the request. + * @type string $type The type of request to make. + * @type array $options Array of options for the request. + * } + */ + protected function format_request( $url, $args ) { $defaults = array( 'method' => 'GET', /** @@ -221,6 +315,7 @@ public function request( $url, $args = array() ) { } $parsed_args = wp_parse_args( $args, $defaults ); + /** * Filters the arguments used in an HTTP request. * @@ -257,7 +352,9 @@ public function request( $url, $args = array() ) { $pre = apply_filters( 'pre_http_request', false, $parsed_args, $url ); if ( false !== $pre ) { - return $pre; + return array( + 'response' => $pre, + ); } if ( function_exists( 'wp_kses_bad_protocol' ) ) { @@ -387,24 +484,34 @@ public function request( $url, $args = array() ) { } } - // Avoid issues where mbstring.func_overload is enabled. - mbstring_binary_safe_encoding(); - - try { - $requests_response = Requests::request( $url, $headers, $data, $type, $options ); + return array( + 'args' => $parsed_args, + 'data' => $data, + 'headers' => $headers, + 'options' => $options, + 'type' => $type, + 'url' => $url, + ); + } - // Convert the response into an array. - $http_response = new WP_HTTP_Requests_Response( $requests_response, $parsed_args['filename'] ); + /** + * Format a response into the expected shape. + * + * @param Requests_Response|WP_Error $response Response to format. + * @param array $args Request arguments. + * @param string $url Request URL. + * @return array|WP_Error + */ + protected function format_response( $response, $args, $url ) { + // Convert the response into an array. + if ( ! is_wp_error( $response ) ) { + $http_response = new WP_HTTP_Requests_Response( $response, $args['filename'] ); $response = $http_response->to_array(); // Add the original object to the array. $response['http_response'] = $http_response; - } catch ( Requests_Exception $e ) { - $response = new WP_Error( 'http_request_failed', $e->getMessage() ); } - reset_mbstring_encoding(); - /** * Fires after an HTTP API response is received and before the response is returned. * @@ -416,12 +523,13 @@ public function request( $url, $args = array() ) { * @param array $parsed_args HTTP request arguments. * @param string $url The request URL. */ - do_action( 'http_api_debug', $response, 'response', 'Requests', $parsed_args, $url ); + do_action( 'http_api_debug', $response, 'response', 'Requests', $args, $url ); + if ( is_wp_error( $response ) ) { return $response; } - if ( ! $parsed_args['blocking'] ) { + if ( ! $args['blocking'] ) { return array( 'headers' => array(), 'body' => '', @@ -443,9 +551,8 @@ public function request( $url, $args = array() ) { * @param array $parsed_args HTTP request arguments. * @param string $url The request URL. */ - return apply_filters( 'http_response', $response, $parsed_args, $url ); + return apply_filters( 'http_response', $response, $args, $url ); } - /** * Normalizes cookies for using in Requests. * @@ -609,9 +716,7 @@ private function _dispatch_request( $url, $args ) { * A WP_Error instance upon error. */ public function post( $url, $args = array() ) { - $defaults = array( 'method' => 'POST' ); - $parsed_args = wp_parse_args( $args, $defaults ); - return $this->request( $url, $parsed_args ); + return $this->normalize_request_args( $url, $args, 'POST' ); } /** @@ -621,15 +726,13 @@ public function post( $url, $args = array() ) { * * @since 2.7.0 * - * @param string $url The request URL. + * @param string|array $url The request URL or array of remote requests that will be run in parallel. * @param string|array $args Optional. Override the defaults. * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. * A WP_Error instance upon error. */ public function get( $url, $args = array() ) { - $defaults = array( 'method' => 'GET' ); - $parsed_args = wp_parse_args( $args, $defaults ); - return $this->request( $url, $parsed_args ); + return $this->normalize_request_args( $url, $args, 'GET' ); } /** @@ -645,8 +748,52 @@ public function get( $url, $args = array() ) { * A WP_Error instance upon error. */ public function head( $url, $args = array() ) { - $defaults = array( 'method' => 'HEAD' ); + return $this->normalize_request_args( $url, $args, 'HEAD' ); + } + + /** + * Normalize the request arguments for a GET, HEAD, or POST request. + * + * @param string|array $url The request URL or array of remote requests that will be run in parallel. + * @param string|array $args Request arguments, optional. Override the defaults. + * @param string $method Request method to make. + * @return array Array containing 'headers', 'body', 'response', 'cookies', 'filename'. + * A WP_Error instance upon error. + */ + protected function normalize_request_args( $url, $args, $method ) { + $defaults = array( 'method' => $method ); + + // Support an array of parallel requests. + if ( is_array( $url ) ) { + $parsed_requests = array(); + + if ( ! empty( $args ) ) { + _doing_it_wrong( + __FUNCTION__, + __( 'Arguments passed to the second $args parameter are ignored when $url is an array of parallel requests.' ), + '6.0.0' + ); + } + + foreach ( $url as $i => $request ) { + // Support an array of string URLs. + if ( ! is_array( $request ) ) { + $request = array( $request, array() ); + } + + list( $request_url, $request_args ) = $request; + + $parsed_requests[ $i ] = array( + $request_url, + wp_parse_args( $request_args, $defaults ), + ); + } + + return $this->request( $parsed_requests ); + } + $parsed_args = wp_parse_args( $args, $defaults ); + return $this->request( $url, $parsed_args ); } diff --git a/src/wp-includes/http.php b/src/wp-includes/http.php index 0ef297921db91..3e019cd5516b6 100644 --- a/src/wp-includes/http.php +++ b/src/wp-includes/http.php @@ -35,7 +35,7 @@ function _wp_http_get_object() { * @since 3.6.0 * * @see wp_remote_request() For more information on the response array format. - * @see WP_Http::request() For default arguments information. + * @see WP_Http::format_request() For default arguments information. * * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. @@ -77,7 +77,7 @@ function wp_safe_remote_get( $url, $args = array() ) { * @since 3.6.0 * * @see wp_remote_request() For more information on the response array format. - * @see WP_Http::request() For default arguments information. + * @see WP_Http::format_request() For default arguments information. * * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. @@ -121,10 +121,10 @@ function wp_safe_remote_head( $url, $args = array() ) { * * @since 2.7.0 * - * @see WP_Http::request() For information on default arguments. + * @see WP_Http::format_request() For information on default arguments. * - * @param string $url URL to retrieve. - * @param array $args Optional. Request arguments. Default empty array. + * @param string|array $url URL to retrieve or array of remote requests that will be run in parallel. + * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error { * The response array or a WP_Error on failure. * @@ -153,8 +153,8 @@ function wp_remote_request( $url, $args = array() ) { * @see wp_remote_request() For more information on the response array format. * @see WP_Http::request() For default arguments information. * - * @param string $url URL to retrieve. - * @param array $args Optional. Request arguments. Default empty array. + * @param string|array $url URL to retrieve or array of remote requests that will be run in parallel. + * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. */ function wp_remote_get( $url, $args = array() ) { @@ -168,10 +168,10 @@ function wp_remote_get( $url, $args = array() ) { * @since 2.7.0 * * @see wp_remote_request() For more information on the response array format. - * @see WP_Http::request() For default arguments information. + * @see WP_Http::format_request() For default arguments information. * - * @param string $url URL to retrieve. - * @param array $args Optional. Request arguments. Default empty array. + * @param string|array $url URL to retrieve or array of remote requests that will be run in parallel. + * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. */ function wp_remote_post( $url, $args = array() ) { @@ -185,10 +185,10 @@ function wp_remote_post( $url, $args = array() ) { * @since 2.7.0 * * @see wp_remote_request() For more information on the response array format. - * @see WP_Http::request() For default arguments information. + * @see WP_Http::format_request() For default arguments information. * - * @param string $url URL to retrieve. - * @param array $args Optional. Request arguments. Default empty array. + * @param string|array $url URL to retrieve or array of remote requests that will be run in parallel. + * @param array $args Optional. Request arguments. Default empty array. * @return array|WP_Error The response or WP_Error on failure. */ function wp_remote_head( $url, $args = array() ) { diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php index ee42056bb3d36..74cb9ef384d47 100644 --- a/tests/phpunit/tests/http/base.php +++ b/tests/phpunit/tests/http/base.php @@ -513,5 +513,78 @@ public function test_url_with_double_slashes_path() { $this->assertNotWPError( $res ); } + /** + * Test parallel requests. + * + * @ticket 33055 + * @covers ::wp_remote_request + */ + public function test_parallel_request() { + $responses = wp_remote_request( + array( + 'https://wordpress.org/', + array( + $this->redirection_script . '?code=301&rt=' . 5, + array( + 'method' => 'POST', + 'redirection' => 0, + ), + ), + ), + ); + + list( $request_wp, $request_redirect ) = $responses; + + $this->skipTestOnTimeout( $request_wp ); + $this->skipTestOnTimeout( $request_redirect ); + + $this->assertNotWPError( $request_wp ); + $this->assertNotWPError( $request_redirect ); + + $this->assertSame( 200, wp_remote_retrieve_response_code( $request_wp ) ); + $this->assertSame( 301, wp_remote_retrieve_response_code( $request_redirect ) ); + } + + /** + * Test parallel requests with short circuiting. + * + * @ticket 33055 + * @covers ::wp_remote_request + */ + public function test_parallel_request_short_circuit() { + add_filter( + 'pre_http_request', + function ( $pre, $args, $url ) { + if ( 'https://login.wordpress.org/wp-login.php' === $url ) { + return array( + 'response' => array( + 'code' => 418, + ), + ); + } + + return $pre; + }, + 10, + 3 + ); + $responses = wp_remote_request( + array( + 'https://wordpress.org/', + 'https://login.wordpress.org/wp-login.php', + ) + ); + + list( $request_wp, $request_login ) = $responses; + + $this->skipTestOnTimeout( $request_wp ); + $this->skipTestOnTimeout( $request_login ); + + $this->assertNotWPError( $request_wp ); + $this->assertNotWPError( $request_login ); + + $this->assertSame( 200, wp_remote_retrieve_response_code( $request_wp ) ); + $this->assertSame( 418, wp_remote_retrieve_response_code( $request_login ) ); + } } From 061220921bafefd4180baa514f16d789363a1b6c Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Mon, 25 Jul 2022 16:37:20 -0400 Subject: [PATCH 2/6] Fix logic to allow for WP_Error --- src/wp-includes/class-wp-http.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-http.php b/src/wp-includes/class-wp-http.php index a33ced3010b35..90845a0066154 100644 --- a/src/wp-includes/class-wp-http.php +++ b/src/wp-includes/class-wp-http.php @@ -160,9 +160,12 @@ public function request( $url, $args = array() ) { $request = $this->format_request( $request[0], isset( $request[1] ) ? $request[1] : array() ); // Handle an error in the pre-request step. Also allow the request to be - // circumvented if the response was short-circuit'd. - if ( is_wp_error( $request ) || isset( $request['response'] ) ) { - $responses[ $index ] = isset( $request['response'] ) ? $request['response'] : $request; + // circumvented if the response was short-circuited. + if ( is_wp_error( $request ) ) { + $responses[ $index ] = $request; + continue; + } elseif ( isset( $request['response'] ) ) { + $responses[ $index ] = $request['response']; continue; } @@ -200,9 +203,11 @@ public function request( $url, $args = array() ) { $formatted = $this->format_request( $url, $args ); // Handle an error in the pre-request step. Also allow the request to be - // circumvented if the response was short-circuit'd. - if ( is_wp_error( $formatted ) || isset( $formatted['response'] ) ) { - return isset( $formatted['response'] ) ? $formatted['response'] : $formatted; + // circumvented if the response was short-circuited. + if ( is_wp_error( $formatted ) ) { + return $formatted; + } elseif ( isset( $formatted['response'] ) ) { + return $formatted['response']; } // Avoid issues where mbstring.func_overload is enabled. From 0d8d57563377ea4b3d7b1c9866593dcc79cde5ec Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Mon, 25 Jul 2022 16:49:00 -0400 Subject: [PATCH 3/6] Remove trailing comma for older php versions --- tests/phpunit/tests/http/base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php index 74cb9ef384d47..96015d2391156 100644 --- a/tests/phpunit/tests/http/base.php +++ b/tests/phpunit/tests/http/base.php @@ -530,7 +530,7 @@ public function test_parallel_request() { 'redirection' => 0, ), ), - ), + ) ); list( $request_wp, $request_redirect ) = $responses; From ebcd618b0f8e0272a93b0b972d37d5ed8a1f009e Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Tue, 26 Jul 2022 17:13:19 -0400 Subject: [PATCH 4/6] Adding linebreak --- src/wp-includes/class-wp-http.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/class-wp-http.php b/src/wp-includes/class-wp-http.php index 90845a0066154..98a3c6e6cf441 100644 --- a/src/wp-includes/class-wp-http.php +++ b/src/wp-includes/class-wp-http.php @@ -558,6 +558,7 @@ protected function format_response( $response, $args, $url ) { */ return apply_filters( 'http_response', $response, $args, $url ); } + /** * Normalizes cookies for using in Requests. * From af9a14f7efe3d4ad2f9a08859579f8307d0eafcf Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 27 Aug 2025 10:44:10 -0400 Subject: [PATCH 5/6] Use WpOrg requests instead --- src/wp-includes/class-wp-http.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/class-wp-http.php b/src/wp-includes/class-wp-http.php index b7d1ea0ebe60d..ad49812bb0aea 100644 --- a/src/wp-includes/class-wp-http.php +++ b/src/wp-includes/class-wp-http.php @@ -197,8 +197,8 @@ public function request( $url, $args = array() ) { mbstring_binary_safe_encoding(); try { - $raw_responses = Requests::request_multiple( $pending_requests ); - } catch ( Requests_Exception $e ) { + $raw_responses = WpOrg\Requests\Requests::request_multiple( $pending_requests ); + } catch ( WpOrg\Requests\Exception $e ) { $raw_responses = new WP_Error( 'http_request_failed', $e->getMessage() ); } @@ -234,14 +234,14 @@ public function request( $url, $args = array() ) { mbstring_binary_safe_encoding(); try { - $response = Requests::request( + $response = WpOrg\Requests\Requests::request( $formatted['url'], $formatted['headers'], $formatted['data'], $formatted['type'], $formatted['options'] ); - } catch ( Requests_Exception $e ) { + } catch ( WpOrg\Requests\Exception $e ) { $response = new WP_Error( 'http_request_failed', $e->getMessage() ); } @@ -523,9 +523,9 @@ protected function format_request( $url, $args ) { /** * Format a response into the expected shape. * - * @param Requests_Response|WP_Error $response Response to format. - * @param array $args Request arguments. - * @param string $url Request URL. + * @param WpOrg\Requests\Response|WP_Error $response Response to format. + * @param array $args Request arguments. + * @param string $url Request URL. * @return array|WP_Error */ protected function format_response( $response, $args, $url ) { @@ -546,10 +546,10 @@ protected function format_response( $response, $args, $url ) { * @param array|WP_Error $response HTTP response or WP_Error object. * @param string $context Context under which the hook is fired. * @param string $class HTTP transport used. - * @param array $parsed_args HTTP request arguments. + * @param array $args HTTP request arguments. * @param string $url The request URL. */ - do_action( 'http_api_debug', $response, 'response', Requests::class, $parsed_args, $url ); + do_action( 'http_api_debug', $response, 'response', \WpOrg\Requests\Requests::class, $args, $url ); if ( is_wp_error( $response ) ) { return $response; From 3bff4a5bbb0034ec78cc87365999e1148875fdff Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 3 Sep 2025 15:33:38 -0400 Subject: [PATCH 6/6] Ensure exception is converted to WP_Error --- src/wp-includes/class-wp-http.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-http.php b/src/wp-includes/class-wp-http.php index ad49812bb0aea..3d7b510cb949f 100644 --- a/src/wp-includes/class-wp-http.php +++ b/src/wp-includes/class-wp-http.php @@ -523,12 +523,16 @@ protected function format_request( $url, $args ) { /** * Format a response into the expected shape. * - * @param WpOrg\Requests\Response|WP_Error $response Response to format. - * @param array $args Request arguments. - * @param string $url Request URL. + * @param \WpOrg\Requests\Response|\WpOrg\Requests\Exception|WP_Error $response Response to format. + * @param array $args Request arguments. + * @param string $url Request URL. * @return array|WP_Error */ protected function format_response( $response, $args, $url ) { + if ( $response instanceof \WpOrg\Requests\Exception ) { + $response = new \WP_Error( 'http_request_failed', $response->getMessage() ); + } + // Convert the response into an array. if ( ! is_wp_error( $response ) ) { $http_response = new WP_HTTP_Requests_Response( $response, $args['filename'] );