Repository navigation
Expand file tree
/
Copy pathRecorderHttpClient.php
More file actions
236 lines (196 loc) · 10.2 KB
/
Copy pathRecorderHttpClient.php
File metadata and controls
236 lines (196 loc) · 10.2 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?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\HttpClient;
use Symfony\Component\HttpClient\Exception\HarEntryNotFoundException;
use Symfony\Component\HttpClient\Har\HarFile;
use Symfony\Component\HttpClient\Recorder\RecorderConfiguration;
use Symfony\Component\HttpClient\Recorder\RecorderConfigurationInterface;
use Symfony\Component\HttpClient\Recorder\RecorderMode;
use Symfony\Component\HttpClient\Recorder\Redactor\DefaultRedactor;
use Symfony\Component\HttpClient\Recorder\Redactor\RedactorInterface;
use Symfony\Component\HttpClient\Response\AsyncContext;
use Symfony\Component\HttpClient\Response\AsyncResponse;
use Symfony\Contracts\HttpClient\ChunkInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
/**
* Records HTTP exchanges into a HAR file and replays them, driven by a RecorderConfigurationInterface.
*
* Requests are matched on their method, URL and body, compared with secrets masked and with the random boundary of a multipart body replaced by a fixed one.
*
* Only fully received responses are recorded: a response that is destroyed unread, canceled, or consumed
* through the exception thrown for an error status is skipped, so that replaying it misses loudly instead
* of serving an empty body. Recording an error response therefore requires consuming it without throwing,
* with getContent(false) or toArray(false).
*/
final class RecorderHttpClient implements HttpClientInterface
{
use AsyncDecoratorTrait {
AsyncDecoratorTrait::withOptions insteadof HttpClientTrait;
}
use HttpClientTrait;
private const MULTIPART_BOUNDARY = 'symfony-http-recorder';
private array $defaultOptions = self::OPTIONS_DEFAULTS;
private ?MockHttpClient $replayClient = null;
private ?ResponseInterface $replayedResponse = null;
public function __construct(
HttpClientInterface $inner,
private readonly RecorderConfigurationInterface $configuration = new RecorderConfiguration(),
private readonly RedactorInterface $redactor = new DefaultRedactor(),
array $defaultOptions = [],
) {
$this->client = $inner;
if ($defaultOptions) {
[, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, $this->defaultOptions);
}
}
public function request(string $method, string $url, array $options = []): ResponseInterface
{
// the caller's original options are forwarded as is in every mode, so that a client
// decorated by this one keeps applying its own defaults; normalization only happens
// internally, to compute the HAR key or feed the replay client
return match ($mode = $this->configuration->getMode()) {
RecorderMode::Passthrough => new AsyncResponse($this->client, $method, $url, $options),
RecorderMode::Record => $this->record($method, $url, $options, true),
RecorderMode::Replay, RecorderMode::Missing => $this->replay($method, $url, $options, RecorderMode::Missing === $mode),
};
}
public function withOptions(array $options): static
{
$clone = clone $this;
$clone->client = $this->client->withOptions($options);
$clone->defaultOptions = self::mergeDefaultOptions($options, $this->defaultOptions);
$clone->replayClient = null;
return $clone;
}
/**
* @return array{string, string, array}
*/
private function prepare(string $method, string $url, array $options): array
{
[$url, $options] = self::prepareRequest($method, $url, $options, $this->defaultOptions);
return [$method, implode('', $url), $options];
}
/**
* Describes the request the way it is recorded: secrets masked and the boundary of a multipart body fixed.
*
* @return array{string, ?string, array<string, string[]>} The URL, the body (null when it is not a string) and the headers
*/
private function describe(string $url, array $options): array
{
$headers = self::normalizeHeadersForRedactor($options['normalized_headers'] ?? []);
$body = \is_string($options['body'] ?? null) ? $options['body'] : null;
if (preg_match('{^multipart/[^;]++;.*?\bboundary=(?|"([^"]++)"|([^\s;]++))}i', $headers['content-type'][0] ?? '', $m, \PREG_OFFSET_CAPTURE)) {
[$boundary, $offset] = $m[1];
$headers['content-type'][0] = substr_replace($headers['content-type'][0], self::MULTIPART_BOUNDARY, $offset, \strlen($boundary));
$body = null !== $body ? str_replace('--'.$boundary, '--'.self::MULTIPART_BOUNDARY, $body) : null;
}
return [$this->redactor->redactUrl($url), null !== $body ? $this->redactor->redactBody($body) : null, $this->redactor->redactHeaders($headers)];
}
/**
* @throws HarEntryNotFoundException when no entry matches and recording what is missing is off
*/
private function replay(string $method, string $url, array $options, bool $recordMissing): ResponseInterface
{
// the replay client never touches the network, so it can safely be fed the fully
// normalized request: there is no inner client whose own defaults could be shadowed
[, $normalizedUrl, $normalizedOptions] = $this->prepare($method, $url, $options);
[$recordedUrl, $recordedBody] = $this->describe($normalizedUrl, $normalizedOptions);
try {
$response = $this->findResponse($method, $recordedUrl, $recordedBody, $recordMissing);
} catch (HarEntryNotFoundException $e) {
if ($recordMissing) {
// the caller's original options, untouched, so the real request below gets them
return $this->record($method, $url, $options, false);
}
$this->configuration->reportMiss($method, $recordedUrl);
throw $e;
}
// clear the query option before handing the options over, because the URL
// prepared by this client already carries it and MockHttpClient would otherwise merge it a second time
$normalizedOptions['query'] = [];
// The client is shared so that multiple responses can be streamed together (AsyncResponse::stream() requires all responses to share one client)
$this->replayClient ??= new MockHttpClient(fn () => $this->replayedResponse);
$this->replayedResponse = $response;
try {
return new AsyncResponse($this->replayClient, $method, $normalizedUrl, $normalizedOptions);
} finally {
$this->replayedResponse = null;
}
}
private function findResponse(string $method, string $url, ?string $body, bool $recordMissing): ResponseInterface
{
if (!is_file($harFilePath = $this->configuration->getHarFilePath())) {
throw new HarEntryNotFoundException(\sprintf('No HAR file found at "%s" for HTTP request "%s" "%s".', $harFilePath, $method, $url));
}
$har = HarFile::fromFile($harFilePath);
// when recording is allowed, a request whose recorded responses were all served is a miss, so that
// e.g. polling until a 202 turns into a 200 records every step instead of replaying the first one forever
$index = $har->findEntryIndex($method, $url, $body, $this->configuration->getConsumedEntries(), !$recordMissing);
$this->configuration->consumeEntry($index);
return $har->createResponse($index);
}
private function record(string $method, string $url, array $options, bool $rewrite): ResponseInterface
{
// normalized only to compute the HAR key: the real request below keeps the caller's
// original options, so that $this->client still applies its own default options
[, $normalizedUrl, $normalizedOptions] = $this->prepare($method, $url, $options);
[$recordedUrl, $requestBody, $requestHeaders] = $this->describe($normalizedUrl, $normalizedOptions);
$buffer = '';
return new AsyncResponse($this->client, $method, $url, $options, function (ChunkInterface $chunk, AsyncContext $context) use (&$buffer, $method, $recordedUrl, $requestBody, $requestHeaders, $rewrite): \Generator {
if (null !== $chunk->getError()) {
yield $chunk;
return;
}
if (!$chunk->isFirst() && !$chunk->isLast()) {
$buffer .= $chunk->getContent();
}
if ($chunk->isLast() && !$context->getInfo('canceled')) {
$this->persist(HarFile::createEntry(
$method,
$recordedUrl,
'' !== $requestBody ? $requestBody : null,
$requestHeaders,
$context->getStatusCode(),
$this->redactor->redactHeaders($context->getHeaders()),
$this->redactor->redactBody($buffer),
), $rewrite);
}
yield $chunk;
});
}
private function persist(array $entry, bool $rewrite): void
{
$harFilePath = $this->configuration->getHarFilePath();
// recording rewrites the file entry by entry, while recording what is missing appends to it
$index = $rewrite
? HarFile::rewrite($harFilePath, \count($this->configuration->getConsumedEntries()), $entry)
: HarFile::update($harFilePath, static fn (HarFile $har): int => $har->addEntry($entry));
// an entry recorded during the session is not served again to the next identical request
$this->configuration->consumeEntry($index);
}
/**
* @param array<string, list<string>> $normalizedHeaders
*
* @return array<string, string[]>
*/
private static function normalizeHeadersForRedactor(array $normalizedHeaders): array
{
$headers = [];
foreach ($normalizedHeaders as $name => $values) {
foreach ($values as $value) {
if (\is_string($value) && str_contains($value, ': ')) {
$headers[$name][] = substr(strstr($value, ': '), 2);
}
}
}
return $headers;
}
}