Skip to content

Commit

Permalink
[HttpClient] Transfer timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyweb committed Jul 30, 2019
1 parent a29aff0 commit f8595d1
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* added support for NTLM authentication
* added `$response->toStream()` to cast responses to regular PHP streams
* made `Psr18Client` implement relevant PSR-17 factories and have streaming responses
* added `transfer_timeout` option

4.3.0
-----
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpClient/CurlHttpClient.php
Expand Up @@ -282,6 +282,10 @@ public function request(string $method, string $url, array $options = []): Respo
$curlopts[file_exists($options['bindto']) ? CURLOPT_UNIX_SOCKET_PATH : CURLOPT_INTERFACE] = $options['bindto'];
}

if (null !== $options['transfer_timeout']) {
$curlopts[CURLOPT_TIMEOUT_MS] = 1000 * $options['transfer_timeout'];
}

$ch = curl_init();

foreach ($curlopts as $opt => $value) {
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/HttpClientTrait.php
Expand Up @@ -125,6 +125,7 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
$options['headers'] = $headers;
$options['http_version'] = (string) ($options['http_version'] ?? '') ?: null;
$options['timeout'] = (float) ($options['timeout'] ?? ini_get('default_socket_timeout'));
$options['transfer_timeout'] = isset($options['transfer_timeout']) ? (float) $options['transfer_timeout'] : null;

return [$url, $options];
}
Expand Down
12 changes: 11 additions & 1 deletion src/Symfony/Component/HttpClient/NativeHttpClient.php
Expand Up @@ -129,10 +129,16 @@ public function request(string $method, string $url, array $options = []): Respo
};
}

$transferTimeout = $options['transfer_timeout'] ?: INF;

