/** * Parses a raw response string, including headers and body, and returns a Response object. * * @param string $response * * @return \Brick\Http\Response * * @throws \RuntimeException */ public static function parse($response) { $responseObject = new Response(); if (preg_match('/^HTTP\\/([0-9]\\.[0-9]) ([0-9]{3}) .*\\r\\n/', $response, $matches) == 0) { throw new \RuntimeException('Could not parse response (error 1).'); } list($line, $protocolVersion, $statusCode) = $matches; $responseObject->setProtocolVersion($protocolVersion); $responseObject->setStatusCode($statusCode); $response = substr($response, strlen($line)); for (;;) { $pos = strpos($response, Message::CRLF); if ($pos === false) { throw new \RuntimeException('Could not parse response (error 2).'); } if ($pos == 0) { break; } $header = substr($response, 0, $pos); if (preg_match('/^(\\S+):\\s*(.*)$/', $header, $matches) == 0) { throw new \RuntimeException('Could not parse response (error 3).'); } list($line, $name, $value) = $matches; if (strtolower($name) == 'set-cookie') { $responseObject->setCookie(Cookie::parse($value)); } else { $responseObject->addHeader($name, $value); } $response = substr($response, strlen($line) + 2); } $body = substr($response, 2); $responseObject->setContent($body); return $responseObject; }
/** * @param string $response */ public function __construct(string $response = null) { if ($response) { $explode = explode("\r\n\r\n", $response); // multiple query (follow redirect) take only the last request $explode = array_slice($explode, sizeof($explode) - 2, 2); // body $this->body = array_pop($explode); // headers & cookies $headers = []; foreach (explode("\n", implode($explode)) as $i => $header) { $explode = explode(':', $header, 2); $key = $this->normalizeHeader($explode[0]); $value = isset($explode[1]) ? trim($explode[1]) : null; if ($key == 'Set-Cookie') { $cookie = Cookie::parse($value); $this->cookies[$cookie->getName()] = $cookie; } elseif (array_key_exists($key, $headers)) { $this->headers[$key] .= ', ' . $value; } elseif ($value) { $this->headers[$key] = $value; } elseif (preg_match('#HTTP/\\d+\\.\\d+ (\\d+)#', $header, $matches)) { $this->statusCode = (int) $matches[1]; } } } }
private function load() { if (file_exists($this->file)) { $array = (array) json_decode(file_get_contents($this->file), true); foreach ($array as $c) { $cookie = Cookie::parse($c); $this->add($cookie); } } }
/** * Navigate to a relative URL * * @param string relative * @param string params * @throws unittest.AssertionFailedError */ public function beginAt($relative, $params = NULL, $method = HttpConstants::GET) { $this->dom = $this->xpath = NULL; $this->conn->getUrl()->setPath($relative); try { $this->response = $this->doRequest($method, $params); // If we get a cookie, store it for this domain and reuse it in // subsequent requests. If cookies are used for sessioning, we // would be creating new sessions with every request otherwise! foreach ((array) $this->response->header('Set-Cookie') as $str) { $cookie = Cookie::parse($str); $this->cookies[$this->conn->getUrl()->getHost()][$cookie->getName()] = $cookie; } } catch (XPException $e) { $this->response = xp::null(); $this->fail($relative, $e, NULL); } }
public function parseCookie() { $cookie = Cookie::parse('Bugzilla_logincookie=e9hR2sFvjX; path=/; expires=Fri, 01-Jan-2038 00:00:00 GMT; secure; HttpOnly'); $this->assertHeaderEquals('Bugzilla_logincookie=e9hR2sFvjX; expires=Fri, 01-Jan-2038 00:00:00 GMT; path=/; secure; HTTPOnly', $cookie); }