StreamHandler.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. <?php
  2. namespace GuzzleHttp\Handler;
  3. use GuzzleHttp\Exception\RequestException;
  4. use GuzzleHttp\Exception\ConnectException;
  5. use GuzzleHttp\Promise\FulfilledPromise;
  6. use GuzzleHttp\Promise\PromiseInterface;
  7. use GuzzleHttp\Psr7;
  8. use GuzzleHttp\TransferStats;
  9. use Psr\Http\Message\RequestInterface;
  10. use Psr\Http\Message\ResponseInterface;
  11. use Psr\Http\Message\StreamInterface;
  12. /**
  13. * HTTP handler that uses PHP's HTTP stream wrapper.
  14. */
  15. class StreamHandler
  16. {
  17. private $lastHeaders = [];
  18. /**
  19. * Sends an HTTP request.
  20. *
  21. * @param RequestInterface $request Request to send.
  22. * @param array $options Request transfer options.
  23. *
  24. * @return PromiseInterface
  25. */
  26. public function __invoke(RequestInterface $request, array $options)
  27. {
  28. // Sleep if there is a delay specified.
  29. if (isset($options['delay'])) {
  30. usleep($options['delay'] * 1000);
  31. }
  32. $startTime = isset($options['on_stats']) ? microtime(true) : null;
  33. try {
  34. // Does not support the expect header.
  35. $request = $request->withoutHeader('Expect');
  36. // Append a content-length header if body size is zero to match
  37. // cURL's behavior.
  38. if (0 === $request->getBody()->getSize()) {
  39. $request = $request->withHeader('Content-Length', 0);
  40. }
  41. return $this->createResponse(
  42. $request,
  43. $options,
  44. $this->createStream($request, $options),
  45. $startTime
  46. );
  47. } catch (\InvalidArgumentException $e) {
  48. throw $e;
  49. } catch (\Exception $e) {
  50. // Determine if the error was a networking error.
  51. $message = $e->getMessage();
  52. // This list can probably get more comprehensive.
  53. if (strpos($message, 'getaddrinfo') // DNS lookup failed
  54. || strpos($message, 'Connection refused')
  55. || strpos($message, "couldn't connect to host") // error on HHVM
  56. || strpos($message, "connection attempt failed")
  57. ) {
  58. $e = new ConnectException($e->getMessage(), $request, $e);
  59. }
  60. $e = RequestException::wrapException($request, $e);
  61. $this->invokeStats($options, $request, $startTime, null, $e);
  62. return \GuzzleHttp\Promise\rejection_for($e);
  63. }
  64. }
  65. private function invokeStats(
  66. array $options,
  67. RequestInterface $request,
  68. $startTime,
  69. ResponseInterface $response = null,
  70. $error = null
  71. ) {
  72. if (isset($options['on_stats'])) {
  73. $stats = new TransferStats(
  74. $request,
  75. $response,
  76. microtime(true) - $startTime,
  77. $error,
  78. []
  79. );
  80. call_user_func($options['on_stats'], $stats);
  81. }
  82. }
  83. private function createResponse(
  84. RequestInterface $request,
  85. array $options,
  86. $stream,
  87. $startTime
  88. ) {
  89. $hdrs = $this->lastHeaders;
  90. $this->lastHeaders = [];
  91. $parts = explode(' ', array_shift($hdrs), 3);
  92. $ver = explode('/', $parts[0])[1];
  93. $status = $parts[1];
  94. $reason = isset($parts[2]) ? $parts[2] : null;
  95. $headers = \GuzzleHttp\headers_from_lines($hdrs);
  96. list($stream, $headers) = $this->checkDecode($options, $headers, $stream);
  97. $stream = Psr7\stream_for($stream);
  98. $sink = $stream;
  99. if (strcasecmp('HEAD', $request->getMethod())) {
  100. $sink = $this->createSink($stream, $options);
  101. }
  102. $response = new Psr7\Response($status, $headers, $sink, $ver, $reason);
  103. if (isset($options['on_headers'])) {
  104. try {
  105. $options['on_headers']($response);
  106. } catch (\Exception $e) {
  107. $msg = 'An error was encountered during the on_headers event';
  108. $ex = new RequestException($msg, $request, $response, $e);
  109. return \GuzzleHttp\Promise\rejection_for($ex);
  110. }
  111. }
  112. // Do not drain when the request is a HEAD request because they have
  113. // no body.
  114. if ($sink !== $stream) {
  115. $this->drain(
  116. $stream,
  117. $sink,
  118. $response->getHeaderLine('Content-Length')
  119. );
  120. }
  121. $this->invokeStats($options, $request, $startTime, $response, null);
  122. return new FulfilledPromise($response);
  123. }
  124. private function createSink(StreamInterface $stream, array $options)
  125. {
  126. if (!empty($options['stream'])) {
  127. return $stream;
  128. }
  129. $sink = isset($options['sink'])
  130. ? $options['sink']
  131. : fopen('php://temp', 'r+');
  132. return is_string($sink)
  133. ? new Psr7\LazyOpenStream($sink, 'w+')
  134. : Psr7\stream_for($sink);
  135. }
  136. private function checkDecode(array $options, array $headers, $stream)
  137. {
  138. // Automatically decode responses when instructed.
  139. if (!empty($options['decode_content'])) {
  140. $normalizedKeys = \GuzzleHttp\normalize_header_keys($headers);
  141. if (isset($normalizedKeys['content-encoding'])) {
  142. $encoding = $headers[$normalizedKeys['content-encoding']];
  143. if ($encoding[0] === 'gzip' || $encoding[0] === 'deflate') {
  144. $stream = new Psr7\InflateStream(
  145. Psr7\stream_for($stream)
  146. );
  147. $headers['x-encoded-content-encoding']
  148. = $headers[$normalizedKeys['content-encoding']];
  149. // Remove content-encoding header
  150. unset($headers[$normalizedKeys['content-encoding']]);
  151. // Fix content-length header
  152. if (isset($normalizedKeys['content-length'])) {
  153. $headers['x-encoded-content-length']
  154. = $headers[$normalizedKeys['content-length']];
  155. $length = (int) $stream->getSize();
  156. if ($length === 0) {
  157. unset($headers[$normalizedKeys['content-length']]);
  158. } else {
  159. $headers[$normalizedKeys['content-length']] = [$length];
  160. }
  161. }
  162. }
  163. }
  164. }
  165. return [$stream, $headers];
  166. }
  167. /**
  168. * Drains the source stream into the "sink" client option.
  169. *
  170. * @param StreamInterface $source
  171. * @param StreamInterface $sink
  172. * @param string $contentLength Header specifying the amount of
  173. * data to read.
  174. *
  175. * @return StreamInterface
  176. * @throws \RuntimeException when the sink option is invalid.
  177. */
  178. private function drain(
  179. StreamInterface $source,
  180. StreamInterface $sink,
  181. $contentLength
  182. ) {
  183. // If a content-length header is provided, then stop reading once
  184. // that number of bytes has been read. This can prevent infinitely
  185. // reading from a stream when dealing with servers that do not honor
  186. // Connection: Close headers.
  187. Psr7\copy_to_stream(
  188. $source,
  189. $sink,
  190. (strlen($contentLength) > 0 && (int) $contentLength > 0) ? (int) $contentLength : -1
  191. );
  192. $sink->seek(0);
  193. $source->close();
  194. return $sink;
  195. }
  196. /**
  197. * Create a resource and check to ensure it was created successfully
  198. *
  199. * @param callable $callback Callable that returns stream resource
  200. *
  201. * @return resource
  202. * @throws \RuntimeException on error
  203. */
  204. private function createResource(callable $callback)
  205. {
  206. $errors = null;
  207. set_error_handler(function ($_, $msg, $file, $line) use (&$errors) {
  208. $errors[] = [
  209. 'message' => $msg,
  210. 'file' => $file,
  211. 'line' => $line
  212. ];
  213. return true;
  214. });
  215. $resource = $callback();
  216. restore_error_handler();
  217. if (!$resource) {
  218. $message = 'Error creating resource: ';
  219. foreach ($errors as $err) {
  220. foreach ($err as $key => $value) {
  221. $message .= "[$key] $value" . PHP_EOL;
  222. }
  223. }
  224. throw new \RuntimeException(trim($message));
  225. }
  226. return $resource;
  227. }
  228. private function createStream(RequestInterface $request, array $options)
  229. {
  230. static $methods;
  231. if (!$methods) {
  232. $methods = array_flip(get_class_methods(__CLASS__));
  233. }
  234. // HTTP/1.1 streams using the PHP stream wrapper require a
  235. // Connection: close header
  236. if ($request->getProtocolVersion() == '1.1'
  237. && !$request->hasHeader('Connection')
  238. ) {
  239. $request = $request->withHeader('Connection', 'close');
  240. }
  241. // Ensure SSL is verified by default
  242. if (!isset($options['verify'])) {
  243. $options['verify'] = true;
  244. }
  245. $params = [];
  246. $context = $this->getDefaultContext($request);
  247. if (isset($options['on_headers']) && !is_callable($options['on_headers'])) {
  248. throw new \InvalidArgumentException('on_headers must be callable');
  249. }
  250. if (!empty($options)) {
  251. foreach ($options as $key => $value) {
  252. $method = "add_{$key}";
  253. if (isset($methods[$method])) {
  254. $this->{$method}($request, $context, $value, $params);
  255. }
  256. }
  257. }
  258. if (isset($options['stream_context'])) {
  259. if (!is_array($options['stream_context'])) {
  260. throw new \InvalidArgumentException('stream_context must be an array');
  261. }
  262. $context = array_replace_recursive(
  263. $context,
  264. $options['stream_context']
  265. );
  266. }
  267. // Microsoft NTLM authentication only supported with curl handler
  268. if (isset($options['auth'])
  269. && is_array($options['auth'])
  270. && isset($options['auth'][2])
  271. && 'ntlm' == $options['auth'][2]
  272. ) {
  273. throw new \InvalidArgumentException('Microsoft NTLM authentication only supported with curl handler');
  274. }
  275. $uri = $this->resolveHost($request, $options);
  276. $context = $this->createResource(
  277. function () use ($context, $params) {
  278. return stream_context_create($context, $params);
  279. }
  280. );
  281. return $this->createResource(
  282. function () use ($uri, &$http_response_header, $context, $options) {
  283. $resource = fopen((string) $uri, 'r', null, $context);
  284. $this->lastHeaders = $http_response_header;
  285. if (isset($options['read_timeout'])) {
  286. $readTimeout = $options['read_timeout'];
  287. $sec = (int) $readTimeout;
  288. $usec = ($readTimeout - $sec) * 100000;
  289. stream_set_timeout($resource, $sec, $usec);
  290. }
  291. return $resource;
  292. }
  293. );
  294. }
  295. private function resolveHost(RequestInterface $request, array $options)
  296. {
  297. $uri = $request->getUri();
  298. if (isset($options['force_ip_resolve']) && !filter_var($uri->getHost(), FILTER_VALIDATE_IP)) {
  299. if ('v4' === $options['force_ip_resolve']) {
  300. $records = dns_get_record($uri->getHost(), DNS_A);
  301. if (!isset($records[0]['ip'])) {
  302. throw new ConnectException(sprintf("Could not resolve IPv4 address for host '%s'", $uri->getHost()), $request);
  303. }
  304. $uri = $uri->withHost($records[0]['ip']);
  305. } elseif ('v6' === $options['force_ip_resolve']) {
  306. $records = dns_get_record($uri->getHost(), DNS_AAAA);
  307. if (!isset($records[0]['ipv6'])) {
  308. throw new ConnectException(sprintf("Could not resolve IPv6 address for host '%s'", $uri->getHost()), $request);
  309. }
  310. $uri = $uri->withHost('[' . $records[0]['ipv6'] . ']');
  311. }
  312. }
  313. return $uri;
  314. }
  315. private function getDefaultContext(RequestInterface $request)
  316. {
  317. $headers = '';
  318. foreach ($request->getHeaders() as $name => $value) {
  319. foreach ($value as $val) {
  320. $headers .= "$name: $val\r\n";
  321. }
  322. }
  323. $context = [
  324. 'http' => [
  325. 'method' => $request->getMethod(),
  326. 'header' => $headers,
  327. 'protocol_version' => $request->getProtocolVersion(),
  328. 'ignore_errors' => true,
  329. 'follow_location' => 0,
  330. ],
  331. ];
  332. $body = (string) $request->getBody();
  333. if (!empty($body)) {
  334. $context['http']['content'] = $body;
  335. // Prevent the HTTP handler from adding a Content-Type header.
  336. if (!$request->hasHeader('Content-Type')) {
  337. $context['http']['header'] .= "Content-Type:\r\n";
  338. }
  339. }
  340. $context['http']['header'] = rtrim($context['http']['header']);
  341. return $context;
  342. }
  343. private function add_proxy(RequestInterface $request, &$options, $value, &$params)
  344. {
  345. if (!is_array($value)) {
  346. $options['http']['proxy'] = $value;
  347. } else {
  348. $scheme = $request->getUri()->getScheme();
  349. if (isset($value[$scheme])) {
  350. if (!isset($value['no'])
  351. || !\GuzzleHttp\is_host_in_noproxy(
  352. $request->getUri()->getHost(),
  353. $value['no']
  354. )
  355. ) {
  356. $options['http']['proxy'] = $value[$scheme];
  357. }
  358. }
  359. }
  360. }
  361. private function add_timeout(RequestInterface $request, &$options, $value, &$params)
  362. {
  363. if ($value > 0) {
  364. $options['http']['timeout'] = $value;
  365. }
  366. }
  367. private function add_verify(RequestInterface $request, &$options, $value, &$params)
  368. {
  369. if ($value === true) {
  370. // PHP 5.6 or greater will find the system cert by default. When
  371. // < 5.6, use the Guzzle bundled cacert.
  372. if (PHP_VERSION_ID < 50600) {
  373. $options['ssl']['cafile'] = \GuzzleHttp\default_ca_bundle();
  374. }
  375. } elseif (is_string($value)) {
  376. $options['ssl']['cafile'] = $value;
  377. if (!file_exists($value)) {
  378. throw new \RuntimeException("SSL CA bundle not found: $value");
  379. }
  380. } elseif ($value === false) {
  381. $options['ssl']['verify_peer'] = false;
  382. $options['ssl']['verify_peer_name'] = false;
  383. return;
  384. } else {
  385. throw new \InvalidArgumentException('Invalid verify request option');
  386. }
  387. $options['ssl']['verify_peer'] = true;
  388. $options['ssl']['verify_peer_name'] = true;
  389. $options['ssl']['allow_self_signed'] = false;
  390. }
  391. private function add_cert(RequestInterface $request, &$options, $value, &$params)
  392. {
  393. if (is_array($value)) {
  394. $options['ssl']['passphrase'] = $value[1];
  395. $value = $value[0];
  396. }
  397. if (!file_exists($value)) {
  398. throw new \RuntimeException("SSL certificate not found: {$value}");
  399. }
  400. $options['ssl']['local_cert'] = $value;
  401. }
  402. private function add_progress(RequestInterface $request, &$options, $value, &$params)
  403. {
  404. $this->addNotification(
  405. $params,
  406. function ($code, $a, $b, $c, $transferred, $total) use ($value) {
  407. if ($code == STREAM_NOTIFY_PROGRESS) {
  408. $value($total, $transferred, null, null);
  409. }
  410. }
  411. );
  412. }
  413. private function add_debug(RequestInterface $request, &$options, $value, &$params)
  414. {
  415. if ($value === false) {
  416. return;
  417. }
  418. static $map = [
  419. STREAM_NOTIFY_CONNECT => 'CONNECT',
  420. STREAM_NOTIFY_AUTH_REQUIRED => 'AUTH_REQUIRED',
  421. STREAM_NOTIFY_AUTH_RESULT => 'AUTH_RESULT',
  422. STREAM_NOTIFY_MIME_TYPE_IS => 'MIME_TYPE_IS',
  423. STREAM_NOTIFY_FILE_SIZE_IS => 'FILE_SIZE_IS',
  424. STREAM_NOTIFY_REDIRECTED => 'REDIRECTED',
  425. STREAM_NOTIFY_PROGRESS => 'PROGRESS',
  426. STREAM_NOTIFY_FAILURE => 'FAILURE',
  427. STREAM_NOTIFY_COMPLETED => 'COMPLETED',
  428. STREAM_NOTIFY_RESOLVE => 'RESOLVE',
  429. ];
  430. static $args = ['severity', 'message', 'message_code',
  431. 'bytes_transferred', 'bytes_max'];
  432. $value = \GuzzleHttp\debug_resource($value);
  433. $ident = $request->getMethod() . ' ' . $request->getUri()->withFragment('');
  434. $this->addNotification(
  435. $params,
  436. function () use ($ident, $value, $map, $args) {
  437. $passed = func_get_args();
  438. $code = array_shift($passed);
  439. fprintf($value, '<%s> [%s] ', $ident, $map[$code]);
  440. foreach (array_filter($passed) as $i => $v) {
  441. fwrite($value, $args[$i] . ': "' . $v . '" ');
  442. }
  443. fwrite($value, "\n");
  444. }
  445. );
  446. }
  447. private function addNotification(array &$params, callable $notify)
  448. {
  449. // Wrap the existing function if needed.
  450. if (!isset($params['notification'])) {
  451. $params['notification'] = $notify;
  452. } else {
  453. $params['notification'] = $this->callArray([
  454. $params['notification'],
  455. $notify
  456. ]);
  457. }
  458. }
  459. private function callArray(array $functions)
  460. {
  461. return function () use ($functions) {
  462. $args = func_get_args();
  463. foreach ($functions as $fn) {
  464. call_user_func_array($fn, $args);
  465. }
  466. };
  467. }
  468. }