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