source: extensions/Google2Piwigo/include/Zend/Http/Response/Stream.php @ 17475

Last change on this file since 17475 was 17475, checked in by mistic100, 12 years ago

new extension: Google2Piwigo

File size: 6.2 KB
Line 
1<?php
2
3/**
4 * Zend Framework
5 *
6 * LICENSE
7 *
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.
15 *
16 * @category   Zend
17 * @package    Zend_Http
18 * @subpackage Response
19 * @version    $Id: Stream.php 24594 2012-01-05 21:27:01Z matthew $
20 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
21 * @license    http://framework.zend.com/license/new-bsd     New BSD License
22 */
23
24/**
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.
28 *
29 * @package    Zend_Http
30 * @subpackage Response
31 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license    http://framework.zend.com/license/new-bsd     New BSD License
33 */
34class Zend_Http_Response_Stream extends Zend_Http_Response
35{
36    /**
37     * Response as stream
38     *
39     * @var resource
40     */
41    protected $stream;
42
43    /**
44     * The name of the file containing the stream
45     *
46     * Will be empty if stream is not file-based.
47     *
48     * @var string
49     */
50    protected $stream_name;
51
52    /**
53     * Should we clean up the stream file when this response is closed?
54     *
55     * @var boolean
56     */
57    protected $_cleanup;
58
59    /**
60     * Get the response as stream
61     *
62     * @return resourse
63     */
64    public function getStream()
65    {
66        return $this->stream;
67    }
68
69    /**
70     * Set the response stream
71     *
72     * @param resourse $stream
73     * @return Zend_Http_Response_Stream
74     */
75    public function setStream($stream)
76    {
77        $this->stream = $stream;
78        return $this;
79    }
80
81    /**
82     * Get the cleanup trigger
83     *
84     * @return boolean
85     */
86    public function getCleanup() {
87        return $this->_cleanup;
88    }
89
90    /**
91     * Set the cleanup trigger
92     *
93     * @param bool $cleanup Set cleanup trigger
94     */
95    public function setCleanup($cleanup = true) {
96        $this->_cleanup = $cleanup;
97    }
98
99    /**
100     * Get file name associated with the stream
101     *
102     * @return string
103     */
104    public function getStreamName() {
105        return $this->stream_name;
106    }
107
108    /**
109     * Set file name associated with the stream
110     *
111     * @param string $stream_name Name to set
112     * @return Zend_Http_Response_Stream
113     */
114    public function setStreamName($stream_name) {
115        $this->stream_name = $stream_name;
116        return $this;
117    }
118
119
120    /**
121     * HTTP response constructor
122     *
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.
125     *
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
128     * responses.
129     *
130     * If no message is passed, the message will be guessed according to the response code.
131     *
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
138     */
139    public function __construct($code, $headers, $body = null, $version = '1.1', $message = null)
140    {
141
142        if(is_resource($body)) {
143            $this->setStream($body);
144            $body = '';
145        }
146        parent::__construct($code, $headers, $body, $version, $message);
147    }
148
149    /**
150     * Create a new Zend_Http_Response_Stream object from a string
151     *
152     * @param string $response_str
153     * @param resource $stream
154     * @return Zend_Http_Response_Stream
155     */
156    public static function fromStream($response_str, $stream)
157    {
158        $code    = self::extractCode($response_str);
159        $headers = self::extractHeaders($response_str);
160        $version = self::extractVersion($response_str);
161        $message = self::extractMessage($response_str);
162
163        return new self($code, $headers, $stream, $version, $message);
164    }
165
166    /**
167     * Get the response body as string
168     *
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.
172     *
173     * If you want to get the raw body (as transfered on wire) use
174     * $this->getRawBody() instead.
175     *
176     * @return string
177     */
178    public function getBody()
179    {
180        if($this->stream != null) {
181            $this->readStream();
182        }
183        return parent::getBody();
184    }
185
186    /**
187     * Get the raw response body (as transfered "on wire") as string
188     *
189     * If the body is encoded (with Transfer-Encoding, not content-encoding -
190     * IE "chunked" body), gzip compressed, etc. it will not be decoded.
191     *
192     * @return string
193     */
194    public function getRawBody()
195    {
196        if($this->stream) {
197            $this->readStream();
198        }
199        return $this->body;
200    }
201
202    /**
203     * Read stream content and return it as string
204     *
205     * Function reads the remainder of the body from the stream and closes the stream.
206     *
207     * @return string
208     */
209    protected function readStream()
210    {
211        if(!is_resource($this->stream)) {
212            return '';
213        }
214
215        if(isset($headers['content-length'])) {
216            $this->body = stream_get_contents($this->stream, $headers['content-length']);
217        } else {
218            $this->body = stream_get_contents($this->stream);
219        }
220        fclose($this->stream);
221        $this->stream = null;
222    }
223
224    public function __destruct()
225    {
226        if(is_resource($this->stream)) {
227            fclose($this->stream);
228            $this->stream = null;
229        }
230        if($this->_cleanup) {
231            @unlink($this->stream_name);
232        }
233    }
234
235}
Note: See TracBrowser for help on using the repository browser.