You are here

protected function HttpCache::validate in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/http-kernel/HttpCache/HttpCache.php \Symfony\Component\HttpKernel\HttpCache\HttpCache::validate()

Validates that a cache entry is fresh.

The original request is used as a template for a conditional GET request with the backend.

Parameters

Request $request A Request instance:

Response $entry A Response instance to validate:

bool $catch Whether to process exceptions:

Return value

Response A Response instance

1 call to HttpCache::validate()
HttpCache::lookup in vendor/symfony/http-kernel/HttpCache/HttpCache.php
Lookups a Response from the cache for the given Request.

File

vendor/symfony/http-kernel/HttpCache/HttpCache.php, line 372

Class

HttpCache
Cache provides HTTP caching.

Namespace

Symfony\Component\HttpKernel\HttpCache

Code

protected function validate(Request $request, Response $entry, $catch = false) {
  $subRequest = clone $request;

  // send no head requests because we want content
  $subRequest
    ->setMethod('GET');

  // add our cached last-modified validator
  $subRequest->headers
    ->set('if_modified_since', $entry->headers
    ->get('Last-Modified'));

  // Add our cached etag validator to the environment.
  // We keep the etags from the client to handle the case when the client
  // has a different private valid entry which is not cached here.
  $cachedEtags = $entry
    ->getEtag() ? array(
    $entry
      ->getEtag(),
  ) : array();
  $requestEtags = $request
    ->getETags();
  if ($etags = array_unique(array_merge($cachedEtags, $requestEtags))) {
    $subRequest->headers
      ->set('if_none_match', implode(', ', $etags));
  }
  $response = $this
    ->forward($subRequest, $catch, $entry);
  if (304 == $response
    ->getStatusCode()) {
    $this
      ->record($request, 'valid');

    // return the response and not the cache entry if the response is valid but not cached
    $etag = $response
      ->getEtag();
    if ($etag && in_array($etag, $requestEtags) && !in_array($etag, $cachedEtags)) {
      return $response;
    }
    $entry = clone $entry;
    $entry->headers
      ->remove('Date');
    foreach (array(
      'Date',
      'Expires',
      'Cache-Control',
      'ETag',
      'Last-Modified',
    ) as $name) {
      if ($response->headers
        ->has($name)) {
        $entry->headers
          ->set($name, $response->headers
          ->get($name));
      }
    }
    $response = $entry;
  }
  else {
    $this
      ->record($request, 'invalid');
  }
  if ($response
    ->isCacheable()) {
    $this
      ->store($request, $response);
  }
  return $response;
}