/wp-content/plugins/w3-total-cache/lib/Microsoft/Http/Response/Stream.php

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