StreamHandler.php 18 KB

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