Skip to content

Commit

Permalink
Merge branch 'jj101k-bugfix-indeterminate-length-body-empty-response'…
Browse files Browse the repository at this point in the history
… into 3.x

Closes slimphp#1739
  • Loading branch information
akrabat committed Feb 5, 2016
2 parents e5bfe54 + 3cff1cd commit 15bf89c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
27 changes: 18 additions & 9 deletions Slim/App.php
Expand Up @@ -385,16 +385,25 @@ public function respond(ResponseInterface $response)
if (!$contentLength) {
$contentLength = $body->getSize();
}
$totalChunks = ceil($contentLength / $chunkSize);
$lastChunkSize = $contentLength % $chunkSize;
$currentChunk = 0;
while (!$body->eof() && $currentChunk < $totalChunks) {
if (++$currentChunk == $totalChunks && $lastChunkSize > 0) {
$chunkSize = $lastChunkSize;
if (isset($contentLength)) {
$totalChunks = ceil($contentLength / $chunkSize);
$lastChunkSize = $contentLength % $chunkSize;
$currentChunk = 0;
while (!$body->eof() && $currentChunk < $totalChunks) {
if (++$currentChunk == $totalChunks && $lastChunkSize > 0) {
$chunkSize = $lastChunkSize;
}
echo $body->read($chunkSize);
if (connection_status() != CONNECTION_NORMAL) {
break;
}
}
echo $body->read($chunkSize);
if (connection_status() != CONNECTION_NORMAL) {
break;
} else {
while (!$body->eof()) {
echo $body->read($chunkSize);
if (connection_status() != CONNECTION_NORMAL) {
break;
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/AppTest.php
Expand Up @@ -1562,6 +1562,23 @@ public function testRespondWithPaddedStreamFilterOutput()
}
}

public function testRespondIndeterminateLength()
{
$app = new App();
$body_stream = fopen('php://temp', 'r+');
$response = new Response();
$body = $this->getMockBuilder("\Slim\Http\Body")
->setMethods(["getSize"])
->setConstructorArgs([$body_stream])
->getMock();
fwrite($body_stream, "Hello");
rewind($body_stream);
$body->method("getSize")->willReturn(null);
$response = $response->withBody($body);
$app->respond($response);
$this->expectOutputString("Hello");
}

public function testExceptionErrorHandlerDoesNotDisplayErrorDetails()
{
$app = new App();
Expand Down

0 comments on commit 15bf89c

Please sign in to comment.