HttpResponse.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_Feed_Pubsubhubbub
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @see Zend_Feed_Pubsubhubbub
  22. */
  23. require_once 'Zend/Feed/Pubsubhubbub.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Feed_Pubsubhubbub
  27. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Feed_Pubsubhubbub_HttpResponse
  31. {
  32. /**
  33. * The body of any response to the current callback request
  34. *
  35. * @var string
  36. */
  37. protected $_body = '';
  38. /**
  39. * Array of headers. Each header is an array with keys 'name' and 'value'
  40. *
  41. * @var array
  42. */
  43. protected $_headers = array();
  44. /**
  45. * HTTP response code to use in headers
  46. *
  47. * @var int
  48. */
  49. protected $_httpResponseCode = 200;
  50. /**
  51. * Send the response, including all headers
  52. *
  53. * @return void
  54. */
  55. public function sendResponse()
  56. {
  57. $this->sendHeaders();
  58. echo $this->getBody();
  59. }
  60. /**
  61. * Send all headers
  62. *
  63. * Sends any headers specified. If an {@link setHttpResponseCode() HTTP response code}
  64. * has been specified, it is sent with the first header.
  65. *
  66. * @return void
  67. */
  68. public function sendHeaders()
  69. {
  70. if (count($this->_headers) || (200 != $this->_httpResponseCode)) {
  71. $this->canSendHeaders(true);
  72. } elseif (200 == $this->_httpResponseCode) {
  73. return;
  74. }
  75. $httpCodeSent = false;
  76. foreach ($this->_headers as $header) {
  77. if (!$httpCodeSent && $this->_httpResponseCode) {
  78. header($header['name'] . ': ' . $header['value'], $header['replace'], $this->_httpResponseCode);
  79. $httpCodeSent = true;
  80. } else {
  81. header($header['name'] . ': ' . $header['value'], $header['replace']);
  82. }
  83. }
  84. if (!$httpCodeSent) {
  85. header('HTTP/1.1 ' . $this->_httpResponseCode);
  86. $httpCodeSent = true;
  87. }
  88. }
  89. /**
  90. * Set a header
  91. *
  92. * If $replace is true, replaces any headers already defined with that
  93. * $name.
  94. *
  95. * @param string $name
  96. * @param string $value
  97. * @param boolean $replace
  98. * @return Zend_Feed_Pubsubhubbub_HttpResponse
  99. */
  100. public function setHeader($name, $value, $replace = false)
  101. {
  102. $name = $this->_normalizeHeader($name);
  103. $value = (string) $value;
  104. if ($replace) {
  105. foreach ($this->_headers as $key => $header) {
  106. if ($name == $header['name']) {
  107. unset($this->_headers[$key]);
  108. }
  109. }
  110. }
  111. $this->_headers[] = array(
  112. 'name' => $name,
  113. 'value' => $value,
  114. 'replace' => $replace,
  115. );
  116. return $this;
  117. }
  118. /**
  119. * Check if a specific Header is set and return its value
  120. *
  121. * @param string $name
  122. * @return string|null
  123. */
  124. public function getHeader($name)
  125. {
  126. $name = $this->_normalizeHeader($name);
  127. foreach ($this->_headers as $header) {
  128. if ($header['name'] == $name) {
  129. return $header['value'];
  130. }
  131. }
  132. }
  133. /**
  134. * Return array of headers; see {@link $_headers} for format
  135. *
  136. * @return array
  137. */
  138. public function getHeaders()
  139. {
  140. return $this->_headers;
  141. }
  142. /**
  143. * Can we send headers?
  144. *
  145. * @param boolean $throw Whether or not to throw an exception if headers have been sent; defaults to false
  146. * @return boolean
  147. * @throws Zend_Feed_Pubsubhubbub_Exception
  148. */
  149. public function canSendHeaders($throw = false)
  150. {
  151. $ok = headers_sent($file, $line);
  152. if ($ok && $throw) {
  153. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  154. throw new Zend_Feed_Pubsubhubbub_Exception('Cannot send headers; headers already sent in ' . $file . ', line ' . $line);
  155. }
  156. return !$ok;
  157. }
  158. /**
  159. * Set HTTP response code to use with headers
  160. *
  161. * @param int $code
  162. * @return Zend_Feed_Pubsubhubbub_HttpResponse
  163. */
  164. public function setHttpResponseCode($code)
  165. {
  166. if (!is_int($code) || (100 > $code) || (599 < $code)) {
  167. require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
  168. throw new Zend_Feed_Pubsubhubbub_Exception('Invalid HTTP response'
  169. . ' code:' . $code);
  170. }
  171. $this->_httpResponseCode = $code;
  172. return $this;
  173. }
  174. /**
  175. * Retrieve HTTP response code
  176. *
  177. * @return int
  178. */
  179. public function getHttpResponseCode()
  180. {
  181. return $this->_httpResponseCode;
  182. }
  183. /**
  184. * Set body content
  185. *
  186. * @param string $content
  187. * @return Zend_Feed_Pubsubhubbub_HttpResponse
  188. */
  189. public function setBody($content)
  190. {
  191. $this->_body = (string) $content;
  192. $this->setHeader('content-length', strlen($content));
  193. return $this;
  194. }
  195. /**
  196. * Return the body content
  197. *
  198. * @return string
  199. */
  200. public function getBody()
  201. {
  202. return $this->_body;
  203. }
  204. /**
  205. * Normalizes a header name to X-Capitalized-Names
  206. *
  207. * @param string $name
  208. * @return string
  209. */
  210. protected function _normalizeHeader($name)
  211. {
  212. $filtered = str_replace(array('-', '_'), ' ', (string) $name);
  213. $filtered = ucwords(strtolower($filtered));
  214. $filtered = str_replace(' ', '-', $filtered);
  215. return $filtered;
  216. }
  217. }