// Always register a notification callback to compute live stats about the response
$notification = static function (int $code, int $severity, ?string $msg, int $msgCode, int $dlNow, int $dlSize) use ($onProgress, &$info) {
$notification = static function (int $code, int $severity, ?string $msg, int $msgCode, int $dlNow, int $dlSize) use ($onProgress, &$info, $transferTimeout) {
$info['total_time'] = microtime(true) - $info['start_time'];

if ($info['total_time'] >= $transferTimeout) {
throw new TransportException(sprintf('Timeout was reached for "%s".', implode('', $info['url'])));
}

if (STREAM_NOTIFY_PROGRESS === $code) {
$info['starttransfer_time'] = $info['starttransfer_time'] ?: $info['total_time'];
$info['size_upload'] += $dlNow ? 0 : $info['size_body'];
Expand Down Expand Up @@ -166,6 +172,10 @@ public function request(string $method, string $url, array $options = []): Respo
$options['request_headers'][] = 'user-agent: Symfony HttpClient/Native';
}

if (null !== $options['transfer_timeout']) {
$options['timeout'] = min($options['transfer_timeout'], $options['timeout']);
}

$context = [
'http' => [
'protocol_version' => $options['http_version'] ?: '1.1',
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php
Expand Up @@ -123,6 +123,15 @@ protected function getHttpClient(string $testCase): HttpClientInterface
$body = ['<1>', '', '<2>'];
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
break;

case 'testTransferTimeout':
$mock = $this->getMockBuilder(ResponseInterface::class)->getMock();
$mock->expects($this->any())
->method('getContent')
->willThrowException(new TransportException('Timeout was reached.'));

$responses[] = $mock;
break;
}

return new MockHttpClient($responses);
Expand Down
61 changes: 31 additions & 30 deletions src/Symfony/Contracts/HttpClient/HttpClientInterface.php
Expand Up @@ -26,35 +26,36 @@
interface HttpClientInterface
{
public const OPTIONS_DEFAULTS = [
'auth_basic' => null, // array|string - an array containing the username as first value, and optionally the
// password as the second one; or string like username:password - enabling HTTP Basic
// authentication (RFC 7617)
'auth_bearer' => null, // string - a token enabling HTTP Bearer authorization (RFC 6750)
'query' => [], // string[] - associative array of query string values to merge with the request's URL
'headers' => [], // iterable|string[]|string[][] - headers names provided as keys or as part of values
'body' => '', // array|string|resource|\Traversable|\Closure - the callback SHOULD yield a string
// smaller than the amount requested as argument; the empty string signals EOF; when
// an array is passed, it is meant as a form payload of field names and values
'json' => null, // array|\JsonSerializable - when set, implementations MUST set the "body" option to
// the JSON-encoded value and set the "content-type" headers to a JSON-compatible
// value if they are not defined - typically "application/json"
'user_data' => null, // mixed - any extra data to attach to the request (scalar, callable, object...) that
// MUST be available via $response->getInfo('user_data') - not used internally
'max_redirects' => 20, // int - the maximum number of redirects to follow; a value lower or equal to 0 means
// redirects should not be followed; "Authorization" and "Cookie" headers MUST
// NOT follow except for the initial host name
'http_version' => null, // string - defaults to the best supported version, typically 1.1 or 2.0
'base_uri' => null, // string - the URI to resolve relative URLs, following rules in RFC 3986, section 2
'buffer' => true, // bool - whether the content of the response should be buffered or not
'on_progress' => null, // callable(int $dlNow, int $dlSize, array $info) - throwing any exceptions MUST abort
// the request; it MUST be called on DNS resolution, on arrival of headers and on
// completion; it SHOULD be called on upload/download of data and at least 1/s
'resolve' => [], // string[] - a map of host to IP address that SHOULD replace DNS resolution
'proxy' => null, // string - by default, the proxy-related env vars handled by curl SHOULD be honored
'no_proxy' => null, // string - a comma separated list of hosts that do not require a proxy to be reached
'timeout' => null, // float - the inactivity timeout - defaults to ini_get('default_socket_timeout')
'bindto' => '0', // string - the interface or the local socket to bind to
'verify_peer' => true, // see https://php.net/context.ssl for the following options
'auth_basic' => null, // array|string - an array containing the username as first value, and optionally the
// password as the second one; or string like username:password - enabling HTTP Basic
// authentication (RFC 7617)
'auth_bearer' => null, // string - a token enabling HTTP Bearer authorization (RFC 6750)
'query' => [], // string[] - associative array of query string values to merge with the request's URL
'headers' => [], // iterable|string[]|string[][] - headers names provided as keys or as part of values
'body' => '', // array|string|resource|\Traversable|\Closure - the callback SHOULD yield a string
// smaller than the amount requested as argument; the empty string signals EOF; when
// an array is passed, it is meant as a form payload of field names and values
'json' => null, // array|\JsonSerializable - when set, implementations MUST set the "body" option to
// the JSON-encoded value and set the "content-type" headers to a JSON-compatible
// value if they are not defined - typically "application/json"
'user_data' => null, // mixed - any extra data to attach to the request (scalar, callable, object...) that
// MUST be available via $response->getInfo('user_data') - not used internally
'max_redirects' => 20, // int - the maximum number of redirects to follow; a value lower or equal to 0 means
// redirects should not be followed; "Authorization" and "Cookie" headers MUST
// NOT follow except for the initial host name
'http_version' => null, // string - defaults to the best supported version, typically 1.1 or 2.0
'base_uri' => null, // string - the URI to resolve relative URLs, following rules in RFC 3986, section 2
'buffer' => true, // bool - whether the content of the response should be buffered or not
'on_progress' => null, // callable(int $dlNow, int $dlSize, array $info) - throwing any exceptions MUST abort
// the request; it MUST be called on DNS resolution, on arrival of headers and on
// completion; it SHOULD be called on upload/download of data and at least 1/s
'resolve' => [], // string[] - a map of host to IP address that SHOULD replace DNS resolution
'proxy' => null, // string - by default, the proxy-related env vars handled by curl SHOULD be honored
'no_proxy' => null, // string - a comma separated list of hosts that do not require a proxy to be reached
'timeout' => null, // float - the inactivity timeout - defaults to ini_get('default_socket_timeout')
'transfer_timeout' => null, // float|null - the total transfer timeout (including the inactivity timeout)
'bindto' => '0', // string - the interface or the local socket to bind to
'verify_peer' => true, // see https://php.net/context.ssl for the following options
'verify_host' => true,
'cafile' => null,
'capath' => null,
Expand All @@ -64,7 +65,7 @@ interface HttpClientInterface
'ciphers' => null,
'peer_fingerprint' => null,
'capture_peer_cert_chain' => false,
'extra' => [], // array - additional options that can be ignored if unsupported, unlike regular options
'extra' => [], // array - additional options that can be ignored if unsupported, unlike regular options
];

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Contracts/HttpClient/Test/Fixtures/web/index.php
Expand Up @@ -132,6 +132,16 @@
header('Content-Encoding: gzip');
echo str_repeat('-', 1000);
exit;

case '/transfer-timeout':
ignore_user_abort(false);
while (true) {
echo '<1>';
@ob_flush();
flush();
usleep(500);
}
exit;
}

header('Content-Type: application/json', true);
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php
Expand Up @@ -778,4 +778,15 @@ public function testGzipBroken()
$this->expectException(TransportExceptionInterface::class);
$response->getContent();
}

public function testTransferTimeout()
{
$client = $this->getHttpClient(__FUNCTION__);
$response = $client->request('GET', 'http://localhost:8057/transfer-timeout', [
'transfer_timeout' => 0.1,
]);

$this->expectException(TransportExceptionInterface::class);
$response->getContent();
}
}

0 comments on commit f8595d1

Please sign in to comment.