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

Commit

Permalink
Merge branch 'stream' of git://github.com/prolic/zf2 into hotfix/3498
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Jan 21, 2013
2 parents d1e0897 + f932275 commit f08d0f8
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions library/Zend/Http/Response/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ class Stream extends Response
*/
protected $cleanup;

/**
* Set content length
*
* @param int $contentLength
*/
public function setContentLength($contentLength = null)
{
$this->contentLength = $contentLength;
}

/**
* Get content length
*
* @return int|null
*/
public function getContentLength()
{
return $this->contentLength;
}

/**
* Get the response as stream
*
Expand Down Expand Up @@ -182,12 +202,12 @@ public static function fromStream($responseString, $stream)
$headers = $response->getHeaders();
foreach ($headers as $header) {
if ($header instanceof \Zend\Http\Header\ContentLength) {
$response->contentLength = (int) $header->getFieldValue();
if (strlen($response->content) > $response->contentLength) {
$response->setContentLength((int) $header->getFieldValue());
if (strlen($response->content) > $response->getContentLength()) {
throw new Exception\OutOfRangeException(sprintf(
'Too much content was extracted from the stream (%d instead of %d bytes)',
strlen($response->content),
$response->contentLength
$response->getContentLength()
));
}
break;
Expand Down Expand Up @@ -242,8 +262,9 @@ public function getRawBody()
*/
protected function readStream()
{
if (!is_null($this->contentLength)) {
$bytes = $this->contentLength - $this->contentStreamed;
$contentLength = $this->getContentLength();
if (!is_null($contentLength)) {
$bytes = $contentLength - $this->contentStreamed;
} else {
$bytes = -1; //Read the whole buffer
}
Expand All @@ -255,7 +276,7 @@ protected function readStream()
$this->content .= stream_get_contents($this->stream, $bytes);
$this->contentStreamed += strlen($this->content);

if ($this->contentLength == $this->contentStreamed) {
if ($this->getContentLength() == $this->contentStreamed) {
$this->stream = null;
}
}
Expand Down

0 comments on commit f08d0f8

Please sign in to comment.