/** * Cast to HTTP query string * * @param mixed $url * @param Zend\OAuth\Config $config * @param null|array $params * @return string */ public function toQueryString($url, Config $config, array $params = null) { $uri = new Uri\Url($url); if (!$uri->isValid() || !in_array($uri->getScheme(), array('http', 'https'))) { throw new OAuth\Exception('\'' . $url . '\' is not a valid URI'); } $params = $this->_httpUtility->assembleParams($url, $config, $params); return $this->_httpUtility->toEncodedQueryString($params); }
/** * Ensures that successive slashes are considered valid * * @return void */ public function testSuccessiveSlashes() { $url = new Url('http://example.com/foo//bar/baz//fob/'); $this->assertTrue($url->isValid()); $this->assertEquals('/foo//bar/baz//fob/', $url->getPath()); }
/** * Checks whether the cookie should be sent or not in a specific scenario * * @param string|\Zend\Uri\Url $uri URI to check against (secure, domain, path) * @param boolean $matchSessionCookies Whether to send session cookies * @param int $now Override the current time when checking for expiry time * @return boolean */ public function match($uri, $matchSessionCookies = true, $now = null) { if (is_string($uri)) { $uri = new Uri\Url($uri); } // Make sure we have a valid Zend_Uri_Http object if (!($uri->isValid() && ($uri->getScheme() == 'http' || $uri->getScheme() == 'https'))) { throw new Exception('Passed URI is not a valid HTTP or HTTPS URI'); } // Check that the cookie is secure (if required) and not expired if ($this->secure && $uri->getScheme() != 'https') { return false; } if ($this->isExpired($now)) { return false; } if ($this->isSessionCookie() && !$matchSessionCookies) { return false; } // Check if the domain matches if (!self::matchCookieDomain($this->getDomain(), $uri->getHost())) { return false; } // Check that path matches using prefix match if (!self::matchCookiePath($this->getPath(), $uri->getPath())) { return false; } // If we didn't die until now, return true. return true; }
/** * Determine if a given URL is valid * * @param string $url * @return void * @throws Zend\OAuth\Exception */ protected function _validateUrl($url) { $uri = new Uri\Url($url); if (!$uri->isValid()) { throw new OAuth\Exception(sprintf("'%s' is not a valid URI", $url)); } elseif (!in_array($uri->getScheme(), array('http', 'https'))) { throw new OAuth\Exception(sprintf("'%s' is not a valid URI", $url)); } }
/** * Constructor * * If a $uri is passed, the object will attempt to populate itself using * that information. * * @param string|\Zend\Uri\Uri $uri * @return void * @throws \Zend\Controller\Request\Exception when invalid URI passed */ public function __construct($uri = null) { if (null !== $uri) { if (!$uri instanceof Uri\Url) { $uri = new Uri\Url($uri); } if ($uri->isValid()) { $path = $uri->getPath(); $query = $uri->getQuery(); if (!empty($query)) { $path .= '?' . $query; } $this->setRequestUri($path); } else { throw new Exception('Invalid URI provided to constructor'); } } else { $this->setRequestUri(); } }
/** * Send the HTTP request and return an HTTP response object * * @param string $method * @return \Zend\Http\Response * @throws \Zend\Http\Client\Exception */ public function request($method = null) { if (!$this->uri instanceof Uri\Url) { throw new Client\Exception('No valid URI has been passed to the client'); } if ($method) { $this->setMethod($method); } $this->redirectCounter = 0; $response = null; // Make sure the adapter is loaded if ($this->adapter == null) { $this->setAdapter($this->config['adapter']); } // Send the first request. If redirected, continue. do { // Clone the URI and add the additional GET parameters to it $uri = clone $this->uri; if (!empty($this->paramsGet)) { $query = $uri->getQuery(); if (!empty($query)) { $query .= '&'; } $query .= http_build_query($this->paramsGet, null, '&'); $uri->setQuery($query); } $body = $this->_prepareBody(); $headers = $this->_prepareHeaders(); // check that adapter supports streaming before using it if (is_resource($body) && !$this->adapter instanceof Client\Adapter\Stream) { throw new Client\Exception('Adapter does not support streaming'); } // Open the connection, send the request and read the response $this->adapter->connect($uri->getHost(), $uri->getPort(), $uri->getScheme() == 'https' ? true : false); if ($this->config['output_stream']) { if ($this->adapter instanceof Client\Adapter\Stream) { $stream = $this->_openTempStream(); $this->adapter->setOutputStream($stream); } else { throw new Client\Exception('Adapter does not support streaming'); } } $this->last_request = $this->adapter->write($this->method, $uri, $this->config['httpversion'], $headers, $body); $response = $this->adapter->read(); if (!$response) { throw new Client\Exception('Unable to read response, or response is empty'); } if ($this->config['output_stream']) { rewind($stream); // cleanup the adapter $this->adapter->setOutputStream(null); $response = Response\Stream::fromStream($response, $stream); $response->setStreamName($this->_stream_name); if (!is_string($this->config['output_stream'])) { // we used temp name, will need to clean up $response->setCleanup(true); } } else { $response = Response::fromString($response); } if ($this->config['storeresponse']) { $this->last_response = $response; } // Load cookies into cookie jar if (isset($this->cookiejar)) { $this->cookiejar->addCookiesFromResponse($response, $uri); } // If we got redirected, look for the Location header if ($response->isRedirect() && ($location = $response->getHeader('location'))) { // Check whether we send the exact same request again, or drop the parameters // and send a GET request if ($response->getStatus() == 303 || !$this->config['strictredirects'] && ($response->getStatus() == 302 || $response->getStatus() == 301)) { $this->resetParameters(); $this->setMethod(self::GET); } // If we got a well formed absolute URI $url = new Uri\Url($location); if ($url->isValid()) { $this->setHeaders('host', null); $this->setUri($location); } else { // Split into path and query and set the query if (strpos($location, '?') !== false) { list($location, $query) = explode('?', $location, 2); } else { $query = ''; } $this->uri->setQuery($query); // Else, if we got just an absolute path, set it if (strpos($location, '/') === 0) { $this->uri->setPath($location); // Else, assume we have a relative path } else { // Get the current path directory, removing any trailing slashes $path = $this->uri->getPath(); $path = rtrim(substr($path, 0, strrpos($path, '/')), "/"); $this->uri->setPath($path . '/' . $location); } } ++$this->redirectCounter; } else { // If we didn't get any location, stop redirecting break; } } while ($this->redirectCounter < $this->config['maxredirects']); return $response; }