8 * This source file is subject to the new BSD license that is bundled
9 * with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-web at this URL:
11 * http://framework.zend.com/license/new-bsd
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@zend.com so we can send you a copy immediately.
18 * @subpackage Response
20 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
21 * @license http://framework.zend.com/license/new-bsd New BSD License
25 * Zend_Http_Response represents an HTTP 1.0 / 1.1 response message. It
26 * includes easy access to all the response's different elemts, as well as some
27 * convenience methods for parsing and validating HTTP responses.
30 * @subpackage Response
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
34 class Zend_Http_Response_Stream extends Zend_Http_Response
44 * The name of the file containing the stream
46 * Will be empty if stream is not file-based.
50 protected $stream_name;
53 * Should we clean up the stream file when this response is closed?
60 * Get the response as stream
64 public function getStream()
70 * Set the response stream
72 * @param resourse $stream
73 * @return Zend_Http_Response_Stream
75 public function setStream($stream)
77 $this->stream = $stream;
82 * Get the cleanup trigger
86 public function getCleanup() {
87 return $this->_cleanup;
91 * Set the cleanup trigger
93 * @param $cleanup Set cleanup trigger
95 public function setCleanup($cleanup = true) {
96 $this->_cleanup = $cleanup;
100 * Get file name associated with the stream
104 public function getStreamName() {
105 return $this->stream_name;
109 * Set file name associated with the stream
111 * @param string $stream_name Name to set
112 * @return Zend_Http_Response_Stream
114 public function setStreamName($stream_name) {
115 $this->stream_name = $stream_name;
121 * HTTP response constructor
123 * In most cases, you would use Zend_Http_Response::fromString to parse an HTTP
124 * response string and create a new Zend_Http_Response object.
126 * NOTE: The constructor no longer accepts nulls or empty values for the code and
127 * headers and will throw an exception if the passed values do not form a valid HTTP
130 * If no message is passed, the message will be guessed according to the response code.
132 * @param int $code Response code (200, 404, ...)
133 * @param array $headers Headers array
134 * @param string $body Response body
135 * @param string $version HTTP version
136 * @param string $message Response code as text
137 * @throws Zend_Http_Exception
139 public function __construct($code, $headers, $body = null, $version = '1.1', $message = null)
142 if(is_resource($body)) {
143 $this->setStream($body);
146 parent::__construct($code, $headers, $body, $version, $message);
150 * Create a new Zend_Http_Response_Stream object from a string
152 * @param string $response_str
153 * @param resource $stream
154 * @return Zend_Http_Response_Stream
156 public static function fromStream($response_str, $stream)
158 $code = self::extractCode($response_str);
159 $headers = self::extractHeaders($response_str);
160 $version = self::extractVersion($response_str);
161 $message = self::extractMessage($response_str);
163 return new self($code, $headers, $stream, $version, $message);
167 * Get the response body as string
169 * This method returns the body of the HTTP response (the content), as it
170 * should be in it's readable version - that is, after decoding it (if it
171 * was decoded), deflating it (if it was gzip compressed), etc.
173 * If you want to get the raw body (as transfered on wire) use
174 * $this->getRawBody() instead.
178 public function getBody()
180 if($this->stream != null) {
183 return parent::getBody();
187 * Get the raw response body (as transfered "on wire") as string
189 * If the body is encoded (with Transfer-Encoding, not content-encoding -
190 * IE "chunked" body), gzip compressed, etc. it will not be decoded.
194 public function getRawBody()
203 * Read stream content and return it as string
205 * Function reads the remainder of the body from the stream and closes the stream.
209 protected function readStream()
211 if(!is_resource($this->stream)) {
215 if(isset($headers['content-length'])) {
216 $this->body = stream_get_contents($this->stream, $headers['content-length']);
218 $this->body = stream_get_contents($this->stream);
220 fclose($this->stream);
221 $this->stream = null;
224 public function __destruct()
226 if(is_resource($this->stream)) {
227 fclose($this->stream);
228 $this->stream = null;
230 if($this->_cleanup) {
231 @unlink($this->stream_name);