Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of git://git.zendframework.com/zf into ZF2-13
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-mabe committed Jul 22, 2011
11 parents 70330aa + 10f278f + 252fdc8 + 023de10 + 85fcc7b + 5d8866f + 634bb62 + 453bb28 + c8dba7b + f7017f6 + 7869641 commit f548f30
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 172 deletions.
68 changes: 28 additions & 40 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* Zend Framework
*
Expand Down Expand Up @@ -35,15 +34,6 @@
* authentication and cookie persistence (using a Zend_Http_CookieJar object)
*
* @todo Implement proxy settings
* @uses Zend\Http\Client\Adapter
* @uses Zend\Http\Client\Exception
* @uses Zend\Http\Cookie
* @uses Zend\Http\CookieJar
* @uses Zend\Http\Response
* @uses Zend\Http\Response\Stream
* @uses Zend\Loader
* @uses Zend\Uri\Uri
* @uses Zend\Uri\Url
* @category Zend
* @package Zend_Http
* @subpackage Client
Expand Down Expand Up @@ -119,7 +109,7 @@ class Client
/**
* Request URI
*
* @var \Zend\Uri\Url
* @var Uri\Uri
*/
protected $uri = null;

Expand Down Expand Up @@ -231,7 +221,7 @@ class Client
* Constructor method. Will create a new HTTP client. Accepts the target
* URL and optionally configuration array.
*
* @param \Zend\Uri\Url|string $uri
* @param Uri\Uri|string $uri
* @param array $config Configuration key-value pairs.
*/
public function __construct($uri = null, $config = null)
Expand All @@ -247,32 +237,33 @@ public function __construct($uri = null, $config = null)
/**
* Set the URI for the next request
*
* @param \Zend\Uri\Url|string $uri
* @return \Zend\Http\Client
* @throws \Zend\Http\Client\Exception
* @param Uri\Url|string $uri
* @return Client
* @throws Client\Exception
*/
public function setUri($uri)
{
if (is_string($uri)) {
try {
$uri = new Uri\Url($uri);
$uri = Uri\UriFactory::factory($uri, 'http');
$uri = new Uri\Uri($uri);
} catch (Uri\Exception $e) {
throw new Client\Exception('Passed parameter is not a valid HTTP URI.');
throw new Client\Exception\InvalidArgumentException('Passed parameter is not a valid HTTP URI', $e->getCode(), $e);
}
}

$scheme = strtolower($uri->getScheme());
if (!empty($scheme) && !in_array($scheme, array('http', 'https'))) {
throw new Client\Exception\InvalidArgumentException('Passed parameter is not a valid HTTP URI.');
throw new Client\Exception\InvalidArgumentException('Passed parameter is not a valid HTTP URI');
}

// Set auth if username and password has been specified in the uri
if ($uri->getUsername() && $uri->getPassword()) {
$this->setAuth($uri->getUsername(), $uri->getPassword());
if ($uri instanceof Uri\Http && $uri->getUser() && $uri->getPassword()) {
$this->setAuth($uri->getUser(), $uri->getPassword());
}

// We have no ports, set the defaults
if (! $uri->getPort()) {
if (!$uri->getPort()) {
$uri->setPort(($uri->getScheme() == 'https' ? 443 : 80));
}

Expand All @@ -285,15 +276,15 @@ public function setUri($uri)
* Get the URI for the next request
*
* @param boolean $as_string If true, will return the URI as a string
* @return \Zend\Uri\Url|string
* @return Uri\Uri|string
*/
public function getUri($as_string = false)
{
if ($as_string && $this->uri instanceof Uri\Url) {
if ($as_string && $this->uri instanceof Uri\Uri) {
return $this->uri->__toString();
} else {
return $this->uri;
}
}

return $this->uri;
}

/**
Expand Down Expand Up @@ -535,8 +526,8 @@ public function setAuth($user, $password = '', $type = self::AUTH_BASIC)
$this->auth = null;

// Clear the auth information in the uri instance as well
if ($this->uri instanceof Uri\Url) {
$this->getUri()->setUsername('');
if ($this->uri instanceof Uri\Http) {
$this->getUri()->setUser('');
$this->getUri()->setPassword('');
}
// Else, set up authentication
Expand Down Expand Up @@ -883,12 +874,12 @@ protected function _openTempStream()
* Send the HTTP request and return an HTTP response object
*
* @param string $method
* @return \Zend\Http\Response
* @throws \Zend\Http\Client\Exception
* @return Response
* @throws Client\Exception
*/
public function request($method = null)
{
if (! $this->uri instanceof Uri\Url) {
if (!$this->uri instanceof Uri\Uri) {
throw new Client\Exception\RuntimeException('No valid URI has been passed to the client');
}

Expand All @@ -907,11 +898,11 @@ public function request($method = null)
do {
// Clone the URI and add the additional GET parameters to it
$uri = clone $this->uri;
if (! empty($this->paramsGet)) {
if (!empty($this->paramsGet)) {
$query = $uri->getQuery();
if (! empty($query)) {
$query .= '&';
}
if (! empty($query)) {
$query .= '&';
}
$query .= http_build_query($this->paramsGet, null, '&');

$uri->setQuery($query);
Expand Down Expand Up @@ -983,14 +974,12 @@ public function request($method = null)
}

// If we got a well formed absolute URI
$url = new Uri\Url($location);
$url = Uri\UriFactory($location, 'http');

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);
Expand All @@ -1002,9 +991,8 @@ public function request($method = null)
// 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 {
// Else, assume we have a relative path
// Get the current path directory, removing any trailing slashes
$path = $this->uri->getPath();
$path = rtrim(substr($path, 0, strrpos($path, '/')), "/");
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function connect($host, $port = 80, $secure = false);
* Send request to the remote server
*
* @param string $method
* @param \Zend\Uri\Url $url
* @param \Zend\Uri\Uri $url
* @param string $http_ver
* @param array $headers
* @param string $body
Expand Down
8 changes: 1 addition & 7 deletions src/Client/Adapter/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@
* An adapter class for Zend\Http\Client based on the curl extension.
* Curl requires libcurl. See for full requirements the PHP manual: http://php.net/curl
*
* @uses \Zend\Http\Client
* @uses \Zend\Http\Client\Exception
* @uses \Zend\Http\Client\Adapter\Exception
* @uses \Zend\Http\Client\Adapter
* @uses \Zend\Http\Client\Adapter\Stream
* @uses \Zend\Uri\Url
* @category Zend
* @package Zend_Http
* @subpackage Client_Adapter
Expand Down Expand Up @@ -248,7 +242,7 @@ public function connect($host, $port = 80, $secure = false)
* Send request to the remote server
*
* @param string $method
* @param \Zend\Uri\Url $uri
* @param \Zend\Uri\Uri $uri
* @param float $http_ver
* @param array $headers
* @param string $body
Expand Down
9 changes: 2 additions & 7 deletions src/Client/Adapter/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@
* default Socket adapter, this adapter does not require any special extensions
* installed.
*
* @uses \Zend\Http\Client\Client
* @uses \Zend\Http\Client\Adapter\Exception
* @uses \Zend\Http\Client\Adapter\Socket
* @uses \Zend\Http\Response
* @uses \Zend\Uri\Url
* @category Zend
* @package Zend_Http
* @subpackage Client_Adapter
Expand Down Expand Up @@ -107,7 +102,7 @@ public function connect($host, $port = 80, $secure = false)
* Send request to the proxy server
*
* @param string $method
* @param \Zend\Uri\Url $uri
* @param \Zend\Uri\Uri $uri
* @param string $http_ver
* @param array $headers
* @param string $body
Expand All @@ -132,7 +127,7 @@ public function write($method, $uri, $http_ver = '1.1', $headers = array(), $bod

// Add Proxy-Authorization header
if ($this->config['proxy_user'] && ! isset($headers['proxy-authorization'])) {
$headers['proxy-authorization'] = Client\Client::encodeAuthHeader(
$headers['proxy-authorization'] = Client::encodeAuthHeader(
$this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth']
);
}
Expand Down
9 changes: 1 addition & 8 deletions src/Client/Adapter/Socket.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* Zend Framework
*
Expand Down Expand Up @@ -32,12 +31,6 @@
* A sockets based (stream\socket\client) adapter class for Zend\Http\Client. Can be used
* on almost every PHP environment, and does not require any special extensions.
*
* @uses Zend\Http\Client
* @uses Zend\Http\Client\Adapter\Exception
* @uses Zend\Http\Client\Adapter
* @uses Zend\Http\Client\Adapter\Stream
* @uses Zend\Http\Response
* @uses Zend\Uri\Url
* @category Zend
* @package Zend_Http
* @subpackage Client_Adapter
Expand Down Expand Up @@ -245,7 +238,7 @@ public function connect($host, $port = 80, $secure = false)
* Send request to the remote server
*
* @param string $method
* @param \Zend\Uri\Url $uri
* @param \Zend\Uri\Uri $uri
* @param string $http_ver
* @param array $headers
* @param string $body
Expand Down
6 changes: 1 addition & 5 deletions src/Client/Adapter/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
* object manually, and then set it as the client's adapter. Then, you can
* set the expected response using the setResponse() method.
*
* @uses \Zend\Http\Client\Adapter\Exception
* @uses \Zend\Http\Client\Adapter
* @uses \Zend\Http\Response
* @uses \Zend\Uri\Url
* @category Zend
* @package Zend_Http
* @subpackage Client_Adapter
Expand Down Expand Up @@ -139,7 +135,7 @@ public function connect($host, $port = 80, $secure = false)
* Send request to the remote server
*
* @param string $method
* @param \Zend\Uri\Url $uri
* @param \Zend\Uri\Uri $uri
* @param string $http_ver
* @param array $headers
* @param string $body
Expand Down
40 changes: 24 additions & 16 deletions src/Cookie.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* Zend Framework
*
Expand All @@ -24,6 +23,7 @@
* @namespace
*/
namespace Zend\Http;

use Zend\Uri;

/**
Expand All @@ -38,9 +38,6 @@
*
* See http://wp.netscape.com/newsref/std/cookie_spec.html for some specs.
*
* @uses \Zend\Date\Date
* @uses \Zend\Http\Exception
* @uses \Zend\Uri\Url
* @category Zend
* @package Zend_Http
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
Expand Down Expand Up @@ -220,26 +217,37 @@ public function isSessionCookie()
/**
* 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 string|Uri\Uri $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);
$uri = Uri\UriFactory::factory($uri, 'http');
}

if (!$uri instanceof Uri\Uri) {
throw new Exception\InvalidArgumentException('Invalid URI provided; does not implement Zend\Uri\Uri');
}

// Make sure we have a valid Zend_Uri_Http object
if (! ($uri->isValid() && ($uri->getScheme() == 'http' || $uri->getScheme() =='https'))) {
$scheme = $uri->getScheme();
if (! ($uri->isValid() && ($scheme == 'http' || $scheme =='https'))) {
throw new Exception\InvalidArgumentException('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;
if ($this->secure && $scheme != '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())) {
Expand Down Expand Up @@ -274,16 +282,16 @@ public function __toString()
* (for example the value of the Set-Cookie HTTP header)
*
* @param string $cookieStr
* @param \Zend\Uri\Url|string $refUri Reference URI for default values (domain, path)
* @param Uri\Uri|string $refUri Reference URI for default values (domain, path)
* @param boolean $encodeValue Weither or not the cookie's value should be
* passed through urlencode/urldecode
* @return \Zend\Http\Cookie A new \Zend\Http\Cookie object or false on failure.
* @return Cookie A new Cookie object or false on failure.
*/
public static function fromString($cookieStr, $refUri = null, $encodeValue = true)
{
// Set default values
if (is_string($refUri)) {
$refUri = new Uri\Url($refUri);
$refUri = Uri\UriFactory::factory($refUri, 'http');
}

$name = '';
Expand All @@ -305,10 +313,10 @@ public static function fromString($cookieStr, $refUri = null, $encodeValue = tru
}

// Set default domain and path
if ($refUri instanceof Uri\Url) {
if ($refUri instanceof Uri\Uri) {
$domain = $refUri->getHost();
$path = $refUri->getPath();
$path = substr($path, 0, strrpos($path, '/'));
$path = $refUri->getPath();
$path = substr($path, 0, strrpos($path, '/'));
}

// Set other cookie parameters
Expand Down
Loading

0 comments on commit f548f30

Please sign in to comment.