Skip to content

Commit c3be43d

Browse files
authored
Merge pull request #316 from clue-labs/keep-chunked
Keep incoming `Transfer-Encoding: chunked` request header
2 parents 6afc50b + 885618f commit c3be43d

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 stop reading from the connection and discard all
400399
incoming data instead of closing it.
401400
A response message can still be sent (unless the connection is already closed).
@@ -678,12 +677,14 @@ in this case (if applicable).
678677

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

689690
```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
@@ -1444,7 +1444,7 @@ public function testRequestChunkedTransferEncodingRequestWillEmitDecodedDataEven
14441444

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

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

14501450
public function testRequestChunkedTransferEncodingWithAdditionalDataWontBeEmitted()
@@ -1509,12 +1509,14 @@ public function testRequestChunkedTransferEncodingHeaderCanBeUpperCase()
15091509
$endEvent = $this->expectCallableOnce();
15101510
$closeEvent = $this->expectCallableOnce();
15111511
$errorEvent = $this->expectCallableNever();
1512+
$requestValidation = null;
15121513

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

15201522
$server->listen($this->socket);
@@ -1529,6 +1531,7 @@ public function testRequestChunkedTransferEncodingHeaderCanBeUpperCase()
15291531
$data .= "0\r\n\r\n";
15301532

15311533
$this->connection->emit('data', array($data));
1534+
$this->assertEquals('CHUNKED', $requestValidation->getHeaderLine('Transfer-Encoding'));
15321535
}
15331536

15341537
public function testRequestChunkedTransferEncodingCanBeMixedUpperAndLowerCase()
@@ -1804,7 +1807,7 @@ public function testRequestContentLengthWillBeIgnoredIfTransferEncodingIsSet()
18041807
$this->connection->emit('data', array($data));
18051808

18061809
$this->assertFalse($requestValidation->hasHeader('Content-Length'));
1807-
$this->assertFalse($requestValidation->hasHeader('Transfer-Encoding'));
1810+
$this->assertEquals('chunked', $requestValidation->getHeaderLine('Transfer-Encoding'));
18081811
}
18091812

18101813
public function testRequestInvalidContentLengthWillBeIgnoreddIfTransferEncodingIsSet()
@@ -1842,7 +1845,7 @@ public function testRequestInvalidContentLengthWillBeIgnoreddIfTransferEncodingI
18421845
$this->connection->emit('data', array($data));
18431846

18441847
$this->assertFalse($requestValidation->hasHeader('Content-Length'));
1845-
$this->assertFalse($requestValidation->hasHeader('Transfer-Encoding'));
1848+
$this->assertEquals('chunked', $requestValidation->getHeaderLine('Transfer-Encoding'));
18461849
}
18471850

18481851
public function testRequestInvalidNonIntegerContentLengthWillEmitServerErrorAndSendResponse()

0 commit comments

Comments
 (0)