-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathHttpClientKernel.php
More file actions
142 lines (119 loc) · 5.29 KB
/
Copy pathHttpClientKernel.php
File metadata and controls
142 lines (119 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Mime\Part\AbstractPart;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
use Symfony\Component\Mime\Part\TextPart;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
// Help opcache.preload discover always-needed symbols
class_exists(ResponseHeaderBag::class);
/**
* An implementation of a Symfony HTTP kernel using a "real" HTTP client.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
final class HttpClientKernel implements HttpKernelInterface
{
// RFC 9651 bare items: decimal, integer, string, token, byte sequence, boolean, date and display string
private const SF_BARE_ITEM = '-?\d{1,12}\.\d{1,3}|-?\d{1,15}|"(?:[\x20\x21\x23-\x5B\x5D-\x7E]|\\\\["\\\\])*"|[A-Za-z*][-!#$%&\'*+.^_`|~0-9A-Za-z:/]*|:[A-Za-z0-9+/=]*:|\?[01]|@-?\d{1,15}|%"(?:[\x20\x21\x23\x24\x26-\x7E]|%[0-9a-f]{2})*"';
private HttpClientInterface $client;
public function __construct(?HttpClientInterface $client = null)
{
if (null === $client && !class_exists(HttpClient::class)) {
throw new \LogicException(\sprintf('You cannot use "%s" as the HttpClient component is not installed. Try running "composer require symfony/http-client".', __CLASS__));
}
$this->client = $client ?? HttpClient::create();
}
public function handle(Request $request, int $type = HttpKernelInterface::MAIN_REQUEST, bool $catch = true): Response
{
$headers = $this->getHeaders($request);
$body = '';
if (null !== $part = $this->getBody($request)) {
$headers = array_merge($headers, $part->getPreparedHeaders()->toArray());
$body = $part->bodyToIterable();
}
$response = $this->client->request($request->getMethod(), $request->getUri(), [
'headers' => $headers,
'body' => $body,
] + $request->attributes->get('http_client_options', []) + [
'buffer' => static fn (array $headers): bool => !self::isIncremental($headers['incremental'] ?? []),
]);
$headers = new class($response->getHeaders(!$catch)) extends ResponseHeaderBag {
protected function computeCacheControlValue(): string
{
return $this->getCacheControlHeader(); // preserve the original value
}
};
$headers->remove('X-Body-File');
$headers->remove('X-Body-Eval');
$headers->remove('X-Content-Digest');
if (self::isIncremental($headers->all('incremental'))) {
return new StreamedResponse($this->streamContent($response), $response->getStatusCode(), $headers);
}
return new Response($response->getContent(!$catch), $response->getStatusCode(), $headers);
}
private function getBody(Request $request): ?AbstractPart
{
if (\in_array($request->getMethod(), ['GET', 'HEAD'], true)) {
return null;
}
if (!class_exists(AbstractPart::class)) {
throw new \LogicException('You cannot pass non-empty bodies as the Mime component is not installed. Try running "composer require symfony/mime".');
}
if ($content = $request->getContent()) {
return new TextPart($content, 'utf-8', 'plain', '8bit');
}
$fields = $request->request->all();
foreach ($request->files->all() as $name => $file) {
$fields[$name] = DataPart::fromPath($file->getPathname(), $file->getClientOriginalName(), $file->getClientMimeType());
}
return new FormDataPart($fields);
}
private function getHeaders(Request $request): array
{
$headers = [];
foreach ($request->headers as $key => $value) {
$headers[$key] = $value;
}
$cookies = [];
foreach ($request->cookies->all() as $name => $value) {
$cookies[] = $name.'='.$value;
}
if ($cookies) {
$headers['cookie'] = implode('; ', $cookies);
}
return $headers;
}
private static function isIncremental(array $values): bool
{
// RFC 10036: the field is a Boolean Item, true is "?1" with optional parameters, and any other value is ignored
return 1 === \count($values) && preg_match('{^ *\?1(?:; *[a-z*][-a-z0-9_.*]*(?:=(?:'.self::SF_BARE_ITEM.'))?)* *$}D', $values[0]);
}
private function streamContent(ResponseInterface $response): \Generator
{
while (true) {
foreach ($this->client->stream($response) as $chunk) {
if ($chunk->isTimeout()) {
// stream() stops watching a response after yielding its timeout chunk
continue 2;
}
yield $chunk->getContent();
}
return;
}
}
}