PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/symfony/2.0.0pr3/src/vendor/symfony/src/Symfony/Component/HttpFoundation/HeaderBag.php

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