public function testLowercaseScheme() { $url = Url::fromString('HTTP://foo.com/'); $this->assertEquals('http', $url->getScheme()); $url->setScheme('HTTPS'); $this->assertEquals('https', $url->getScheme()); }
public function setUrl($url) { $this->url = $url instanceof Url ? $url : Url::fromString($url); $this->updateHostHeaderFromUrl(); }
/** * Create a request or response object from an HTTP message string * * @param string $message Message to parse * * @return RequestInterface|ResponseInterface * @throws \InvalidArgumentException if unable to parse a message */ public function fromMessage($message) { static $parser; if (!$parser) { $parser = new MessageParser(); } // Parse a response if (strtoupper(substr($message, 0, 4)) == 'HTTP') { $data = $parser->parseResponse($message); return $this->createResponse($data['code'], $data['headers'], $data['body'] === '' ? null : $data['body'], $data); } // Parse a request if (!($data = $parser->parseRequest($message))) { throw new \InvalidArgumentException('Unable to parse request'); } return $this->createRequest($data['method'], Url::buildUrl($data['request_url']), ['headers' => $data['headers'], 'body' => $data['body'] === '' ? null : $data['body'], 'config' => ['protocol_version' => $data['protocol_version']]]); }
/** * Set the appropriate URL on the request based on the location header * * @param RequestInterface $request * @param ResponseInterface $response * @param array $protocols */ private function setRedirectUrl(RequestInterface $request, ResponseInterface $response, array $protocols) { $location = $response->getHeader('Location'); $location = Url::fromString($location); // Combine location with the original URL if it is not absolute. if (!$location->isAbsolute()) { $originalUrl = Url::fromString($request->getUrl()); // Remove query string parameters and just take what is present on // the redirect Location header $originalUrl->getQuery()->clear(); $location = $originalUrl->combine($location); } // Ensure that the redirect URL is allowed based on the protocols. if (!in_array($location->getScheme(), $protocols)) { throw new BadResponseException(sprintf('Redirect URL, %s, does not use one of the allowed redirect protocols: %s', $location, implode(', ', $protocols)), $request, $response); } $request->setUrl($location); }
public function testCanUseUrlWithCustomQuery() { $client = new Client(); $url = Url::fromString('http://foo.com/bar'); $query = new Query(['baz' => '123%20']); $query->setEncodingType(false); $url->setQuery($query); $r = $client->createRequest('GET', $url); $this->assertEquals('http://foo.com/bar?baz=123%20', $r->getUrl()); }
private function configureBaseUrl(&$config) { if (!isset($config['base_url'])) { $this->baseUrl = new Url('', ''); } elseif (!is_array($config['base_url'])) { $this->baseUrl = Url::fromString($config['base_url']); } elseif (count($config['base_url']) < 2) { throw new \InvalidArgumentException('You must provide a hash of ' . 'varname options in the second element of a base_url array.'); } else { $this->baseUrl = Url::fromString(Utils::uriTemplate($config['base_url'][0], $config['base_url'][1])); $config['base_url'] = (string) $this->baseUrl; } }