Skip to content

Commit

Permalink
Ensure that responses with an indeterminate-length body have their co…
Browse files Browse the repository at this point in the history
…ntent sent.
  • Loading branch information
jj101k committed Jan 18, 2016
1 parent 03b44a4 commit 3cff1cd
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 3cff1cd

Please sign in to comment.