/** * Factory method to create a new URL from a URL string * * @param string $url Full URL used to create a Url object * * @return Url */ public static function factory($url) { $parts = ParserRegistry::get('url')->parseUrl($url); // Convert the query string into a QueryString object if ($parts['query']) { $parts['query'] = QueryString::fromString($parts['query']); } return new self($parts['scheme'], $parts['host'], $parts['user'], $parts['pass'], $parts['port'], $parts['path'], $parts['query'], $parts['fragment']); }
public function testReturnsLazyLoadedDefault() { // Clear out what might be cached $refObject = new \ReflectionClass('Guzzle\\Http\\Parser\\ParserRegistry'); $refProperty = $refObject->getProperty('instances'); $refProperty->setAccessible(true); $refProperty->setValue(null, array()); $c = ParserRegistry::get('cookie'); $this->assertInstanceOf('Guzzle\\Http\\Parser\\Cookie\\CookieParser', $c); $this->assertSame($c, ParserRegistry::get('cookie')); }
/** * {@inheritdoc} */ protected function build() { if (!$this->apiCommand->getUri()) { $url = $this->getClient()->getBaseUrl(); } else { // Get the path values and use the client config settings $variables = $this->getClient()->getConfig()->getAll(); foreach ($this->apiCommand->getParams() as $name => $arg) { $configValue = $this->get($name); if (is_scalar($configValue)) { $variables[$name] = $arg->getPrepend() . $configValue . $arg->getAppend(); } } // Expand the URI template using the URI values $uri = ParserRegistry::get('uri_template')->expand($this->apiCommand->getUri(), $variables); // Merge the client's base URL with the URI template $url = Url::factory($this->getClient()->getBaseUrl()); $url->combine($uri); $url = (string) $url; } // Inject path and base_url values into the URL $this->request = $this->getClient()->createRequest($this->apiCommand->getMethod(), $url); // Add arguments to the request using the location attribute foreach ($this->apiCommand->getParams() as $name => $arg) { $configValue = $this->get($name); $location = $arg->getLocation(); if (!$configValue || !$location) { continue; } // Create the value based on prepend and append settings $value = $arg->getPrepend() . $configValue . $arg->getAppend(); // Determine the location and key setting location[:key] $parts = explode(':', $location); $place = $parts[0]; // If a key is specified (using location:key), use it $key = isset($parts[1]) ? $parts[1] : $name; // Add the parameter to the request switch ($place) { case 'body': $this->request->setBody(EntityBody::factory($value)); break; case 'header': $this->request->setHeader($key, $value); break; case 'query': $this->request->getQuery()->set($key, $value); break; } } }
/** * Create a new Response based on a raw response message * * @param string $message Response message * * @return Response|bool Returns false on error */ public static function fromMessage($message) { $data = ParserRegistry::get('message')->parseResponse($message); if (!$data) { return false; } // Always set the appropriate Content-Length unset($data['headers']['Content-Length']); unset($data['headers']['content-length']); $data['headers']['Content-Length'] = strlen($data['body']); $response = new static($data['code'], $data['headers'], $data['body']); $response->setProtocol($data['protocol'], $data['version'])->setStatus($data['code'], $data['reason_phrase']); return $response; }
/** * {@inheritdoc} */ public function fromMessage($message) { $parsed = ParserRegistry::get('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; }
/** * Get the URI template expander used by the client. A default UriTemplate * object will be created if one does not exist. * * @return UriTemplateInterface */ public function getUriTemplate() { if (!$this->uriTemplate) { $this->uriTemplate = ParserRegistry::get('uri_template'); } return $this->uriTemplate; }
/** * Update a request based on the log messages of the CurlHandle * * @param RequestInterface $request Request to update */ public function updateRequestFromTransfer(RequestInterface $request) { $log = $this->getStderr(true); if (!$log || !$request->getResponse()) { return; } // Update the transfer stats of the response $request->getResponse()->setInfo($this->getInfo()); // 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::get('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']); } } }
/** * Extracts cookies from an HTTP Response object, looking for Set-Cookie: * and Set-Cookie2: headers and persists them to the cookie storage. * * @param Response $response */ public function extractCookies(Response $response) { if (!($cookie = $response->getSetCookie())) { return array(); } $cookieData = array(); $parser = ParserRegistry::get('cookie'); foreach ($cookie as $c) { $request = $response->getRequest(); if ($request) { $cdata = $parser->parseCookie($c, $request->getHost(), $request->getPath()); } else { $cdata = $parser->parseCookie($c); } //@codeCoverageIgnoreStart if (!$cdata) { continue; } //@codeCoverageIgnoreEnd $cookies = array(); // Break up cookie v2 into multiple cookies if (count($cdata['cookies']) == 1) { $cdata['cookie'] = array(key($cdata['cookies']), current($cdata['cookies'])); unset($cdata['cookies']); $cookies = array($cdata); } else { foreach ($cdata['cookies'] as $key => $cookie) { $row = $cdata; unset($row['cookies']); $row['cookie'] = array($key, $cookie); $cookies[] = $row; } } if (count($cookies)) { foreach ($cookies as &$c) { $this->jar->save($c); $cookieData[] = $c; } } } return $cookieData; }