/** * Filter and mark incoming requests * * @priority high * * @param ProxyRequestEvent $event */ public function onProxyRequest(ProxyRequestEvent $event) { if (!$event->isCancelled() && !$event->hasResponse()) { $response = null; $request = $event->getRequest(); $config = $event->getProxy()->getConfig(); if ($this->isSelfRequest($request, $config)) { $event->setIsCancelled(); $event->getLogger()->warning(sprintf('Proxy server made a call to itself that cannot be handled, request will be cancelled')); } elseif (in_array($request->getHost(), $config->getHostsOnPort($request->getPort()))) { // display proxy welcome page as it is a direct hit $response = new Response($this->getResource('home.html', $event->getProxy()), 200); } elseif (!$this->firewall->isAllowed($request)) { $event->setIsCancelled(); $event->getLogger()->debug(sprintf('Request was cancelled as it was not allowed by a firewall')); } if ($response) { $event->setResponse($response); } } }
/** * * @priority normal * @param ProxyRequestEvent $event * * @throws \Exception */ public function onProxyRequest(ProxyRequestEvent $event) { if (!$event->isCancelled() && !$event->hasResponse()) { $response = $this->storage->fetch($event->getRequest()); $request = $event->getRequest(); if ($this->cacheStrategy->canUseResponseFromCache($request, $response)) { /** * The "Date" header field represents the date and time at which the message was originated * http://tools.ietf.org/html/rfc7231#section-7.1.1.2 */ $event->getLogger()->debug('Response was read from cache'); $response->addHeader('X-Cache', 'HIT'); $response->addHeader('X-Cache-Hits', 1); $now = new \DateTime('now'); $oldAge = $response->getHeader('Age', 0); $oldAge = $oldAge[0]; $response->setHeader('Age', $oldAge + ($now->getTimestamp() - $response->getDate()->getTimestamp())); $event->setResponse($response); } } }