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

Ignore empty writes to not mess up chunked transfer encoding #112

Merged
merged 1 commit into from
Feb 11, 2017
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
14 changes: 9 additions & 5 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,19 @@ public function write($data)
throw new \Exception('Response head has not yet been written.');
}

// prefix with chunk length for chunked transfer encoding
if ($this->chunkedEncoding) {
$len = strlen($data);
$chunk = dechex($len)."\r\n".$data."\r\n";
$flushed = $this->conn->write($chunk);
} else {
$flushed = $this->conn->write($data);

// skip empty chunks
if ($len === 0) {
return true;
}

$data = dechex($len) . "\r\n" . $data . "\r\n";
}

return $flushed;
return $this->conn->write($data);
}

public function end($data = null)
Expand Down
27 changes: 27 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,33 @@ public function testResponseBodyShouldBeChunkedCorrectly()
$response->end();
}

public function testResponseBodyShouldSkipEmptyChunks()
{
$conn = $this
->getMockBuilder('React\Socket\ConnectionInterface')
->getMock();
$conn
->expects($this->at(4))
->method('write')
->with("5\r\nHello\r\n");
$conn
->expects($this->at(5))
->method('write')
->with("5\r\nWorld\r\n");
$conn
->expects($this->at(6))
->method('write')
->with("0\r\n\r\n");

$response = new Response($conn);
$response->writeHead();

$response->write('Hello');
$response->write('');
$response->write('World');
$response->end();
}

public function testResponseShouldEmitEndOnStreamEnd()
{
$ended = false;
Expand Down