NativeHttpClient.php 19 KB

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