Exception
2 years ago
File
1 year ago
RateLimiter
2 years ago
Session
1 year ago
Test
1 year ago
AcceptHeader.php
1 year ago
AcceptHeaderItem.php
2 years ago
BinaryFileResponse.php
1 year ago
Cookie.php
1 year ago
ExpressionRequestMatcher.php
2 years ago
FileBag.php
1 year ago
HeaderBag.php
1 year ago
HeaderUtils.php
1 year ago
InputBag.php
2 years ago
IpUtils.php
1 year ago
JsonResponse.php
1 year ago
LICENSE
2 years ago
ParameterBag.php
1 year ago
README.md
2 years ago
RedirectResponse.php
2 years ago
Request.php
7 months ago
RequestMatcher.php
1 year ago
RequestMatcherInterface.php
2 years ago
RequestStack.php
2 years ago
Response.php
1 year ago
ResponseHeaderBag.php
1 year ago
ServerBag.php
1 year ago
StreamedResponse.php
1 year ago
UrlHelper.php
2 years ago
ResponseHeaderBag.php
242 lines
| 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 | namespace Matomo\Dependencies\Symfony\Component\HttpFoundation; |
| 12 | |
| 13 | /** |
| 14 | * ResponseHeaderBag is a container for Response HTTP headers. |
| 15 | * |
| 16 | * @author Fabien Potencier <fabien@symfony.com> |
| 17 | */ |
| 18 | class ResponseHeaderBag extends HeaderBag |
| 19 | { |
| 20 | public const COOKIES_FLAT = 'flat'; |
| 21 | public const COOKIES_ARRAY = 'array'; |
| 22 | public const DISPOSITION_ATTACHMENT = 'attachment'; |
| 23 | public const DISPOSITION_INLINE = 'inline'; |
| 24 | protected $computedCacheControl = []; |
| 25 | protected $cookies = []; |
| 26 | protected $headerNames = []; |
| 27 | public function __construct(array $headers = []) |
| 28 | { |
| 29 | parent::__construct($headers); |
| 30 | if (!isset($this->headers['cache-control'])) { |
| 31 | $this->set('Cache-Control', ''); |
| 32 | } |
| 33 | /* RFC2616 - 14.18 says all Responses need to have a Date */ |
| 34 | if (!isset($this->headers['date'])) { |
| 35 | $this->initDate(); |
| 36 | } |
| 37 | } |
| 38 | /** |
| 39 | * Returns the headers, with original capitalizations. |
| 40 | * |
| 41 | * @return array |
| 42 | */ |
| 43 | public function allPreserveCase() |
| 44 | { |
| 45 | $headers = []; |
| 46 | foreach ($this->all() as $name => $value) { |
| 47 | $headers[$this->headerNames[$name] ?? $name] = $value; |
| 48 | } |
| 49 | return $headers; |
| 50 | } |
| 51 | public function allPreserveCaseWithoutCookies() |
| 52 | { |
| 53 | $headers = $this->allPreserveCase(); |
| 54 | if (isset($this->headerNames['set-cookie'])) { |
| 55 | unset($headers[$this->headerNames['set-cookie']]); |
| 56 | } |
| 57 | return $headers; |
| 58 | } |
| 59 | /** |
| 60 | * {@inheritdoc} |
| 61 | */ |
| 62 | public function replace(array $headers = []) |
| 63 | { |
| 64 | $this->headerNames = []; |
| 65 | parent::replace($headers); |
| 66 | if (!isset($this->headers['cache-control'])) { |
| 67 | $this->set('Cache-Control', ''); |
| 68 | } |
| 69 | if (!isset($this->headers['date'])) { |
| 70 | $this->initDate(); |
| 71 | } |
| 72 | } |
| 73 | /** |
| 74 | * {@inheritdoc} |
| 75 | */ |
| 76 | public function all(?string $key = null) |
| 77 | { |
| 78 | $headers = parent::all(); |
| 79 | if (null !== $key) { |
| 80 | $key = strtr($key, self::UPPER, self::LOWER); |
| 81 | return 'set-cookie' !== $key ? $headers[$key] ?? [] : array_map('strval', $this->getCookies()); |
| 82 | } |
| 83 | foreach ($this->getCookies() as $cookie) { |
| 84 | $headers['set-cookie'][] = (string) $cookie; |
| 85 | } |
| 86 | return $headers; |
| 87 | } |
| 88 | /** |
| 89 | * {@inheritdoc} |
| 90 | */ |
| 91 | public function set(string $key, $values, bool $replace = \true) |
| 92 | { |
| 93 | $uniqueKey = strtr($key, self::UPPER, self::LOWER); |
| 94 | if ('set-cookie' === $uniqueKey) { |
| 95 | if ($replace) { |
| 96 | $this->cookies = []; |
| 97 | } |
| 98 | foreach ((array) $values as $cookie) { |
| 99 | $this->setCookie(Cookie::fromString($cookie)); |
| 100 | } |
| 101 | $this->headerNames[$uniqueKey] = $key; |
| 102 | return; |
| 103 | } |
| 104 | $this->headerNames[$uniqueKey] = $key; |
| 105 | parent::set($key, $values, $replace); |
| 106 | // ensure the cache-control header has sensible defaults |
| 107 | if (\in_array($uniqueKey, ['cache-control', 'etag', 'last-modified', 'expires'], \true) && '' !== ($computed = $this->computeCacheControlValue())) { |
| 108 | $this->headers['cache-control'] = [$computed]; |
| 109 | $this->headerNames['cache-control'] = 'Cache-Control'; |
| 110 | $this->computedCacheControl = $this->parseCacheControl($computed); |
| 111 | } |
| 112 | } |
| 113 | /** |
| 114 | * {@inheritdoc} |
| 115 | */ |
| 116 | public function remove(string $key) |
| 117 | { |
| 118 | $uniqueKey = strtr($key, self::UPPER, self::LOWER); |
| 119 | unset($this->headerNames[$uniqueKey]); |
| 120 | if ('set-cookie' === $uniqueKey) { |
| 121 | $this->cookies = []; |
| 122 | return; |
| 123 | } |
| 124 | parent::remove($key); |
| 125 | if ('cache-control' === $uniqueKey) { |
| 126 | $this->computedCacheControl = []; |
| 127 | } |
| 128 | if ('date' === $uniqueKey) { |
| 129 | $this->initDate(); |
| 130 | } |
| 131 | } |
| 132 | /** |
| 133 | * {@inheritdoc} |
| 134 | */ |
| 135 | public function hasCacheControlDirective(string $key) |
| 136 | { |
| 137 | return \array_key_exists($key, $this->computedCacheControl); |
| 138 | } |
| 139 | /** |
| 140 | * {@inheritdoc} |
| 141 | */ |
| 142 | public function getCacheControlDirective(string $key) |
| 143 | { |
| 144 | return $this->computedCacheControl[$key] ?? null; |
| 145 | } |
| 146 | public function setCookie(Cookie $cookie) |
| 147 | { |
| 148 | $this->cookies[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie; |
| 149 | $this->headerNames['set-cookie'] = 'Set-Cookie'; |
| 150 | } |
| 151 | /** |
| 152 | * Removes a cookie from the array, but does not unset it in the browser. |
| 153 | */ |
| 154 | public function removeCookie(string $name, ?string $path = '/', ?string $domain = null) |
| 155 | { |
| 156 | if (null === $path) { |
| 157 | $path = '/'; |
| 158 | } |
| 159 | unset($this->cookies[$domain][$path][$name]); |
| 160 | if (empty($this->cookies[$domain][$path])) { |
| 161 | unset($this->cookies[$domain][$path]); |
| 162 | if (empty($this->cookies[$domain])) { |
| 163 | unset($this->cookies[$domain]); |
| 164 | } |
| 165 | } |
| 166 | if (empty($this->cookies)) { |
| 167 | unset($this->headerNames['set-cookie']); |
| 168 | } |
| 169 | } |
| 170 | /** |
| 171 | * Returns an array with all cookies. |
| 172 | * |
| 173 | * @return Cookie[] |
| 174 | * |
| 175 | * @throws \InvalidArgumentException When the $format is invalid |
| 176 | */ |
| 177 | public function getCookies(string $format = self::COOKIES_FLAT) |
| 178 | { |
| 179 | if (!\in_array($format, [self::COOKIES_FLAT, self::COOKIES_ARRAY])) { |
| 180 | throw new \InvalidArgumentException(sprintf('Format "%s" invalid (%s).', $format, implode(', ', [self::COOKIES_FLAT, self::COOKIES_ARRAY]))); |
| 181 | } |
| 182 | if (self::COOKIES_ARRAY === $format) { |
| 183 | return $this->cookies; |
| 184 | } |
| 185 | $flattenedCookies = []; |
| 186 | foreach ($this->cookies as $path) { |
| 187 | foreach ($path as $cookies) { |
| 188 | foreach ($cookies as $cookie) { |
| 189 | $flattenedCookies[] = $cookie; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | return $flattenedCookies; |
| 194 | } |
| 195 | /** |
| 196 | * Clears a cookie in the browser. |
| 197 | */ |
| 198 | public function clearCookie(string $name, ?string $path = '/', ?string $domain = null, bool $secure = \false, bool $httpOnly = \true, ?string $sameSite = null) |
| 199 | { |
| 200 | $this->setCookie(new Cookie($name, null, 1, $path, $domain, $secure, $httpOnly, \false, $sameSite)); |
| 201 | } |
| 202 | /** |
| 203 | * @see HeaderUtils::makeDisposition() |
| 204 | */ |
| 205 | public function makeDisposition(string $disposition, string $filename, string $filenameFallback = '') |
| 206 | { |
| 207 | return HeaderUtils::makeDisposition($disposition, $filename, $filenameFallback); |
| 208 | } |
| 209 | /** |
| 210 | * Returns the calculated value of the cache-control header. |
| 211 | * |
| 212 | * This considers several other headers and calculates or modifies the |
| 213 | * cache-control header to a sensible, conservative value. |
| 214 | * |
| 215 | * @return string |
| 216 | */ |
| 217 | protected function computeCacheControlValue() |
| 218 | { |
| 219 | if (!$this->cacheControl) { |
| 220 | if ($this->has('Last-Modified') || $this->has('Expires')) { |
| 221 | return 'private, must-revalidate'; |
| 222 | // allows for heuristic expiration (RFC 7234 Section 4.2.2) in the case of "Last-Modified" |
| 223 | } |
| 224 | // conservative by default |
| 225 | return 'no-cache, private'; |
| 226 | } |
| 227 | $header = $this->getCacheControlHeader(); |
| 228 | if (isset($this->cacheControl['public']) || isset($this->cacheControl['private'])) { |
| 229 | return $header; |
| 230 | } |
| 231 | // public if s-maxage is defined, private otherwise |
| 232 | if (!isset($this->cacheControl['s-maxage'])) { |
| 233 | return $header . ', private'; |
| 234 | } |
| 235 | return $header; |
| 236 | } |
| 237 | private function initDate() : void |
| 238 | { |
| 239 | $this->set('Date', gmdate('D, d M Y H:i:s') . ' GMT'); |
| 240 | } |
| 241 | } |
| 242 |