function 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
bool $catch Whether to process exceptions:
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 363
Class
- HttpCache
- Cache provides HTTP caching.
Namespace
Symfony\Component\HttpKernel\HttpCacheCode
protected function validate(Request $request, Response $entry, bool $catch = false) : Response {
$subRequest = clone $request;
// send no head requests because we want content
if ('HEAD' === $request->getMethod()) {
$subRequest->setMethod('GET');
}
// add our cached last-modified validator
if ($entry->headers
->has('Last-Modified')) {
$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() ? [
$entry->getEtag(),
] : [];
$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, true) && !\in_array($etag, $cachedEtags, true)) {
return $response;
}
$entry = clone $entry;
$entry->headers
->remove('Date');
foreach ([
'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;
}