Skip to content
Permalink
Browse files

Don't eagerly call callback responses.

Don't eagerly evaluate callback/streaming responses. This can result in
allocating a pile of memory too soon in the request. Instead it is
preferrable to allocate that memory when we're flushing to the SAPI.
  • Loading branch information...
markstory committed Oct 19, 2016
1 parent 0c0f32f commit 4fc472d9a7595fbd45ee56d3f5ed50704fac2762
Showing with 11 additions and 15 deletions.
  1. +11 −14 src/Network/Response.php
  2. +0 −1 tests/TestCase/Network/ResponseTest.php
@@ -16,6 +16,7 @@
use Cake\Core\Configure;
use Cake\Filesystem\File;
use Cake\Http\CallbackStream;
use Cake\Log\Log;
use Cake\Network\Exception\NotFoundException;
use DateTime;
@@ -739,30 +740,26 @@ protected function _setHeader($header, $value)
public function body($content = null)
{
if ($content === null) {
$this->stream->rewind();
if ($this->stream->isSeekable()) {
$this->stream->rewind();
}
$result = $this->stream->getContents();
if (empty($result) && strlen($result) === 0) {
if (strlen($result) === 0) {
return null;
}
return $result;
}
// BC compatibility
// Compatibility with closure/streaming responses
if (is_callable($content)) {
$content = $this->_handleCallableBody($content);
}
$this->_createStream();
$this->stream->write($content);
$this->stream->rewind();
$result = $this->stream->getContents();
if (empty($result) && strlen($result) === 0) {
return null;
$this->stream = new CallbackStream($content);
} else {
$this->_createStream();
$this->stream->write($content);
}
return $result;
return $content;
}
/**
@@ -2350,5 +2350,4 @@ public function testHasHeader()
$this->assertFalse($response->hasHeader('Accept'));
$this->assertFalse($response->hasHeader('accept'));
}
}

0 comments on commit 4fc472d

Please sign in to comment.
You can’t perform that action at this time.