Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP client: Fix empty streaming request body, omit Transfer-Encoding: chunked #516

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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