Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ResponseCacheStrategy.php

function ResponseCacheStrategy::willMakeFinalResponseUncacheable

RFC2616, Section 13.4.

See also

https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.4

1 call to ResponseCacheStrategy::willMakeFinalResponseUncacheable()
ResponseCacheStrategy::add in vendor/symfony/http-kernel/HttpCache/ResponseCacheStrategy.php
Adds a Response.

File

vendor/symfony/http-kernel/HttpCache/ResponseCacheStrategy.php, line 176

Class

ResponseCacheStrategy
ResponseCacheStrategy knows how to compute the Response cache HTTP header based on the different response cache headers.

Namespace

Symfony\Component\HttpKernel\HttpCache

Code

private function willMakeFinalResponseUncacheable(Response $response) : bool {
    // RFC2616: A response received with a status code of 200, 203, 300, 301 or 410
    // MAY be stored by a cache […] unless a cache-control directive prohibits caching.
    if ($response->headers
        ->hasCacheControlDirective('no-cache') || $response->headers
        ->hasCacheControlDirective('no-store')) {
        return true;
    }
    // Etag headers cannot be merged, they render the response uncacheable
    // by default (except if the response also has max-age etc.).
    if (null === $response->getEtag() && \in_array($response->getStatusCode(), [
        200,
        203,
        300,
        301,
        410,
    ])) {
        return false;
    }
    // RFC2616: A response received with any other status code (e.g. status codes 302 and 307)
    // MUST NOT be returned in a reply to a subsequent request unless there are
    // cache-control directives or another header(s) that explicitly allow it.
    $cacheControl = [
        'max-age',
        's-maxage',
        'must-revalidate',
        'proxy-revalidate',
        'public',
        'private',
    ];
    foreach ($cacheControl as $key) {
        if ($response->headers
            ->hasCacheControlDirective($key)) {
            return false;
        }
    }
    if ($response->headers
        ->has('Expires')) {
        return false;
    }
    return true;
}
RSS feed
Powered by Drupal