|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\HttpKernel; |
| 13 | + |
| 14 | +use Psr\Log\LoggerInterface; |
| 15 | +use Psr\Log\NullLogger; |
| 16 | +use Symfony\Component\HttpClient\HttpClient; |
| 17 | +use Symfony\Component\HttpFoundation\Request; |
| 18 | +use Symfony\Component\HttpFoundation\Response; |
| 19 | +use Symfony\Component\Mime\Part\AbstractPart; |
| 20 | +use Symfony\Component\Mime\Part\DataPart; |
| 21 | +use Symfony\Component\Mime\Part\Multipart\FormDataPart; |
| 22 | +use Symfony\Component\Mime\Part\TextPart; |
| 23 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 24 | + |
| 25 | +/** |
| 26 | + * An implementation of a Symfony HTTP kernel using a "real" HTTP client. |
| 27 | + * |
| 28 | + * @author Fabien Potencier <[email protected]> |
| 29 | + */ |
| 30 | +final class RealHttpKernel implements HttpKernelInterface |
| 31 | +{ |
| 32 | + private $client; |
| 33 | + private $logger; |
| 34 | + |
| 35 | + public function __construct(HttpClientInterface $client = null, LoggerInterface $logger = null) |
| 36 | + { |
| 37 | + if (!class_exists(HttpClient::class)) { |
| 38 | + throw new \LogicException(sprintf('You cannot use "%s" as the HttpClient component is not installed. Try running "composer require symfony/http-client".', __CLASS__)); |
| 39 | + } |
| 40 | + |
| 41 | + $this->client = $client ?? HttpClient::create(); |
| 42 | + $this->logger = $logger ?? new NullLogger(); |
| 43 | + } |
| 44 | + |
| 45 | + public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) |
| 46 | + { |
| 47 | + $this->logger->debug(sprintf('Request: %s %s', $request->getMethod(), $request->getUri())); |
| 48 | + |
| 49 | + $headers = $this->getHeaders($request); |
| 50 | + $body = ''; |
| 51 | + if (null !== $part = $this->getBody($request)) { |
| 52 | + $headers = array_merge($headers, $part->getPreparedHeaders()->toArray()); |
| 53 | + $body = $part->bodyToIterable(); |
| 54 | + } |
| 55 | + $response = $this->client->request($request->getMethod(), $request->getUri(), [ |
| 56 | + 'headers' => $headers, |
| 57 | + 'body' => $body, |
| 58 | + 'max_redirects' => 0, |
| 59 | + ] + $request->attributes->get('http_client_options', [])); |
| 60 | + |
| 61 | + $this->logger->debug(sprintf('Response: %s %s', $response->getStatusCode(), $request->getUri())); |
| 62 | + |
| 63 | + return new Response($response->getContent(!$catch), $response->getStatusCode(), $response->getHeaders(!$catch)); |
| 64 | + } |
| 65 | + |
| 66 | + private function getBody(Request $request): ?AbstractPart |
| 67 | + { |
| 68 | + if (\in_array($request->getMethod(), ['GET', 'HEAD'])) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + if (!class_exists(AbstractPart::class)) { |
| 73 | + throw new \LogicException('You cannot pass non-empty bodies as the Mime component is not installed. Try running "composer require symfony/mime".'); |
| 74 | + } |
| 75 | + |
| 76 | + if ($content = $request->getContent()) { |
| 77 | + return new TextPart($content, 'utf-8', 'plain', '8bit'); |
| 78 | + } |
| 79 | + |
| 80 | + $fields = $request->request->all(); |
| 81 | + foreach ($request->files->all() as $name => $file) { |
| 82 | + $fields[$name] = DataPart::fromPath($file->getPathname(), $file->getClientOriginalName(), $file->getClientMimeType()); |
| 83 | + } |
| 84 | + |
| 85 | + return new FormDataPart($fields); |
| 86 | + } |
| 87 | + |
| 88 | + private function getHeaders(Request $request): array |
| 89 | + { |
| 90 | + $headers = []; |
| 91 | + foreach ($request->headers as $key => $value) { |
| 92 | + $headers[$key] = $value; |
| 93 | + } |
| 94 | + $cookies = []; |
| 95 | + foreach ($request->cookies->all() as $name => $value) { |
| 96 | + $cookies[] = $name.'='.$value; |
| 97 | + } |
| 98 | + if ($cookies) { |
| 99 | + $headers['cookie'] = implode('; ', $cookies); |
| 100 | + } |
| 101 | + |
| 102 | + return $headers; |
| 103 | + } |
| 104 | +} |
0 commit comments