diff --git a/src/Response/SapiEmitter.php b/src/Response/SapiEmitter.php index ab1d7e08..45c97448 100644 --- a/src/Response/SapiEmitter.php +++ b/src/Response/SapiEmitter.php @@ -26,9 +26,7 @@ class SapiEmitter implements EmitterInterface */ public function emit(ResponseInterface $response) { - $this->checkForPreviousOutput(); - - $response = $this->injectContentLength($response); + $this->assertNoPreviousOutput(); $this->emitStatusLine($response); $this->emitHeaders($response); diff --git a/src/Response/SapiEmitterTrait.php b/src/Response/SapiEmitterTrait.php index dd4a6508..2d00b7ef 100644 --- a/src/Response/SapiEmitterTrait.php +++ b/src/Response/SapiEmitterTrait.php @@ -17,40 +17,21 @@ trait SapiEmitterTrait /** * Checks to see if content has previously been sent. * - * If either headers have been sent, or the current output buffer contains - * content, raises an exception. + * If either headers have been sent or the output buffer contains content, + * raises an exception. * * @throws RuntimeException if headers have already been sent. - * @throws RuntimeException if the current output buffer is not empty. + * @throws RuntimeException if output is present in the output buffer. */ - private function checkForPreviousOutput() + private function assertNoPreviousOutput() { if (headers_sent()) { throw new RuntimeException('Unable to emit response; headers already sent'); } - $bufferContents = ob_get_contents(); - if (! empty($bufferContents)) { - throw new RuntimeException('Output has been emitted previously; cannot emit response: ' . $bufferContents); - } - } - /** - * Inject the Content-Length header if is not already present. - * - * @param ResponseInterface $response - * @return ResponseInterface - */ - private function injectContentLength(ResponseInterface $response) - { - if (! $response->hasHeader('Content-Length')) { - // PSR-7 indicates int OR null for the stream size; for null values, - // we will not auto-inject the Content-Length. - if (null !== $response->getBody()->getSize()) { - return $response->withHeader('Content-Length', (string) $response->getBody()->getSize()); - } + if (ob_get_level() > 0 && ob_get_length() > 0) { + throw new RuntimeException('Output has been emitted previously; cannot emit response: ' . $bufferContents); } - - return $response; } /** diff --git a/src/Response/SapiStreamEmitter.php b/src/Response/SapiStreamEmitter.php index 7f95c824..3f61d66c 100644 --- a/src/Response/SapiStreamEmitter.php +++ b/src/Response/SapiStreamEmitter.php @@ -28,10 +28,7 @@ class SapiStreamEmitter implements EmitterInterface */ public function emit(ResponseInterface $response, $maxBufferLength = 8192) { - $this->checkForPreviousOutput(); - - $response = $this->injectContentLength($response); - + $this->assertNoPreviousOutput(); $this->emitStatusLine($response); $this->emitHeaders($response); diff --git a/test/Response/AbstractEmitterTest.php b/test/Response/AbstractEmitterTest.php index 6c3de377..46532bb8 100644 --- a/test/Response/AbstractEmitterTest.php +++ b/test/Response/AbstractEmitterTest.php @@ -44,7 +44,6 @@ public function testEmitsResponseHeaders() ob_end_clean(); $this->assertContains('HTTP/1.1 200 OK', HeaderStack::stack()); $this->assertContains('Content-Type: text/plain', HeaderStack::stack()); - $this->assertContains('Content-Length: 8', HeaderStack::stack()); } public function testEmitsMessageBody() diff --git a/test/ServerTest.php b/test/ServerTest.php index c9bf5297..2abf7ffb 100644 --- a/test/ServerTest.php +++ b/test/ServerTest.php @@ -251,7 +251,6 @@ public function testEmitsHeadersWithMultipleValuesMultipleTimes() */ public function testHeaderOrderIsHonoredWhenEmitted($stack) { - array_pop($stack); // ignore "Content-Length" automatically set by the response emitter $header = array_pop($stack); $this->assertContains( 'Set-Cookie: bar=baz; expires=Wed, 8 Oct 2014 10:30; path=/foo/bar; domain=example.com',