public function fromMessage($message) { $parsed = ParserRegistry::getInstance()->getParser('message')->parseRequest($message); if (!$parsed) { return false; } $request = $this->fromParts($parsed['method'], $parsed['request_url'], $parsed['headers'], $parsed['body'], $parsed['protocol'], $parsed['version']); // EntityEnclosingRequest adds an "Expect: 100-Continue" header when using a raw request body for PUT or POST // requests. This factory method should accurately reflect the message, so here we are removing the Expect // header if one was not supplied in the message. if (!isset($parsed['headers']['Expect']) && !isset($parsed['headers']['expect'])) { $request->removeHeader('Expect'); } return $request; }
/** * Create a new Response based on a raw response message * * @param string $message Response message * * @return self|bool Returns false on error */ public static function fromMessage($message) { $data = ParserRegistry::getInstance()->getParser('message')->parseResponse($message); if (!$data) { return false; } $response = new static($data['code'], $data['headers'], $data['body']); $response->setProtocol($data['protocol'], $data['version'])->setStatus($data['code'], $data['reason_phrase']); // Set the appropriate Content-Length if the one set is inaccurate (e.g. setting to X) $contentLength = (string) $response->getHeader('Content-Length'); $actualLength = strlen($data['body']); if (strlen($data['body']) > 0 && $contentLength != $actualLength) { $response->setHeader('Content-Length', $actualLength); } return $response; }
public function getCookies() { if ($cookie = $this->getHeader('Cookie')) { $data = ParserRegistry::getInstance()->getParser('cookie')->parseCookie($cookie); return $data['cookies']; } return array(); }
/** * Update a request based on the log messages of the CurlHandle * * @param RequestInterface $request Request to update */ public function updateRequestFromTransfer(RequestInterface $request) { if (!$request->getResponse()) { return; } // Update the transfer stats of the response $request->getResponse()->setInfo($this->getInfo()); if (!($log = $this->getStderr(true))) { return; } // Parse the cURL stderr output for outgoing requests $headers = ''; fseek($log, 0); while (($line = fgets($log)) !== false) { if ($line && $line[0] == '>') { $headers = substr(trim($line), 2) . "\r\n"; while (($line = fgets($log)) !== false) { if ($line[0] == '*' || $line[0] == '<') { break; } else { $headers .= trim($line) . "\r\n"; } } } } // Add request headers to the request exactly as they were sent if ($headers) { $parsed = ParserRegistry::getInstance()->getParser('message')->parseRequest($headers); if (!empty($parsed['headers'])) { $request->setHeaders(array()); foreach ($parsed['headers'] as $name => $value) { $request->setHeader($name, $value); } } if (!empty($parsed['version'])) { $request->setProtocolVersion($parsed['version']); } } }
/** * Get the URI template expander used by the client * * @return UriTemplateInterface */ protected function getUriTemplate() { if (!$this->uriTemplate) { $this->uriTemplate = ParserRegistry::getInstance()->getParser('uri_template'); } return $this->uriTemplate; }
public function addCookiesFromResponse(Response $response, RequestInterface $request = null) { if ($cookieHeader = $response->getHeader('Set-Cookie')) { $parser = ParserRegistry::getInstance()->getParser('cookie'); foreach ($cookieHeader as $cookie) { if ($parsed = $request ? $parser->parseCookie($cookie, $request->getHost(), $request->getPath()) : $parser->parseCookie($cookie)) { // Break up cookie v2 into multiple cookies foreach ($parsed['cookies'] as $key => $value) { $row = $parsed; $row['name'] = $key; $row['value'] = $value; unset($row['cookies']); $this->add(new Cookie($row)); } } } } }