protected PageCache::lookup(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE)
Retrieves a response from the cache or fetches it from the backend.
@returns \Symfony\Component\HttpFoundation\Response $response A response object.
Parameters
\Symfony\Component\HttpFoundation\Request $request: A request object.
int $type: The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
bool $catch: Whether to catch exceptions or not
File
- core/modules/page_cache/src/StackMiddleware/PageCache.php, line 116
Class
- PageCache
- Executes the page caching before the main kernel takes over the request.
Namespace
Drupal\page_cache\StackMiddleware
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | protected function lookup(Request $request , $type = self::MASTER_REQUEST, $catch = TRUE) { if ( $response = $this ->get( $request )) { $response ->headers->set( 'X-Drupal-Cache' , 'HIT' ); } else { $response = $this ->fetch( $request , $type , $catch ); } // Only allow caching in the browser and prevent that the response is stored // by an external proxy server when the following conditions apply: // 1. There is a session cookie on the request. // 2. The Vary: Cookie header is on the response. // 3. The Cache-Control header does not contain the no-cache directive. if ( $request ->cookies->has(session_name()) && in_array( 'Cookie' , $response ->getVary()) && ! $response ->headers->hasCacheControlDirective( 'no-cache' )) { $response ->setPrivate(); } // Negotiate whether to use compression. if ( extension_loaded ( 'zlib' ) && $response ->headers->get( 'Content-Encoding' ) === 'gzip' ) { if ( strpos ( $request ->headers->get( 'Accept-Encoding' ), 'gzip' ) !== FALSE) { // The response content is already gzip'ed, so make sure // zlib.output_compression does not compress it once more. ini_set ( 'zlib.output_compression' , '0' ); } else { // The client does not support compression. Decompress the content and // remove the Content-Encoding header. $content = $response ->getContent(); $content = gzinflate( substr ( substr ( $content , 10), 0, -8)); $response ->setContent( $content ); $response ->headers->remove( 'Content-Encoding' ); } } // Perform HTTP revalidation. // @todo Use Response::isNotModified() as $last_modified = $response ->getLastModified(); if ( $last_modified ) { // See if the client has provided the required HTTP headers. $if_modified_since = $request ->server->has( 'HTTP_IF_MODIFIED_SINCE' ) ? strtotime ( $request ->server->get( 'HTTP_IF_MODIFIED_SINCE' )) : FALSE; $if_none_match = $request ->server->has( 'HTTP_IF_NONE_MATCH' ) ? stripslashes ( $request ->server->get( 'HTTP_IF_NONE_MATCH' )) : FALSE; if ( $if_modified_since && $if_none_match && $if_none_match == $response ->getEtag() // etag must match && $if_modified_since == $last_modified ->getTimestamp()) { // if-modified-since must match $response ->setStatusCode(304); $response ->setContent(NULL); // In the case of a 304 response, certain headers must be sent, and the // remaining may not (see RFC 2616, section 10.3.5). foreach ( array_keys ( $response ->headers->all()) as $name ) { if (!in_array( $name , array ( 'content-location' , 'expires' , 'cache-control' , 'vary' ))) { $response ->headers->remove( $name ); } } } } return $response ; } |
Please login to continue.