Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions src/Transport/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,36 @@ 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;

do {
$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
Expand Down Expand Up @@ -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']);
Expand All @@ -330,21 +363,51 @@ 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]);
}

++$completed;
}
} 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);

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
*
Expand Down
26 changes: 26 additions & 0 deletions tests/Transport/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
Loading