NativeHttpClient.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpClient;
  11. use Psr\Log\LoggerAwareInterface;
  12. use Psr\Log\LoggerAwareTrait;
  13. use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
  14. use Symfony\Component\HttpClient\Exception\TransportException;
  15. use Symfony\Component\HttpClient\Internal\NativeClientState;
  16. use Symfony\Component\HttpClient\Response\NativeResponse;
  17. use Symfony\Component\HttpClient\Response\ResponseStream;
  18. use Symfony\Contracts\HttpClient\HttpClientInterface;
  19. use Symfony\Contracts\HttpClient\ResponseInterface;
  20. use Symfony\Contracts\HttpClient\ResponseStreamInterface;
  21. use Symfony\Contracts\Service\ResetInterface;
  22. /**
  23. * A portable implementation of the HttpClientInterface contracts based on PHP stream wrappers.
  24. *
  25. * PHP stream wrappers are able to fetch response bodies concurrently,
  26. * but each request is opened synchronously.
  27. *
  28. * @author Nicolas Grekas <p@tchwork.com>
  29. */
  30. final class NativeHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
  31. {
  32. use HttpClientTrait;
  33. use LoggerAwareTrait;
  34. public const OPTIONS_DEFAULTS = HttpClientInterface::OPTIONS_DEFAULTS + [
  35. 'crypto_method' => \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
  36. ];
  37. /**
  38. * @var mixed[]
  39. */
  40. private $defaultOptions = self::OPTIONS_DEFAULTS;
  41. /**
  42. * @var mixed[]
  43. */
  44. private static $emptyDefaults = self::OPTIONS_DEFAULTS;
  45. /**
  46. * @var \Symfony\Component\HttpClient\Internal\NativeClientState
  47. */
  48. private $multi;
  49. /**
  50. * @param array $defaultOptions Default request's options
  51. * @param int $maxHostConnections The maximum number of connections to open
  52. *
  53. * @see HttpClientInterface::OPTIONS_DEFAULTS for available options
  54. */
  55. public function __construct($defaultOptions = [], $maxHostConnections = 6)
  56. {
  57. $this->defaultOptions['buffer'] = $this->defaultOptions['buffer'] ?? \Closure::fromCallable([self::class, 'shouldBuffer']);
  58. if ($defaultOptions) {
  59. [, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, $this->defaultOptions);
  60. }
  61. $this->multi = new NativeClientState();
  62. $this->multi->maxHostConnections = 0 < $maxHostConnections ? $maxHostConnections : \PHP_INT_MAX;
  63. }
  64. /**
  65. * @see HttpClientInterface::OPTIONS_DEFAULTS for available options
  66. * @param string $method
  67. * @param string $url
  68. * @param mixed[] $options
  69. */
  70. public function request($method, $url, $options = [])
  71. {
  72. [$url, $options] = self::prepareRequest($method, $url, $options, $this->defaultOptions);
  73. if ($options['bindto']) {
  74. if (file_exists($options['bindto'])) {
  75. throw new TransportException(__CLASS__.' cannot bind to local Unix sockets, use e.g. CurlHttpClient instead.');
  76. }
  77. if (strncmp($options['bindto'], 'if!', strlen('if!')) === 0) {
  78. throw new TransportException(__CLASS__.' cannot bind to network interfaces, use e.g. CurlHttpClient instead.');
  79. }
  80. if (strncmp($options['bindto'], 'host!', strlen('host!')) === 0) {
  81. $options['bindto'] = substr($options['bindto'], 5);
  82. }
  83. }
  84. $hasContentLength = isset($options['normalized_headers']['content-length']);
  85. $hasBody = '' !== $options['body'] || 'POST' === $method || $hasContentLength;
  86. $options['body'] = self::getBodyAsString($options['body']);
  87. if ('chunked' === substr($options['normalized_headers']['transfer-encoding'][0] ?? '', \strlen('Transfer-Encoding: '))) {
  88. unset($options['normalized_headers']['transfer-encoding']);
  89. $options['headers'] = array_merge(...array_values($options['normalized_headers']));
  90. $options['body'] = self::dechunk($options['body']);
  91. }
  92. if ('' === $options['body'] && $hasBody && !$hasContentLength) {
  93. $options['headers'][] = 'Content-Length: 0';
  94. }
  95. if ($hasBody && !isset($options['normalized_headers']['content-type'])) {
  96. $options['headers'][] = 'Content-Type: application/x-www-form-urlencoded';
  97. }
  98. if (\extension_loaded('zlib') && !isset($options['normalized_headers']['accept-encoding'])) {
  99. // gzip is the most widely available algo, no need to deal with deflate
  100. $options['headers'][] = 'Accept-Encoding: gzip';
  101. }
  102. if ($options['peer_fingerprint']) {
  103. if (isset($options['peer_fingerprint']['pin-sha256']) && 1 === \count($options['peer_fingerprint'])) {
  104. throw new TransportException(__CLASS__.' cannot verify "pin-sha256" fingerprints, please provide a "sha256" one.');
  105. }
  106. unset($options['peer_fingerprint']['pin-sha256']);
  107. }
  108. $info = [
  109. 'response_headers' => [],
  110. 'url' => $url,
  111. 'error' => null,
  112. 'canceled' => false,
  113. 'http_method' => $method,
  114. 'http_code' => 0,
  115. 'redirect_count' => 0,
  116. 'start_time' => 0.0,
  117. 'connect_time' => 0.0,
  118. 'redirect_time' => 0.0,
  119. 'pretransfer_time' => 0.0,
  120. 'starttransfer_time' => 0.0,
  121. 'total_time' => 0.0,
  122. 'namelookup_time' => 0.0,
  123. 'size_upload' => 0,
  124. 'size_download' => 0,
  125. 'size_body' => \strlen($options['body']),
  126. 'primary_ip' => '',
  127. 'primary_port' => 'http:' === $url['scheme'] ? 80 : 443,
  128. 'debug' => \extension_loaded('curl') ? '' : "* Enable the curl extension for better performance\n",
  129. ];
  130. if ($onProgress = $options['on_progress']) {
  131. $maxDuration = 0 < $options['max_duration'] ? $options['max_duration'] : \INF;
  132. $onProgress = static function (...$progress) use ($onProgress, &$info, $maxDuration) {
  133. if ($info['total_time'] >= $maxDuration) {
  134. throw new TransportException(sprintf('Max duration was reached for "%s".', implode('', $info['url'])));
  135. }
  136. $progressInfo = $info;
  137. $progressInfo['url'] = implode('', $info['url']);
  138. unset($progressInfo['size_body']);
  139. // Memoize the last progress to ease calling the callback periodically when no network transfer happens
  140. static $lastProgress = [0, 0];
  141. if ($progress && -1 === $progress[0]) {
  142. // Response completed
  143. $lastProgress[0] = max($lastProgress);
  144. } else {
  145. $lastProgress = $progress ?: $lastProgress;
  146. }
  147. $onProgress($lastProgress[0], $lastProgress[1], $progressInfo);
  148. };
  149. } elseif (0 < $options['max_duration']) {
  150. $maxDuration = $options['max_duration'];
  151. $onProgress = static function () use (&$info, $maxDuration): void {
  152. if ($info['total_time'] >= $maxDuration) {
  153. throw new TransportException(sprintf('Max duration was reached for "%s".', implode('', $info['url'])));
  154. }
  155. };
  156. }
  157. // Always register a notification callback to compute live stats about the response
  158. $notification = static function (int $code, int $severity, ?string $msg, int $msgCode, int $dlNow, int $dlSize) use ($onProgress, &$info) {
  159. $info['total_time'] = microtime(true) - $info['start_time'];
  160. if (\STREAM_NOTIFY_PROGRESS === $code) {
  161. $info['starttransfer_time'] = $info['starttransfer_time'] ?: $info['total_time'];
  162. $info['size_upload'] += $dlNow ? 0 : $info['size_body'];
  163. $info['size_download'] = $dlNow;
  164. } elseif (\STREAM_NOTIFY_CONNECT === $code) {
  165. $info['connect_time'] = $info['total_time'];
  166. $info['debug'] .= $info['request_header'];
  167. unset($info['request_header']);
  168. } else {
  169. return;
  170. }
  171. if ($onProgress) {
  172. $onProgress($dlNow, $dlSize);
  173. }
  174. };
  175. if ($options['resolve']) {
  176. $this->multi->dnsCache = $options['resolve'] + $this->multi->dnsCache;
  177. }
  178. ($logger = $this->logger) ? $logger->info(sprintf('Request: "%s %s"', $method, implode('', $url))) : null;
  179. if (!isset($options['normalized_headers']['user-agent'])) {
  180. $options['headers'][] = 'User-Agent: Symfony HttpClient (Native)';
  181. }
  182. if (0 < $options['max_duration']) {
  183. $options['timeout'] = min($options['max_duration'], $options['timeout']);
  184. }
  185. switch ($cryptoMethod = $options['crypto_method']) {
  186. case \STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT: $cryptoMethod |= \STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
  187. case \STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT: $cryptoMethod |= \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
  188. case \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT: $cryptoMethod |= \STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT;
  189. }
  190. $context = [
  191. 'http' => [
  192. 'protocol_version' => min($options['http_version'] ?: '1.1', '1.1'),
  193. 'method' => $method,
  194. 'content' => $options['body'],
  195. 'ignore_errors' => true,
  196. 'curl_verify_ssl_peer' => $options['verify_peer'],
  197. 'curl_verify_ssl_host' => $options['verify_host'],
  198. 'auto_decode' => false, // Disable dechunk filter, it's incompatible with stream_select()
  199. 'timeout' => $options['timeout'],
  200. 'follow_location' => false, // We follow redirects ourselves - the native logic is too limited
  201. ],
  202. 'ssl' => array_filter([
  203. 'verify_peer' => $options['verify_peer'],
  204. 'verify_peer_name' => $options['verify_host'],
  205. 'cafile' => $options['cafile'],
  206. 'capath' => $options['capath'],
  207. 'local_cert' => $options['local_cert'],
  208. 'local_pk' => $options['local_pk'],
  209. 'passphrase' => $options['passphrase'],
  210. 'ciphers' => $options['ciphers'],
  211. 'peer_fingerprint' => $options['peer_fingerprint'],
  212. 'capture_peer_cert_chain' => $options['capture_peer_cert_chain'],
  213. 'allow_self_signed' => (bool) $options['peer_fingerprint'],
  214. 'SNI_enabled' => true,
  215. 'disable_compression' => true,
  216. 'crypto_method' => $cryptoMethod,
  217. ], static function ($v) {
  218. return null !== $v;
  219. }),
  220. 'socket' => [
  221. 'bindto' => $options['bindto'],
  222. 'tcp_nodelay' => true,
  223. ],
  224. ];
  225. $context = stream_context_create($context, ['notification' => $notification]);
  226. $resolver = static function ($multi) use ($context, $options, $url, &$info, $onProgress) {
  227. [$host, $port] = self::parseHostPort($url, $info);
  228. if (!isset($options['normalized_headers']['host'])) {
  229. $options['headers'][] = 'Host: '.$host.$port;
  230. }
  231. $proxy = self::getProxy($options['proxy'], $url, $options['no_proxy']);
  232. if (!self::configureHeadersAndProxy($context, $host, $options['headers'], $proxy, 'https:' === $url['scheme'])) {
  233. $ip = self::dnsResolve($host, $multi, $info, $onProgress);
  234. $url['authority'] = substr_replace($url['authority'], $ip, -\strlen($host) - \strlen($port), \strlen($host));
  235. }
  236. return [self::createRedirectResolver($options, $host, $port, $proxy, $info, $onProgress), implode('', $url)];
  237. };
  238. return new NativeResponse($this->multi, $context, implode('', $url), $options, $info, $resolver, $onProgress, $this->logger);
  239. }
  240. /**
  241. * @param \Symfony\Contracts\HttpClient\ResponseInterface|mixed[] $responses
  242. * @param float|null $timeout
  243. */
  244. public function stream($responses, $timeout = null)
  245. {
  246. if ($responses instanceof NativeResponse) {
  247. $responses = [$responses];
  248. }
  249. return new ResponseStream(NativeResponse::stream($responses, $timeout));
  250. }
  251. public function reset()
  252. {
  253. $this->multi->reset();
  254. }
  255. private static function getBodyAsString($body)
  256. {
  257. if (\is_resource($body)) {
  258. return stream_get_contents($body);
  259. }
  260. if (!$body instanceof \Closure) {
  261. return $body;
  262. }
  263. $result = '';
  264. while ('' !== $data = $body(self::$CHUNK_SIZE)) {
  265. if (!\is_string($data)) {
  266. throw new TransportException(sprintf('Return value of the "body" option callback must be string, "%s" returned.', get_debug_type($data)));
  267. }
  268. $result .= $data;
  269. }
  270. return $result;
  271. }
  272. /**
  273. * Extracts the host and the port from the URL.
  274. * @param mixed[] $url
  275. * @param mixed[] $info
  276. */
  277. private static function parseHostPort($url, &$info)
  278. {
  279. if ($port = parse_url($url['authority'], \PHP_URL_PORT) ?: '') {
  280. $info['primary_port'] = $port;
  281. $port = ':'.$port;
  282. } else {
  283. $info['primary_port'] = 'http:' === $url['scheme'] ? 80 : 443;
  284. }
  285. return [parse_url($url['authority'], \PHP_URL_HOST), $port];
  286. }
  287. /**
  288. * Resolves the IP of the host using the local DNS cache if possible.
  289. * @param string $host
  290. * @param \Symfony\Component\HttpClient\Internal\NativeClientState $multi
  291. * @param mixed[] $info
  292. * @param \Closure|null $onProgress
  293. */
  294. private static function dnsResolve($host, $multi, &$info, $onProgress)
  295. {
  296. if (null === $ip = $multi->dnsCache[$host] ?? null) {
  297. $info['debug'] .= "* Hostname was NOT found in DNS cache\n";
  298. $now = microtime(true);
  299. if (!$ip = gethostbynamel($host)) {
  300. throw new TransportException(sprintf('Could not resolve host "%s".', $host));
  301. }
  302. $info['namelookup_time'] = microtime(true) - ($info['start_time'] ?: $now);
  303. $multi->dnsCache[$host] = $ip = $ip[0];
  304. $info['debug'] .= "* Added {$host}:0:{$ip} to DNS cache\n";
  305. } else {
  306. $info['debug'] .= "* Hostname was found in DNS cache\n";
  307. }
  308. $info['primary_ip'] = $ip;
  309. if ($onProgress) {
  310. // Notify DNS resolution
  311. $onProgress();
  312. }
  313. return $ip;
  314. }
  315. /**
  316. * Handles redirects - the native logic is too buggy to be used.
  317. * @param mixed[] $options
  318. * @param string $host
  319. * @param string $port
  320. * @param mixed[]|null $proxy
  321. * @param mixed[] $info
  322. * @param \Closure|null $onProgress
  323. */
  324. private static function createRedirectResolver($options, $host, $port, $proxy, &$info, $onProgress)
  325. {
  326. $redirectHeaders = [];
  327. if (0 < $maxRedirects = $options['max_redirects']) {
  328. $redirectHeaders = ['host' => $host, 'port' => $port];
  329. $redirectHeaders['with_auth'] = $redirectHeaders['no_auth'] = array_filter($options['headers'], static function ($h) {
  330. return 0 !== stripos($h, 'Host:');
  331. });
  332. if (isset($options['normalized_headers']['authorization']) || isset($options['normalized_headers']['cookie'])) {
  333. $redirectHeaders['no_auth'] = array_filter($redirectHeaders['no_auth'], static function ($h) {
  334. return 0 !== stripos($h, 'Authorization:') && 0 !== stripos($h, 'Cookie:');
  335. });
  336. }
  337. }
  338. return static function (NativeClientState $multi, ?string $location, $context) use (&$redirectHeaders, $proxy, &$info, $maxRedirects, $onProgress): ?string {
  339. if (null === $location || $info['http_code'] < 300 || 400 <= $info['http_code']) {
  340. $info['redirect_url'] = null;
  341. return null;
  342. }
  343. try {
  344. $url = self::parseUrl($location);
  345. } catch (InvalidArgumentException $exception) {
  346. $info['redirect_url'] = null;
  347. return null;
  348. }
  349. $url = self::resolveUrl($url, $info['url']);
  350. $info['redirect_url'] = implode('', $url);
  351. if ($info['redirect_count'] >= $maxRedirects) {
  352. return null;
  353. }
  354. $info['url'] = $url;
  355. ++$info['redirect_count'];
  356. $info['redirect_time'] = microtime(true) - $info['start_time'];
  357. // Do like curl and browsers: turn POST to GET on 301, 302 and 303
  358. if (\in_array($info['http_code'], [301, 302, 303], true)) {
  359. $options = stream_context_get_options($context)['http'];
  360. if ('POST' === $options['method'] || 303 === $info['http_code']) {
  361. $info['http_method'] = $options['method'] = 'HEAD' === $options['method'] ? 'HEAD' : 'GET';
  362. $options['content'] = '';
  363. $filterContentHeaders = static function ($h) {
  364. return 0 !== stripos($h, 'Content-Length:') && 0 !== stripos($h, 'Content-Type:') && 0 !== stripos($h, 'Transfer-Encoding:');
  365. };
  366. $options['header'] = array_filter($options['header'], $filterContentHeaders);
  367. $redirectHeaders['no_auth'] = array_filter($redirectHeaders['no_auth'], $filterContentHeaders);
  368. $redirectHeaders['with_auth'] = array_filter($redirectHeaders['with_auth'], $filterContentHeaders);
  369. stream_context_set_option($context, ['http' => $options]);
  370. }
  371. }
  372. [$host, $port] = self::parseHostPort($url, $info);
  373. if (false !== (parse_url($location, \PHP_URL_HOST) ?? false)) {
  374. // Authorization and Cookie headers MUST NOT follow except for the initial host name
  375. $requestHeaders = $redirectHeaders['host'] === $host && $redirectHeaders['port'] === $port ? $redirectHeaders['with_auth'] : $redirectHeaders['no_auth'];
  376. $requestHeaders[] = 'Host: '.$host.$port;
  377. $dnsResolve = !self::configureHeadersAndProxy($context, $host, $requestHeaders, $proxy, 'https:' === $url['scheme']);
  378. } else {
  379. $dnsResolve = isset(stream_context_get_options($context)['ssl']['peer_name']);
  380. }
  381. if ($dnsResolve) {
  382. $ip = self::dnsResolve($host, $multi, $info, $onProgress);
  383. $url['authority'] = substr_replace($url['authority'], $ip, -\strlen($host) - \strlen($port), \strlen($host));
  384. }
  385. return implode('', $url);
  386. };
  387. }
  388. /**
  389. * @param string $host
  390. * @param mixed[] $requestHeaders
  391. * @param mixed[]|null $proxy
  392. * @param bool $isSsl
  393. */
  394. private static function configureHeadersAndProxy($context, $host, $requestHeaders, $proxy, $isSsl)
  395. {
  396. if (null === $proxy) {
  397. stream_context_set_option($context, 'http', 'header', $requestHeaders);
  398. stream_context_set_option($context, 'ssl', 'peer_name', $host);
  399. return false;
  400. }
  401. // Matching "no_proxy" should follow the behavior of curl
  402. foreach ($proxy['no_proxy'] as $rule) {
  403. $dotRule = '.'.ltrim($rule, '.');
  404. if ('*' === $rule || $host === $rule || substr_compare($host, $dotRule, -strlen($dotRule)) === 0) {
  405. stream_context_set_option($context, 'http', 'proxy', null);
  406. stream_context_set_option($context, 'http', 'request_fulluri', false);
  407. stream_context_set_option($context, 'http', 'header', $requestHeaders);
  408. stream_context_set_option($context, 'ssl', 'peer_name', $host);
  409. return false;
  410. }
  411. }
  412. if (null !== $proxy['auth']) {
  413. $requestHeaders[] = 'Proxy-Authorization: '.$proxy['auth'];
  414. }
  415. stream_context_set_option($context, 'http', 'proxy', $proxy['url']);
  416. stream_context_set_option($context, 'http', 'request_fulluri', !$isSsl);
  417. stream_context_set_option($context, 'http', 'header', $requestHeaders);
  418. stream_context_set_option($context, 'ssl', 'peer_name', null);
  419. return true;
  420. }
  421. }