No Description

HeaderBag.php 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation;
  11. /**
  12. * HeaderBag is a container for HTTP headers.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. *
  16. * @api
  17. */
  18. class HeaderBag implements \IteratorAggregate, \Countable
  19. {
  20. protected $headers = array();
  21. protected $cacheControl = array();
  22. /**
  23. * Constructor.
  24. *
  25. * @param array $headers An array of HTTP headers
  26. *
  27. * @api
  28. */
  29. public function __construct(array $headers = array())
  30. {
  31. foreach ($headers as $key => $values) {
  32. $this->set($key, $values);
  33. }
  34. }
  35. /**
  36. * Returns the headers as a string.
  37. *
  38. * @return string The headers
  39. */
  40. public function __toString()
  41. {
  42. if (!$this->headers) {
  43. return '';
  44. }
  45. $max = max(array_map('strlen', array_keys($this->headers))) + 1;
  46. $content = '';
  47. ksort($this->headers);
  48. foreach ($this->headers as $name => $values) {
  49. $name = implode('-', array_map('ucfirst', explode('-', $name)));
  50. foreach ($values as $value) {
  51. $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value);
  52. }
  53. }
  54. return $content;
  55. }
  56. /**
  57. * Returns the headers.
  58. *
  59. * @return array An array of headers
  60. *
  61. * @api
  62. */
  63. public function all()
  64. {
  65. return $this->headers;
  66. }
  67. /**
  68. * Returns the parameter keys.
  69. *
  70. * @return array An array of parameter keys
  71. *
  72. * @api
  73. */
  74. public function keys()
  75. {
  76. return array_keys($this->headers);
  77. }
  78. /**
  79. * Replaces the current HTTP headers by a new set.
  80. *
  81. * @param array $headers An array of HTTP headers
  82. *
  83. * @api
  84. */
  85. public function replace(array $headers = array())
  86. {
  87. $this->headers = array();
  88. $this->add($headers);
  89. }
  90. /**
  91. * Adds new headers the current HTTP headers set.
  92. *
  93. * @param array $headers An array of HTTP headers
  94. *
  95. * @api
  96. */
  97. public function add(array $headers)
  98. {
  99. foreach ($headers as $key => $values) {
  100. $this->set($key, $values);
  101. }
  102. }
  103. /**
  104. * Returns a header value by name.
  105. *
  106. * @param string $key The header name
  107. * @param mixed $default The default value
  108. * @param bool $first Whether to return the first value or all header values
  109. *
  110. * @return string|array The first header value if $first is true, an array of values otherwise
  111. *
  112. * @api
  113. */
  114. public function get($key, $default = null, $first = true)
  115. {
  116. $key = strtr(strtolower($key), '_', '-');
  117. if (!array_key_exists($key, $this->headers)) {
  118. if (null === $default) {
  119. return $first ? null : array();
  120. }
  121. return $first ? $default : array($default);
  122. }
  123. if ($first) {
  124. return count($this->headers[$key]) ? $this->headers[$key][0] : $default;
  125. }
  126. return $this->headers[$key];
  127. }
  128. /**
  129. * Sets a header by name.
  130. *
  131. * @param string $key The key
  132. * @param string|array $values The value or an array of values
  133. * @param bool $replace Whether to replace the actual value or not (true by default)
  134. *
  135. * @api
  136. */
  137. public function set($key, $values, $replace = true)
  138. {
  139. $key = strtr(strtolower($key), '_', '-');
  140. $values = array_values((array) $values);
  141. if (true === $replace || !isset($this->headers[$key])) {
  142. $this->headers[$key] = $values;
  143. } else {
  144. $this->headers[$key] = array_merge($this->headers[$key], $values);
  145. }
  146. if ('cache-control' === $key) {
  147. $this->cacheControl = $this->parseCacheControl($values[0]);
  148. }
  149. }
  150. /**
  151. * Returns true if the HTTP header is defined.
  152. *
  153. * @param string $key The HTTP header
  154. *
  155. * @return bool true if the parameter exists, false otherwise
  156. *
  157. * @api
  158. */
  159. public function has($key)
  160. {
  161. return array_key_exists(strtr(strtolower($key), '_', '-'), $this->headers);
  162. }
  163. /**
  164. * Returns true if the given HTTP header contains the given value.
  165. *
  166. * @param string $key The HTTP header name
  167. * @param string $value The HTTP value
  168. *
  169. * @return bool true if the value is contained in the header, false otherwise
  170. *
  171. * @api
  172. */
  173. public function contains($key, $value)
  174. {
  175. return in_array($value, $this->get($key, null, false));
  176. }
  177. /**
  178. * Removes a header.
  179. *
  180. * @param string $key The HTTP header name
  181. *
  182. * @api
  183. */
  184. public function remove($key)
  185. {
  186. $key = strtr(strtolower($key), '_', '-');
  187. unset($this->headers[$key]);
  188. if ('cache-control' === $key) {
  189. $this->cacheControl = array();
  190. }
  191. }
  192. /**
  193. * Returns the HTTP header value converted to a date.
  194. *
  195. * @param string $key The parameter key
  196. * @param \DateTime $default The default value
  197. *
  198. * @return null|\DateTime The parsed DateTime or the default value if the header does not exist
  199. *
  200. * @throws \RuntimeException When the HTTP header is not parseable
  201. *
  202. * @api
  203. */
  204. public function getDate($key, \DateTime $default = null)
  205. {
  206. if (null === $value = $this->get($key)) {
  207. return $default;
  208. }
  209. if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) {
  210. throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value));
  211. }
  212. return $date;
  213. }
  214. /**
  215. * Adds a custom Cache-Control directive.
  216. *
  217. * @param string $key The Cache-Control directive name
  218. * @param mixed $value The Cache-Control directive value
  219. */
  220. public function addCacheControlDirective($key, $value = true)
  221. {
  222. $this->cacheControl[$key] = $value;
  223. $this->set('Cache-Control', $this->getCacheControlHeader());
  224. }
  225. /**
  226. * Returns true if the Cache-Control directive is defined.
  227. *
  228. * @param string $key The Cache-Control directive
  229. *
  230. * @return bool true if the directive exists, false otherwise
  231. */
  232. public function hasCacheControlDirective($key)
  233. {
  234. return array_key_exists($key, $this->cacheControl);
  235. }
  236. /**
  237. * Returns a Cache-Control directive value by name.
  238. *
  239. * @param string $key The directive name
  240. *
  241. * @return mixed|null The directive value if defined, null otherwise
  242. */
  243. public function getCacheControlDirective($key)
  244. {
  245. return array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
  246. }
  247. /**
  248. * Removes a Cache-Control directive.
  249. *
  250. * @param string $key The Cache-Control directive
  251. */
  252. public function removeCacheControlDirective($key)
  253. {
  254. unset($this->cacheControl[$key]);
  255. $this->set('Cache-Control', $this->getCacheControlHeader());
  256. }
  257. /**
  258. * Returns an iterator for headers.
  259. *
  260. * @return \ArrayIterator An \ArrayIterator instance
  261. */
  262. public function getIterator()
  263. {
  264. return new \ArrayIterator($this->headers);
  265. }
  266. /**
  267. * Returns the number of headers.
  268. *
  269. * @return int The number of headers
  270. */
  271. public function count()
  272. {
  273. return count($this->headers);
  274. }
  275. protected function getCacheControlHeader()
  276. {
  277. $parts = array();
  278. ksort($this->cacheControl);
  279. foreach ($this->cacheControl as $key => $value) {
  280. if (true === $value) {
  281. $parts[] = $key;
  282. } else {
  283. if (preg_match('#[^a-zA-Z0-9._-]#', $value)) {
  284. $value = '"'.$value.'"';
  285. }
  286. $parts[] = "$key=$value";
  287. }
  288. }
  289. return implode(', ', $parts);
  290. }
  291. /**
  292. * Parses a Cache-Control HTTP header.
  293. *
  294. * @param string $header The value of the Cache-Control HTTP header
  295. *
  296. * @return array An array representing the attribute values
  297. */
  298. protected function parseCacheControl($header)
  299. {
  300. $cacheControl = array();
  301. preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER);
  302. foreach ($matches as $match) {
  303. $cacheControl[strtolower($match[1])] = isset($match[3]) ? $match[3] : (isset($match[2]) ? $match[2] : true);
  304. }
  305. return $cacheControl;
  306. }
  307. }