Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

add method to Zend\Http\Response\Stream #3498

Merged
merged 2 commits into from Jan 21, 2013
Jump to file or symbol
Failed to load files and symbols.
+27 −6
Split
@@ -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
*
@@ -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()) {
@weierophinney

weierophinney Jan 21, 2013

Owner

retrieve the content length into a local variable, as it's fetched twice on a valid condition.

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;
@@ -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
}
@@ -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;
}
}