public function __construct($storagePath) { if (!file_exists($storagePath)) { $cookieFileHandle = $this->createStorageFile($storagePath); } elseif (false === ($cookieFileHandle = @fopen($storagePath, 'r+'))) { throw new \RuntimeException('Failed opening cookie storage file for reading: ' . $storagePath); } while (!feof($cookieFileHandle)) { if ($line = fgets($cookieFileHandle)) { $cookie = Cookie::fromString($line); $this->store($cookie); } } $this->storagePath = $storagePath; }
public function updateFromSetCookie(array $setCookies, $uri = null) { $cookies = array(); foreach ($setCookies as $cookie) { foreach (explode(',', $cookie) as $i => $part) { if (0 === $i || preg_match('/^(?P<token>\\s*[0-9A-Za-z!#\\$%\\&\'\\*\\+\\-\\.^_`\\|~]+)=/', $part)) { $cookies[] = ltrim($part); } else { $cookies[count($cookies) - 1] .= ',' . $part; } } } foreach ($cookies as $cookie) { $this->set(Cookie::fromString($cookie, $uri)); } }
/** * Updates the cookie jar from a Response object. * * @param Symfony\Components\BrowserKit\Response $response A Response object * @param string $url The base URL */ public function updateFromResponse(Response $response, $uri = null) { foreach ($response->getHeader('Set-Cookie', false) as $cookie) { $this->set(Cookie::fromString($cookie), $uri); } }
/** * {@inheritdoc} */ public function set($key, $values, $replace = true) { $uniqueKey = str_replace('_', '-', strtolower($key)); if ('set-cookie' === $uniqueKey) { if ($replace) { $this->cookies = array(); } foreach ((array) $values as $cookie) { $this->setCookie(Cookie::fromString($cookie)); } $this->headerNames[$uniqueKey] = $key; return; } $this->headerNames[$uniqueKey] = $key; parent::set($key, $values, $replace); // ensure the cache-control header has sensible defaults if (in_array($uniqueKey, array('cache-control', 'etag', 'last-modified', 'expires'))) { $computed = $this->computeCacheControlValue(); $this->headers['cache-control'] = array($computed); $this->headerNames['cache-control'] = 'Cache-Control'; $this->computedCacheControl = $this->parseCacheControl($computed); } }
/** * Get HTTP response cookies * * @param $deleted boolean if true, get deleted cookies too * @return Cookie[] */ public function getResponseCookies($deleted = true) { $cookies = []; foreach ($this->response_headers as $header) { $pos = strpos($header, ': '); if ($pos !== false) { $name = substr($header, 0, $pos); $value = substr($header, $pos + 2); if ($name == 'Set-Cookie') { $cookie = new Cookie(); $cookie->fromString($value); if ($deleted || !($cookie->value == 'deleted')) { $cookies[] = $cookie; } } } } return $cookies; }