PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Components/HttpFoundation/HeaderBag.php

https://github.com/come/symfony
PHP | 261 lines | 127 code | 34 blank | 100 comment | 26 complexity | 95fa3b3beb4b8c422b84937d24bb514c MD5 | raw file
Possible License(s): ISC
  1. <?php
  2. namespace Symfony\Components\HttpFoundation;
  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. * HeaderBag is a container for HTTP headers.
  13. *
  14. * @package Symfony
  15. * @subpackage Components_HttpFoundation
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. */
  18. class HeaderBag
  19. {
  20. protected $headers;
  21. protected $cacheControl;
  22. protected $type;
  23. /**
  24. * Constructor.
  25. *
  26. * @param array $headers An array of HTTP headers
  27. * @param string $type The type (null, request, or response)
  28. */
  29. public function __construct(array $headers = array(), $type = null)
  30. {
  31. $this->replace($headers);
  32. if (null !== $type && !in_array($type, array('request', 'response'))) {
  33. throw new \InvalidArgumentException(sprintf('The "%s" type is not supported by the HeaderBag constructor.', $type));
  34. }
  35. $this->type = $type;
  36. }
  37. /**
  38. * Returns the headers.
  39. *
  40. * @return array An array of headers
  41. */
  42. public function all()
  43. {
  44. return $this->headers;
  45. }
  46. /**
  47. * Replaces the current HTTP headers by a new set.
  48. *
  49. * @param array $headers An array of HTTP headers
  50. */
  51. public function replace(array $headers = array())
  52. {
  53. $this->cacheControl = null;
  54. $this->headers = array();
  55. foreach ($headers as $key => $values) {
  56. $this->set($key, $values);
  57. }
  58. }
  59. /**
  60. * Returns a header value by name.
  61. *
  62. * @param string $key The header name
  63. * @param Boolean $first Whether to return the first value or all header values
  64. *
  65. * @return string|array The first header value if $first is true, an array of values otherwise
  66. */
  67. public function get($key, $first = true)
  68. {
  69. $key = strtr(strtolower($key), '_', '-');
  70. if (!array_key_exists($key, $this->headers)) {
  71. return $first ? null : array();
  72. }
  73. if ($first) {
  74. return count($this->headers[$key]) ? $this->headers[$key][0] : '';
  75. } else {
  76. return $this->headers[$key];
  77. }
  78. }
  79. /**
  80. * Sets a header by name.
  81. *
  82. * @param string $key The key
  83. * @param string|array $values The value or an array of values
  84. * @param Boolean $replace Whether to replace the actual value of not (true by default)
  85. */
  86. public function set($key, $values, $replace = true)
  87. {
  88. $key = strtr(strtolower($key), '_', '-');
  89. if (!is_array($values)) {
  90. $values = array($values);
  91. }
  92. if (true === $replace || !isset($this->headers[$key])) {
  93. $this->headers[$key] = $values;
  94. } else {
  95. $this->headers[$key] = array_merge($this->headers[$key], $values);
  96. }
  97. }
  98. /**
  99. * Returns true if the HTTP header is defined.
  100. *
  101. * @param string $key The HTTP header
  102. *
  103. * @return Boolean true if the parameter exists, false otherwise
  104. */
  105. public function has($key)
  106. {
  107. return array_key_exists(strtr(strtolower($key), '_', '-'), $this->headers);
  108. }
  109. /**
  110. * Returns true if the given HTTP header contains the given value.
  111. *
  112. * @param string $key The HTTP header name
  113. * @param string $value The HTTP value
  114. *
  115. * @return Boolean true if the value is contained in the header, false otherwise
  116. */
  117. public function contains($key, $value)
  118. {
  119. return in_array($value, $this->get($key, false));
  120. }
  121. /**
  122. * Deletes a header.
  123. *
  124. * @param string $key The HTTP header name
  125. */
  126. public function delete($key)
  127. {
  128. unset($this->headers[strtr(strtolower($key), '_', '-')]);
  129. }
  130. /**
  131. * Returns an instance able to manage the Cache-Control header.
  132. *
  133. * @return \Symfony\Components\HttpFoundation\CacheControl A CacheControl instance
  134. */
  135. public function getCacheControl()
  136. {
  137. if (null === $this->cacheControl) {
  138. $this->cacheControl = new CacheControl($this, $this->get('Cache-Control'), $this->type);
  139. }
  140. return $this->cacheControl;
  141. }
  142. /**
  143. * Sets a cookie.
  144. *
  145. * @param string $name The cookie name
  146. * @param string $value The value of the cookie
  147. * @param string $domain The domain that the cookie is available
  148. * @param string $expire The time the cookie expires
  149. * @param string $path The path on the server in which the cookie will be available on
  150. * @param bool $secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client
  151. * @param bool $httponly When TRUE the cookie will not be made accessible to JavaScript, preventing XSS attacks from stealing cookies
  152. *
  153. * @throws \InvalidArgumentException When the cookie expire parameter is not valid
  154. */
  155. public function setCookie($name, $value, $domain = null, $expires = null, $path = '/', $secure = false, $httponly = true)
  156. {
  157. // from PHP source code
  158. if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
  159. throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
  160. }
  161. if (preg_match("/[,; \t\r\n\013\014]/", $value)) {
  162. throw new \InvalidArgumentException(sprintf('The cookie value "%s" contains invalid characters.', $name));
  163. }
  164. if (!$name) {
  165. throw new \InvalidArgumentException('The cookie name cannot be empty');
  166. }
  167. $cookie = sprintf('%s=%s', $name, urlencode($value));
  168. if ('request' === $this->type) {
  169. return $this->set('Cookie', $cookie);
  170. }
  171. if (null !== $expires) {
  172. if (is_numeric($expires)) {
  173. $expires = (int) $expires;
  174. } elseif ($expires instanceof \DateTime) {
  175. $expires = $expires->getTimestamp();
  176. } else {
  177. $expires = strtotime($expires);
  178. if (false === $expires || -1 == $expires) {
  179. throw new \InvalidArgumentException(sprintf('The "expires" cookie parameter is not valid.', $expires));
  180. }
  181. }
  182. $cookie .= '; expires='.substr(\DateTime::createFromFormat('U', $expires, new \DateTimeZone('UTC'))->format('D, d-M-Y H:i:s T'), 0, -5);
  183. }
  184. if ($domain) {
  185. $cookie .= '; domain='.$domain;
  186. }
  187. if ('/' !== $path) {
  188. $cookie .= '; path='.$path;
  189. }
  190. if ($secure) {
  191. $cookie .= '; secure';
  192. }
  193. if ($httponly) {
  194. $cookie .= '; httponly';
  195. }
  196. $this->set('Set-Cookie', $cookie, false);
  197. }
  198. /**
  199. * Returns the HTTP header value converted to a date.
  200. *
  201. * @param string $key The parameter key
  202. * @param \DateTime $default The default value
  203. *
  204. * @return \DateTime The filtered value
  205. */
  206. public function getDate($key, \DateTime $default = null)
  207. {
  208. if (null === $value = $this->get($key)) {
  209. return $default;
  210. }
  211. if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) {
  212. throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value));
  213. }
  214. return $date;
  215. }
  216. /**
  217. * Normalizes a HTTP header name.
  218. *
  219. * @param string $key The HTTP header name
  220. *
  221. * @return string The normalized HTTP header name
  222. */
  223. static public function normalizeHeaderName($key)
  224. {
  225. return strtr(strtolower($key), '_', '-');
  226. }
  227. }