PageRenderTime 63ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/Http/Response/Stream.php

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