Skip to content

Commit

Permalink
Don't eagerly call callback responses.
Browse files Browse the repository at this point in the history
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 4fc472d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
25 changes: 11 additions & 14 deletions src/Network/Response.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down
1 change: 0 additions & 1 deletion tests/TestCase/Network/ResponseTest.php
Expand Up @@ -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.