/** * Sets the configuration parameter(s) * * The following parameters are available: * <ul> * <li> 'adapter' - adapter to use (string)</li> * <li> 'connect_timeout' - Connection timeout in seconds (integer)</li> * <li> 'timeout' - Total number of seconds a request can take. * Use 0 for no limit, should be greater than * 'connect_timeout' if set (integer)</li> * <li> 'use_brackets' - Whether to append [] to array variable names (bool)</li> * <li> 'protocol_version' - HTTP Version to use, '1.0' or '1.1' (string)</li> * <li> 'buffer_size' - Buffer size to use for reading and writing (int)</li> * <li> 'store_body' - Whether to store response body in response object. * Set to false if receiving a huge response and * using an Observer to save it (boolean)</li> * <li> 'local_ip' - Specifies the IP address that will be used for accessing * the network (string)</li> * <li> 'proxy_type' - Proxy type, 'http' or 'socks5' (string)</li> * <li> 'proxy_host' - Proxy server host (string)</li> * <li> 'proxy_port' - Proxy server port (integer)</li> * <li> 'proxy_user' - Proxy auth username (string)</li> * <li> 'proxy_password' - Proxy auth password (string)</li> * <li> 'proxy_auth_scheme' - Proxy auth scheme, one of HTTP_Request2::AUTH_* constants (string)</li> * <li> 'proxy' - Shorthand for proxy_* parameters, proxy given as URL, * e.g. 'socks5://localhost:1080/' (string)</li> * <li> 'ssl_verify_peer' - Whether to verify peer's SSL certificate (bool)</li> * <li> 'ssl_verify_host' - Whether to check that Common Name in SSL * certificate matches host name (bool)</li> * <li> 'ssl_cafile' - Cerificate Authority file to verify the peer * with (use with 'ssl_verify_peer') (string)</li> * <li> 'ssl_capath' - Directory holding multiple Certificate * Authority files (string)</li> * <li> 'ssl_local_cert' - Name of a file containing local cerificate (string)</li> * <li> 'ssl_passphrase' - Passphrase with which local certificate * was encoded (string)</li> * <li> 'digest_compat_ie' - Whether to imitate behaviour of MSIE 5 and 6 * in using URL without query string in digest * authentication (boolean)</li> * <li> 'follow_redirects' - Whether to automatically follow HTTP Redirects (boolean)</li> * <li> 'max_redirects' - Maximum number of redirects to follow (integer)</li> * <li> 'strict_redirects' - Whether to keep request method on redirects via status 301 and * 302 (true, needed for compatibility with RFC 2616) * or switch to GET (false, needed for compatibility with most * browsers) (boolean)</li> * </ul> * * @param string|array $nameOrConfig configuration parameter name or array * ('parameter name' => 'parameter value') * @param mixed $value parameter value if $nameOrConfig is not an array * * @return HTTP_Request2 * @throws HTTP_Request2_LogicException If the parameter is unknown */ public function setConfig($nameOrConfig, $value = null) { if (is_array($nameOrConfig)) { foreach ($nameOrConfig as $name => $value) { $this->setConfig($name, $value); } } elseif ('proxy' == $nameOrConfig) { $url = new Net_URL2($value); $this->setConfig(array('proxy_type' => $url->getScheme(), 'proxy_host' => $url->getHost(), 'proxy_port' => $url->getPort(), 'proxy_user' => rawurldecode($url->getUser()), 'proxy_password' => rawurldecode($url->getPassword()))); } else { if (!array_key_exists($nameOrConfig, $this->config)) { throw new HTTP_Request2_LogicException("Unknown configuration parameter '{$nameOrConfig}'", HTTP_Request2_Exception::INVALID_ARGUMENT); } $this->config[$nameOrConfig] = $value; } return $this; }
/** * Returns all cookies matching a given request URL * * The following checks are made: * - cookie domain should match request host * - cookie path should be a prefix for request path * - 'secure' cookies will only be sent for HTTPS requests * * @param Net_URL2 $url Request url * @param bool $asString Whether to return cookies as string for "Cookie: " header * * @return array|string Matching cookies */ public function getMatching(Net_URL2 $url, $asString = false) { $host = $url->getHost(); $path = $url->getPath(); $secure = 0 == strcasecmp($url->getScheme(), 'https'); $matched = $ret = array(); foreach (array_keys($this->cookies) as $domain) { if ($this->domainMatch($host, $domain)) { foreach (array_keys($this->cookies[$domain]) as $cPath) { if (0 === strpos($path, $cPath)) { foreach ($this->cookies[$domain][$cPath] as $name => $cookie) { if (!$cookie['secure'] || $secure) { $matched[$name][strlen($cookie['path'])] = $cookie; } } } } } } foreach ($matched as $cookies) { krsort($cookies); $ret = array_merge($ret, $cookies); } if (!$asString) { return $ret; } else { $str = ''; foreach ($ret as $c) { $str .= (empty($str) ? '' : '; ') . $c['name'] . '=' . $c['value']; } return $str; } }
/** * Property getter interceptor to handle "uri" and "host" special cases. * * @param string $property The property to retrieve * * @return mixed */ public function __get($property) { switch ($property) { case 'uri': if ($this->_uri !== null) { return $this->_uri->getURL(); } return null; case 'proxy': return $this->_proxy; case 'path': if ($this->_uri === null) { return null; } if ($this->httpVersion == self::HTTP_VERSION_1_0 || $this->proxy) { // if http 1.0 or proxy given, the request uri must be absolute return $this->_uri->getURL(); } if (($path = $this->_uri->getPath()) === false) { $path = '/'; } if (($query = $this->_uri->getQuery()) !== false) { $path .= '?' . $query; } if (($fragment = $this->_uri->getFragment()) !== false) { $path .= '#' . $fragment; } return $path; case 'host': if (isset($this->headers['host'])) { return trim($this->headers['host']); } if ($this->_proxy !== null) { return $this->_proxy; } if ($this->_uri !== null) { $host = $this->_uri->getHost(); if (($port = $this->_uri->getPort()) !== false) { $host .= ':' . $port; } } return $host; case 'port': $port = $this->_uri->getPort(); if (!is_numeric($port)) { return $this->isSecure() ? 443 : 80; } return $port; case 'connection': if ($this->_connection === null) { include_once 'HTTP/Connection.php'; $this->_connection = HTTP_Connection::factory('Socket'); } return $this->_connection; default: return parent::__get($property); } }
/** * Detect service from a short URL * * Takes a URL and inpects it to see if there is a service driver that can * be used to expand it. * * @param string $url The short URL to inspect * * @return object Instance of {@link Services_ShortURL_Interface} */ public static function detect($url) { $url = new Net_URL2($url); $host = $url->getHost(); if (!isset(self::$services[$host])) { throw new Services_ShortURL_Exception_UnknownService(); } return self::factory(self::$services[$host]); }
/** * test that an authority containing host and port maps to expected host and port * * This is also a regression test to test that using ip-literals works along- * side ipv4 and reg-name hosts incl. port numbers * * It was reported as Bug #20423 on 2014-10-06 18:25 UTC that * http://[::1]// URI drops the host * * @param string $authority string * @param string $expectedHost string * @param string|bool $expectedPort string or FALSE * * @return void * @dataProvider provideHostAndPort * @covers Net_URL2::setAuthority() * @link https://pear.php.net/bugs/bug.php?id=20423 * @link http://tools.ietf.org/html/rfc3986#section-3.2.2 * @link http://tools.ietf.org/html/rfc3986#section-3.2 * @link http://tools.ietf.org/html/rfc3986#section-3.2.3 */ public function testHostAndPort($authority, $expectedHost, $expectedPort) { $uri = "http://{$authority}"; $url = new Net_URL2($uri); $this->assertSame($expectedHost, $url->getHost()); $this->assertSame($expectedPort, $url->getPort()); }