Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 0725467

Browse files
committed
Removes Content-Length injection from SAPI stream emitters
Per #251, this patch removes Content-Length injection from SAPI stream emitters. It also renames the `checkForPreviousOutput()` method to `assertNoPreviousOutput()` to make it more clear that the method is a no-op under proper usage. The `assertNoPreviousOutput()` method also borrows an idea from the comment to the issue, indicating we can check for (1) a non-zero output buffer, and (2) use `ob_get_length()` to determine if it has content.
1 parent ba5b16e commit 0725467

File tree

5 files changed

+8
-34
lines changed

5 files changed

+8
-34
lines changed

src/Response/SapiEmitter.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ class SapiEmitter implements EmitterInterface
2626
*/
2727
public function emit(ResponseInterface $response)
2828
{
29-
$this->checkForPreviousOutput();
30-
31-
$response = $this->injectContentLength($response);
29+
$this->assertNoPreviousOutput();
3230

3331
$this->emitStatusLine($response);
3432
$this->emitHeaders($response);

src/Response/SapiEmitterTrait.php

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,21 @@ trait SapiEmitterTrait
1717
/**
1818
* Checks to see if content has previously been sent.
1919
*
20-
* If either headers have been sent, or the current output buffer contains
21-
* content, raises an exception.
20+
* If either headers have been sent or the output buffer contains content,
21+
* raises an exception.
2222
*
2323
* @throws RuntimeException if headers have already been sent.
24-
* @throws RuntimeException if the current output buffer is not empty.
24+
* @throws RuntimeException if output is present in the output buffer.
2525
*/
26-
private function checkForPreviousOutput()
26+
private function assertNoPreviousOutput()
2727
{
2828
if (headers_sent()) {
2929
throw new RuntimeException('Unable to emit response; headers already sent');
3030
}
31-
$bufferContents = ob_get_contents();
32-
if (! empty($bufferContents)) {
33-
throw new RuntimeException('Output has been emitted previously; cannot emit response: ' . $bufferContents);
34-
}
35-
}
3631

37-
/**
38-
* Inject the Content-Length header if is not already present.
39-
*
40-
* @param ResponseInterface $response
41-
* @return ResponseInterface
42-
*/
43-
private function injectContentLength(ResponseInterface $response)
44-
{
45-
if (! $response->hasHeader('Content-Length')) {
46-
// PSR-7 indicates int OR null for the stream size; for null values,
47-
// we will not auto-inject the Content-Length.
48-
if (null !== $response->getBody()->getSize()) {
49-
return $response->withHeader('Content-Length', (string) $response->getBody()->getSize());
50-
}
32+
if (ob_get_level() > 0 && ob_get_length() > 0) {
33+
throw new RuntimeException('Output has been emitted previously; cannot emit response');
5134
}
52-
53-
return $response;
5435
}
5536

5637
/**

src/Response/SapiStreamEmitter.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ class SapiStreamEmitter implements EmitterInterface
2828
*/
2929
public function emit(ResponseInterface $response, $maxBufferLength = 8192)
3030
{
31-
$this->checkForPreviousOutput();
32-
33-
$response = $this->injectContentLength($response);
34-
31+
$this->assertNoPreviousOutput();
3532
$this->emitStatusLine($response);
3633
$this->emitHeaders($response);
3734

test/Response/AbstractEmitterTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public function testEmitsResponseHeaders()
4444
ob_end_clean();
4545
$this->assertContains('HTTP/1.1 200 OK', HeaderStack::stack());
4646
$this->assertContains('Content-Type: text/plain', HeaderStack::stack());
47-
$this->assertContains('Content-Length: 8', HeaderStack::stack());
4847
}
4948

5049
public function testEmitsMessageBody()

test/ServerTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ public function testEmitsHeadersWithMultipleValuesMultipleTimes()
251251
*/
252252
public function testHeaderOrderIsHonoredWhenEmitted($stack)
253253
{
254-
array_pop($stack); // ignore "Content-Length" automatically set by the response emitter
255254
$header = array_pop($stack);
256255
$this->assertContains(
257256
'Set-Cookie: bar=baz; expires=Wed, 8 Oct 2014 10:30; path=/foo/bar; domain=example.com',

0 commit comments

Comments
 (0)