Debian Codegraph @

lib/3rdParty/composer/symfony/http-foundation/ResponseHeaderBag.php

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