Skip to content

Commit

Permalink
Fix empty streaming request body, omit Transfer-Encoding: chunked
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Feb 22, 2024
1 parent 706edec commit 9bf5456
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Io/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function send(RequestInterface $request)
} elseif ($size === 0 && \in_array($request->getMethod(), array('POST', 'PUT', 'PATCH'))) {
// only assign a "Content-Length: 0" request header if the body is expected for certain methods
$request = $request->withHeader('Content-Length', '0');
} elseif ($body instanceof ReadableStreamInterface && $body->isReadable() && !$request->hasHeader('Content-Length')) {
} elseif ($body instanceof ReadableStreamInterface && $size !== 0 && $body->isReadable() && !$request->hasHeader('Content-Length')) {
// use "Transfer-Encoding: chunked" when this is a streaming body and body size is unknown
$request = $request->withHeader('Transfer-Encoding', 'chunked');
} else {
Expand Down
16 changes: 16 additions & 0 deletions tests/Io/SenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Psr\Http\Message\RequestInterface;
use React\Http\Client\Client as HttpClient;
use React\Http\Io\ClientConnectionManager;
use React\Http\Io\EmptyBodyStream;
use React\Http\Io\ReadableBodyStream;
use React\Http\Io\Sender;
use React\Http\Message\Request;
Expand Down Expand Up @@ -264,6 +265,21 @@ public function testSendGetWillNotPassContentLengthHeaderForEmptyRequestBody()
$sender->send($request);
}

public function testSendGetWithEmptyBodyStreamWillNotPassContentLengthOrTransferEncodingHeader()
{
$client = $this->getMockBuilder('React\Http\Client\Client')->disableOriginalConstructor()->getMock();
$client->expects($this->once())->method('request')->with($this->callback(function (RequestInterface $request) {
return !$request->hasHeader('Content-Length') && !$request->hasHeader('Transfer-Encoding');
}))->willReturn($this->getMockBuilder('React\Http\Io\ClientRequestStream')->disableOriginalConstructor()->getMock());

$sender = new Sender($client);

$body = new EmptyBodyStream();
$request = new Request('GET', 'http://www.google.com/', array(), $body);

$sender->send($request);
}

public function testSendCustomMethodWillNotPassContentLengthHeaderForEmptyRequestBody()
{
$client = $this->getMockBuilder('React\Http\Client\Client')->disableOriginalConstructor()->getMock();
Expand Down

0 comments on commit 9bf5456

Please sign in to comment.