Symfony\Component\HttpKernel\HttpCache\HttpCache::forward PHP Method

forward() protected method

Forwards the Request to the backend and returns the Response.
protected forward ( Request $request, boolean $catch = false, Response $entry = null ) : Response
$request Symfony\Component\HttpFoundation\Request A Request instance
$catch boolean Whether to catch exceptions or not
$entry Symfony\Component\HttpFoundation\Response A Response instance (the stale entry if present, null otherwise)
return Symfony\Component\HttpFoundation\Response A Response instance
    protected function forward(Request $request, $catch = false, Response $entry = null)
    {
        if ($this->surrogate) {
            $this->surrogate->addSurrogateCapability($request);
        }
        // modify the X-Forwarded-For header if needed
        $forwardedFor = $request->headers->get('X-Forwarded-For');
        if ($forwardedFor) {
            $request->headers->set('X-Forwarded-For', $forwardedFor . ', ' . $request->server->get('REMOTE_ADDR'));
        } else {
            $request->headers->set('X-Forwarded-For', $request->server->get('REMOTE_ADDR'));
        }
        // fix the client IP address by setting it to 127.0.0.1 as HttpCache
        // is always called from the same process as the backend.
        $request->server->set('REMOTE_ADDR', '127.0.0.1');
        // make sure HttpCache is a trusted proxy
        if (!in_array('127.0.0.1', $trustedProxies = Request::getTrustedProxies())) {
            $trustedProxies[] = '127.0.0.1';
            Request::setTrustedProxies($trustedProxies);
        }
        // always a "master" request (as the real master request can be in cache)
        $response = $this->kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch);
        // 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->getCacheControlDirective('stale-if-error'))) {
                $age = $this->options['stale_if_error'];
            }
            if (abs($entry->getTtl()) < $age) {
                $this->record($request, 'stale-if-error');
                return $entry;
            }
        }
        $this->processResponseBody($request, $response);
        if ($this->isPrivateRequest($request) && !$response->headers->hasCacheControlDirective('public')) {
            $response->setPrivate();
        } elseif ($this->options['default_ttl'] > 0 && null === $response->getTtl() && !$response->headers->getCacheControlDirective('must-revalidate')) {
            $response->setTtl($this->options['default_ttl']);
        }
        return $response;
    }

Usage Example

Beispiel #1
0
 /**
  * Forwards the Request to the backend and returns the Response.
  *
  * @param Request  $request A Request instance
  * @param Boolean  $raw     Whether to catch exceptions or not
  * @param Response $entry   A Response instance (the stale entry if present, null otherwise)
  *
  * @return Response A Response instance
  */
 protected function forward(Request $request, $raw = false, Response $entry = null)
 {
     $this->getKernel()->boot();
     $this->getKernel()->getContainer()->set('cache', $this);
     $this->getKernel()->getContainer()->set('esi', $this->getEsi());
     return parent::forward($request, $raw, $entry);
 }
All Usage Examples Of Symfony\Component\HttpKernel\HttpCache\HttpCache::forward