annotate vendor/symfony/http-kernel/HttpCache/ResponseCacheStrategy.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\HttpKernel\HttpCache;
Chris@0 13
Chris@0 14 use Symfony\Component\HttpFoundation\Response;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * ResponseCacheStrategy knows how to compute the Response cache HTTP header
Chris@0 18 * based on the different response cache headers.
Chris@0 19 *
Chris@0 20 * This implementation changes the master response TTL to the smallest TTL received
Chris@0 21 * or force validation if one of the surrogates has validation cache strategy.
Chris@0 22 *
Chris@0 23 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 24 */
Chris@0 25 class ResponseCacheStrategy implements ResponseCacheStrategyInterface
Chris@0 26 {
Chris@5 27 /**
Chris@5 28 * Cache-Control headers that are sent to the final response if they appear in ANY of the responses.
Chris@5 29 */
Chris@5 30 private static $overrideDirectives = ['private', 'no-cache', 'no-store', 'no-transform', 'must-revalidate', 'proxy-revalidate'];
Chris@5 31
Chris@5 32 /**
Chris@5 33 * Cache-Control headers that are sent to the final response if they appear in ALL of the responses.
Chris@5 34 */
Chris@5 35 private static $inheritDirectives = ['public', 'immutable'];
Chris@5 36
Chris@0 37 private $embeddedResponses = 0;
Chris@0 38 private $isNotCacheableResponseEmbedded = false;
Chris@5 39 private $age = 0;
Chris@5 40 private $flagDirectives = [
Chris@5 41 'no-cache' => null,
Chris@5 42 'no-store' => null,
Chris@5 43 'no-transform' => null,
Chris@5 44 'must-revalidate' => null,
Chris@5 45 'proxy-revalidate' => null,
Chris@5 46 'public' => null,
Chris@5 47 'private' => null,
Chris@5 48 'immutable' => null,
Chris@5 49 ];
Chris@5 50 private $ageDirectives = [
Chris@5 51 'max-age' => null,
Chris@5 52 's-maxage' => null,
Chris@5 53 'expires' => null,
Chris@5 54 ];
Chris@0 55
Chris@0 56 /**
Chris@0 57 * {@inheritdoc}
Chris@0 58 */
Chris@0 59 public function add(Response $response)
Chris@0 60 {
Chris@5 61 ++$this->embeddedResponses;
Chris@0 62
Chris@5 63 foreach (self::$overrideDirectives as $directive) {
Chris@5 64 if ($response->headers->hasCacheControlDirective($directive)) {
Chris@5 65 $this->flagDirectives[$directive] = true;
Chris@0 66 }
Chris@0 67 }
Chris@0 68
Chris@5 69 foreach (self::$inheritDirectives as $directive) {
Chris@5 70 if (false !== $this->flagDirectives[$directive]) {
Chris@5 71 $this->flagDirectives[$directive] = $response->headers->hasCacheControlDirective($directive);
Chris@5 72 }
Chris@5 73 }
Chris@5 74
Chris@5 75 $age = $response->getAge();
Chris@5 76 $this->age = max($this->age, $age);
Chris@5 77
Chris@5 78 if ($this->willMakeFinalResponseUncacheable($response)) {
Chris@5 79 $this->isNotCacheableResponseEmbedded = true;
Chris@5 80
Chris@5 81 return;
Chris@5 82 }
Chris@5 83
Chris@5 84 $this->storeRelativeAgeDirective('max-age', $response->headers->getCacheControlDirective('max-age'), $age);
Chris@5 85 $this->storeRelativeAgeDirective('s-maxage', $response->headers->getCacheControlDirective('s-maxage') ?: $response->headers->getCacheControlDirective('max-age'), $age);
Chris@5 86
Chris@5 87 $expires = $response->getExpires();
Chris@5 88 $expires = null !== $expires ? $expires->format('U') - $response->getDate()->format('U') : null;
Chris@5 89 $this->storeRelativeAgeDirective('expires', $expires >= 0 ? $expires : null, 0);
Chris@0 90 }
Chris@0 91
Chris@0 92 /**
Chris@0 93 * {@inheritdoc}
Chris@0 94 */
Chris@0 95 public function update(Response $response)
Chris@0 96 {
Chris@0 97 // if we have no embedded Response, do nothing
Chris@0 98 if (0 === $this->embeddedResponses) {
Chris@0 99 return;
Chris@0 100 }
Chris@0 101
Chris@5 102 // Remove validation related headers of the master response,
Chris@5 103 // because some of the response content comes from at least
Chris@5 104 // one embedded response (which likely has a different caching strategy).
Chris@5 105 $response->setEtag(null);
Chris@5 106 $response->setLastModified(null);
Chris@0 107
Chris@5 108 $this->add($response);
Chris@0 109
Chris@5 110 $response->headers->set('Age', $this->age);
Chris@5 111
Chris@5 112 if ($this->isNotCacheableResponseEmbedded) {
Chris@5 113 $response->setExpires($response->getDate());
Chris@5 114
Chris@5 115 if ($this->flagDirectives['no-store']) {
Chris@5 116 $response->headers->set('Cache-Control', 'no-cache, no-store, must-revalidate');
Chris@5 117 } else {
Chris@5 118 $response->headers->set('Cache-Control', 'no-cache, must-revalidate');
Chris@5 119 }
Chris@0 120
Chris@0 121 return;
Chris@0 122 }
Chris@0 123
Chris@5 124 $flags = array_filter($this->flagDirectives);
Chris@0 125
Chris@5 126 if (isset($flags['must-revalidate'])) {
Chris@5 127 $flags['no-cache'] = true;
Chris@0 128 }
Chris@5 129
Chris@5 130 $response->headers->set('Cache-Control', implode(', ', array_keys($flags)));
Chris@5 131
Chris@5 132 $maxAge = null;
Chris@5 133 $sMaxage = null;
Chris@5 134
Chris@5 135 if (\is_numeric($this->ageDirectives['max-age'])) {
Chris@5 136 $maxAge = $this->ageDirectives['max-age'] + $this->age;
Chris@5 137 $response->headers->addCacheControlDirective('max-age', $maxAge);
Chris@5 138 }
Chris@5 139
Chris@5 140 if (\is_numeric($this->ageDirectives['s-maxage'])) {
Chris@5 141 $sMaxage = $this->ageDirectives['s-maxage'] + $this->age;
Chris@5 142
Chris@5 143 if ($maxAge !== $sMaxage) {
Chris@5 144 $response->headers->addCacheControlDirective('s-maxage', $sMaxage);
Chris@5 145 }
Chris@5 146 }
Chris@5 147
Chris@5 148 if (\is_numeric($this->ageDirectives['expires'])) {
Chris@5 149 $date = clone $response->getDate();
Chris@5 150 $date->modify('+'.($this->ageDirectives['expires'] + $this->age).' seconds');
Chris@5 151 $response->setExpires($date);
Chris@5 152 }
Chris@5 153 }
Chris@5 154
Chris@5 155 /**
Chris@5 156 * RFC2616, Section 13.4.
Chris@5 157 *
Chris@5 158 * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.4
Chris@5 159 *
Chris@5 160 * @return bool
Chris@5 161 */
Chris@5 162 private function willMakeFinalResponseUncacheable(Response $response)
Chris@5 163 {
Chris@5 164 // RFC2616: A response received with a status code of 200, 203, 300, 301 or 410
Chris@5 165 // MAY be stored by a cache […] unless a cache-control directive prohibits caching.
Chris@5 166 if ($response->headers->hasCacheControlDirective('no-cache')
Chris@5 167 || $response->headers->getCacheControlDirective('no-store')
Chris@5 168 ) {
Chris@5 169 return true;
Chris@5 170 }
Chris@5 171
Chris@5 172 // Last-Modified and Etag headers cannot be merged, they render the response uncacheable
Chris@5 173 // by default (except if the response also has max-age etc.).
Chris@5 174 if (\in_array($response->getStatusCode(), [200, 203, 300, 301, 410])
Chris@5 175 && null === $response->getLastModified()
Chris@5 176 && null === $response->getEtag()
Chris@5 177 ) {
Chris@5 178 return false;
Chris@5 179 }
Chris@5 180
Chris@5 181 // RFC2616: A response received with any other status code (e.g. status codes 302 and 307)
Chris@5 182 // MUST NOT be returned in a reply to a subsequent request unless there are
Chris@5 183 // cache-control directives or another header(s) that explicitly allow it.
Chris@5 184 $cacheControl = ['max-age', 's-maxage', 'must-revalidate', 'proxy-revalidate', 'public', 'private'];
Chris@5 185 foreach ($cacheControl as $key) {
Chris@5 186 if ($response->headers->hasCacheControlDirective($key)) {
Chris@5 187 return false;
Chris@5 188 }
Chris@5 189 }
Chris@5 190
Chris@5 191 if ($response->headers->has('Expires')) {
Chris@5 192 return false;
Chris@5 193 }
Chris@5 194
Chris@5 195 return true;
Chris@5 196 }
Chris@5 197
Chris@5 198 /**
Chris@5 199 * Store lowest max-age/s-maxage/expires for the final response.
Chris@5 200 *
Chris@5 201 * The response might have been stored in cache a while ago. To keep things comparable,
Chris@5 202 * we have to subtract the age so that the value is normalized for an age of 0.
Chris@5 203 *
Chris@5 204 * If the value is lower than the currently stored value, we update the value, to keep a rolling
Chris@5 205 * minimal value of each instruction. If the value is NULL, the directive will not be set on the final response.
Chris@5 206 *
Chris@5 207 * @param string $directive
Chris@5 208 * @param int|null $value
Chris@5 209 * @param int $age
Chris@5 210 */
Chris@5 211 private function storeRelativeAgeDirective($directive, $value, $age)
Chris@5 212 {
Chris@5 213 if (null === $value) {
Chris@5 214 $this->ageDirectives[$directive] = false;
Chris@5 215 }
Chris@5 216
Chris@5 217 if (false !== $this->ageDirectives[$directive]) {
Chris@5 218 $value -= $age;
Chris@5 219 $this->ageDirectives[$directive] = null !== $this->ageDirectives[$directive] ? min($this->ageDirectives[$directive], $value) : $value;
Chris@5 220 }
Chris@0 221 }
Chris@0 222 }