public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain) { $signature = null; if ($request->hasHeader('Cookie')) { $cookies = Cookie::parseList($request->getHeader('Cookie')); foreach ($cookies as $cookie) { if ($cookie->getName() == self::COOKIE_NAME) { $data = $cookie->getValue(); $parts = explode('.', $data, 2); $payload = isset($parts[0]) ? $parts[0] : null; $signature = isset($parts[1]) ? $parts[1] : null; if (strcmp($signature, $this->generateSignature($payload)) === 0) { $request->setAttribute(self::COOKIE_NAME, $this->unserializeData($payload)); } else { // invalid signature } break; } } } $filterChain->handle($request, $response); $data = $request->getAttribute(self::COOKIE_NAME); if (!empty($data)) { $payload = $this->serializeData($data); $newSignature = $this->generateSignature($payload); // send only a new cookie if the data has changed if ($newSignature != $signature) { $response->addHeader('Set-Cookie', self::COOKIE_NAME . '=' . $payload . '.' . $newSignature); } } }
public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain) { if ($request->hasHeader('Accept-Encoding')) { $acceptEncoding = $request->getHeader('Accept-Encoding'); if (strpos($acceptEncoding, 'gzip') !== false) { // the sender will compress the response if the content encoding // header is available $response->setHeader('Content-Encoding', 'gzip'); } } $filterChain->handle($request, $response); }
public static function assignHttpContext($context, RequestInterface $request, Options $options = null) { stream_context_set_option($context, 'http', 'method', $request->getMethod()); stream_context_set_option($context, 'http', 'protocol_version', $request->getProtocolVersion() ?: 1.1); // until chunked transfer encoding if fully implemented we remove the // header if ($request->hasHeader('Transfer-Encoding')) { $request->removeHeader('Transfer-Encoding'); } // set header $headers = implode(Http::$newLine, ResponseParser::buildHeaderFromMessage($request)); stream_context_set_option($context, 'http', 'header', $headers); // set body $body = $request->getBody(); if ($body !== null && !in_array($request->getMethod(), array('HEAD', 'GET'))) { stream_context_set_option($context, 'http', 'content', (string) $body); } if ($options !== null) { // set proxy $proxy = $options->getProxy(); if (!empty($proxy)) { stream_context_set_option($context, 'http', 'proxy', $proxy); } // set follow location stream_context_set_option($context, 'http', 'follow_location', (int) $options->getFollowLocation()); stream_context_set_option($context, 'http', 'max_redirects', $options->getMaxRedirects()); // set timeout $timeout = $options->getTimeout(); if (!empty($timeout)) { stream_context_set_option($context, 'http', 'timeout', $timeout); } } }