PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/concreteOLD/libraries/3rdparty/Zend/Http/Response/Stream.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 235 lines | 82 code | 21 blank | 132 comment | 9 complexity | cbd9f5a55d9ae3d607d29812b94bc84f MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Http
  17. * @subpackage Response
  18. * @version $Id: Stream.php 23775 2011-03-01 17:25:24Z ralph $
  19. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /**
  23. * Zend_Http_Response represents an HTTP 1.0 / 1.1 response message. It
  24. * includes easy access to all the response's different elemts, as well as some
  25. * convenience methods for parsing and validating HTTP responses.
  26. *
  27. * @package Zend_Http
  28. * @subpackage Response
  29. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Http_Response_Stream extends Zend_Http_Response
  33. {
  34. /**
  35. * Response as stream
  36. *
  37. * @var resource
  38. */
  39. protected $stream;
  40. /**
  41. * The name of the file containing the stream
  42. *
  43. * Will be empty if stream is not file-based.
  44. *
  45. * @var string
  46. */
  47. protected $stream_name;
  48. /**
  49. * Should we clean up the stream file when this response is closed?
  50. *
  51. * @var boolean
  52. */
  53. protected $_cleanup;
  54. /**
  55. * Get the response as stream
  56. *
  57. * @return resourse
  58. */
  59. public function getStream()
  60. {
  61. return $this->stream;
  62. }
  63. /**
  64. * Set the response stream
  65. *
  66. * @param resourse $stream
  67. * @return Zend_Http_Response_Stream
  68. */
  69. public function setStream($stream)
  70. {
  71. $this->stream = $stream;
  72. return $this;
  73. }
  74. /**
  75. * Get the cleanup trigger
  76. *
  77. * @return boolean
  78. */
  79. public function getCleanup() {
  80. return $this->_cleanup;
  81. }
  82. /**
  83. * Set the cleanup trigger
  84. *
  85. * @param bool $cleanup Set cleanup trigger
  86. */
  87. public function setCleanup($cleanup = true) {
  88. $this->_cleanup = $cleanup;
  89. }
  90. /**
  91. * Get file name associated with the stream
  92. *
  93. * @return string
  94. */
  95. public function getStreamName() {
  96. return $this->stream_name;
  97. }
  98. /**
  99. * Set file name associated with the stream
  100. *
  101. * @param string $stream_name Name to set
  102. * @return Zend_Http_Response_Stream
  103. */
  104. public function setStreamName($stream_name) {
  105. $this->stream_name = $stream_name;
  106. return $this;
  107. }
  108. /**
  109. * HTTP response constructor
  110. *
  111. * In most cases, you would use Zend_Http_Response::fromString to parse an HTTP
  112. * response string and create a new Zend_Http_Response object.
  113. *
  114. * NOTE: The constructor no longer accepts nulls or empty values for the code and
  115. * headers and will throw an exception if the passed values do not form a valid HTTP
  116. * responses.
  117. *
  118. * If no message is passed, the message will be guessed according to the response code.
  119. *
  120. * @param int $code Response code (200, 404, ...)
  121. * @param array $headers Headers array
  122. * @param string $body Response body
  123. * @param string $version HTTP version
  124. * @param string $message Response code as text
  125. * @throws Zend_Http_Exception
  126. */
  127. public function __construct($code, $headers, $body = null, $version = '1.1', $message = null)
  128. {
  129. if(is_resource($body)) {
  130. $this->setStream($body);
  131. $body = '';
  132. }
  133. parent::__construct($code, $headers, $body, $version, $message);
  134. }
  135. /**
  136. * Create a new Zend_Http_Response_Stream object from a string
  137. *
  138. * @param string $response_str
  139. * @param resource $stream
  140. * @return Zend_Http_Response_Stream
  141. */
  142. public static function fromStream($response_str, $stream)
  143. {
  144. $code = self::extractCode($response_str);
  145. $headers = self::extractHeaders($response_str);
  146. $version = self::extractVersion($response_str);
  147. $message = self::extractMessage($response_str);
  148. return new self($code, $headers, $stream, $version, $message);
  149. }
  150. /**
  151. * Get the response body as string
  152. *
  153. * This method returns the body of the HTTP response (the content), as it
  154. * should be in it's readable version - that is, after decoding it (if it
  155. * was decoded), deflating it (if it was gzip compressed), etc.
  156. *
  157. * If you want to get the raw body (as transfered on wire) use
  158. * $this->getRawBody() instead.
  159. *
  160. * @return string
  161. */
  162. public function getBody()
  163. {
  164. if($this->stream != null) {
  165. $this->readStream();
  166. }
  167. return parent::getBody();
  168. }
  169. /**
  170. * Get the raw response body (as transfered "on wire") as string
  171. *
  172. * If the body is encoded (with Transfer-Encoding, not content-encoding -
  173. * IE "chunked" body), gzip compressed, etc. it will not be decoded.
  174. *
  175. * @return string
  176. */
  177. public function getRawBody()
  178. {
  179. if($this->stream) {
  180. $this->readStream();
  181. }
  182. return $this->body;
  183. }
  184. /**
  185. * Read stream content and return it as string
  186. *
  187. * Function reads the remainder of the body from the stream and closes the stream.
  188. *
  189. * @return string
  190. */
  191. protected function readStream()
  192. {
  193. if(!is_resource($this->stream)) {
  194. return '';
  195. }
  196. if(isset($headers['content-length'])) {
  197. $this->body = stream_get_contents($this->stream, $headers['content-length']);
  198. } else {
  199. $this->body = stream_get_contents($this->stream);
  200. }
  201. fclose($this->stream);
  202. $this->stream = null;
  203. }
  204. public function __destruct()
  205. {
  206. if(is_resource($this->stream)) {
  207. fclose($this->stream);
  208. $this->stream = null;
  209. }
  210. if($this->_cleanup) {
  211. @unlink($this->stream_name);
  212. }
  213. }
  214. }