Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ public function handleRequest(ConnectionInterface $conn, RequestInterface $reque
// 'chunked' must always be the final value of 'Transfer-Encoding' according to: https://tools.ietf.org/html/rfc7230#section-3.3.1
if (strtolower(end($transferEncodingHeader)) === 'chunked') {
$stream = new ChunkedDecoder($stream);

$request = $request->withoutHeader('Transfer-Encoding');

$contentLength = null;
}
} elseif ($request->hasHeader('Content-Length')) {
Expand All @@ -162,6 +165,7 @@ public function handleRequest(ConnectionInterface $conn, RequestInterface $reque
$stream = new LengthLimitedStream($stream, $contentLength);
}

$request = $request->withoutHeader('Content-Length');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point in always removing this? Isn't this something (for example) a middleware might be interested in?

Also, I don't see this documented anywhere, in particular not the WHY?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some lines to the README and changed the code. The 'Content-Length' will now only be removed if also 'Transfer-Encoding' is given.

$request = new Request($request, $stream);

// attach remote ip to the request as metadata
Expand Down
17 changes: 11 additions & 6 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,12 +487,14 @@ public function testChunkedEncodedRequestWillBeParsedForRequestEvent()
$endEvent = $this->expectCallableOnce();
$closeEvent = $this->expectCallableOnce();
$errorEvent = $this->expectCallableNever();
$requestValidation = null;

$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) {
$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent, &$requestValidation) {
$request->on('data', $dataEvent);
$request->on('end', $endEvent);
$request->on('close', $closeEvent);
$request->on('error', $errorEvent);
$requestValidation = $request;
});

$this->socket->emit('connection', array($this->connection));
Expand All @@ -506,6 +508,8 @@ public function testChunkedEncodedRequestWillBeParsedForRequestEvent()
$data .= "0\r\n\r\n";

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

$this->assertFalse($requestValidation->hasHeader('Transfer-Encoding'));
}

public function testChunkedEncodedRequestAdditionalDataWontBeEmitted()
Expand Down Expand Up @@ -911,8 +915,9 @@ public function testContentLengthWillBeIgnoredIfTransferEncodingIsSet()
$data .= "0\r\n\r\n";

$this->connection->emit('data', array($data));
$this->assertEquals('4', $requestValidation->getHeaderLine('Content-Length'));
$this->assertEquals('chunked', $requestValidation->getHeaderLine('Transfer-Encoding'));

$this->assertFalse($requestValidation->hasHeader('Content-Length'));
$this->assertFalse($requestValidation->hasHeader('Transfer-Encoding'));
}

public function testInvalidContentLengthWillBeIgnoreddIfTransferEncodingIsSet()
Expand All @@ -938,6 +943,7 @@ public function testInvalidContentLengthWillBeIgnoreddIfTransferEncodingIsSet()
$data = "GET / HTTP/1.1\r\n";
$data .= "Host: example.com:80\r\n";
$data .= "Connection: close\r\n";
// this is valid behavior according to: https://www.ietf.org/rfc/rfc2616.txt chapter 4.4
$data .= "Content-Length: hello world\r\n";
$data .= "Transfer-Encoding: chunked\r\n";
$data .= "\r\n";
Expand All @@ -949,9 +955,8 @@ public function testInvalidContentLengthWillBeIgnoreddIfTransferEncodingIsSet()

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

// this is valid behavior according to: https://www.ietf.org/rfc/rfc2616.txt chapter 4.4
$this->assertEquals('hello world', $requestValidation->getHeaderLine('Content-Length'));
$this->assertEquals('chunked', $requestValidation->getHeaderLine('Transfer-Encoding'));
$this->assertFalse($requestValidation->hasHeader('Content-Length'));
$this->assertFalse($requestValidation->hasHeader('Transfer-Encoding'));
}

public function testNonIntegerContentLengthValueWillLeadToError()
Expand Down