You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.8 KiB

3 years ago
  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, int $type = HttpKernelInterface::MAIN_REQUEST, bool $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. }