|
3 | 3 | namespace Amp\Http\Client\Connection\Internal;
|
4 | 4 |
|
5 | 5 | use Amp\ByteStream\ReadableBuffer;
|
| 6 | +use Amp\Cancellation; |
6 | 7 | use Amp\ForbidCloning;
|
7 | 8 | use Amp\ForbidSerialization;
|
8 | 9 | use Amp\Future;
|
@@ -72,6 +73,7 @@ public function __construct(
|
72 | 73 | private readonly Request $request,
|
73 | 74 | private readonly Stream $stream,
|
74 | 75 | private readonly \Closure $bodyDataCallback,
|
| 76 | + private readonly Cancellation $bodyCancellation, |
75 | 77 | private readonly \Closure $trailersCallback,
|
76 | 78 | ) {
|
77 | 79 | $this->maxHeaderBytes = $request->getHeaderSizeLimit();
|
@@ -167,8 +169,11 @@ public function parse(?string $data = null): ?Response
|
167 | 169 | }
|
168 | 170 |
|
169 | 171 | $requestMethod = $this->request->getMethod();
|
170 |
| - $skipBody = $statusCode < HttpStatus::OK || $statusCode === HttpStatus::NOT_MODIFIED || $statusCode === HttpStatus::NO_CONTENT |
171 |
| - || $requestMethod === 'HEAD' || $requestMethod === 'CONNECT'; |
| 172 | + $skipBody = $statusCode < HttpStatus::OK |
| 173 | + || $statusCode === HttpStatus::NOT_MODIFIED |
| 174 | + || $statusCode === HttpStatus::NO_CONTENT |
| 175 | + || $requestMethod === 'HEAD' |
| 176 | + || $requestMethod === 'CONNECT'; |
172 | 177 |
|
173 | 178 | if ($skipBody) {
|
174 | 179 | $this->complete = true;
|
@@ -328,7 +333,11 @@ private function shiftHeadersFromBuffer(): ?string
|
328 | 333 | }
|
329 | 334 |
|
330 | 335 | if ($this->maxHeaderBytes > 0 && $headersSize > $this->maxHeaderBytes) {
|
331 |
| - throw new ParseException("Configured header size exceeded: {$headersSize} bytes received, while the configured limit is {$this->maxHeaderBytes} bytes", HttpStatus::REQUEST_HEADER_FIELDS_TOO_LARGE); |
| 336 | + throw new ParseException( |
| 337 | + "Configured header size exceeded: {$headersSize} bytes received, while the configured " . |
| 338 | + "limit is {$this->maxHeaderBytes} bytes", |
| 339 | + HttpStatus::REQUEST_HEADER_FIELDS_TOO_LARGE, |
| 340 | + ); |
332 | 341 | }
|
333 | 342 |
|
334 | 343 | return $headers;
|
@@ -357,13 +366,17 @@ private function parseRawHeaders(string $rawHeaders): array
|
357 | 366 | $this->chunkedEncoding = \in_array('chunked', $transferEncodings, true);
|
358 | 367 | } elseif (!empty($headerMap['content-length'])) {
|
359 | 368 | if (\count($headerMap['content-length']) > 1) {
|
360 |
| - throw new ParseException('Can\'t determine body length, because multiple content-length headers present in the response', HttpStatus::BAD_REQUEST); |
| 369 | + throw new ParseException('Can\'t determine body length, because multiple content-length ' . |
| 370 | + 'headers present in the response', HttpStatus::BAD_REQUEST); |
361 | 371 | }
|
362 | 372 |
|
363 | 373 | $contentLength = $headerMap['content-length'][0];
|
364 | 374 |
|
365 | 375 | if (!\preg_match('/^(0|[1-9][0-9]*)$/', $contentLength)) {
|
366 |
| - throw new ParseException('Can\'t determine body length, because the content-length header value is invalid', HttpStatus::BAD_REQUEST); |
| 376 | + throw new ParseException( |
| 377 | + 'Can\'t determine body length, because the content-length header value is invalid', |
| 378 | + HttpStatus::BAD_REQUEST, |
| 379 | + ); |
367 | 380 | }
|
368 | 381 |
|
369 | 382 | $this->remainingBodyBytes = (int) $contentLength;
|
@@ -467,9 +480,13 @@ private function addToBody(string $data): void
|
467 | 480 | $this->bodyBytesConsumed += $length;
|
468 | 481 |
|
469 | 482 | if ($this->maxBodyBytes > 0 && $this->bodyBytesConsumed > $this->maxBodyBytes) {
|
470 |
| - throw new ParseException("Configured body size exceeded: {$this->bodyBytesConsumed} bytes received, while the configured limit is {$this->maxBodyBytes} bytes", HttpStatus::PAYLOAD_TOO_LARGE); |
| 483 | + throw new ParseException( |
| 484 | + "Configured body size exceeded: {$this->bodyBytesConsumed} bytes received," . |
| 485 | + " while the configured limit is {$this->maxBodyBytes} bytes", |
| 486 | + HttpStatus::PAYLOAD_TOO_LARGE, |
| 487 | + ); |
471 | 488 | }
|
472 | 489 |
|
473 |
| - ($this->bodyDataCallback)($data)->ignore(); |
| 490 | + ($this->bodyDataCallback)($data)->await($this->bodyCancellation); |
474 | 491 | }
|
475 | 492 | }
|
0 commit comments