Attribute
2 years ago
Bundle
1 year ago
CacheClearer
2 years ago
CacheWarmer
1 year ago
Config
1 year ago
Controller
1 year ago
ControllerMetadata
1 year ago
DataCollector
1 year ago
Debug
1 year ago
DependencyInjection
1 year ago
Event
1 year ago
EventListener
1 year ago
Exception
1 year ago
Fragment
1 year ago
HttpCache
1 year ago
Log
1 year ago
Profiler
1 year ago
Resources
2 years ago
HttpClientKernel.php
1 year ago
HttpKernel.php
1 year ago
HttpKernelBrowser.php
1 year ago
HttpKernelInterface.php
1 year ago
Kernel.php
3 months ago
KernelEvents.php
2 years ago
KernelInterface.php
2 years ago
LICENSE
2 years ago
README.md
2 years ago
RebootableInterface.php
2 years ago
TerminableInterface.php
2 years ago
UriSigner.php
1 year ago
HttpClientKernel.php
95 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace Matomo\Dependencies\Symfony\Component\HttpKernel; |
| 12 | |
| 13 | use Symfony\Component\HttpClient\HttpClient; |
| 14 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\Request; |
| 15 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\Response; |
| 16 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\ResponseHeaderBag; |
| 17 | use Symfony\Component\Mime\Part\AbstractPart; |
| 18 | use Symfony\Component\Mime\Part\DataPart; |
| 19 | use Symfony\Component\Mime\Part\Multipart\FormDataPart; |
| 20 | use Symfony\Component\Mime\Part\TextPart; |
| 21 | use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 22 | // Help opcache.preload discover always-needed symbols |
| 23 | class_exists(ResponseHeaderBag::class); |
| 24 | /** |
| 25 | * An implementation of a Symfony HTTP kernel using a "real" HTTP client. |
| 26 | * |
| 27 | * @author Fabien Potencier <fabien@symfony.com> |
| 28 | */ |
| 29 | final class HttpClientKernel implements HttpKernelInterface |
| 30 | { |
| 31 | private $client; |
| 32 | public function __construct(?HttpClientInterface $client = null) |
| 33 | { |
| 34 | if (null === $client && !class_exists(HttpClient::class)) { |
| 35 | throw new \LogicException(sprintf('You cannot use "%s" as the HttpClient component is not installed. Try running "composer require symfony/http-client".', __CLASS__)); |
| 36 | } |
| 37 | $this->client = $client ?? HttpClient::create(); |
| 38 | } |
| 39 | public function handle(Request $request, int $type = HttpKernelInterface::MAIN_REQUEST, bool $catch = \true) : Response |
| 40 | { |
| 41 | $headers = $this->getHeaders($request); |
| 42 | $body = ''; |
| 43 | if (null !== ($part = $this->getBody($request))) { |
| 44 | $headers = array_merge($headers, $part->getPreparedHeaders()->toArray()); |
| 45 | $body = $part->bodyToIterable(); |
| 46 | } |
| 47 | $response = $this->client->request($request->getMethod(), $request->getUri(), ['headers' => $headers, 'body' => $body] + $request->attributes->get('http_client_options', [])); |
| 48 | $response = new Response($response->getContent(!$catch), $response->getStatusCode(), $response->getHeaders(!$catch)); |
| 49 | $response->headers->remove('X-Body-File'); |
| 50 | $response->headers->remove('X-Body-Eval'); |
| 51 | $response->headers->remove('X-Content-Digest'); |
| 52 | $response->headers = new class($response->headers->all()) extends ResponseHeaderBag |
| 53 | { |
| 54 | protected function computeCacheControlValue() : string |
| 55 | { |
| 56 | return $this->getCacheControlHeader(); |
| 57 | // preserve the original value |
| 58 | } |
| 59 | }; |
| 60 | return $response; |
| 61 | } |
| 62 | private function getBody(Request $request) : ?AbstractPart |
| 63 | { |
| 64 | if (\in_array($request->getMethod(), ['GET', 'HEAD'])) { |
| 65 | return null; |
| 66 | } |
| 67 | if (!class_exists(AbstractPart::class)) { |
| 68 | throw new \LogicException('You cannot pass non-empty bodies as the Mime component is not installed. Try running "composer require symfony/mime".'); |
| 69 | } |
| 70 | if ($content = $request->getContent()) { |
| 71 | return new TextPart($content, 'utf-8', 'plain', '8bit'); |
| 72 | } |
| 73 | $fields = $request->request->all(); |
| 74 | foreach ($request->files->all() as $name => $file) { |
| 75 | $fields[$name] = DataPart::fromPath($file->getPathname(), $file->getClientOriginalName(), $file->getClientMimeType()); |
| 76 | } |
| 77 | return new FormDataPart($fields); |
| 78 | } |
| 79 | private function getHeaders(Request $request) : array |
| 80 | { |
| 81 | $headers = []; |
| 82 | foreach ($request->headers as $key => $value) { |
| 83 | $headers[$key] = $value; |
| 84 | } |
| 85 | $cookies = []; |
| 86 | foreach ($request->cookies->all() as $name => $value) { |
| 87 | $cookies[] = $name . '=' . $value; |
| 88 | } |
| 89 | if ($cookies) { |
| 90 | $headers['cookie'] = implode('; ', $cookies); |
| 91 | } |
| 92 | return $headers; |
| 93 | } |
| 94 | } |
| 95 |