StreamHandler.php 21 KB

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