Response.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace Symfony\Components\RequestHandler;
  3. /*
  4. * This file is part of the Symfony package.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Response represents an HTTP response.
  13. *
  14. * @package Symfony
  15. * @subpackage Components_RequestHandler
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. */
  18. class Response
  19. {
  20. protected $content;
  21. protected $version;
  22. protected $statusCode;
  23. protected $statusText;
  24. protected $headers;
  25. protected $cookies;
  26. static public $statusTexts = array(
  27. 100 => 'Continue',
  28. 101 => 'Switching Protocols',
  29. 200 => 'OK',
  30. 201 => 'Created',
  31. 202 => 'Accepted',
  32. 203 => 'Non-Authoritative Information',
  33. 204 => 'No Content',
  34. 205 => 'Reset Content',
  35. 206 => 'Partial Content',
  36. 300 => 'Multiple Choices',
  37. 301 => 'Moved Permanently',
  38. 302 => 'Found',
  39. 303 => 'See Other',
  40. 304 => 'Not Modified',
  41. 305 => 'Use Proxy',
  42. 307 => 'Temporary Redirect',
  43. 400 => 'Bad Request',
  44. 401 => 'Unauthorized',
  45. 402 => 'Payment Required',
  46. 403 => 'Forbidden',
  47. 404 => 'Not Found',
  48. 405 => 'Method Not Allowed',
  49. 406 => 'Not Acceptable',
  50. 407 => 'Proxy Authentication Required',
  51. 408 => 'Request Timeout',
  52. 409 => 'Conflict',
  53. 410 => 'Gone',
  54. 411 => 'Length Required',
  55. 412 => 'Precondition Failed',
  56. 413 => 'Request Entity Too Large',
  57. 414 => 'Request-URI Too Long',
  58. 415 => 'Unsupported Media Type',
  59. 416 => 'Requested Range Not Satisfiable',
  60. 417 => 'Expectation Failed',
  61. 500 => 'Internal Server Error',
  62. 501 => 'Not Implemented',
  63. 502 => 'Bad Gateway',
  64. 503 => 'Service Unavailable',
  65. 504 => 'Gateway Timeout',
  66. 505 => 'HTTP Version Not Supported',
  67. );
  68. /**
  69. * Constructor.
  70. *
  71. * @param string $content The response content
  72. * @param integer $status The response status code
  73. * @param array $headers An array of response headers
  74. */
  75. public function __construct($content = '', $status = 200, $headers = array())
  76. {
  77. $this->setContent($content);
  78. $this->setStatusCode($status);
  79. $this->setProtocolVersion('1.0');
  80. $this->headers = array();
  81. foreach ($headers as $name => $value)
  82. {
  83. $this->setHeader($name, $value);
  84. }
  85. $this->cookies = array();
  86. }
  87. /**
  88. * Returns the response content after sending the headers.
  89. *
  90. * @return string The response content
  91. */
  92. public function __toString()
  93. {
  94. $this->sendHeaders();
  95. return (string) $this->getContent();
  96. }
  97. /**
  98. * Sets the response content
  99. *
  100. * @param string $content
  101. */
  102. public function setContent($content)
  103. {
  104. $this->content = $content;
  105. }
  106. /**
  107. * Gets the current response content
  108. *
  109. * @return string Content
  110. */
  111. public function getContent()
  112. {
  113. return $this->content;
  114. }
  115. /**
  116. * Sets the HTTP protocol version (1.0 or 1.1).
  117. *
  118. * @param string $version The HTTP protocol version
  119. */
  120. public function setProtocolVersion($version)
  121. {
  122. $this->version = $version;
  123. }
  124. /**
  125. * Gets the HTTP protocol version.
  126. *
  127. * @return string The HTTP protocol version
  128. */
  129. public function getProtocolVersion()
  130. {
  131. return $this->version;
  132. }
  133. /**
  134. * Sets a cookie.
  135. *
  136. * @param string $name The cookie name
  137. * @param string $value The value of the cookie
  138. * @param string $expire The time the cookie expires
  139. * @param string $path The path on the server in which the cookie will be available on
  140. * @param string $domain The domain that the cookie is available
  141. * @param bool $secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client
  142. * @param bool $httpOnly When TRUE the cookie will be made accessible only through the HTTP protocol
  143. *
  144. * @throws \InvalidArgumentException When the cookie expire parameter is not valid
  145. */
  146. public function setCookie($name, $value, $expire = null, $path = '/', $domain = '', $secure = false, $httpOnly = false)
  147. {
  148. if (null !== $expire)
  149. {
  150. if (is_numeric($expire))
  151. {
  152. $expire = (int) $expire;
  153. }
  154. else
  155. {
  156. $expire = strtotime($expire);
  157. if (false === $expire || -1 == $expire)
  158. {
  159. throw new \InvalidArgumentException('The cookie expire parameter is not valid.');
  160. }
  161. }
  162. }
  163. $this->cookies[$name] = array(
  164. 'name' => $name,
  165. 'value' => $value,
  166. 'expire' => $expire,
  167. 'path' => $path,
  168. 'domain' => $domain,
  169. 'secure' => (Boolean) $secure,
  170. 'httpOnly' => (Boolean) $httpOnly,
  171. );
  172. }
  173. /**
  174. * Retrieves cookies from the current web response.
  175. *
  176. * @return array Cookies
  177. */
  178. public function getCookies()
  179. {
  180. return $this->cookies;
  181. }
  182. /**
  183. * Sets response status code.
  184. *
  185. * @param integer $code HTTP status code
  186. * @param string $text HTTP status text
  187. *
  188. * @throws \InvalidArgumentException When the HTTP status code is not valid
  189. */
  190. public function setStatusCode($code, $text = null)
  191. {
  192. $this->statusCode = (int) $code;
  193. if ($this->statusCode < 100 || $this->statusCode > 599)
  194. {
  195. throw new \InvalidArgumentException(sprintf('The HTTP status code "%s" is not valid.', $code));
  196. }
  197. $this->statusText = false === $text ? '' : (null === $text ? self::$statusTexts[$this->statusCode] : $text);
  198. }
  199. /**
  200. * Retrieves status code for the current web response.
  201. *
  202. * @return string Status code
  203. */
  204. public function getStatusCode()
  205. {
  206. return $this->statusCode;
  207. }
  208. /**
  209. * Sets a HTTP header.
  210. *
  211. * @param string $name HTTP header name
  212. * @param string $value Value (if null, remove the HTTP header)
  213. * @param bool $replace Replace for the value
  214. *
  215. */
  216. public function setHeader($name, $value, $replace = true)
  217. {
  218. $name = $this->normalizeHeaderName($name);
  219. if (null === $value)
  220. {
  221. unset($this->headers[$name]);
  222. return;
  223. }
  224. if (!$replace)
  225. {
  226. $current = isset($this->headers[$name]) ? $this->headers[$name] : '';
  227. $value = ($current ? $current.', ' : '').$value;
  228. }
  229. $this->headers[$name] = $value;
  230. return $this;
  231. }
  232. /**
  233. * Gets HTTP header current value.
  234. *
  235. * @param string $name HTTP header name
  236. * @param string $default Default value returned if named HTTP header is not found
  237. *
  238. * @return array
  239. */
  240. public function getHeader($name, $default = null)
  241. {
  242. $name = $this->normalizeHeaderName($name);
  243. return isset($this->headers[$name]) ? $this->headers[$name] : $default;
  244. }
  245. /**
  246. * Checks if the response has given HTTP header.
  247. *
  248. * @param string $name HTTP header name
  249. *
  250. * @return bool
  251. */
  252. public function hasHeader($name)
  253. {
  254. return array_key_exists($this->normalizeHeaderName($name), $this->headers);
  255. }
  256. /**
  257. * Retrieves HTTP headers from the current web response.
  258. *
  259. * @return string HTTP headers
  260. */
  261. public function getHeaders()
  262. {
  263. return $this->headers;
  264. }
  265. /**
  266. * Sends HTTP headers, including cookies.
  267. */
  268. public function sendHeaders()
  269. {
  270. if (!$this->hasHeader('Content-Type'))
  271. {
  272. $this->setHeader('Content-Type', 'text/html');
  273. }
  274. // status
  275. header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText));
  276. // headers
  277. foreach ($this->headers as $name => $value)
  278. {
  279. header($name.': '.$value);
  280. }
  281. // cookies
  282. foreach ($this->cookies as $cookie)
  283. {
  284. setrawcookie($cookie['name'], $cookie['value'], $cookie['expire'], $cookie['path'], $cookie['domain'], $cookie['secure'], $cookie['httpOnly']);
  285. }
  286. }
  287. /**
  288. * Sends content for the current web response.
  289. */
  290. public function sendContent()
  291. {
  292. echo $this->content;
  293. }
  294. /**
  295. * Sends HTTP headers and content.
  296. */
  297. public function send()
  298. {
  299. $this->sendHeaders();
  300. $this->sendContent();
  301. }
  302. /**
  303. * Normalizes a HTTP header name.
  304. *
  305. * @param string $name The HTTP header name
  306. *
  307. * @return string The normalized HTTP header name
  308. */
  309. protected function normalizeHeaderName($name)
  310. {
  311. return strtr(strtolower($name), '_', '-');
  312. }
  313. }