2 namespace GuzzleHttp\Handler;
4 use GuzzleHttp\Exception\RequestException;
5 use GuzzleHttp\Exception\ConnectException;
6 use GuzzleHttp\Promise\FulfilledPromise;
7 use GuzzleHttp\Promise\PromiseInterface;
9 use GuzzleHttp\TransferStats;
10 use Psr\Http\Message\RequestInterface;
11 use Psr\Http\Message\ResponseInterface;
12 use Psr\Http\Message\StreamInterface;
15 * HTTP handler that uses PHP's HTTP stream wrapper.
19 private $lastHeaders = [];
22 * Sends an HTTP request.
24 * @param RequestInterface $request Request to send.
25 * @param array $options Request transfer options.
27 * @return PromiseInterface
29 public function __invoke(RequestInterface $request, array $options)
31 // Sleep if there is a delay specified.
32 if (isset($options['delay'])) {
33 usleep($options['delay'] * 1000);
36 $startTime = isset($options['on_stats']) ? microtime(true) : null;
39 // Does not support the expect header.
40 $request = $request->withoutHeader('Expect');
42 // Append a content-length header if body size is zero to match
44 if (0 === $request->getBody()->getSize()) {
45 $request = $request->withHeader('Content-Length', 0);
48 return $this->createResponse(
51 $this->createStream($request, $options),
54 } catch (\InvalidArgumentException $e) {
56 } catch (\Exception $e) {
57 // Determine if the error was a networking error.
58 $message = $e->getMessage();
59 // This list can probably get more comprehensive.
60 if (strpos($message, 'getaddrinfo') // DNS lookup failed
61 || strpos($message, 'Connection refused')
62 || strpos($message, "couldn't connect to host") // error on HHVM
63 || strpos($message, "connection attempt failed")
65 $e = new ConnectException($e->getMessage(), $request, $e);
67 $e = RequestException::wrapException($request, $e);
68 $this->invokeStats($options, $request, $startTime, null, $e);
70 return \GuzzleHttp\Promise\rejection_for($e);
74 private function invokeStats(
76 RequestInterface $request,
78 ResponseInterface $response = null,
81 if (isset($options['on_stats'])) {
82 $stats = new TransferStats(
85 microtime(true) - $startTime,
89 call_user_func($options['on_stats'], $stats);
93 private function createResponse(
94 RequestInterface $request,
99 $hdrs = $this->lastHeaders;
100 $this->lastHeaders = [];
101 $parts = explode(' ', array_shift($hdrs), 3);
102 $ver = explode('/', $parts[0])[1];
104 $reason = isset($parts[2]) ? $parts[2] : null;
105 $headers = \GuzzleHttp\headers_from_lines($hdrs);
106 list($stream, $headers) = $this->checkDecode($options, $headers, $stream);
107 $stream = Psr7\stream_for($stream);
110 if (strcasecmp('HEAD', $request->getMethod())) {
111 $sink = $this->createSink($stream, $options);
114 $response = new Psr7\Response($status, $headers, $sink, $ver, $reason);
116 if (isset($options['on_headers'])) {
118 $options['on_headers']($response);
119 } catch (\Exception $e) {
120 $msg = 'An error was encountered during the on_headers event';
121 $ex = new RequestException($msg, $request, $response, $e);
122 return \GuzzleHttp\Promise\rejection_for($ex);
126 // Do not drain when the request is a HEAD request because they have
128 if ($sink !== $stream) {
132 $response->getHeaderLine('Content-Length')
136 $this->invokeStats($options, $request, $startTime, $response, null);
138 return new FulfilledPromise($response);
141 private function createSink(StreamInterface $stream, array $options)
143 if (!empty($options['stream'])) {
147 $sink = isset($options['sink'])
149 : fopen('php://temp', 'r+');
151 return is_string($sink)
152 ? new Psr7\LazyOpenStream($sink, 'w+')
153 : Psr7\stream_for($sink);
156 private function checkDecode(array $options, array $headers, $stream)
158 // Automatically decode responses when instructed.
159 if (!empty($options['decode_content'])) {
160 $normalizedKeys = \GuzzleHttp\normalize_header_keys($headers);
161 if (isset($normalizedKeys['content-encoding'])) {
162 $encoding = $headers[$normalizedKeys['content-encoding']];
163 if ($encoding[0] === 'gzip' || $encoding[0] === 'deflate') {
164 $stream = new Psr7\InflateStream(
165 Psr7\stream_for($stream)
167 $headers['x-encoded-content-encoding']
168 = $headers[$normalizedKeys['content-encoding']];
169 // Remove content-encoding header
170 unset($headers[$normalizedKeys['content-encoding']]);
171 // Fix content-length header
172 if (isset($normalizedKeys['content-length'])) {
173 $headers['x-encoded-content-length']
174 = $headers[$normalizedKeys['content-length']];
176 $length = (int) $stream->getSize();
178 unset($headers[$normalizedKeys['content-length']]);
180 $headers[$normalizedKeys['content-length']] = [$length];
187 return [$stream, $headers];
191 * Drains the source stream into the "sink" client option.
193 * @param StreamInterface $source
194 * @param StreamInterface $sink
195 * @param string $contentLength Header specifying the amount of
198 * @return StreamInterface
199 * @throws \RuntimeException when the sink option is invalid.
201 private function drain(
202 StreamInterface $source,
203 StreamInterface $sink,
206 // If a content-length header is provided, then stop reading once
207 // that number of bytes has been read. This can prevent infinitely
208 // reading from a stream when dealing with servers that do not honor
209 // Connection: Close headers.
213 (strlen($contentLength) > 0 && (int) $contentLength > 0) ? (int) $contentLength : -1
223 * Create a resource and check to ensure it was created successfully
225 * @param callable $callback Callable that returns stream resource
228 * @throws \RuntimeException on error
230 private function createResource(callable $callback)
233 set_error_handler(function ($_, $msg, $file, $line) use (&$errors) {
242 $resource = $callback();
243 restore_error_handler();
246 $message = 'Error creating resource: ';
247 foreach ($errors as $err) {
248 foreach ($err as $key => $value) {
249 $message .= "[$key] $value" . PHP_EOL;
252 throw new \RuntimeException(trim($message));
258 private function createStream(RequestInterface $request, array $options)
262 $methods = array_flip(get_class_methods(__CLASS__));
265 // HTTP/1.1 streams using the PHP stream wrapper require a
266 // Connection: close header
267 if ($request->getProtocolVersion() == '1.1'
268 && !$request->hasHeader('Connection')
270 $request = $request->withHeader('Connection', 'close');
273 // Ensure SSL is verified by default
274 if (!isset($options['verify'])) {
275 $options['verify'] = true;
279 $context = $this->getDefaultContext($request);
281 if (isset($options['on_headers']) && !is_callable($options['on_headers'])) {
282 throw new \InvalidArgumentException('on_headers must be callable');
285 if (!empty($options)) {
286 foreach ($options as $key => $value) {
287 $method = "add_{$key}";
288 if (isset($methods[$method])) {
289 $this->{$method}($request, $context, $value, $params);
294 if (isset($options['stream_context'])) {
295 if (!is_array($options['stream_context'])) {
296 throw new \InvalidArgumentException('stream_context must be an array');
298 $context = array_replace_recursive(
300 $options['stream_context']
304 // Microsoft NTLM authentication only supported with curl handler
305 if (isset($options['auth'])
306 && is_array($options['auth'])
307 && isset($options['auth'][2])
308 && 'ntlm' == $options['auth'][2]
310 throw new \InvalidArgumentException('Microsoft NTLM authentication only supported with curl handler');
313 $uri = $this->resolveHost($request, $options);
315 $context = $this->createResource(
316 function () use ($context, $params) {
317 return stream_context_create($context, $params);
321 return $this->createResource(
322 function () use ($uri, &$http_response_header, $context, $options) {
323 $resource = fopen((string) $uri, 'r', null, $context);
324 $this->lastHeaders = $http_response_header;
326 if (isset($options['read_timeout'])) {
327 $readTimeout = $options['read_timeout'];
328 $sec = (int) $readTimeout;
329 $usec = ($readTimeout - $sec) * 100000;
330 stream_set_timeout($resource, $sec, $usec);
338 private function resolveHost(RequestInterface $request, array $options)
340 $uri = $request->getUri();
342 if (isset($options['force_ip_resolve']) && !filter_var($uri->getHost(), FILTER_VALIDATE_IP)) {
343 if ('v4' === $options['force_ip_resolve']) {
344 $records = dns_get_record($uri->getHost(), DNS_A);
345 if (!isset($records[0]['ip'])) {
346 throw new ConnectException(sprintf("Could not resolve IPv4 address for host '%s'", $uri->getHost()), $request);
348 $uri = $uri->withHost($records[0]['ip']);
349 } elseif ('v6' === $options['force_ip_resolve']) {
350 $records = dns_get_record($uri->getHost(), DNS_AAAA);
351 if (!isset($records[0]['ipv6'])) {
352 throw new ConnectException(sprintf("Could not resolve IPv6 address for host '%s'", $uri->getHost()), $request);
354 $uri = $uri->withHost('[' . $records[0]['ipv6'] . ']');
361 private function getDefaultContext(RequestInterface $request)
364 foreach ($request->getHeaders() as $name => $value) {
365 foreach ($value as $val) {
366 $headers .= "$name: $val\r\n";
372 'method' => $request->getMethod(),
373 'header' => $headers,
374 'protocol_version' => $request->getProtocolVersion(),
375 'ignore_errors' => true,
376 'follow_location' => 0,
380 $body = (string) $request->getBody();
383 $context['http']['content'] = $body;
384 // Prevent the HTTP handler from adding a Content-Type header.
385 if (!$request->hasHeader('Content-Type')) {
386 $context['http']['header'] .= "Content-Type:\r\n";
390 $context['http']['header'] = rtrim($context['http']['header']);
395 private function add_proxy(RequestInterface $request, &$options, $value, &$params)
397 if (!is_array($value)) {
398 $options['http']['proxy'] = $value;
400 $scheme = $request->getUri()->getScheme();
401 if (isset($value[$scheme])) {
402 if (!isset($value['no'])
403 || !\GuzzleHttp\is_host_in_noproxy(
404 $request->getUri()->getHost(),
408 $options['http']['proxy'] = $value[$scheme];
414 private function add_timeout(RequestInterface $request, &$options, $value, &$params)
417 $options['http']['timeout'] = $value;
421 private function add_verify(RequestInterface $request, &$options, $value, &$params)
423 if ($value === true) {
424 // PHP 5.6 or greater will find the system cert by default. When
425 // < 5.6, use the Guzzle bundled cacert.
426 if (PHP_VERSION_ID < 50600) {
427 $options['ssl']['cafile'] = \GuzzleHttp\default_ca_bundle();
429 } elseif (is_string($value)) {
430 $options['ssl']['cafile'] = $value;
431 if (!file_exists($value)) {
432 throw new \RuntimeException("SSL CA bundle not found: $value");
434 } elseif ($value === false) {
435 $options['ssl']['verify_peer'] = false;
436 $options['ssl']['verify_peer_name'] = false;
439 throw new \InvalidArgumentException('Invalid verify request option');
442 $options['ssl']['verify_peer'] = true;
443 $options['ssl']['verify_peer_name'] = true;
444 $options['ssl']['allow_self_signed'] = false;
447 private function add_cert(RequestInterface $request, &$options, $value, &$params)
449 if (is_array($value)) {
450 $options['ssl']['passphrase'] = $value[1];
454 if (!file_exists($value)) {
455 throw new \RuntimeException("SSL certificate not found: {$value}");
458 $options['ssl']['local_cert'] = $value;
461 private function add_progress(RequestInterface $request, &$options, $value, &$params)
463 $this->addNotification(
465 function ($code, $a, $b, $c, $transferred, $total) use ($value) {
466 if ($code == STREAM_NOTIFY_PROGRESS) {
467 $value($total, $transferred, null, null);
473 private function add_debug(RequestInterface $request, &$options, $value, &$params)
475 if ($value === false) {
480 STREAM_NOTIFY_CONNECT => 'CONNECT',
481 STREAM_NOTIFY_AUTH_REQUIRED => 'AUTH_REQUIRED',
482 STREAM_NOTIFY_AUTH_RESULT => 'AUTH_RESULT',
483 STREAM_NOTIFY_MIME_TYPE_IS => 'MIME_TYPE_IS',
484 STREAM_NOTIFY_FILE_SIZE_IS => 'FILE_SIZE_IS',
485 STREAM_NOTIFY_REDIRECTED => 'REDIRECTED',
486 STREAM_NOTIFY_PROGRESS => 'PROGRESS',
487 STREAM_NOTIFY_FAILURE => 'FAILURE',
488 STREAM_NOTIFY_COMPLETED => 'COMPLETED',
489 STREAM_NOTIFY_RESOLVE => 'RESOLVE',
491 static $args = ['severity', 'message', 'message_code',
492 'bytes_transferred', 'bytes_max'];
494 $value = \GuzzleHttp\debug_resource($value);
495 $ident = $request->getMethod() . ' ' . $request->getUri()->withFragment('');
496 $this->addNotification(
498 function () use ($ident, $value, $map, $args) {
499 $passed = func_get_args();
500 $code = array_shift($passed);
501 fprintf($value, '<%s> [%s] ', $ident, $map[$code]);
502 foreach (array_filter($passed) as $i => $v) {
503 fwrite($value, $args[$i] . ': "' . $v . '" ');
505 fwrite($value, "\n");
510 private function addNotification(array &$params, callable $notify)
512 // Wrap the existing function if needed.
513 if (!isset($params['notification'])) {
514 $params['notification'] = $notify;
516 $params['notification'] = $this->callArray([
517 $params['notification'],
523 private function callArray(array $functions)
525 return function () use ($functions) {
526 $args = func_get_args();
527 foreach ($functions as $fn) {
528 call_user_func_array($fn, $args);