Exemplo n.º 1
0
 public function testGetTtl()
 {
     $response = new Response();
     $this->assertNull($response->getTtl(), '->getTtl() returns null when no Expires or Cache-Control headers are present');
     $response = new Response();
     $response->headers->set('Expires', $this->createDateTimeOneHourLater()->format(DATE_RFC2822));
     $this->assertLessThan(1, 3600 - $response->getTtl(), '->getTtl() uses the Expires header when no max-age is present');
     $response = new Response();
     $response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(DATE_RFC2822));
     $this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in part');
     $response = new Response();
     $response->headers->set('Cache-Control', 'max-age=60');
     $this->assertLessThan(1, 60 - $response->getTtl(), '->getTtl() uses Cache-Control max-age when present');
 }
Exemplo n.º 2
0
 /**
  * Forwards the Request to the backend and returns the Response.
  *
  * @param Symfony\Components\HttpFoundation\Request  $request  A Request instance
  * @param Boolean                                $raw      Whether to catch exceptions or not
  * @param Symfony\Components\HttpFoundation\Response $response A Response instance (the stale entry if present, null otherwise)
  *
  * @return Symfony\Components\HttpFoundation\Response A Response instance
  */
 protected function forward(Request $request, $raw = false, Response $entry = null)
 {
     if ($this->esi) {
         $this->esi->addSurrogateEsiCapability($request);
     }
     // always a "master" request (as the real master request can be in cache)
     $response = $this->kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, $raw);
     // FIXME: we probably need to also catch exceptions if raw === true
     // we don't implement the stale-if-error on Requests, which is nonetheless part of the RFC
     if (null !== $entry && in_array($response->getStatusCode(), array(500, 502, 503, 504))) {
         if (null === ($age = $entry->headers->getCacheControl()->getStaleIfError())) {
             $age = $this->options['stale_if_error'];
         }
         if (abs($entry->getTtl()) < $age) {
             $this->record($request, 'stale-if-error');
             return $entry;
         }
     }
     $this->processResponseBody($request, $response);
     return $response;
 }