PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / trunk
Matomo Analytics – Powerful, Privacy-First Insights for WordPress vtrunk
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / vendor / prefixed / symfony / http-kernel / HttpCache / ResponseCacheStrategy.php
matomo / app / vendor / prefixed / symfony / http-kernel / HttpCache Last commit date
AbstractSurrogate.php 1 year ago Esi.php 1 year ago HttpCache.php 1 year ago ResponseCacheStrategy.php 1 year ago ResponseCacheStrategyInterface.php 2 years ago Ssi.php 1 year ago Store.php 1 year ago StoreInterface.php 2 years ago SubRequestHandler.php 2 years ago SurrogateInterface.php 1 year ago
ResponseCacheStrategy.php
176 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\HttpKernel\HttpCache;
12
13 use Matomo\Dependencies\Symfony\Component\HttpFoundation\Response;
14 /**
15 * ResponseCacheStrategy knows how to compute the Response cache HTTP header
16 * based on the different response cache headers.
17 *
18 * This implementation changes the main response TTL to the smallest TTL received
19 * or force validation if one of the surrogates has validation cache strategy.
20 *
21 * @author Fabien Potencier <fabien@symfony.com>
22 */
23 class ResponseCacheStrategy implements ResponseCacheStrategyInterface
24 {
25 /**
26 * Cache-Control headers that are sent to the final response if they appear in ANY of the responses.
27 */
28 private const OVERRIDE_DIRECTIVES = ['private', 'no-cache', 'no-store', 'no-transform', 'must-revalidate', 'proxy-revalidate'];
29 /**
30 * Cache-Control headers that are sent to the final response if they appear in ALL of the responses.
31 */
32 private const INHERIT_DIRECTIVES = ['public', 'immutable'];
33 private $embeddedResponses = 0;
34 private $isNotCacheableResponseEmbedded = \false;
35 private $age = 0;
36 private $flagDirectives = ['no-cache' => null, 'no-store' => null, 'no-transform' => null, 'must-revalidate' => null, 'proxy-revalidate' => null, 'public' => null, 'private' => null, 'immutable' => null];
37 private $ageDirectives = ['max-age' => null, 's-maxage' => null, 'expires' => \false];
38 /**
39 * {@inheritdoc}
40 */
41 public function add(Response $response)
42 {
43 ++$this->embeddedResponses;
44 foreach (self::OVERRIDE_DIRECTIVES as $directive) {
45 if ($response->headers->hasCacheControlDirective($directive)) {
46 $this->flagDirectives[$directive] = \true;
47 }
48 }
49 foreach (self::INHERIT_DIRECTIVES as $directive) {
50 if (\false !== $this->flagDirectives[$directive]) {
51 $this->flagDirectives[$directive] = $response->headers->hasCacheControlDirective($directive);
52 }
53 }
54 $age = $response->getAge();
55 $this->age = max($this->age, $age);
56 if ($this->willMakeFinalResponseUncacheable($response)) {
57 $this->isNotCacheableResponseEmbedded = \true;
58 return;
59 }
60 $maxAge = $response->headers->hasCacheControlDirective('max-age') ? (int) $response->headers->getCacheControlDirective('max-age') : null;
61 $sharedMaxAge = $response->headers->hasCacheControlDirective('s-maxage') ? (int) $response->headers->getCacheControlDirective('s-maxage') : $maxAge;
62 $expires = $response->getExpires();
63 $expires = null !== $expires ? (int) $expires->format('U') - (int) $response->getDate()->format('U') : null;
64 // See https://datatracker.ietf.org/doc/html/rfc7234#section-4.2.2
65 // If a response is "public" but does not have maximum lifetime, heuristics might be applied.
66 // Do not store NULL values so the final response can have more limiting value from other responses.
67 $isHeuristicallyCacheable = $response->headers->hasCacheControlDirective('public') && null === $maxAge && null === $sharedMaxAge && null === $expires;
68 if (!$isHeuristicallyCacheable || null !== $maxAge || null !== $expires) {
69 $this->storeRelativeAgeDirective('max-age', $maxAge, $expires, $age);
70 }
71 if (!$isHeuristicallyCacheable || null !== $sharedMaxAge || null !== $expires) {
72 $this->storeRelativeAgeDirective('s-maxage', $sharedMaxAge, $expires, $age);
73 }
74 if (null !== $expires) {
75 $this->ageDirectives['expires'] = \true;
76 }
77 }
78 /**
79 * {@inheritdoc}
80 */
81 public function update(Response $response)
82 {
83 // if we have no embedded Response, do nothing
84 if (0 === $this->embeddedResponses) {
85 return;
86 }
87 // Remove validation related headers of the final response,
88 // because some of the response content comes from at least
89 // one embedded response (which likely has a different caching strategy).
90 $response->setEtag(null);
91 $response->setLastModified(null);
92 $this->add($response);
93 $response->headers->set('Age', $this->age);
94 if ($this->isNotCacheableResponseEmbedded) {
95 if ($this->flagDirectives['no-store']) {
96 $response->headers->set('Cache-Control', 'no-cache, no-store, must-revalidate');
97 } else {
98 $response->headers->set('Cache-Control', 'no-cache, must-revalidate');
99 }
100 return;
101 }
102 $flags = array_filter($this->flagDirectives);
103 if (isset($flags['must-revalidate'])) {
104 $flags['no-cache'] = \true;
105 }
106 $response->headers->set('Cache-Control', implode(', ', array_keys($flags)));
107 $maxAge = null;
108 if (is_numeric($this->ageDirectives['max-age'])) {
109 $maxAge = $this->ageDirectives['max-age'] + $this->age;
110 $response->headers->addCacheControlDirective('max-age', $maxAge);
111 }
112 if (is_numeric($this->ageDirectives['s-maxage'])) {
113 $sMaxage = $this->ageDirectives['s-maxage'] + $this->age;
114 if ($maxAge !== $sMaxage) {
115 $response->headers->addCacheControlDirective('s-maxage', $sMaxage);
116 }
117 }
118 if ($this->ageDirectives['expires'] && null !== $maxAge) {
119 $date = clone $response->getDate();
120 $date = $date->modify('+' . $maxAge . ' seconds');
121 $response->setExpires($date);
122 }
123 }
124 /**
125 * RFC2616, Section 13.4.
126 *
127 * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.4
128 */
129 private function willMakeFinalResponseUncacheable(Response $response) : bool
130 {
131 // RFC2616: A response received with a status code of 200, 203, 300, 301 or 410
132 // MAY be stored by a cache […] unless a cache-control directive prohibits caching.
133 if ($response->headers->hasCacheControlDirective('no-cache') || $response->headers->getCacheControlDirective('no-store')) {
134 return \true;
135 }
136 // Last-Modified and Etag headers cannot be merged, they render the response uncacheable
137 // by default (except if the response also has max-age etc.).
138 if (\in_array($response->getStatusCode(), [200, 203, 300, 301, 410]) && null === $response->getLastModified() && null === $response->getEtag()) {
139 return \false;
140 }
141 // RFC2616: A response received with any other status code (e.g. status codes 302 and 307)
142 // MUST NOT be returned in a reply to a subsequent request unless there are
143 // cache-control directives or another header(s) that explicitly allow it.
144 $cacheControl = ['max-age', 's-maxage', 'must-revalidate', 'proxy-revalidate', 'public', 'private'];
145 foreach ($cacheControl as $key) {
146 if ($response->headers->hasCacheControlDirective($key)) {
147 return \false;
148 }
149 }
150 if ($response->headers->has('Expires')) {
151 return \false;
152 }
153 return \true;
154 }
155 /**
156 * Store lowest max-age/s-maxage/expires for the final response.
157 *
158 * The response might have been stored in cache a while ago. To keep things comparable,
159 * we have to subtract the age so that the value is normalized for an age of 0.
160 *
161 * If the value is lower than the currently stored value, we update the value, to keep a rolling
162 * minimal value of each instruction. If the value is NULL, the directive will not be set on the final response.
163 */
164 private function storeRelativeAgeDirective(string $directive, ?int $value, ?int $expires, int $age) : void
165 {
166 if (null === $value && null === $expires) {
167 $this->ageDirectives[$directive] = \false;
168 }
169 if (\false !== $this->ageDirectives[$directive]) {
170 $value = min($value ?? \PHP_INT_MAX, $expires ?? \PHP_INT_MAX);
171 $value -= $age;
172 $this->ageDirectives[$directive] = null !== $this->ageDirectives[$directive] ? min($this->ageDirectives[$directive], $value) : $value;
173 }
174 }
175 }
176