import v1.1.0_RC2 | 2009-09-20
This commit is contained in:
@ -16,8 +16,8 @@
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client
|
||||
* @version $Id: Client.php 15577 2009-05-14 12:43:34Z matthew $
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @version $Id: Client.php 17374 2009-08-04 12:43:04Z shahar $
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
@ -55,7 +55,7 @@ require_once 'Zend/Http/Response.php';
|
||||
* @package Zend_Http
|
||||
* @subpackage Client
|
||||
* @throws Zend_Http_Client_Exception
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Http_Client
|
||||
@ -223,14 +223,14 @@ class Zend_Http_Client
|
||||
|
||||
/**
|
||||
* Fileinfo magic database resource
|
||||
*
|
||||
*
|
||||
* This varaiable is populated the first time _detectFileMimeType is called
|
||||
* and is then reused on every call to this method
|
||||
*
|
||||
* @var resource
|
||||
*/
|
||||
static protected $_fileInfoDb = null;
|
||||
|
||||
|
||||
/**
|
||||
* Contructor method. Will create a new HTTP client. Accepts the target
|
||||
* URL and optionally configuration array.
|
||||
@ -295,26 +295,30 @@ class Zend_Http_Client
|
||||
/**
|
||||
* Set configuration parameters for this HTTP client
|
||||
*
|
||||
* @param array $config
|
||||
* @param Zend_Config | array $config
|
||||
* @return Zend_Http_Client
|
||||
* @throws Zend_Http_Client_Exception
|
||||
*/
|
||||
public function setConfig($config = array())
|
||||
{
|
||||
if (! is_array($config)) {
|
||||
if ($config instanceof Zend_Config) {
|
||||
$config = $config->toArray();
|
||||
|
||||
} elseif (! is_array($config)) {
|
||||
/** @see Zend_Http_Client_Exception */
|
||||
require_once 'Zend/Http/Client/Exception.php';
|
||||
throw new Zend_Http_Client_Exception('Expected array parameter, given ' . gettype($config));
|
||||
throw new Zend_Http_Client_Exception('Array or Zend_Config object expected, got ' . gettype($config));
|
||||
}
|
||||
|
||||
foreach ($config as $k => $v)
|
||||
foreach ($config as $k => $v) {
|
||||
$this->config[strtolower($k)] = $v;
|
||||
}
|
||||
|
||||
// Pass configuration options to the adapter if it exists
|
||||
if ($this->adapter instanceof Zend_Http_Client_Adapter_Interface) {
|
||||
$this->adapter->setConfig($config);
|
||||
}
|
||||
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -331,15 +335,15 @@ class Zend_Http_Client
|
||||
*/
|
||||
public function setMethod($method = self::GET)
|
||||
{
|
||||
$regex = '/^[^\x00-\x1f\x7f-\xff\(\)<>@,;:\\\\"\/\[\]\?={}\s]+$/';
|
||||
if (! preg_match($regex, $method)) {
|
||||
if (! preg_match('/^[^\x00-\x1f\x7f-\xff\(\)<>@,;:\\\\"\/\[\]\?={}\s]+$/', $method)) {
|
||||
/** @see Zend_Http_Client_Exception */
|
||||
require_once 'Zend/Http/Client/Exception.php';
|
||||
throw new Zend_Http_Client_Exception("'{$method}' is not a valid HTTP request method.");
|
||||
}
|
||||
|
||||
if ($method == self::POST && $this->enctype === null)
|
||||
if ($method == self::POST && $this->enctype === null) {
|
||||
$this->setEncType(self::ENC_URLENCODED);
|
||||
}
|
||||
|
||||
$this->method = $method;
|
||||
|
||||
@ -388,7 +392,7 @@ class Zend_Http_Client
|
||||
require_once 'Zend/Http/Client/Exception.php';
|
||||
throw new Zend_Http_Client_Exception("{$name} is not a valid HTTP header name");
|
||||
}
|
||||
|
||||
|
||||
$normalized_name = strtolower($name);
|
||||
|
||||
// If $value is null or false, unset the header
|
||||
@ -397,7 +401,7 @@ class Zend_Http_Client
|
||||
|
||||
// Else, set the header
|
||||
} else {
|
||||
// Header names are storred lowercase internally.
|
||||
// Header names are stored lowercase internally.
|
||||
if (is_string($value)) {
|
||||
$value = trim($value);
|
||||
}
|
||||
@ -691,7 +695,12 @@ class Zend_Http_Client
|
||||
// Force enctype to multipart/form-data
|
||||
$this->setEncType(self::ENC_FORMDATA);
|
||||
|
||||
$this->files[$formname] = array(basename($filename), $ctype, $data);
|
||||
$this->files[] = array(
|
||||
'formname' => $formname,
|
||||
'filename' => basename($filename),
|
||||
'ctype' => $ctype,
|
||||
'data' => $data
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -975,14 +984,14 @@ class Zend_Http_Client
|
||||
$headers[] = 'Accept-encoding: identity';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Set the Content-Type header
|
||||
if ($this->method == self::POST &&
|
||||
(! isset($this->headers[strtolower(self::CONTENT_TYPE)]) && isset($this->enctype))) {
|
||||
|
||||
$headers[] = self::CONTENT_TYPE . ': ' . $this->enctype;
|
||||
}
|
||||
|
||||
|
||||
// Set the user agent header
|
||||
if (! isset($this->headers['user-agent']) && isset($this->config['useragent'])) {
|
||||
$headers[] = "User-Agent: {$this->config['useragent']}";
|
||||
@ -1030,9 +1039,22 @@ class Zend_Http_Client
|
||||
return '';
|
||||
}
|
||||
|
||||
// If mbstring overloads substr and strlen functions, we have to
|
||||
// override it's internal encoding
|
||||
if (function_exists('mb_internal_encoding') &&
|
||||
((int) ini_get('mbstring.func_overload')) & 2) {
|
||||
|
||||
$mbIntEnc = mb_internal_encoding();
|
||||
mb_internal_encoding('ASCII');
|
||||
}
|
||||
|
||||
// If we have raw_post_data set, just use it as the body.
|
||||
if (isset($this->raw_post_data)) {
|
||||
$this->setHeaders(self::CONTENT_LENGTH, strlen($this->raw_post_data));
|
||||
if (isset($mbIntEnc)) {
|
||||
mb_internal_encoding($mbIntEnc);
|
||||
}
|
||||
|
||||
return $this->raw_post_data;
|
||||
}
|
||||
|
||||
@ -1052,15 +1074,15 @@ class Zend_Http_Client
|
||||
$this->setHeaders(self::CONTENT_TYPE, self::ENC_FORMDATA . "; boundary={$boundary}");
|
||||
|
||||
// Get POST parameters and encode them
|
||||
$params = $this->_getParametersRecursive($this->paramsPost);
|
||||
$params = self::_flattenParametersArray($this->paramsPost);
|
||||
foreach ($params as $pp) {
|
||||
$body .= self::encodeFormData($boundary, $pp[0], $pp[1]);
|
||||
}
|
||||
|
||||
// Encode files
|
||||
foreach ($this->files as $name => $file) {
|
||||
$fhead = array(self::CONTENT_TYPE => $file[1]);
|
||||
$body .= self::encodeFormData($boundary, $name, $file[2], $file[0], $fhead);
|
||||
foreach ($this->files as $file) {
|
||||
$fhead = array(self::CONTENT_TYPE => $file['ctype']);
|
||||
$body .= self::encodeFormData($boundary, $file['formname'], $file['data'], $file['filename'], $fhead);
|
||||
}
|
||||
|
||||
$body .= "--{$boundary}--\r\n";
|
||||
@ -1073,6 +1095,10 @@ class Zend_Http_Client
|
||||
break;
|
||||
|
||||
default:
|
||||
if (isset($mbIntEnc)) {
|
||||
mb_internal_encoding($mbIntEnc);
|
||||
}
|
||||
|
||||
/** @see Zend_Http_Client_Exception */
|
||||
require_once 'Zend/Http/Client/Exception.php';
|
||||
throw new Zend_Http_Client_Exception("Cannot handle content type '{$this->enctype}' automatically." .
|
||||
@ -1080,12 +1106,16 @@ class Zend_Http_Client
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Set the Content-Length if we have a body or if request is POST/PUT
|
||||
if ($body || $this->method == self::POST || $this->method == self::PUT) {
|
||||
$this->setHeaders(self::CONTENT_LENGTH, strlen($body));
|
||||
}
|
||||
|
||||
if (isset($mbIntEnc)) {
|
||||
mb_internal_encoding($mbIntEnc);
|
||||
}
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
@ -1097,12 +1127,21 @@ class Zend_Http_Client
|
||||
* necessarily unique. If one of the parameters in as array, it will also
|
||||
* add a [] suffix to the key.
|
||||
*
|
||||
* @param array $parray The parameters array
|
||||
* @param bool $urlencode Whether to urlencode the name and value
|
||||
* This method is deprecated since Zend Framework 1.9 in favour of
|
||||
* self::_flattenParametersArray() and will be dropped in 2.0
|
||||
*
|
||||
* @deprecated since 1.9
|
||||
*
|
||||
* @param array $parray The parameters array
|
||||
* @param bool $urlencode Whether to urlencode the name and value
|
||||
* @return array
|
||||
*/
|
||||
protected function _getParametersRecursive($parray, $urlencode = false)
|
||||
{
|
||||
// Issue a deprecated notice
|
||||
trigger_error("The " . __METHOD__ . " method is deprecated and will be dropped in 2.0.",
|
||||
E_USER_NOTICE);
|
||||
|
||||
if (! is_array($parray)) {
|
||||
return $parray;
|
||||
}
|
||||
@ -1132,15 +1171,15 @@ class Zend_Http_Client
|
||||
|
||||
return $parameters;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Attempt to detect the MIME type of a file using available extensions
|
||||
*
|
||||
*
|
||||
* This method will try to detect the MIME type of a file. If the fileinfo
|
||||
* extension is available, it will be used. If not, the mime_magic
|
||||
* extension is available, it will be used. If not, the mime_magic
|
||||
* extension which is deprected but is still available in many PHP setups
|
||||
* will be tried.
|
||||
*
|
||||
* will be tried.
|
||||
*
|
||||
* If neither extension is available, the default application/octet-stream
|
||||
* MIME type will be returned
|
||||
*
|
||||
@ -1150,26 +1189,26 @@ class Zend_Http_Client
|
||||
protected function _detectFileMimeType($file)
|
||||
{
|
||||
$type = null;
|
||||
|
||||
|
||||
// First try with fileinfo functions
|
||||
if (function_exists('finfo_open')) {
|
||||
if (self::$_fileInfoDb === null) {
|
||||
self::$_fileInfoDb = @finfo_open(FILEINFO_MIME);
|
||||
}
|
||||
|
||||
if (self::$_fileInfoDb) {
|
||||
|
||||
if (self::$_fileInfoDb) {
|
||||
$type = finfo_file(self::$_fileInfoDb, $file);
|
||||
}
|
||||
|
||||
|
||||
} elseif (function_exists('mime_content_type')) {
|
||||
$type = mime_content_type($file);
|
||||
}
|
||||
|
||||
|
||||
// Fallback to the default application/octet-stream
|
||||
if (! $type) {
|
||||
$type = 'application/octet-stream';
|
||||
}
|
||||
|
||||
|
||||
return $type;
|
||||
}
|
||||
|
||||
@ -1243,4 +1282,51 @@ class Zend_Http_Client
|
||||
|
||||
return $authHeader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an array of parameters into a flat array of (key, value) pairs
|
||||
*
|
||||
* Will flatten a potentially multi-dimentional array of parameters (such
|
||||
* as POST parameters) into a flat array of (key, value) paris. In case
|
||||
* of multi-dimentional arrays, square brackets ([]) will be added to the
|
||||
* key to indicate an array.
|
||||
*
|
||||
* @since 1.9
|
||||
*
|
||||
* @param array $parray
|
||||
* @param string $prefix
|
||||
* @return array
|
||||
*/
|
||||
static protected function _flattenParametersArray($parray, $prefix = null)
|
||||
{
|
||||
if (! is_array($parray)) {
|
||||
return $parray;
|
||||
}
|
||||
|
||||
$parameters = array();
|
||||
|
||||
foreach($parray as $name => $value) {
|
||||
|
||||
// Calculate array key
|
||||
if ($prefix) {
|
||||
if (is_int($name)) {
|
||||
$key = $prefix . '[]';
|
||||
} else {
|
||||
$key = $prefix . "[$name]";
|
||||
}
|
||||
} else {
|
||||
$key = $name;
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
$parameters = array_merge($parameters, self::_flattenParametersArray($value, $key));
|
||||
|
||||
} else {
|
||||
$parameters[] = array($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $parameters;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,8 +16,8 @@
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @version $Id: Curl.php 14379 2009-03-19 14:57:23Z matthew $
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @version $Id: Curl.php 17118 2009-07-26 09:41:41Z shahar $
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
@ -34,7 +34,7 @@ require_once 'Zend/Http/Client/Adapter/Interface.php';
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Http_Client_Adapter_Curl implements Zend_Http_Client_Adapter_Interface
|
||||
@ -62,7 +62,7 @@ class Zend_Http_Client_Adapter_Curl implements Zend_Http_Client_Adapter_Interfac
|
||||
|
||||
/**
|
||||
* List of cURL options that should never be overwritten
|
||||
*
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_invalidOverwritableCurlOptions = array(
|
||||
@ -110,18 +110,39 @@ class Zend_Http_Client_Adapter_Curl implements Zend_Http_Client_Adapter_Interfac
|
||||
* Set the configuration array for the adapter
|
||||
*
|
||||
* @throws Zend_Http_Client_Adapter_Exception
|
||||
* @param array $config
|
||||
* @param Zend_Config | array $config
|
||||
* @return Zend_Http_Client_Adapter_Curl
|
||||
*/
|
||||
public function setConfig($config = array())
|
||||
{
|
||||
if (!is_array($config)) {
|
||||
if ($config instanceof Zend_Config) {
|
||||
$config = $config->toArray();
|
||||
|
||||
} elseif (! is_array($config)) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception('Http Adapter configuration expects an array, ' . gettype($config) . ' recieved.');
|
||||
throw new Zend_Http_Client_Adapter_Exception(
|
||||
'Array or Zend_Config object expected, got ' . gettype($config)
|
||||
);
|
||||
}
|
||||
|
||||
if(isset($config['proxy_user']) && isset($config['proxy_pass'])) {
|
||||
$this->setCurlOption(CURLOPT_PROXYUSERPWD, $config['proxy_user'].":".$config['proxy_pass']);
|
||||
unset($config['proxy_user'], $config['proxy_pass']);
|
||||
}
|
||||
|
||||
foreach ($config as $k => $v) {
|
||||
$this->_config[strtolower($k)] = $v;
|
||||
$option = strtolower($k);
|
||||
switch($option) {
|
||||
case 'proxy_host':
|
||||
$this->setCurlOption(CURLOPT_PROXY, $v);
|
||||
break;
|
||||
case 'proxy_port':
|
||||
$this->setCurlOption(CURLOPT_PROXYPORT, $v);
|
||||
break;
|
||||
default:
|
||||
$this->_config[$option] = $v;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
@ -129,7 +150,7 @@ class Zend_Http_Client_Adapter_Curl implements Zend_Http_Client_Adapter_Interfac
|
||||
|
||||
/**
|
||||
* Direct setter for cURL adapter related options.
|
||||
*
|
||||
*
|
||||
* @param string|int $option
|
||||
* @param mixed $value
|
||||
* @return Zend_Http_Adapter_Curl
|
||||
@ -160,9 +181,9 @@ class Zend_Http_Client_Adapter_Curl implements Zend_Http_Client_Adapter_Interfac
|
||||
}
|
||||
|
||||
// If we are connected to a different server or port, disconnect first
|
||||
if ($this->_curl
|
||||
&& is_array($this->_connected_to)
|
||||
&& ($this->_connected_to[0] != $host
|
||||
if ($this->_curl
|
||||
&& is_array($this->_connected_to)
|
||||
&& ($this->_connected_to[0] != $host
|
||||
|| $this->_connected_to[1] != $port)
|
||||
) {
|
||||
$this->close();
|
||||
@ -389,4 +410,14 @@ class Zend_Http_Client_Adapter_Curl implements Zend_Http_Client_Adapter_Interfac
|
||||
$this->_curl = null;
|
||||
$this->_connected_to = array(null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cUrl Handle
|
||||
*
|
||||
* @return resource
|
||||
*/
|
||||
public function getHandle()
|
||||
{
|
||||
return $this->_curl;
|
||||
}
|
||||
}
|
||||
|
@ -15,19 +15,24 @@
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter_Exception
|
||||
* @version $Id: Exception.php 8064 2008-02-16 10:58:39Z thomas $
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @version $Id: Exception.php 17118 2009-07-26 09:41:41Z shahar $
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Http_Client_Exception
|
||||
*/
|
||||
require_once 'Zend/Http/Client/Exception.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Http_Client_Adapter_Exception extends Zend_Http_Client_Exception
|
||||
{}
|
||||
{
|
||||
const READ_TIMEOUT = 1000;
|
||||
}
|
||||
|
@ -16,8 +16,8 @@
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @version $Id: Interface.php 8064 2008-02-16 10:58:39Z thomas $
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @version $Id: Interface.php 16214 2009-06-21 19:34:03Z thomas $
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
@ -30,7 +30,7 @@
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
interface Zend_Http_Client_Adapter_Interface
|
||||
|
@ -16,13 +16,22 @@
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @version $Id: Proxy.php 12504 2008-11-10 16:28:46Z matthew $
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @version $Id: Proxy.php 17118 2009-07-26 09:41:41Z shahar $
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Uri_Http
|
||||
*/
|
||||
require_once 'Zend/Uri/Http.php';
|
||||
/**
|
||||
* @see Zend_Http_Client
|
||||
*/
|
||||
require_once 'Zend/Http/Client.php';
|
||||
/**
|
||||
* @see Zend_Http_Client_Adapter_Socket
|
||||
*/
|
||||
require_once 'Zend/Http/Client/Adapter/Socket.php';
|
||||
|
||||
/**
|
||||
@ -37,7 +46,7 @@ require_once 'Zend/Http/Client/Adapter/Socket.php';
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Http_Client_Adapter_Proxy extends Zend_Http_Client_Adapter_Socket
|
||||
@ -49,6 +58,8 @@ class Zend_Http_Client_Adapter_Proxy extends Zend_Http_Client_Adapter_Socket
|
||||
*/
|
||||
protected $config = array(
|
||||
'ssltransport' => 'ssl',
|
||||
'sslcert' => null,
|
||||
'sslpassphrase' => null,
|
||||
'proxy_host' => '',
|
||||
'proxy_port' => 8080,
|
||||
'proxy_user' => '',
|
||||
@ -63,7 +74,7 @@ class Zend_Http_Client_Adapter_Proxy extends Zend_Http_Client_Adapter_Socket
|
||||
* @var boolean
|
||||
*/
|
||||
protected $negotiated = false;
|
||||
|
||||
|
||||
/**
|
||||
* Connect to the remote server
|
||||
*
|
||||
@ -73,41 +84,20 @@ class Zend_Http_Client_Adapter_Proxy extends Zend_Http_Client_Adapter_Socket
|
||||
* @param string $host
|
||||
* @param int $port
|
||||
* @param boolean $secure
|
||||
* @param int $timeout
|
||||
*/
|
||||
public function connect($host, $port = 80, $secure = false)
|
||||
{
|
||||
// If no proxy is set, fall back to Socket adapter
|
||||
if (! $this->config['proxy_host']) return parent::connect($host, $port, $secure);
|
||||
|
||||
// Go through a proxy - the connection is actually to the proxy server
|
||||
$host = $this->config['proxy_host'];
|
||||
$port = $this->config['proxy_port'];
|
||||
|
||||
// If we are connected to the wrong proxy, disconnect first
|
||||
if (($this->connected_to[0] != $host || $this->connected_to[1] != $port)) {
|
||||
if (is_resource($this->socket)) $this->close();
|
||||
if (! $this->config['proxy_host']) {
|
||||
return parent::connect($host, $port, $secure);
|
||||
}
|
||||
|
||||
// Now, if we are not connected, connect
|
||||
if (! is_resource($this->socket) || ! $this->config['keepalive']) {
|
||||
$this->socket = @fsockopen($host, $port, $errno, $errstr, (int) $this->config['timeout']);
|
||||
if (! $this->socket) {
|
||||
$this->close();
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception(
|
||||
'Unable to Connect to proxy server ' . $host . ':' . $port . '. Error #' . $errno . ': ' . $errstr);
|
||||
}
|
||||
|
||||
// Set the stream timeout
|
||||
if (!stream_set_timeout($this->socket, (int) $this->config['timeout'])) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception('Unable to set the connection timeout');
|
||||
}
|
||||
|
||||
// Update connected_to
|
||||
$this->connected_to = array($host, $port);
|
||||
}
|
||||
// Connect (a non-secure connection) to the proxy server
|
||||
return parent::connect(
|
||||
$this->config['proxy_host'],
|
||||
$this->config['proxy_port'],
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -134,7 +124,7 @@ class Zend_Http_Client_Adapter_Proxy extends Zend_Http_Client_Adapter_Socket
|
||||
$host = $this->config['proxy_host'];
|
||||
$port = $this->config['proxy_port'];
|
||||
|
||||
if ($this->connected_to[0] != $host || $this->connected_to[1] != $port) {
|
||||
if ($this->connected_to[0] != "tcp://$host" || $this->connected_to[1] != $port) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are connected to the wrong proxy server");
|
||||
}
|
||||
@ -145,18 +135,26 @@ class Zend_Http_Client_Adapter_Proxy extends Zend_Http_Client_Adapter_Socket
|
||||
$this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth']
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// if we are proxying HTTPS, preform CONNECT handshake with the proxy
|
||||
if ($uri->getScheme() == 'https' && (! $this->negotiated)) {
|
||||
$this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers);
|
||||
$this->negotiated = true;
|
||||
}
|
||||
|
||||
|
||||
// Save request method for later
|
||||
$this->method = $method;
|
||||
|
||||
// Build request headers
|
||||
$request = "{$method} {$uri->__toString()} HTTP/{$http_ver}\r\n";
|
||||
if ($this->negotiated) {
|
||||
$path = $uri->getPath();
|
||||
if ($uri->getQuery()) {
|
||||
$path .= '?' . $uri->getQuery();
|
||||
}
|
||||
$request = "$method $path HTTP/$http_ver\r\n";
|
||||
} else {
|
||||
$request = "$method $uri HTTP/$http_ver\r\n";
|
||||
}
|
||||
|
||||
// Add all headers to the request string
|
||||
foreach ($headers as $k => $v) {
|
||||
@ -186,21 +184,21 @@ class Zend_Http_Client_Adapter_Proxy extends Zend_Http_Client_Adapter_Socket
|
||||
*/
|
||||
protected function connectHandshake($host, $port = 443, $http_ver = '1.1', array &$headers = array())
|
||||
{
|
||||
$request = "CONNECT $host:$port HTTP/$http_ver\r\n" .
|
||||
$request = "CONNECT $host:$port HTTP/$http_ver\r\n" .
|
||||
"Host: " . $this->config['proxy_host'] . "\r\n";
|
||||
|
||||
// Add the user-agent header
|
||||
if (isset($this->config['useragent'])) {
|
||||
$request .= "User-agent: " . $this->config['useragent'] . "\r\n";
|
||||
}
|
||||
|
||||
|
||||
// If the proxy-authorization header is set, send it to proxy but remove
|
||||
// it from headers sent to target host
|
||||
if (isset($headers['proxy-authorization'])) {
|
||||
$request .= "Proxy-authorization: " . $headers['proxy-authorization'] . "\r\n";
|
||||
unset($headers['proxy-authorization']);
|
||||
}
|
||||
|
||||
|
||||
$request .= "\r\n";
|
||||
|
||||
// Send the request
|
||||
@ -219,35 +217,35 @@ class Zend_Http_Client_Adapter_Proxy extends Zend_Http_Client_Adapter_Socket
|
||||
if (!chop($line)) break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Check that the response from the proxy is 200
|
||||
if (Zend_Http_Response::extractCode($response) != 200) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception("Unable to connect to HTTPS proxy. Server response: " . $response);
|
||||
}
|
||||
|
||||
|
||||
// If all is good, switch socket to secure mode. We have to fall back
|
||||
// through the different modes
|
||||
// through the different modes
|
||||
$modes = array(
|
||||
STREAM_CRYPTO_METHOD_TLS_CLIENT,
|
||||
STREAM_CRYPTO_METHOD_TLS_CLIENT,
|
||||
STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
|
||||
STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
|
||||
STREAM_CRYPTO_METHOD_SSLv2_CLIENT
|
||||
STREAM_CRYPTO_METHOD_SSLv2_CLIENT
|
||||
);
|
||||
|
||||
$success = false;
|
||||
|
||||
$success = false;
|
||||
foreach($modes as $mode) {
|
||||
$success = stream_socket_enable_crypto($this->socket, true, $mode);
|
||||
if ($success) break;
|
||||
}
|
||||
|
||||
|
||||
if (! $success) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception("Unable to connect to" .
|
||||
throw new Zend_Http_Client_Adapter_Exception("Unable to connect to" .
|
||||
" HTTPS server through proxy: could not negotiate secure connection.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Close the connection to the server
|
||||
*
|
||||
@ -257,7 +255,7 @@ class Zend_Http_Client_Adapter_Proxy extends Zend_Http_Client_Adapter_Socket
|
||||
parent::close();
|
||||
$this->negotiated = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Destructor: make sure the socket is disconnected
|
||||
*
|
||||
|
@ -16,12 +16,18 @@
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @version $Id: Socket.php 13013 2008-12-04 12:04:24Z yoshida@zend.co.jp $
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @version $Id: Socket.php 17124 2009-07-26 09:46:42Z shahar $
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Uri_Http
|
||||
*/
|
||||
require_once 'Zend/Uri/Http.php';
|
||||
/**
|
||||
* @see Zend_Http_Client_Adapter_Interface
|
||||
*/
|
||||
require_once 'Zend/Http/Client/Adapter/Interface.php';
|
||||
|
||||
/**
|
||||
@ -31,7 +37,7 @@ require_once 'Zend/Http/Client/Adapter/Interface.php';
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interface
|
||||
@ -69,6 +75,13 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
|
||||
*/
|
||||
protected $method = null;
|
||||
|
||||
/**
|
||||
* Stream context
|
||||
*
|
||||
* @var resource
|
||||
*/
|
||||
protected $_context = null;
|
||||
|
||||
/**
|
||||
* Adapter constructor, currently empty. Config is set using setConfig()
|
||||
*
|
||||
@ -80,14 +93,18 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
|
||||
/**
|
||||
* Set the configuration array for the adapter
|
||||
*
|
||||
* @param array $config
|
||||
* @param Zend_Config | array $config
|
||||
*/
|
||||
public function setConfig($config = array())
|
||||
{
|
||||
if (! is_array($config)) {
|
||||
if ($config instanceof Zend_Config) {
|
||||
$config = $config->toArray();
|
||||
|
||||
} elseif (! is_array($config)) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception(
|
||||
'$concig expects an array, ' . gettype($config) . ' recieved.');
|
||||
'Array or Zend_Config object expected, got ' . gettype($config)
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($config as $k => $v) {
|
||||
@ -95,13 +112,60 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the stream context for the TCP connection to the server
|
||||
*
|
||||
* Can accept either a pre-existing stream context resource, or an array
|
||||
* of stream options, similar to the options array passed to the
|
||||
* stream_context_create() PHP function. In such case a new stream context
|
||||
* will be created using the passed options.
|
||||
*
|
||||
* @since Zend Framework 1.9
|
||||
*
|
||||
* @param mixed $context Stream context or array of context options
|
||||
* @return Zend_Http_Client_Adapter_Socket
|
||||
*/
|
||||
public function setStreamContext($context)
|
||||
{
|
||||
if (is_resource($context) && get_resource_type($context) == 'stream-context') {
|
||||
$this->_context = $context;
|
||||
|
||||
} elseif (is_array($context)) {
|
||||
$this->_context = stream_context_create($context);
|
||||
|
||||
} else {
|
||||
// Invalid parameter
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception(
|
||||
"Expecting either a stream context resource or array, got " . gettype($context)
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the stream context for the TCP connection to the server.
|
||||
*
|
||||
* If no stream context is set, will create a default one.
|
||||
*
|
||||
* @return resource
|
||||
*/
|
||||
public function getStreamContext()
|
||||
{
|
||||
if (! $this->_context) {
|
||||
$this->_context = stream_context_create();
|
||||
}
|
||||
|
||||
return $this->_context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to the remote server
|
||||
*
|
||||
* @param string $host
|
||||
* @param int $port
|
||||
* @param boolean $secure
|
||||
* @param int $timeout
|
||||
*/
|
||||
public function connect($host, $port = 80, $secure = false)
|
||||
{
|
||||
@ -115,7 +179,7 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
|
||||
|
||||
// Now, if we are not connected, connect
|
||||
if (! is_resource($this->socket) || ! $this->config['keepalive']) {
|
||||
$context = stream_context_create();
|
||||
$context = $this->getStreamContext();
|
||||
if ($secure) {
|
||||
if ($this->config['sslcert'] !== null) {
|
||||
if (! stream_context_set_option($context, 'ssl', 'local_cert',
|
||||
@ -142,6 +206,7 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
|
||||
(int) $this->config['timeout'],
|
||||
$flags,
|
||||
$context);
|
||||
|
||||
if (! $this->socket) {
|
||||
$this->close();
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
@ -219,6 +284,7 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
|
||||
// First, read headers only
|
||||
$response = '';
|
||||
$gotStatus = false;
|
||||
|
||||
while (($line = @fgets($this->socket)) !== false) {
|
||||
$gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
|
||||
if ($gotStatus) {
|
||||
@ -227,26 +293,39 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
|
||||
}
|
||||
}
|
||||
|
||||
$this->_checkSocketReadTimeout();
|
||||
|
||||
$statusCode = Zend_Http_Response::extractCode($response);
|
||||
|
||||
// Handle 100 and 101 responses internally by restarting the read again
|
||||
if ($statusCode == 100 || $statusCode == 101) return $this->read();
|
||||
|
||||
// Check headers to see what kind of connection / transfer encoding we have
|
||||
$headers = Zend_Http_Response::extractHeaders($response);
|
||||
|
||||
/**
|
||||
* Responses to HEAD requests and 204 or 304 responses are not expected
|
||||
* to have a body - stop reading here
|
||||
*/
|
||||
if ($statusCode == 304 || $statusCode == 204 ||
|
||||
$this->method == Zend_Http_Client::HEAD) return $response;
|
||||
$this->method == Zend_Http_Client::HEAD) {
|
||||
|
||||
// Check headers to see what kind of connection / transfer encoding we have
|
||||
$headers = Zend_Http_Response::extractHeaders($response);
|
||||
// Close the connection if requested to do so by the server
|
||||
if (isset($headers['connection']) && $headers['connection'] == 'close') {
|
||||
$this->close();
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
// If we got a 'transfer-encoding: chunked' header
|
||||
if (isset($headers['transfer-encoding'])) {
|
||||
if ($headers['transfer-encoding'] == 'chunked') {
|
||||
|
||||
if (strtolower($headers['transfer-encoding']) == 'chunked') {
|
||||
|
||||
do {
|
||||
$line = @fgets($this->socket);
|
||||
$this->_checkSocketReadTimeout();
|
||||
|
||||
$chunk = $line;
|
||||
|
||||
// Figure out the next chunk size
|
||||
@ -261,60 +340,70 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
|
||||
// Convert the hexadecimal value to plain integer
|
||||
$chunksize = hexdec($chunksize);
|
||||
|
||||
// Read chunk
|
||||
$left_to_read = $chunksize;
|
||||
while ($left_to_read > 0) {
|
||||
$line = @fread($this->socket, $left_to_read);
|
||||
if ($line === false || strlen($line) === 0)
|
||||
{
|
||||
// Read next chunk
|
||||
$read_to = ftell($this->socket) + $chunksize;
|
||||
|
||||
do {
|
||||
$current_pos = ftell($this->socket);
|
||||
if ($current_pos >= $read_to) break;
|
||||
|
||||
$line = @fread($this->socket, $read_to - $current_pos);
|
||||
if ($line === false || strlen($line) === 0) {
|
||||
$this->_checkSocketReadTimeout();
|
||||
break;
|
||||
} else {
|
||||
$chunk .= $line;
|
||||
$left_to_read -= strlen($line);
|
||||
}
|
||||
|
||||
// Break if the connection ended prematurely
|
||||
if (feof($this->socket)) break;
|
||||
}
|
||||
} while (! feof($this->socket));
|
||||
|
||||
$chunk .= @fgets($this->socket);
|
||||
$this->_checkSocketReadTimeout();
|
||||
|
||||
$response .= $chunk;
|
||||
} while ($chunksize > 0);
|
||||
|
||||
} else {
|
||||
$this->close();
|
||||
throw new Zend_Http_Client_Adapter_Exception('Cannot handle "' .
|
||||
$headers['transfer-encoding'] . '" transfer encoding');
|
||||
}
|
||||
|
||||
// Else, if we got the content-length header, read this number of bytes
|
||||
} elseif (isset($headers['content-length'])) {
|
||||
$left_to_read = $headers['content-length'];
|
||||
|
||||
$current_pos = ftell($this->socket);
|
||||
$chunk = '';
|
||||
while ($left_to_read > 0) {
|
||||
$chunk = @fread($this->socket, $left_to_read);
|
||||
if ($chunk === false || strlen($chunk) === 0)
|
||||
{
|
||||
|
||||
for ($read_to = $current_pos + $headers['content-length'];
|
||||
$read_to > $current_pos;
|
||||
$current_pos = ftell($this->socket)) {
|
||||
|
||||
$chunk = @fread($this->socket, $read_to - $current_pos);
|
||||
if ($chunk === false || strlen($chunk) === 0) {
|
||||
$this->_checkSocketReadTimeout();
|
||||
break;
|
||||
} else {
|
||||
$left_to_read -= strlen($chunk);
|
||||
$response .= $chunk;
|
||||
}
|
||||
|
||||
$response .= $chunk;
|
||||
|
||||
// Break if the connection ended prematurely
|
||||
if (feof($this->socket)) break;
|
||||
}
|
||||
|
||||
// Fallback: just read the response until EOF
|
||||
} else {
|
||||
do {
|
||||
$buff = @fread($this->socket, 8192);
|
||||
if ($buff === false || strlen($buff) === 0)
|
||||
{
|
||||
break;
|
||||
} else {
|
||||
|
||||
do {
|
||||
$buff = @fread($this->socket, 8192);
|
||||
if ($buff === false || strlen($buff) === 0) {
|
||||
$this->_checkSocketReadTimeout();
|
||||
break;
|
||||
} else {
|
||||
$response .= $buff;
|
||||
}
|
||||
} while (feof($this->socket) === false);
|
||||
}
|
||||
|
||||
} while (feof($this->socket) === false);
|
||||
|
||||
$this->close();
|
||||
}
|
||||
@ -338,6 +427,28 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
|
||||
$this->connected_to = array(null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the socket has timed out - if so close connection and throw
|
||||
* an exception
|
||||
*
|
||||
* @throws Zend_Http_Client_Adapter_Exception with READ_TIMEOUT code
|
||||
*/
|
||||
protected function _checkSocketReadTimeout()
|
||||
{
|
||||
if ($this->socket) {
|
||||
$info = stream_get_meta_data($this->socket);
|
||||
$timedout = $info['timed_out'];
|
||||
if ($timedout) {
|
||||
$this->close();
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception(
|
||||
"Read timed out after {$this->config['timeout']} seconds",
|
||||
Zend_Http_Client_Adapter_Exception::READ_TIMEOUT
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor: make sure the socket is disconnected
|
||||
*
|
||||
|
@ -15,13 +15,22 @@
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @version $Id: Test.php 12104 2008-10-23 22:36:28Z shahar $
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @version $Id: Test.php 17118 2009-07-26 09:41:41Z shahar $
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Uri_Http
|
||||
*/
|
||||
require_once 'Zend/Uri/Http.php';
|
||||
/**
|
||||
* @see Zend_Http_Response
|
||||
*/
|
||||
require_once 'Zend/Http/Response.php';
|
||||
/**
|
||||
* @see Zend_Http_Client_Adapter_Interface
|
||||
*/
|
||||
require_once 'Zend/Http/Client/Adapter/Interface.php';
|
||||
|
||||
/**
|
||||
@ -35,7 +44,7 @@ require_once 'Zend/Http/Client/Adapter/Interface.php';
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Http_Client_Adapter_Test implements Zend_Http_Client_Adapter_Interface
|
||||
@ -72,14 +81,18 @@ class Zend_Http_Client_Adapter_Test implements Zend_Http_Client_Adapter_Interfac
|
||||
/**
|
||||
* Set the configuration array for the adapter
|
||||
*
|
||||
* @param array $config
|
||||
* @param Zend_Config | array $config
|
||||
*/
|
||||
public function setConfig($config = array())
|
||||
{
|
||||
if (! is_array($config)) {
|
||||
if ($config instanceof Zend_Config) {
|
||||
$config = $config->toArray();
|
||||
|
||||
} elseif (! is_array($config)) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception(
|
||||
'$config expects an array, ' . gettype($config) . ' recieved.');
|
||||
'Array or Zend_Config object expected, got ' . gettype($config)
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($config as $k => $v) {
|
||||
@ -87,6 +100,7 @@ class Zend_Http_Client_Adapter_Test implements Zend_Http_Client_Adapter_Interfac
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Connect to the remote server
|
||||
*
|
||||
@ -158,7 +172,7 @@ class Zend_Http_Client_Adapter_Test implements Zend_Http_Client_Adapter_Interfac
|
||||
public function setResponse($response)
|
||||
{
|
||||
if ($response instanceof Zend_Http_Response) {
|
||||
$response = $response->asString();
|
||||
$response = $response->asString("\r\n");
|
||||
}
|
||||
|
||||
$this->responses = (array)$response;
|
||||
@ -168,10 +182,14 @@ class Zend_Http_Client_Adapter_Test implements Zend_Http_Client_Adapter_Interfac
|
||||
/**
|
||||
* Add another response to the response buffer.
|
||||
*
|
||||
* @param string $response
|
||||
* @param string Zend_Http_Response|$response
|
||||
*/
|
||||
public function addResponse($response)
|
||||
{
|
||||
if ($response instanceof Zend_Http_Response) {
|
||||
$response = $response->asString("\r\n");
|
||||
}
|
||||
|
||||
$this->responses[] = $response;
|
||||
}
|
||||
|
||||
|
@ -15,18 +15,21 @@
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Exception
|
||||
* @version $Id: Exception.php 8064 2008-02-16 10:58:39Z thomas $
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @version $Id: Exception.php 16872 2009-07-20 11:47:08Z mikaelkael $
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Http_Exception
|
||||
*/
|
||||
require_once 'Zend/Http/Exception.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Http_Client_Exception extends Zend_Http_Exception
|
||||
|
@ -16,11 +16,14 @@
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Cookie
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com/)
|
||||
* @version $Id: Cookie.php 14530 2009-03-29 14:17:14Z shahar $
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @version $Id: Cookie.php 17124 2009-07-26 09:46:42Z shahar $
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Uri_Http
|
||||
*/
|
||||
require_once 'Zend/Uri/Http.php';
|
||||
|
||||
|
||||
@ -38,7 +41,7 @@ require_once 'Zend/Uri/Http.php';
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com/)
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Http_Cookie
|
||||
@ -233,12 +236,15 @@ class Zend_Http_Cookie
|
||||
if ($this->isExpired($now)) return false;
|
||||
if ($this->isSessionCookie() && ! $matchSessionCookies) return false;
|
||||
|
||||
// Validate domain and path
|
||||
// Domain is validated using tail match, while path is validated using head match
|
||||
$domain_preg = preg_quote($this->getDomain(), "/");
|
||||
if (! preg_match("/{$domain_preg}$/", $uri->getHost())) return false;
|
||||
$path_preg = preg_quote($this->getPath(), "/");
|
||||
if (! preg_match("/^{$path_preg}/", $uri->getPath())) 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;
|
||||
@ -311,24 +317,24 @@ class Zend_Http_Cookie
|
||||
* The expiration is past Tue, 19 Jan 2038 03:14:07 UTC
|
||||
* the maximum for 32-bit signed integer. Zend_Date
|
||||
* can get around that limit.
|
||||
*
|
||||
*
|
||||
* @see Zend_Date
|
||||
*/
|
||||
require_once 'Zend/Date.php';
|
||||
|
||||
|
||||
$expireDate = new Zend_Date($v);
|
||||
$expires = $expireDate->getTimestamp();
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case 'path':
|
||||
$path = $v;
|
||||
break;
|
||||
|
||||
|
||||
case 'domain':
|
||||
$domain = $v;
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -341,4 +347,62 @@ class Zend_Http_Cookie
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a cookie's domain matches a host name.
|
||||
*
|
||||
* Used by Zend_Http_Cookie and Zend_Http_CookieJar for cookie matching
|
||||
*
|
||||
* @param string $cookieDomain
|
||||
* @param string $host
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function matchCookieDomain($cookieDomain, $host)
|
||||
{
|
||||
if (! $cookieDomain) {
|
||||
require_once 'Zend/Http/Exception.php';
|
||||
throw new Zend_Http_Exception("\$cookieDomain is expected to be a cookie domain");
|
||||
}
|
||||
|
||||
if (! $host) {
|
||||
require_once 'Zend/Http/Exception.php';
|
||||
throw new Zend_Http_Exception("\$host is expected to be a host name");
|
||||
}
|
||||
|
||||
$cookieDomain = strtolower($cookieDomain);
|
||||
$host = strtolower($host);
|
||||
|
||||
if ($cookieDomain[0] == '.') {
|
||||
$cookieDomain = substr($cookieDomain, 1);
|
||||
}
|
||||
|
||||
// Check for either exact match or suffix match
|
||||
return ($cookieDomain == $host ||
|
||||
preg_match("/\.$cookieDomain$/", $host));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a cookie's path matches a URL path
|
||||
*
|
||||
* Used by Zend_Http_Cookie and Zend_Http_CookieJar for cookie matching
|
||||
*
|
||||
* @param string $cookiePath
|
||||
* @param string $path
|
||||
* @return boolean
|
||||
*/
|
||||
public static function matchCookiePath($cookiePath, $path)
|
||||
{
|
||||
if (! $cookiePath) {
|
||||
require_once 'Zend/Http/Exception.php';
|
||||
throw new Zend_Http_Exception("\$cookiePath is expected to be a cookie path");
|
||||
}
|
||||
|
||||
if (! $path) {
|
||||
require_once 'Zend/Http/Exception.php';
|
||||
throw new Zend_Http_Exception("\$path is expected to be a host name");
|
||||
}
|
||||
|
||||
return (strpos($path, $cookiePath) === 0);
|
||||
}
|
||||
}
|
||||
|
@ -15,13 +15,22 @@
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage CookieJar
|
||||
* @version $Id: CookieJar.php 13641 2009-01-14 21:58:25Z doctorrock83 $
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com/)
|
||||
* @version $Id: CookieJar.php 17124 2009-07-26 09:46:42Z shahar $
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Uri
|
||||
*/
|
||||
require_once "Zend/Uri.php";
|
||||
/**
|
||||
* @see Zend_Http_Cookie
|
||||
*/
|
||||
require_once "Zend/Http/Cookie.php";
|
||||
/**
|
||||
* @see Zend_Http_Response
|
||||
*/
|
||||
require_once "Zend/Http/Response.php";
|
||||
|
||||
/**
|
||||
@ -41,14 +50,14 @@ require_once "Zend/Http/Response.php";
|
||||
* (by passing Zend_Http_CookieJar::COOKIE_STRING_CONCAT).
|
||||
*
|
||||
* @link http://wp.netscape.com/newsref/std/cookie_spec.html for some specs.
|
||||
*
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage CookieJar
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com/)
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Http_CookieJar implements Countable, IteratorAggregate
|
||||
class Zend_Http_CookieJar implements Countable, IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* Return cookie(s) as a Zend_Http_Cookie object
|
||||
@ -137,7 +146,7 @@ class Zend_Http_CookieJar implements Countable, IteratorAggregate
|
||||
public function addCookiesFromResponse($response, $ref_uri)
|
||||
{
|
||||
if (! $response instanceof Zend_Http_Response) {
|
||||
require_once 'Zend/Http/Exception.php';
|
||||
require_once 'Zend/Http/Exception.php';
|
||||
throw new Zend_Http_Exception('$response is expected to be a Response object, ' .
|
||||
gettype($response) . ' was passed');
|
||||
}
|
||||
@ -181,18 +190,13 @@ class Zend_Http_CookieJar implements Countable, IteratorAggregate
|
||||
{
|
||||
if (is_string($uri)) $uri = Zend_Uri::factory($uri);
|
||||
if (! $uri instanceof Zend_Uri_Http) {
|
||||
require_once 'Zend/Http/Exception.php';
|
||||
require_once 'Zend/Http/Exception.php';
|
||||
throw new Zend_Http_Exception("Invalid URI string or object passed");
|
||||
}
|
||||
|
||||
// Set path
|
||||
$path = $uri->getPath();
|
||||
$path = substr($path, 0, strrpos($path, '/'));
|
||||
if (! $path) $path = '/';
|
||||
|
||||
// First, reduce the array of cookies to only those matching domain and path
|
||||
$cookies = $this->_matchDomain($uri->getHost());
|
||||
$cookies = $this->_matchPath($cookies, $path);
|
||||
$cookies = $this->_matchPath($cookies, $uri->getPath());
|
||||
$cookies = $this->_flattenCookiesArray($cookies, self::COOKIE_OBJECT);
|
||||
|
||||
// Next, run Cookie->match on all cookies to check secure, time and session mathcing
|
||||
@ -296,17 +300,17 @@ class Zend_Http_CookieJar implements Countable, IteratorAggregate
|
||||
/**
|
||||
* Return a subset of the cookies array matching a specific domain
|
||||
*
|
||||
* Returned array is actually an array of pointers to items in the $this->cookies array.
|
||||
*
|
||||
* @param string $domain
|
||||
* @return array
|
||||
*/
|
||||
protected function _matchDomain($domain) {
|
||||
protected function _matchDomain($domain)
|
||||
{
|
||||
$ret = array();
|
||||
|
||||
foreach (array_keys($this->cookies) as $cdom) {
|
||||
$regex = "/" . preg_quote($cdom, "/") . "$/i";
|
||||
if (preg_match($regex, $domain)) $ret[$cdom] = &$this->cookies[$cdom];
|
||||
if (Zend_Http_Cookie::matchCookieDomain($cdom, $domain)) {
|
||||
$ret[$cdom] = $this->cookies[$cdom];
|
||||
}
|
||||
}
|
||||
|
||||
return $ret;
|
||||
@ -315,22 +319,22 @@ class Zend_Http_CookieJar implements Countable, IteratorAggregate
|
||||
/**
|
||||
* Return a subset of a domain-matching cookies that also match a specified path
|
||||
*
|
||||
* Returned array is actually an array of pointers to items in the $passed array.
|
||||
*
|
||||
* @param array $dom_array
|
||||
* @param string $path
|
||||
* @return array
|
||||
*/
|
||||
protected function _matchPath($domains, $path) {
|
||||
protected function _matchPath($domains, $path)
|
||||
{
|
||||
$ret = array();
|
||||
if (substr($path, -1) != '/') $path .= '/';
|
||||
|
||||
foreach ($domains as $dom => $paths_array) {
|
||||
foreach (array_keys($paths_array) as $cpath) {
|
||||
$regex = "|^" . preg_quote($cpath, "|") . "|i";
|
||||
if (preg_match($regex, $path)) {
|
||||
if (! isset($ret[$dom])) $ret[$dom] = array();
|
||||
$ret[$dom][$cpath] = &$paths_array[$cpath];
|
||||
if (Zend_Http_Cookie::matchCookiePath($cpath, $path)) {
|
||||
if (! isset($ret[$dom])) {
|
||||
$ret[$dom] = array();
|
||||
}
|
||||
|
||||
$ret[$dom][$cpath] = $paths_array[$cpath];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,18 +15,21 @@
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Exception
|
||||
* @version $Id: Exception.php 8064 2008-02-16 10:58:39Z thomas $
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @version $Id: Exception.php 16872 2009-07-20 11:47:08Z mikaelkael $
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Exception
|
||||
*/
|
||||
require_once 'Zend/Exception.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Http_Exception extends Zend_Exception
|
||||
|
@ -16,8 +16,8 @@
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Response
|
||||
* @version $Id: Response.php 16024 2009-06-12 15:44:56Z doctorrock83 $
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @version $Id: Response.php 17124 2009-07-26 09:46:42Z shahar $
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
@ -28,7 +28,7 @@
|
||||
*
|
||||
* @package Zend_Http
|
||||
* @subpackage Response
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Http_Response
|
||||
@ -253,7 +253,7 @@ class Zend_Http_Response
|
||||
$body = '';
|
||||
|
||||
// Decode the body if it was transfer-encoded
|
||||
switch ($this->getHeader('transfer-encoding')) {
|
||||
switch (strtolower($this->getHeader('transfer-encoding'))) {
|
||||
|
||||
// Handle chunked body
|
||||
case 'chunked':
|
||||
@ -493,11 +493,11 @@ class Zend_Http_Response
|
||||
public static function extractHeaders($response_str)
|
||||
{
|
||||
$headers = array();
|
||||
|
||||
|
||||
// First, split body and headers
|
||||
$parts = preg_split('|(?:\r?\n){2}|m', $response_str, 2);
|
||||
if (! $parts[0]) return $headers;
|
||||
|
||||
|
||||
// Split headers part to lines
|
||||
$lines = explode("\n", $parts[0]);
|
||||
unset($parts);
|
||||
@ -545,7 +545,7 @@ class Zend_Http_Response
|
||||
public static function extractBody($response_str)
|
||||
{
|
||||
$parts = preg_split('|(?:\r?\n){2}|m', $response_str, 2);
|
||||
if (isset($parts[1])) {
|
||||
if (isset($parts[1])) {
|
||||
return $parts[1];
|
||||
}
|
||||
return '';
|
||||
@ -560,7 +560,16 @@ class Zend_Http_Response
|
||||
public static function decodeChunkedBody($body)
|
||||
{
|
||||
$decBody = '';
|
||||
|
||||
|
||||
// If mbstring overloads substr and strlen functions, we have to
|
||||
// override it's internal encoding
|
||||
if (function_exists('mb_internal_encoding') &&
|
||||
((int) ini_get('mbstring.func_overload')) & 2) {
|
||||
|
||||
$mbIntEnc = mb_internal_encoding();
|
||||
mb_internal_encoding('ASCII');
|
||||
}
|
||||
|
||||
while (trim($body)) {
|
||||
if (! preg_match("/^([\da-fA-F]+)[^\r\n]*\r\n/sm", $body, $m)) {
|
||||
require_once 'Zend/Http/Exception.php';
|
||||
@ -569,11 +578,14 @@ class Zend_Http_Response
|
||||
|
||||
$length = hexdec(trim($m[1]));
|
||||
$cut = strlen($m[0]);
|
||||
|
||||
$decBody .= substr($body, $cut, $length);
|
||||
$body = substr($body, $cut + $length + 2);
|
||||
}
|
||||
|
||||
if (isset($mbIntEnc)) {
|
||||
mb_internal_encoding($mbIntEnc);
|
||||
}
|
||||
|
||||
return $decBody;
|
||||
}
|
||||
|
||||
@ -589,8 +601,9 @@ class Zend_Http_Response
|
||||
{
|
||||
if (! function_exists('gzinflate')) {
|
||||
require_once 'Zend/Http/Exception.php';
|
||||
throw new Zend_Http_Exception('Unable to decode gzipped response ' .
|
||||
'body: perhaps the zlib extension is not loaded?');
|
||||
throw new Zend_Http_Exception(
|
||||
'zlib extension is required in order to decode "gzip" encoding'
|
||||
);
|
||||
}
|
||||
|
||||
return gzinflate(substr($body, 10));
|
||||
@ -608,11 +621,28 @@ class Zend_Http_Response
|
||||
{
|
||||
if (! function_exists('gzuncompress')) {
|
||||
require_once 'Zend/Http/Exception.php';
|
||||
throw new Zend_Http_Exception('Unable to decode deflated response ' .
|
||||
'body: perhaps the zlib extension is not loaded?');
|
||||
throw new Zend_Http_Exception(
|
||||
'zlib extension is required in order to decode "deflate" encoding'
|
||||
);
|
||||
}
|
||||
|
||||
return gzuncompress($body);
|
||||
/**
|
||||
* Some servers (IIS ?) send a broken deflate response, without the
|
||||
* RFC-required zlib header.
|
||||
*
|
||||
* We try to detect the zlib header, and if it does not exsit we
|
||||
* teat the body is plain DEFLATE content.
|
||||
*
|
||||
* This method was adapted from PEAR HTTP_Request2 by (c) Alexey Borzov
|
||||
*
|
||||
* @link http://framework.zend.com/issues/browse/ZF-6040
|
||||
*/
|
||||
$zlibHeader = unpack('n', substr($body, 0, 2));
|
||||
if ($zlibHeader[1] % 31 == 0) {
|
||||
return gzuncompress($body);
|
||||
} else {
|
||||
return gzinflate($body);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user