Skip to content

Commit b898824

Browse files
committed
Keep incoming Transfer-Encoding: chunked request header
1 parent 8ce5d7a commit b898824

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -386,16 +386,15 @@ See also [example #9](examples) for more details.
386386

387387
The `data` event will be emitted whenever new data is available on the request
388388
body stream.
389-
The server automatically takes care of decoding chunked transfer encoding
390-
and will only emit the actual payload as data.
391-
In this case, the `Transfer-Encoding` header will be removed.
389+
The server also automatically takes care of decoding any incoming requests using
390+
`Transfer-Encoding: chunked` and will only emit the actual payload as data.
392391

393392
The `end` event will be emitted when the request body stream terminates
394393
successfully, i.e. it was read until its expected end.
395394

396395
The `error` event will be emitted in case the request stream contains invalid
397-
chunked data or the connection closes before the complete request stream has
398-
been received.
396+
data for `Transfer-Encoding: chunked` or when the connection closes before
397+
the complete request stream has been received.
399398
The server will automatically `pause()` the connection instead of closing it.
400399
A response message can still be sent (unless the connection is already closed).
401400

@@ -677,12 +676,14 @@ in this case (if applicable).
677676

678677
If the response body is a `string`, a `Content-Length` header will be added
679678
automatically.
679+
680680
If the response body is a ReactPHP `ReadableStreamInterface` and you do not
681-
specify a `Content-Length` header, HTTP/1.1 responses will automatically use
682-
chunked transfer encoding and send the respective header
683-
(`Transfer-Encoding: chunked`) automatically.
681+
specify a `Content-Length` header, outgoing HTTP/1.1 response messages will
682+
automatically use `Transfer-Encoding: chunked` and send the respective header
683+
automatically.
684684
The server is responsible for handling `Transfer-Encoding`, so you SHOULD NOT
685685
pass this header yourself.
686+
686687
If you know the length of your stream body, you MAY specify it like this instead:
687688

688689
```php

src/StreamingServer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,6 @@ public function handleRequest(ConnectionInterface $conn, ServerRequestInterface
204204
}
205205

206206
$stream = new ChunkedDecoder($stream);
207-
208-
$request = $request->withoutHeader('Transfer-Encoding');
209207
$request = $request->withoutHeader('Content-Length');
210208

211209
$contentLength = null;

tests/StreamingServerTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ public function testRequestChunkedTransferEncodingRequestWillEmitDecodedDataEven
14431443

14441444
$this->connection->emit('data', array($data));
14451445

1446-
$this->assertFalse($requestValidation->hasHeader('Transfer-Encoding'));
1446+
$this->assertEquals('chunked', $requestValidation->getHeaderLine('Transfer-Encoding'));
14471447
}
14481448

14491449
public function testRequestChunkedTransferEncodingWithAdditionalDataWontBeEmitted()
@@ -1508,12 +1508,14 @@ public function testRequestChunkedTransferEncodingHeaderCanBeUpperCase()
15081508
$endEvent = $this->expectCallableOnce();
15091509
$closeEvent = $this->expectCallableOnce();
15101510
$errorEvent = $this->expectCallableNever();
1511+
$requestValidation = null;
15111512

1512-
$server = new StreamingServer(function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) {
1513+
$server = new StreamingServer(function (ServerRequestInterface $request) use ($dataEvent, $endEvent, $closeEvent, $errorEvent, &$requestValidation) {
15131514
$request->getBody()->on('data', $dataEvent);
15141515
$request->getBody()->on('end', $endEvent);
15151516
$request->getBody()->on('close', $closeEvent);
15161517
$request->getBody()->on('error', $errorEvent);
1518+
$requestValidation = $request;
15171519
});
15181520

15191521
$server->listen($this->socket);
@@ -1528,6 +1530,7 @@ public function testRequestChunkedTransferEncodingHeaderCanBeUpperCase()
15281530
$data .= "0\r\n\r\n";
15291531

15301532
$this->connection->emit('data', array($data));
1533+
$this->assertEquals('CHUNKED', $requestValidation->getHeaderLine('Transfer-Encoding'));
15311534
}
15321535

15331536
public function testRequestChunkedTransferEncodingCanBeMixedUpperAndLowerCase()
@@ -1803,7 +1806,7 @@ public function testRequestContentLengthWillBeIgnoredIfTransferEncodingIsSet()
18031806
$this->connection->emit('data', array($data));
18041807

18051808
$this->assertFalse($requestValidation->hasHeader('Content-Length'));
1806-
$this->assertFalse($requestValidation->hasHeader('Transfer-Encoding'));
1809+
$this->assertEquals('chunked', $requestValidation->getHeaderLine('Transfer-Encoding'));
18071810
}
18081811

18091812
public function testRequestInvalidContentLengthWillBeIgnoreddIfTransferEncodingIsSet()
@@ -1841,7 +1844,7 @@ public function testRequestInvalidContentLengthWillBeIgnoreddIfTransferEncodingI
18411844
$this->connection->emit('data', array($data));
18421845

18431846
$this->assertFalse($requestValidation->hasHeader('Content-Length'));
1844-
$this->assertFalse($requestValidation->hasHeader('Transfer-Encoding'));
1847+
$this->assertEquals('chunked', $requestValidation->getHeaderLine('Transfer-Encoding'));
18451848
}
18461849

18471850
public function testRequestInvalidNonIntegerContentLengthWillEmitServerErrorAndSendResponse()

0 commit comments

Comments
 (0)