-
Notifications
You must be signed in to change notification settings - Fork 150
Removes Content-Length injection and output buffer flushing #270
Changes from 2 commits
ba5b16e
0725467
755ded6
a22b273
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,26 +10,28 @@ | |
namespace Zend\Diactoros\Response; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use RuntimeException; | ||
|
||
trait SapiEmitterTrait | ||
{ | ||
/** | ||
* Inject the Content-Length header if is not already present. | ||
* Checks to see if content has previously been sent. | ||
* | ||
* @param ResponseInterface $response | ||
* @return ResponseInterface | ||
* 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 output is present in the output buffer. | ||
*/ | ||
private function injectContentLength(ResponseInterface $response) | ||
private function assertNoPreviousOutput() | ||
{ | ||
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 (headers_sent()) { | ||
throw new RuntimeException('Unable to emit response; headers already sent'); | ||
} | ||
|
||
return $response; | ||
if (ob_get_level() > 0 && ob_get_length() > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mindplay-dk: note that I separated out this from the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line 32: the PR is called 'Remove output buffer awareness', but line 32 is checking the output buffer :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically, so is calling I suppose I should retitle it "remove output buffer flushing"... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Re-named the PR to "Removes Content-Length injection and output buffer flushing". |
||
throw new RuntimeException('Output has been emitted previously; cannot emit response'); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -77,23 +79,6 @@ private function emitHeaders(ResponseInterface $response) | |
} | ||
} | ||
|
||
/** | ||
* Loops through the output buffer, flushing each, before emitting | ||
* the response. | ||
* | ||
* @param int|null $maxBufferLevel Flush up to this buffer level. | ||
*/ | ||
private function flush($maxBufferLevel = null) | ||
{ | ||
if (null === $maxBufferLevel) { | ||
$maxBufferLevel = ob_get_level(); | ||
} | ||
|
||
while (ob_get_level() > $maxBufferLevel) { | ||
ob_end_flush(); | ||
} | ||
} | ||
|
||
/** | ||
* Filter a header name to wordcase | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,20 +24,13 @@ class SapiStreamEmitter implements EmitterInterface | |
* body content via the output buffer. | ||
* | ||
* @param ResponseInterface $response | ||
* @param null|int $maxBufferLevel Maximum output buffering level to unwrap. | ||
* @param int $maxBufferLength Maximum output buffering size for each iteration | ||
*/ | ||
public function emit(ResponseInterface $response, $maxBufferLevel = null, $maxBufferLength = 8192) | ||
public function emit(ResponseInterface $response, $maxBufferLength = 8192) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ironically, we changed the signature of this method to swap the |
||
{ | ||
if (headers_sent()) { | ||
throw new RuntimeException('Unable to emit response; headers already sent'); | ||
} | ||
|
||
$response = $this->injectContentLength($response); | ||
|
||
$this->assertNoPreviousOutput(); | ||
$this->emitStatusLine($response); | ||
$this->emitHeaders($response); | ||
$this->flush($maxBufferLevel); | ||
|
||
$range = $this->parseContentRange($response->getHeaderLine('Content-Range')); | ||
|
||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this removed? You won't be able to emit the status line or headers if output has already been started - the first call to
header()
will error. Isn't it better to have the exception, which explains why?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, never mind, you just moved it somewhere else ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😄