Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Http / Response / Stream.php
blob94c61e46cd39d298a6dbaddaef8fb0d3e0a4c931
1 <?php
2 /**
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
8 */
10 namespace Zend\Http\Response;
12 use Zend\Http\Exception;
13 use Zend\Http\Response;
14 use Zend\Stdlib\ErrorHandler;
16 /**
17 * Represents an HTTP response message as PHP stream resource
19 class Stream extends Response
21 /**
22 * The Content-Length value, if set
24 * @var int
26 protected $contentLength = null;
28 /**
29 * The portion of the body that has already been streamed
31 * @var int
33 protected $contentStreamed = 0;
35 /**
36 * Response as stream
38 * @var resource
40 protected $stream;
42 /**
43 * The name of the file containing the stream
45 * Will be empty if stream is not file-based.
47 * @var string
49 protected $streamName;
51 /**
52 * Should we clean up the stream file when this response is closed?
54 * @var bool
56 protected $cleanup;
58 /**
59 * Set content length
61 * @param int $contentLength
63 public function setContentLength($contentLength = null)
65 $this->contentLength = $contentLength;
68 /**
69 * Get content length
71 * @return int|null
73 public function getContentLength()
75 return $this->contentLength;
78 /**
79 * Get the response as stream
81 * @return resource
83 public function getStream()
85 return $this->stream;
88 /**
89 * Set the response stream
91 * @param resource $stream
92 * @return Stream
94 public function setStream($stream)
96 $this->stream = $stream;
97 return $this;
101 * Get the cleanup trigger
103 * @return bool
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
123 * @return string
125 public function getStreamName()
127 return $this->streamName;
131 * Set file name associated with the stream
133 * @param string $streamName Name to set
134 * @return Stream
136 public function setStreamName($streamName)
138 $this->streamName = $streamName;
139 return $this;
143 * Create a new Zend\Http\Response\Stream object from a stream
145 * @param string $responseString
146 * @param resource $stream
147 * @return 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;
158 $headersString = '';
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;
168 break;
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;
178 break;
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),
207 $contentLength
210 break;
214 return $response;
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.
227 * @return string
229 public function getBody()
231 if ($this->stream != null) {
232 $this->readStream();
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.
243 * @return string
245 public function getRawBody()
247 if ($this->stream) {
248 $this->readStream();
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.
258 * @return string
260 protected function readStream()
262 $contentLength = $this->getContentLength();
263 if (null !== $contentLength) {
264 $bytes = $contentLength - $this->contentStreamed;
265 } else {
266 $bytes = -1; // Read the whole buffer
269 if (!is_resource($this->stream) || $bytes == 0) {
270 return '';
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;
282 * Destructor
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();