public function remove($domain, Cookie $cookie) { $key = md5($domain); if (isset($this->container[$key][$cookie->getName()])) { unset($this->container[$key][$cookie->getName()]); } }
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); } } }
/** * Sends the request through the given handler and returns the response * * @param \PSX\Http\Request $request * @param \PSX\Http\Options $options * @param integer $count * @return \PSX\Http\Response */ public function request(Request $request, Options $options = null, $count = 0) { if (!$request->getUri()->isAbsolute()) { throw new InvalidArgumentException('Request url must be absolute'); } // set cookie headers if ($this->cookieStore !== null) { $cookies = $this->cookieStore->load($request->getUri()->getHost()); if (!empty($cookies)) { $kv = array(); foreach ($cookies as $cookie) { $path = ltrim($cookie->getPath(), '/'); if ($cookie->getExpires() !== null && $cookie->getExpires()->getTimestamp() < time()) { $this->cookieStore->remove($request->getUri()->getHost(), $cookie); } elseif ($cookie->getPath() !== null && substr($request->getUri()->getPath(), 0, strlen($path)) != $path) { // path does not fit } else { $kv[] = $cookie->getName() . '=' . $cookie->getValue(); } } $request->addHeader('Cookie', implode('; ', $kv)); } } // set content length $body = $request->getBody(); if ($body !== null && $request->hasHeader('Transfer-Encoding') != 'chunked' && !in_array($request->getMethod(), array('HEAD', 'GET'))) { $size = $body->getSize(); if ($size !== false) { $request->setHeader('Content-Length', $size); } } // set default options if ($options === null) { $options = new Options(); } // make request $response = $this->handler->request($request, $options); // store cookies if ($this->cookieStore !== null) { $cookies = $response->getHeaderLines('Set-Cookie'); foreach ($cookies as $rawCookie) { try { $cookie = new Cookie($rawCookie); $domain = $cookie->getDomain() !== null ? $cookie->getDomain() : $request->getUri()->getHost(); $this->cookieStore->store($domain, $cookie); } catch (InvalidArgumentException $e) { // invalid cookies } } } // check follow location if ($options->getFollowLocation() && ($response->getStatusCode() >= 300 && $response->getStatusCode() < 400)) { $location = (string) $response->getHeader('Location'); if (!empty($location) && $location != $request->getUri()->toString()) { if ($options->getMaxRedirects() > $count) { $location = UriResolver::resolve($request->getUri(), new Uri($location)); return $this->request(new GetRequest($location), $options, ++$count); } else { throw new RedirectException('Max redirection reached'); } } } return $response; }