diff --git a/src/Transport/Curl.php b/src/Transport/Curl.php index 215b77e97..2c4c2a33f 100644 --- a/src/Transport/Curl.php +++ b/src/Transport/Curl.php @@ -287,6 +287,9 @@ public function request_multiple($requests, $options) { $request['options']['hooks']->dispatch('curl.before_multi_exec', [&$multihandle]); + // Responses that will be redirected, parsed once all transfers have finished. + $redirected = []; + do { $active = 0; @@ -294,6 +297,26 @@ public function request_multiple($requests, $options) { $status = curl_multi_exec($multihandle, $active); } while ($status === CURLM_CALL_MULTI_PERFORM); + if ($status !== CURLM_OK) { + // The transfers cannot progress anymore, so stop instead of looping forever. + $exception = new CurlException( + curl_multi_strerror($status), + CurlException::MULTI, + $multihandle, + $status + ); + + foreach ($subhandles as $key => $subhandle) { + if (!isset($responses[$key]) && !isset($redirected[$key])) { + $responses[$key] = $exception; + $requests[$key]['options']['hooks']->dispatch('transport.internal.parse_error', [&$responses[$key], $requests[$key]]); + $requests[$key]['options']['hooks']->dispatch('multiple.request.complete', [&$responses[$key], $key]); + } + } + + break; + } + $to_process = []; // Read the information as needed @@ -321,7 +344,17 @@ public function request_multiple($requests, $options) { } else { $responses[$key] = $subrequests[$key]->process_response($subrequests[$key]->response_data, $options); - $options['hooks']->dispatch('transport.internal.parse_response', [&$responses[$key], $requests[$key]]); + if ($this->will_follow_redirect($subrequests[$key]->info, $options)) { + /* + * Following a redirect sends a new request, which blocks. Doing that now + * would stall the other transfers, which can then time out, so do it once + * they have all finished. + */ + $redirected[$key] = $responses[$key]; + unset($responses[$key]); + } else { + $options['hooks']->dispatch('transport.internal.parse_response', [&$responses[$key], $requests[$key]]); + } } curl_multi_remove_handle($multihandle, $done['handle']); @@ -330,7 +363,7 @@ public function request_multiple($requests, $options) { curl_close($done['handle']); } - if (!is_string($responses[$key])) { + if (isset($responses[$key]) && !is_string($responses[$key])) { $options['hooks']->dispatch('multiple.request.complete', [&$responses[$key], $key]); } @@ -338,6 +371,16 @@ public function request_multiple($requests, $options) { } } while ($active || $completed < $subrequestcount); + foreach ($redirected as $key => $response) { + $options = $requests[$key]['options']; + $responses[$key] = $response; + $options['hooks']->dispatch('transport.internal.parse_response', [&$responses[$key], $requests[$key]]); + + if (!is_string($responses[$key])) { + $options['hooks']->dispatch('multiple.request.complete', [&$responses[$key], $key]); + } + } + $request['options']['hooks']->dispatch('curl.after_multi_exec', [&$multihandle]); curl_multi_close($multihandle); @@ -345,6 +388,26 @@ public function request_multiple($requests, $options) { return $responses; } + /** + * Determine whether a finished transfer is a redirect that will be followed. + * + * This mirrors the conditions under which {@see \WpOrg\Requests\Requests::parse_response()} + * follows a redirect, using the information cURL collected about the transfer. + * + * @param array|null $info Transfer information, as returned by curl_getinfo(). + * @param array $options Request options. + * @return bool + */ + private function will_follow_redirect($info, $options) { + if (!is_array($info) || empty($options['follow_redirects']) || $options['blocking'] === false) { + return false; + } + + $code = isset($info['http_code']) ? (int) $info['http_code'] : 0; + + return $code >= 300 && $code < 400 && $code !== 304 && $code !== 305 && $code !== 306; + } + /** * Get the cURL handle for use in a multi-request * diff --git a/tests/Transport/BaseTestCase.php b/tests/Transport/BaseTestCase.php index bbe6cf40e..9b3f531ca 100644 --- a/tests/Transport/BaseTestCase.php +++ b/tests/Transport/BaseTestCase.php @@ -994,6 +994,32 @@ public function testMultiple() { $this->assertEmpty($result['args']); } + /** + * Following a redirect in a batch must not stall the other transfers of the batch, + * which could then time out although their server answered in time. + */ + public function testMultipleRedirectDoesNotStallOtherRequests() { + $requests = [ + 'redirect' => [ + 'url' => $this->httpbin('/redirect-to?url=' . rawurlencode($this->httpbin('/delay/3'))), + ], + 'fast' => [ + 'url' => $this->httpbin('/delay/1'), + 'options' => [ + 'timeout' => 2, + ], + ], + ]; + $responses = Requests::request_multiple($requests, $this->getOptions(['timeout' => 10])); + + $this->assertInstanceOf(Response::class, $responses['fast'], 'The request that answers within its timeout should succeed.'); + $this->assertSame(200, $responses['fast']->status_code); + + $this->assertInstanceOf(Response::class, $responses['redirect'], 'The redirected request should succeed.'); + $this->assertSame(200, $responses['redirect']->status_code); + $this->assertSame(1, $responses['redirect']->redirects); + } + public function testMultipleWithDifferingMethods() { $requests = [ 'get' => [