HttpClientKernel.php 3.6 KB

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