/** * @param RequestInterface $request * @param ResponseInterface $response * * @return array */ protected function getLogData(RequestInterface $request, ResponseInterface $response = null) { $time = $this->stopwatch->stop(self::STOPWATCH_EVENT)->getDuration(); $uagent = $request->getHeader('User-Agent', '-'); $uagent = $uagent[0]; $xcache = $response && $response->hasHeaderWithValue('x-cache', 'HIT') ? 'HIT' : 'MISS'; $postDumpLimit = 200; $postData = json_encode($request->getPostParams()); if (strlen($postData) > $postDumpLimit) { $postData = substr($postData, 0, $postDumpLimit) . '...'; } $data = array(); $data[] = $xcache; $data[] = bcdiv($time, 1000, 4); // milliseconds $data[] = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1'; // @todo ip is always set to 127.0.0.1 due to broken request object $data[] = $request->getMethod(); $data[] = $request->getUri(); $data[] = $response ? $response->getStatusCode() : '-'; // bytes $data[] = $response ? $response->getLength() : '-'; // bytes $data[] = sprintf('"%s"', $uagent); $data[] = $postData; return $data; }
/** * Returns true if proxy detected a self-request * * @param RequestInterface $request * @param ConfigInterface $config * * @return bool */ private function isSelfRequest(RequestInterface $request, ConfigInterface $config) { $proxyUniqueHeaderName = 'PROXY-ID'; $proxyUniqueHeaderValue = md5($config->getSecret() . $request->getFingerprint()); if ($request->hasHeaderWithValue($proxyUniqueHeaderName, $proxyUniqueHeaderValue)) { return true; } $request->addHeader($proxyUniqueHeaderName, $proxyUniqueHeaderValue); return false; }
/** * Tell if given response with its max age is fresh enough for the client * that can also request a response of some age * * @param RequestInterface $request * @param ResponseInterface $response * * @return bool */ protected function isResponseFreshEnoughForTheClient(RequestInterface $request, ResponseInterface $response) { $compare = array(); if ($request->getMaxAge() !== null) { $compare[] = $request->getMaxAge(); } if ($response->getMaxAge() !== null) { $compare[] = $response->getMaxAge(); } if (count($compare) == 0 || ($maxAge = min($compare)) <= 0) { return false; } $expirationDate = $response->getDate()->add(new \DateInterval(sprintf('PT%sS', $maxAge))); $now = new \DateTime('now', $response->getDate()->getTimezone()); return $expirationDate >= $now; }
/** * {@inheritdoc} */ public function isAllowed(RequestInterface $request) { if (!empty($this->allowedIps)) { $clientIp = $this->normalize($request->getClientIp()); if (!in_array($clientIp, $this->allowedIps)) { return false; } } if (!empty($this->allowedDomains)) { $domain = $this->normalize($request->getHost()); if (!in_array($domain, $this->allowedDomains)) { return false; } } return true; }
/** * @param RequestInterface $request * * @return \GuzzleHttp\Message\RequestInterface */ protected function createGuzzleRequestFromRequest(RequestInterface $request) { $headers = array(); foreach ($request->getHeaders() as $headerName => $headerValue) { $headers[$headerName] = $headerValue[0]; } $headers['X-Forwarded-For'] = $request->getClientIp(); $headers['X-Forwarded-Proto'] = $request->getScheme(); if (isset($headers['cookie'])) { unset($headers['cookie']); } // see http://guzzle.readthedocs.org/en/latest/clients.html#request-options $guzzleRequest = $this->client->createRequest($request->getMethod(), $request->getUri(), array('headers' => $headers, 'body' => $request->getBody(), 'query' => $request->getQueryParams(), 'cookies' => $request->getCookies(), 'exceptions' => false, 'decode_content' => true)); return $guzzleRequest; }
/** * {@inheritdoc} */ public function delete(RequestInterface $request) { $this->doctrineCacheAdapter->delete($request->getFingerprint()); return $this; }