File indexing completed on 2025-01-19 05:21:14

0001 <?php
0002 
0003 /**
0004  * Zend Framework
0005  *
0006  * LICENSE
0007  *
0008  * This source file is subject to the new BSD license that is bundled
0009  * with this package in the file LICENSE.txt.
0010  * It is also available through the world-wide-web at this URL:
0011  * http://framework.zend.com/license/new-bsd
0012  * If you did not receive a copy of the license and are unable to
0013  * obtain it through the world-wide-web, please send an email
0014  * to license@zend.com so we can send you a copy immediately.
0015  *
0016  * @category   Zend
0017  * @package    Zend_Http
0018  * @subpackage Response
0019  * @version    $Id$
0020  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0021  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0022  */
0023 
0024 /**
0025  * Zend_Http_Response represents an HTTP 1.0 / 1.1 response message. It
0026  * includes easy access to all the response's different elemts, as well as some
0027  * convenience methods for parsing and validating HTTP responses.
0028  *
0029  * @package    Zend_Http
0030  * @subpackage Response
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  */
0034 class Zend_Http_Response_Stream extends Zend_Http_Response
0035 {
0036     /**
0037      * Response as stream
0038      *
0039      * @var resource
0040      */
0041     protected $stream;
0042 
0043     /**
0044      * The name of the file containing the stream
0045      *
0046      * Will be empty if stream is not file-based.
0047      *
0048      * @var string
0049      */
0050     protected $stream_name;
0051 
0052     /**
0053      * Should we clean up the stream file when this response is closed?
0054      *
0055      * @var boolean
0056      */
0057     protected $_cleanup;
0058 
0059     /**
0060      * Get the response as stream
0061      *
0062      * @return resourse
0063      */
0064     public function getStream()
0065     {
0066         return $this->stream;
0067     }
0068 
0069     /**
0070      * Set the response stream
0071      *
0072      * @param resourse $stream
0073      * @return Zend_Http_Response_Stream
0074      */
0075     public function setStream($stream)
0076     {
0077         $this->stream = $stream;
0078         return $this;
0079     }
0080 
0081     /**
0082      * Get the cleanup trigger
0083      *
0084      * @return boolean
0085      */
0086     public function getCleanup() {
0087         return $this->_cleanup;
0088     }
0089 
0090     /**
0091      * Set the cleanup trigger
0092      *
0093      * @param bool $cleanup Set cleanup trigger
0094      */
0095     public function setCleanup($cleanup = true) {
0096         $this->_cleanup = $cleanup;
0097     }
0098 
0099     /**
0100      * Get file name associated with the stream
0101      *
0102      * @return string
0103      */
0104     public function getStreamName() {
0105         return $this->stream_name;
0106     }
0107 
0108     /**
0109      * Set file name associated with the stream
0110      *
0111      * @param string $stream_name Name to set
0112      * @return Zend_Http_Response_Stream
0113      */
0114     public function setStreamName($stream_name) {
0115         $this->stream_name = $stream_name;
0116         return $this;
0117     }
0118 
0119 
0120     /**
0121      * HTTP response constructor
0122      *
0123      * In most cases, you would use Zend_Http_Response::fromString to parse an HTTP
0124      * response string and create a new Zend_Http_Response object.
0125      *
0126      * NOTE: The constructor no longer accepts nulls or empty values for the code and
0127      * headers and will throw an exception if the passed values do not form a valid HTTP
0128      * responses.
0129      *
0130      * If no message is passed, the message will be guessed according to the response code.
0131      *
0132      * @param int $code Response code (200, 404, ...)
0133      * @param array $headers Headers array
0134      * @param string $body Response body
0135      * @param string $version HTTP version
0136      * @param string $message Response code as text
0137      * @throws Zend_Http_Exception
0138      */
0139     public function __construct($code, $headers, $body = null, $version = '1.1', $message = null)
0140     {
0141 
0142         if(is_resource($body)) {
0143             $this->setStream($body);
0144             $body = '';
0145         }
0146         parent::__construct($code, $headers, $body, $version, $message);
0147     }
0148 
0149     /**
0150      * Create a new Zend_Http_Response_Stream object from a string
0151      *
0152      * @param string $response_str
0153      * @param resource $stream
0154      * @return Zend_Http_Response_Stream
0155      */
0156     public static function fromStream($response_str, $stream)
0157     {
0158         $code    = self::extractCode($response_str);
0159         $headers = self::extractHeaders($response_str);
0160         $version = self::extractVersion($response_str);
0161         $message = self::extractMessage($response_str);
0162 
0163         return new self($code, $headers, $stream, $version, $message);
0164     }
0165 
0166     /**
0167      * Get the response body as string
0168      *
0169      * This method returns the body of the HTTP response (the content), as it
0170      * should be in it's readable version - that is, after decoding it (if it
0171      * was decoded), deflating it (if it was gzip compressed), etc.
0172      *
0173      * If you want to get the raw body (as transfered on wire) use
0174      * $this->getRawBody() instead.
0175      *
0176      * @return string
0177      */
0178     public function getBody()
0179     {
0180         if($this->stream != null) {
0181             $this->readStream();
0182         }
0183         return parent::getBody();
0184     }
0185 
0186     /**
0187      * Get the raw response body (as transfered "on wire") as string
0188      *
0189      * If the body is encoded (with Transfer-Encoding, not content-encoding -
0190      * IE "chunked" body), gzip compressed, etc. it will not be decoded.
0191      *
0192      * @return string
0193      */
0194     public function getRawBody()
0195     {
0196         if($this->stream) {
0197             $this->readStream();
0198         }
0199         return $this->body;
0200     }
0201 
0202     /**
0203      * Read stream content and return it as string
0204      *
0205      * Function reads the remainder of the body from the stream and closes the stream.
0206      *
0207      * @return string
0208      */
0209     protected function readStream()
0210     {
0211         if(!is_resource($this->stream)) {
0212             return '';
0213         }
0214 
0215         if(isset($headers['content-length'])) {
0216             $this->body = stream_get_contents($this->stream, $headers['content-length']);
0217         } else {
0218             $this->body = stream_get_contents($this->stream);
0219         }
0220         fclose($this->stream);
0221         $this->stream = null;
0222     }
0223 
0224     public function __destruct()
0225     {
0226         if(is_resource($this->stream)) {
0227             fclose($this->stream);
0228             $this->stream = null;
0229         }
0230         if($this->_cleanup) {
0231             @unlink($this->stream_name);
0232         }
0233     }
0234 
0235 }