Ingen beskrivning

HttpClientKernel.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\HttpKernel;
  11. use Symfony\Component\HttpClient\HttpClient;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  15. use Symfony\Component\Mime\Part\AbstractPart;
  16. use Symfony\Component\Mime\Part\DataPart;
  17. use Symfony\Component\Mime\Part\Multipart\FormDataPart;
  18. use Symfony\Component\Mime\Part\TextPart;
  19. use Symfony\Contracts\HttpClient\HttpClientInterface;
  20. // Help opcache.preload discover always-needed symbols
  21. class_exists(ResponseHeaderBag::class);
  22. /**
  23. * An implementation of a Symfony HTTP kernel using a "real" HTTP client.
  24. *
  25. * @author Fabien Potencier <fabien@symfony.com>
  26. */
  27. final class HttpClientKernel implements HttpKernelInterface
  28. {
  29. private $client;
  30. public function __construct(HttpClientInterface $client = null)
  31. {
  32. if (null === $client && !class_exists(HttpClient::class)) {
  33. throw new \LogicException(sprintf('You cannot use "%s" as the HttpClient component is not installed. Try running "composer require symfony/http-client".', __CLASS__));
  34. }
  35. $this->client = $client ?? HttpClient::create();
  36. }
  37. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true): Response
  38. {
  39. $headers = $this->getHeaders($request);
  40. $body = '';
  41. if (null !== $part = $this->getBody($request)) {
  42. $headers = array_merge($headers, $part->getPreparedHeaders()->toArray());
  43. $body = $part->bodyToIterable();
  44. }
  45. $response = $this->client->request($request->getMethod(), $request->getUri(), [
  46. 'headers' => $headers,
  47. 'body' => $body,
  48. ] + $request->attributes->get('http_client_options', []));
  49. $response = new Response($response->getContent(!$catch), $response->getStatusCode(), $response->getHeaders(!$catch));
  50. $response->headers->remove('X-Body-File');
  51. $response->headers->remove('X-Body-Eval');
  52. $response->headers->remove('X-Content-Digest');
  53. $response->headers = new class($response->headers->all()) extends ResponseHeaderBag {
  54. protected function computeCacheControlValue(): string
  55. {
  56. return $this->getCacheControlHeader(); // preserve the original value
  57. }
  58. };
  59. return $response;
  60. }
  61. private function getBody(Request $request): ?AbstractPart
  62. {
  63. if (\in_array($request->getMethod(), ['GET', 'HEAD'])) {
  64. return null;
  65. }
  66. if (!class_exists(AbstractPart::class)) {
  67. throw new \LogicException('You cannot pass non-empty bodies as the Mime component is not installed. Try running "composer require symfony/mime".');
  68. }
  69. if ($content = $request->getContent()) {
  70. return new TextPart($content, 'utf-8', 'plain', '8bit');
  71. }
  72. $fields = $request->request->all();
  73. foreach ($request->files->all() as $name => $file) {
  74. $fields[$name] = DataPart::fromPath($file->getPathname(), $file->getClientOriginalName(), $file->getClientMimeType());
  75. }
  76. return new FormDataPart($fields);
  77. }
  78. private function getHeaders(Request $request): array
  79. {
  80. $headers = [];
  81. foreach ($request->headers as $key => $value) {
  82. $headers[$key] = $value;
  83. }
  84. $cookies = [];
  85. foreach ($request->cookies->all() as $name => $value) {
  86. $cookies[] = $name.'='.$value;
  87. }
  88. if ($cookies) {
  89. $headers['cookie'] = implode('; ', $cookies);
  90. }
  91. return $headers;
  92. }
  93. }