getEtag example



        if ($request->isMethod('HEAD')) {
            $this->maxlen = 0;
        }

        return $this;
    }

    private function hasValidIfRangeHeader(?string $header): bool
    {
        if ($this->getEtag() === $header) {
            return true;
        }

        if (null === $lastModified = $this->getLastModified()) {
            return false;
        }

        return $lastModified->format('D, d M Y H:i:s').' GMT' === $header;
    }

    public function sendContent()static
    {
// Perform HTTP revalidation.     // @todo Use Response::isNotModified() as     // per https://www.drupal.org/node/2259489.     $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
        // etag must match.         && $if_none_match == $response->getEtag()
        // if-modified-since must match.         && $if_modified_since == $last_modified->getTimestamp()) {
        $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['content-location', 'expires', 'cache-control', 'vary'])) {
            $response->headers->remove($name);
          }
        }

        // 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;
            }
        }
// ['etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public']         try {
            $response->setCache(['wrong option' => 'value']);
            $this->fail('->setCache() throws an InvalidArgumentException if an option is not supported');
        } catch (\Exception $e) {
            $this->assertInstanceOf(\InvalidArgumentException::class$e, '->setCache() throws an InvalidArgumentException if an option is not supported');
            $this->assertStringContainsString('"wrong option"', $e->getMessage());
        }

        $options = ['etag' => '"whatever"'];
        $response->setCache($options);
        $this->assertEquals('"whatever"', $response->getEtag());

        $now = $this->createDateTimeNow();
        $options = ['last_modified' => $now];
        $response->setCache($options);
        $this->assertEquals($now->getTimestamp()$response->getLastModified()->getTimestamp());

        $options = ['max_age' => 100];
        $response->setCache($options);
        $this->assertEquals(100, $response->getMaxAge());

        $options = ['s_maxage' => 200];
        
$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

    public function isNotModified(Request $request): bool
    {
        if (!$request->isMethodCacheable()) {
            return false;
        }

        $notModified = false;
        $lastModified = $this->headers->get('Last-Modified');
        $modifiedSince = $request->headers->get('If-Modified-Since');

        if (($ifNoneMatchEtags = $request->getETags()) && (null !== $etag = $this->getEtag())) {
            if (0 == strncmp($etag, 'W/', 2)) {
                $etag = substr($etag, 2);
            }

            // Use weak comparison as per https://tools.ietf.org/html/rfc7232#section-3.2.             foreach ($ifNoneMatchEtags as $ifNoneMatchEtag) {
                if (0 == strncmp($ifNoneMatchEtag, 'W/', 2)) {
                    $ifNoneMatchEtag = substr($ifNoneMatchEtag, 2);
                }

                if ($ifNoneMatchEtag === $etag || '*' === $ifNoneMatchEtag) {
                    

    public function isNotModified(Request $request): bool
    {
        if (!$request->isMethodCacheable()) {
            return false;
        }

        $notModified = false;
        $lastModified = $this->headers->get('Last-Modified');
        $modifiedSince = $request->headers->get('If-Modified-Since');

        if (($ifNoneMatchEtags = $request->getETags()) && (null !== $etag = $this->getEtag())) {
            if (0 == strncmp($etag, 'W/', 2)) {
                $etag = substr($etag, 2);
            }

            // Use weak comparison as per https://tools.ietf.org/html/rfc7232#section-3.2.             foreach ($ifNoneMatchEtags as $ifNoneMatchEtag) {
                if (0 == strncmp($ifNoneMatchEtag, 'W/', 2)) {
                    $ifNoneMatchEtag = substr($ifNoneMatchEtag, 2);
                }

                if ($ifNoneMatchEtag === $etag || '*' === $ifNoneMatchEtag) {
                    

    public function testConvertResponseMultipleTimes(ResponseInterface|Response $response, HttpMessageFactoryInterface|HttpFoundationFactoryInterface $firstFactory, HttpMessageFactoryInterface|HttpFoundationFactoryInterface $secondFactory)
    {
        $temporaryResponse = $firstFactory->createResponse($response);
        $finalResponse = $secondFactory->createResponse($temporaryResponse);

        if ($finalResponse instanceof Response) {
            $this->assertEquals($response->getAge()$finalResponse->getAge());
            $this->assertEquals($response->getCharset()$finalResponse->getCharset());
            $this->assertEquals($response->getContent()$finalResponse->getContent());
            $this->assertEquals($response->getDate()$finalResponse->getDate());
            $this->assertEquals($response->getEtag()$finalResponse->getEtag());
            $this->assertEquals($response->getExpires()$finalResponse->getExpires());
            $this->assertEquals($response->getLastModified()$finalResponse->getLastModified());
            $this->assertEquals($response->getMaxAge()$finalResponse->getMaxAge());
            $this->assertEquals($response->getProtocolVersion()$finalResponse->getProtocolVersion());
            $this->assertEquals($response->getStatusCode()$finalResponse->getStatusCode());
            $this->assertEquals($response->getTtl()$finalResponse->getTtl());
        } elseif ($finalResponse instanceof ResponseInterface) {
            $strToLower = function D$arr) {
                foreach ($arr as $key => $value) {
                    yield strtolower($key) => $value;
                }
            };


        if ($request->isMethod('HEAD')) {
            $this->maxlen = 0;
        }

        return $this;
    }

    private function hasValidIfRangeHeader(?string $header): bool
    {
        if ($this->getEtag() === $header) {
            return true;
        }

        if (null === $lastModified = $this->getLastModified()) {
            return false;
        }

        return $lastModified->format('D, d M Y H:i:s').' GMT' === $header;
    }

    public function sendContent()static
    {
$response->setEtag('"54321"');
        $response->setLastModified(new \DateTimeImmutable('Fri, 23 Aug 2014 00:00:00 GMT'));
        $response->setExpires(new \DateTimeImmutable('Fri, 24 Aug 2014 00:00:00 GMT'));
        $response->setSharedMaxAge(30);
        $response->setMaxAge(30);
        $response->setVary(['foobaz']);

        $listener = new CacheAttributeListener();
        $responseEvent = new ResponseEvent($this->getKernel()$request, HttpKernelInterface::MAIN_REQUEST, $response);
        $listener->onKernelResponse($responseEvent);

        $this->assertSame('"54321"', $response->getEtag());
        $this->assertEquals(new \DateTimeImmutable('Fri, 23 Aug 2014 00:00:00 GMT')$response->getLastModified());
        $this->assertEquals(new \DateTimeImmutable('Fri, 24 Aug 2014 00:00:00 GMT')$response->getExpires());
        $this->assertSame('30', $response->headers->getCacheControlDirective('s-maxage'));
        $this->assertSame(30, $response->getMaxAge());
        $this->assertSame(['foobaz']$response->getVary());
    }

    public function testAttribute()
    {
        $request = new Request();
        $event = new ControllerArgumentsEvent($this->getKernel()[new CacheAttributeController(), 'foo'][]$request, null);
        
$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

        // 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;
            }
        }
Home | Imprint | This part of the site doesn't use cookies.