Skip to content

Commit a9505c6

Browse files
committed
Unify chunked decoder and always skip trailers also for HTTP server
Remove dedicated client-side chunked decoder in favor of existing server-side chunked decoder. Unify handling to always skip (unused) trailers for both sides.
1 parent 788526e commit a9505c6

5 files changed

Lines changed: 40 additions & 442 deletions

File tree

src/Client/ChunkedStreamDecoder.php

Lines changed: 0 additions & 207 deletions
This file was deleted.

src/Client/Response.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace React\Http\Client;
44

55
use Evenement\EventEmitter;
6+
use React\Http\Io\ChunkedDecoder;
67
use React\Stream\ReadableStreamInterface;
78
use React\Stream\Util;
89
use React\Stream\WritableStreamInterface;
@@ -33,7 +34,7 @@ public function __construct(ReadableStreamInterface $stream, $protocol, $version
3334
$this->headers = $headers;
3435

3536
if (strtolower($this->getHeaderLine('Transfer-Encoding')) === 'chunked') {
36-
$this->stream = new ChunkedStreamDecoder($stream);
37+
$this->stream = new ChunkedDecoder($stream);
3738
$this->removeHeader('Transfer-Encoding');
3839
}
3940

src/Io/ChunkedDecoder.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function handleData($data)
116116
}
117117

118118
if ($hexValue !== '') {
119-
$hexValue = \ltrim($hexValue, "0");
119+
$hexValue = \ltrim(\trim($hexValue), "0");
120120
if ($hexValue === '') {
121121
$hexValue = "0";
122122
}
@@ -155,16 +155,19 @@ public function handleData($data)
155155
$this->headerCompleted = false;
156156
$this->transferredSize = 0;
157157
$this->buffer = (string)\substr($this->buffer, 2);
158+
} elseif ($this->chunkSize === 0) {
159+
// end chunk received, skip all trailer data
160+
$this->buffer = (string)\substr($this->buffer, $positionCrlf);
158161
}
159162

160-
if ($positionCrlf !== 0 && $this->chunkSize === $this->transferredSize && \strlen($this->buffer) > 2) {
161-
// the first 2 characters are not CLRF, send error event
162-
$this->handleError(new Exception('Chunk does not end with a CLRF'));
163+
if ($positionCrlf !== 0 && $this->chunkSize !== 0 && $this->chunkSize === $this->transferredSize && \strlen($this->buffer) > 2) {
164+
// the first 2 characters are not CRLF, send error event
165+
$this->handleError(new Exception('Chunk does not end with a CRLF'));
163166
return;
164167
}
165168

166169
if ($positionCrlf !== 0 && \strlen($this->buffer) < 2) {
167-
// No CLRF found, wait for additional data which could be a CLRF
170+
// No CRLF found, wait for additional data which could be a CRLF
168171
return;
169172
}
170173
}

0 commit comments

Comments
 (0)