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