diff --git a/Slim/App.php b/Slim/App.php index 4abd33368..aff622e61 100644 --- a/Slim/App.php +++ b/Slim/App.php @@ -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; + } } } } diff --git a/tests/AppTest.php b/tests/AppTest.php index 883d680a0..eeeae820e 100644 --- a/tests/AppTest.php +++ b/tests/AppTest.php @@ -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();