import v1.0.0-RC4 | 2009-05-20
This commit is contained in:
33
libs/Zend/Http/Client/Adapter/Exception.php
Normal file
33
libs/Zend/Http/Client/Adapter/Exception.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @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)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
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)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Http_Client_Adapter_Exception extends Zend_Http_Client_Exception
|
||||
{}
|
78
libs/Zend/Http/Client/Adapter/Interface.php
Normal file
78
libs/Zend/Http/Client/Adapter/Interface.php
Normal file
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @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)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/**
|
||||
* An interface description for Zend_Http_Client_Adapter classes.
|
||||
*
|
||||
* These classes are used as connectors for Zend_Http_Client, performing the
|
||||
* tasks of connecting, writing, reading and closing connection to the server.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @copyright Copyright (c) 2005-2008 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
|
||||
{
|
||||
/**
|
||||
* Set the configuration array for the adapter
|
||||
*
|
||||
* @param array $config
|
||||
*/
|
||||
public function setConfig($config = array());
|
||||
|
||||
/**
|
||||
* Connect to the remote server
|
||||
*
|
||||
* @param string $host
|
||||
* @param int $port
|
||||
* @param boolean $secure
|
||||
*/
|
||||
public function connect($host, $port = 80, $secure = false);
|
||||
|
||||
/**
|
||||
* Send request to the remote server
|
||||
*
|
||||
* @param string $method
|
||||
* @param Zend_Uri_Http $url
|
||||
* @param string $http_ver
|
||||
* @param array $headers
|
||||
* @param string $body
|
||||
* @return string Request as text
|
||||
*/
|
||||
public function write($method, $url, $http_ver = '1.1', $headers = array(), $body = '');
|
||||
|
||||
/**
|
||||
* Read response from server
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function read();
|
||||
|
||||
/**
|
||||
* Close the connection to the server
|
||||
*
|
||||
*/
|
||||
public function close();
|
||||
}
|
269
libs/Zend/Http/Client/Adapter/Proxy.php
Normal file
269
libs/Zend/Http/Client/Adapter/Proxy.php
Normal file
@ -0,0 +1,269 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @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)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
require_once 'Zend/Uri/Http.php';
|
||||
require_once 'Zend/Http/Client.php';
|
||||
require_once 'Zend/Http/Client/Adapter/Socket.php';
|
||||
|
||||
/**
|
||||
* HTTP Proxy-supporting Zend_Http_Client adapter class, based on the default
|
||||
* socket based adapter.
|
||||
*
|
||||
* Should be used if proxy HTTP access is required. If no proxy is set, will
|
||||
* fall back to Zend_Http_Client_Adapter_Socket behavior. Just like the
|
||||
* default Socket adapter, this adapter does not require any special extensions
|
||||
* installed.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @copyright Copyright (c) 2005-2008 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
|
||||
{
|
||||
/**
|
||||
* Parameters array
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $config = array(
|
||||
'ssltransport' => 'ssl',
|
||||
'proxy_host' => '',
|
||||
'proxy_port' => 8080,
|
||||
'proxy_user' => '',
|
||||
'proxy_pass' => '',
|
||||
'proxy_auth' => Zend_Http_Client::AUTH_BASIC,
|
||||
'persistent' => false
|
||||
);
|
||||
|
||||
/**
|
||||
* Whether HTTPS CONNECT was already negotiated with the proxy or not
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $negotiated = false;
|
||||
|
||||
/**
|
||||
* Connect to the remote server
|
||||
*
|
||||
* Will try to connect to the proxy server. If no proxy was set, will
|
||||
* fall back to the target server (behave like regular Socket adapter)
|
||||
*
|
||||
* @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();
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send request to the proxy server
|
||||
*
|
||||
* @param string $method
|
||||
* @param Zend_Uri_Http $uri
|
||||
* @param string $http_ver
|
||||
* @param array $headers
|
||||
* @param string $body
|
||||
* @return string Request as string
|
||||
*/
|
||||
public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
|
||||
{
|
||||
// If no proxy is set, fall back to default Socket adapter
|
||||
if (! $this->config['proxy_host']) return parent::write($method, $uri, $http_ver, $headers, $body);
|
||||
|
||||
// Make sure we're properly connected
|
||||
if (! $this->socket) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are not connected");
|
||||
}
|
||||
|
||||
$host = $this->config['proxy_host'];
|
||||
$port = $this->config['proxy_port'];
|
||||
|
||||
if ($this->connected_to[0] != $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");
|
||||
}
|
||||
|
||||
// Add Proxy-Authorization header
|
||||
if ($this->config['proxy_user'] && ! isset($headers['proxy-authorization'])) {
|
||||
$headers['proxy-authorization'] = Zend_Http_Client::encodeAuthHeader(
|
||||
$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";
|
||||
|
||||
// Add all headers to the request string
|
||||
foreach ($headers as $k => $v) {
|
||||
if (is_string($k)) $v = "$k: $v";
|
||||
$request .= "$v\r\n";
|
||||
}
|
||||
|
||||
// Add the request body
|
||||
$request .= "\r\n" . $body;
|
||||
|
||||
// Send the request
|
||||
if (! @fwrite($this->socket, $request)) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception("Error writing request to proxy server");
|
||||
}
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Preform handshaking with HTTPS proxy using CONNECT method
|
||||
*
|
||||
* @param string $host
|
||||
* @param integer $port
|
||||
* @param string $http_ver
|
||||
* @param array $headers
|
||||
*/
|
||||
protected function connectHandshake($host, $port = 443, $http_ver = '1.1', array &$headers = array())
|
||||
{
|
||||
$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
|
||||
if (! @fwrite($this->socket, $request)) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception("Error writing request to proxy server");
|
||||
}
|
||||
|
||||
// Read response headers only
|
||||
$response = '';
|
||||
$gotStatus = false;
|
||||
while ($line = @fgets($this->socket)) {
|
||||
$gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
|
||||
if ($gotStatus) {
|
||||
$response .= $line;
|
||||
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
|
||||
$modes = array(
|
||||
STREAM_CRYPTO_METHOD_TLS_CLIENT,
|
||||
STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
|
||||
STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
|
||||
STREAM_CRYPTO_METHOD_SSLv2_CLIENT
|
||||
);
|
||||
|
||||
$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" .
|
||||
" HTTPS server through proxy: could not negotiate secure connection.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the connection to the server
|
||||
*
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
parent::close();
|
||||
$this->negotiated = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor: make sure the socket is disconnected
|
||||
*
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->socket) $this->close();
|
||||
}
|
||||
}
|
338
libs/Zend/Http/Client/Adapter/Socket.php
Normal file
338
libs/Zend/Http/Client/Adapter/Socket.php
Normal file
@ -0,0 +1,338 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @version $Id: Socket.php 12105 2008-10-23 23:38:55Z shahar $
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
require_once 'Zend/Uri/Http.php';
|
||||
require_once 'Zend/Http/Client/Adapter/Interface.php';
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @copyright Copyright (c) 2005-2008 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
|
||||
{
|
||||
/**
|
||||
* The socket for server connection
|
||||
*
|
||||
* @var resource|null
|
||||
*/
|
||||
protected $socket = null;
|
||||
|
||||
/**
|
||||
* What host/port are we connected to?
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $connected_to = array(null, null);
|
||||
|
||||
/**
|
||||
* Parameters array
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $config = array(
|
||||
'persistent' => false,
|
||||
'ssltransport' => 'ssl',
|
||||
'sslcert' => null,
|
||||
'sslpassphrase' => null
|
||||
);
|
||||
|
||||
/**
|
||||
* Request method - will be set by write() and might be used by read()
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $method = null;
|
||||
|
||||
/**
|
||||
* Adapter constructor, currently empty. Config is set using setConfig()
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the configuration array for the adapter
|
||||
*
|
||||
* @param array $config
|
||||
*/
|
||||
public function setConfig($config = array())
|
||||
{
|
||||
if (! 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.');
|
||||
}
|
||||
|
||||
foreach ($config as $k => $v) {
|
||||
$this->config[strtolower($k)] = $v;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
// If the URI should be accessed via SSL, prepend the Hostname with ssl://
|
||||
$host = ($secure ? $this->config['ssltransport'] : 'tcp') . '://' . $host;
|
||||
|
||||
// If we are connected to the wrong host, disconnect first
|
||||
if (($this->connected_to[0] != $host || $this->connected_to[1] != $port)) {
|
||||
if (is_resource($this->socket)) $this->close();
|
||||
}
|
||||
|
||||
// Now, if we are not connected, connect
|
||||
if (! is_resource($this->socket) || ! $this->config['keepalive']) {
|
||||
$context = stream_context_create();
|
||||
if ($secure) {
|
||||
if ($this->config['sslcert'] !== null) {
|
||||
if (! stream_context_set_option($context, 'ssl', 'local_cert',
|
||||
$this->config['sslcert'])) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception('Unable to set sslcert option');
|
||||
}
|
||||
}
|
||||
if ($this->config['sslpassphrase'] !== null) {
|
||||
if (! stream_context_set_option($context, 'ssl', 'passphrase',
|
||||
$this->config['sslpassphrase'])) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception('Unable to set sslpassphrase option');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$flags = STREAM_CLIENT_CONNECT;
|
||||
if ($this->config['persistent']) $flags |= STREAM_CLIENT_PERSISTENT;
|
||||
|
||||
$this->socket = @stream_socket_client($host . ':' . $port,
|
||||
$errno,
|
||||
$errstr,
|
||||
(int) $this->config['timeout'],
|
||||
$flags,
|
||||
$context);
|
||||
if (! $this->socket) {
|
||||
$this->close();
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception(
|
||||
'Unable to Connect to ' . $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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send request to the remote server
|
||||
*
|
||||
* @param string $method
|
||||
* @param Zend_Uri_Http $uri
|
||||
* @param string $http_ver
|
||||
* @param array $headers
|
||||
* @param string $body
|
||||
* @return string Request as string
|
||||
*/
|
||||
public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
|
||||
{
|
||||
// Make sure we're properly connected
|
||||
if (! $this->socket) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception('Trying to write but we are not connected');
|
||||
}
|
||||
|
||||
$host = $uri->getHost();
|
||||
$host = (strtolower($uri->getScheme()) == 'https' ? $this->config['ssltransport'] : 'tcp') . '://' . $host;
|
||||
if ($this->connected_to[0] != $host || $this->connected_to[1] != $uri->getPort()) {
|
||||
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 host');
|
||||
}
|
||||
|
||||
// Save request method for later
|
||||
$this->method = $method;
|
||||
|
||||
// Build request headers
|
||||
$path = $uri->getPath();
|
||||
if ($uri->getQuery()) $path .= '?' . $uri->getQuery();
|
||||
$request = "{$method} {$path} HTTP/{$http_ver}\r\n";
|
||||
foreach ($headers as $k => $v) {
|
||||
if (is_string($k)) $v = ucfirst($k) . ": $v";
|
||||
$request .= "$v\r\n";
|
||||
}
|
||||
|
||||
// Add the request body
|
||||
$request .= "\r\n" . $body;
|
||||
|
||||
// Send the request
|
||||
if (! @fwrite($this->socket, $request)) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception('Error writing request to server');
|
||||
}
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read response from server
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function read()
|
||||
{
|
||||
// First, read headers only
|
||||
$response = '';
|
||||
$gotStatus = false;
|
||||
while (($line = @fgets($this->socket)) !== false) {
|
||||
$gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
|
||||
if ($gotStatus) {
|
||||
$response .= $line;
|
||||
if (rtrim($line) === '') break;
|
||||
}
|
||||
}
|
||||
|
||||
$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();
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
// Check headers to see what kind of connection / transfer encoding we have
|
||||
$headers = Zend_Http_Response::extractHeaders($response);
|
||||
|
||||
// If we got a 'transfer-encoding: chunked' header
|
||||
if (isset($headers['transfer-encoding'])) {
|
||||
if ($headers['transfer-encoding'] == 'chunked') {
|
||||
do {
|
||||
$line = @fgets($this->socket);
|
||||
$chunk = $line;
|
||||
|
||||
// Figure out the next chunk size
|
||||
$chunksize = trim($line);
|
||||
if (! ctype_xdigit($chunksize)) {
|
||||
$this->close();
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception('Invalid chunk size "' .
|
||||
$chunksize . '" unable to read chunked body');
|
||||
}
|
||||
|
||||
// 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);
|
||||
$chunk .= $line;
|
||||
$left_to_read -= strlen($line);
|
||||
|
||||
// Break if the connection ended prematurely
|
||||
if (feof($this->socket)) break;
|
||||
}
|
||||
|
||||
$chunk .= @fgets($this->socket);
|
||||
$response .= $chunk;
|
||||
} while ($chunksize > 0);
|
||||
|
||||
} else {
|
||||
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'];
|
||||
$chunk = '';
|
||||
while ($left_to_read > 0) {
|
||||
$chunk = @fread($this->socket, $left_to_read);
|
||||
$left_to_read -= strlen($chunk);
|
||||
$response .= $chunk;
|
||||
|
||||
// Break if the connection ended prematurely
|
||||
if (feof($this->socket)) break;
|
||||
}
|
||||
|
||||
// Fallback: just read the response until EOF
|
||||
} else {
|
||||
while (($buff = @fread($this->socket, 8192)) !== false) {
|
||||
$response .= $buff;
|
||||
if (feof($this->socket)) break;
|
||||
}
|
||||
|
||||
$this->close();
|
||||
}
|
||||
|
||||
// Close the connection if requested to do so by the server
|
||||
if (isset($headers['connection']) && $headers['connection'] == 'close') {
|
||||
$this->close();
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the connection to the server
|
||||
*
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
if (is_resource($this->socket)) @fclose($this->socket);
|
||||
$this->socket = null;
|
||||
$this->connected_to = array(null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor: make sure the socket is disconnected
|
||||
*
|
||||
* If we are in persistent TCP mode, will not close the connection
|
||||
*
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
if (! $this->config['persistent']) {
|
||||
if ($this->socket) $this->close();
|
||||
}
|
||||
}
|
||||
}
|
193
libs/Zend/Http/Client/Adapter/Test.php
Normal file
193
libs/Zend/Http/Client/Adapter/Test.php
Normal file
@ -0,0 +1,193 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @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)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
require_once 'Zend/Uri/Http.php';
|
||||
require_once 'Zend/Http/Response.php';
|
||||
require_once 'Zend/Http/Client/Adapter/Interface.php';
|
||||
|
||||
/**
|
||||
* A testing-purposes adapter.
|
||||
*
|
||||
* Should be used to test all components that rely on Zend_Http_Client,
|
||||
* without actually performing an HTTP request. You should instantiate this
|
||||
* object manually, and then set it as the client's adapter. Then, you can
|
||||
* set the expected response using the setResponse() method.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @copyright Copyright (c) 2005-2008 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
|
||||
{
|
||||
/**
|
||||
* Parameters array
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $config = array();
|
||||
|
||||
/**
|
||||
* Buffer of responses to be returned by the read() method. Can be
|
||||
* set using setResponse() and addResponse().
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $responses = array("HTTP/1.1 400 Bad Request\r\n\r\n");
|
||||
|
||||
/**
|
||||
* Current position in the response buffer
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected $responseIndex = 0;
|
||||
|
||||
/**
|
||||
* Adapter constructor, currently empty. Config is set using setConfig()
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{ }
|
||||
|
||||
/**
|
||||
* Set the configuration array for the adapter
|
||||
*
|
||||
* @param array $config
|
||||
*/
|
||||
public function setConfig($config = array())
|
||||
{
|
||||
if (! 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.');
|
||||
}
|
||||
|
||||
foreach ($config as $k => $v) {
|
||||
$this->config[strtolower($k)] = $v;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{ }
|
||||
|
||||
/**
|
||||
* Send request to the remote server
|
||||
*
|
||||
* @param string $method
|
||||
* @param Zend_Uri_Http $uri
|
||||
* @param string $http_ver
|
||||
* @param array $headers
|
||||
* @param string $body
|
||||
* @return string Request as string
|
||||
*/
|
||||
public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
|
||||
{
|
||||
$host = $uri->getHost();
|
||||
$host = (strtolower($uri->getScheme()) == 'https' ? 'sslv2://' . $host : $host);
|
||||
|
||||
// Build request headers
|
||||
$path = $uri->getPath();
|
||||
if ($uri->getQuery()) $path .= '?' . $uri->getQuery();
|
||||
$request = "{$method} {$path} HTTP/{$http_ver}\r\n";
|
||||
foreach ($headers as $k => $v) {
|
||||
if (is_string($k)) $v = ucfirst($k) . ": $v";
|
||||
$request .= "$v\r\n";
|
||||
}
|
||||
|
||||
// Add the request body
|
||||
$request .= "\r\n" . $body;
|
||||
|
||||
// Do nothing - just return the request as string
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the response set in $this->setResponse()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function read()
|
||||
{
|
||||
if ($this->responseIndex >= count($this->responses)) {
|
||||
$this->responseIndex = 0;
|
||||
}
|
||||
return $this->responses[$this->responseIndex++];
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the connection (dummy)
|
||||
*
|
||||
*/
|
||||
public function close()
|
||||
{ }
|
||||
|
||||
/**
|
||||
* Set the HTTP response(s) to be returned by this adapter
|
||||
*
|
||||
* @param Zend_Http_Response|array|string $response
|
||||
*/
|
||||
public function setResponse($response)
|
||||
{
|
||||
if ($response instanceof Zend_Http_Response) {
|
||||
$response = $response->asString();
|
||||
}
|
||||
|
||||
$this->responses = (array)$response;
|
||||
$this->responseIndex = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add another response to the response buffer.
|
||||
*
|
||||
* @param string $response
|
||||
*/
|
||||
public function addResponse($response)
|
||||
{
|
||||
$this->responses[] = $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the position of the response buffer. Selects which
|
||||
* response will be returned on the next call to read().
|
||||
*
|
||||
* @param integer $index
|
||||
*/
|
||||
public function setResponseIndex($index)
|
||||
{
|
||||
if ($index < 0 || $index >= count($this->responses)) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception(
|
||||
'Index out of range of response buffer size');
|
||||
}
|
||||
$this->responseIndex = $index;
|
||||
}
|
||||
}
|
33
libs/Zend/Http/Client/Exception.php
Normal file
33
libs/Zend/Http/Client/Exception.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @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)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
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)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Http_Client_Exception extends Zend_Http_Exception
|
||||
{}
|
Reference in New Issue
Block a user