3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Http\Response
;
12 use Zend\Http\Exception
;
13 use Zend\Http\Response
;
14 use Zend\Stdlib\ErrorHandler
;
17 * Represents an HTTP response message as PHP stream resource
19 class Stream
extends Response
22 * The Content-Length value, if set
26 protected $contentLength = null;
29 * The portion of the body that has already been streamed
33 protected $contentStreamed = 0;
43 * The name of the file containing the stream
45 * Will be empty if stream is not file-based.
49 protected $streamName;
52 * Should we clean up the stream file when this response is closed?
61 * @param int $contentLength
63 public function setContentLength($contentLength = null)
65 $this->contentLength
= $contentLength;
73 public function getContentLength()
75 return $this->contentLength
;
79 * Get the response as stream
83 public function getStream()
89 * Set the response stream
91 * @param resource $stream
94 public function setStream($stream)
96 $this->stream
= $stream;
101 * Get the cleanup trigger
105 public function getCleanup()
107 return $this->cleanup
;
111 * Set the cleanup trigger
113 * @param bool $cleanup
115 public function setCleanup($cleanup = true)
117 $this->cleanup
= $cleanup;
121 * Get file name associated with the stream
125 public function getStreamName()
127 return $this->streamName
;
131 * Set file name associated with the stream
133 * @param string $streamName Name to set
136 public function setStreamName($streamName)
138 $this->streamName
= $streamName;
143 * Create a new Zend\Http\Response\Stream object from a stream
145 * @param string $responseString
146 * @param resource $stream
148 * @throws Exception\InvalidArgumentException
149 * @throws Exception\OutOfRangeException
151 public static function fromStream($responseString, $stream)
153 if (!is_resource($stream) ||
get_resource_type($stream) !== 'stream') {
154 throw new Exception\
InvalidArgumentException('A valid stream is required');
157 $headerComplete = false;
160 $responseArray = explode("\n", $responseString);
162 while (count($responseArray)) {
163 $nextLine = array_shift($responseArray);
164 $headersString .= $nextLine."\n";
165 $nextLineTrimmed = trim($nextLine);
166 if ($nextLineTrimmed == '') {
167 $headerComplete = true;
172 if (!$headerComplete) {
173 while (false !== ($nextLine = fgets($stream))) {
175 $headersString .= trim($nextLine)."\r\n";
176 if ($nextLine == "\r\n" ||
$nextLine == "\n") {
177 $headerComplete = true;
183 if (!$headerComplete) {
184 throw new Exception\
OutOfRangeException('End of header not found');
187 /** @var Stream $response */
188 $response = static::fromString($headersString);
190 if (is_resource($stream)) {
191 $response->setStream($stream);
194 if (count($responseArray)) {
195 $response->content
= implode("\n", $responseArray);
198 $headers = $response->getHeaders();
199 foreach ($headers as $header) {
200 if ($header instanceof \Zend\Http\Header\ContentLength
) {
201 $response->setContentLength((int) $header->getFieldValue());
202 $contentLength = $response->getContentLength();
203 if (strlen($response->content
) > $contentLength) {
204 throw new Exception\
OutOfRangeException(sprintf(
205 'Too much content was extracted from the stream (%d instead of %d bytes)',
206 strlen($response->content
),
218 * Get the response body as string
220 * This method returns the body of the HTTP response (the content), as it
221 * should be in it's readable version - that is, after decoding it (if it
222 * was decoded), deflating it (if it was gzip compressed), etc.
224 * If you want to get the raw body (as transferred on wire) use
225 * $this->getRawBody() instead.
229 public function getBody()
231 if ($this->stream
!= null) {
234 return parent
::getBody();
238 * Get the raw response body (as transferred "on wire") as string
240 * If the body is encoded (with Transfer-Encoding, not content-encoding -
241 * IE "chunked" body), gzip compressed, etc. it will not be decoded.
245 public function getRawBody()
250 return $this->content
;
254 * Read stream content and return it as string
256 * Function reads the remainder of the body from the stream and closes the stream.
260 protected function readStream()
262 $contentLength = $this->getContentLength();
263 if (null !== $contentLength) {
264 $bytes = $contentLength - $this->contentStreamed
;
266 $bytes = -1; // Read the whole buffer
269 if (!is_resource($this->stream
) ||
$bytes == 0) {
273 $this->content
.= stream_get_contents($this->stream
, $bytes);
274 $this->contentStreamed +
= strlen($this->content
);
276 if ($this->getContentLength() == $this->contentStreamed
) {
277 $this->stream
= null;
284 public function __destruct()
286 if (is_resource($this->stream
)) {
287 $this->stream
= null; //Could be listened by others
289 if ($this->cleanup
) {
290 ErrorHandler
::start(E_WARNING
);
291 unlink($this->streamName
);
292 ErrorHandler
::stop();