Source for file Proxy.php
Documentation is available at Proxy.php
* 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.
* @package Microsoft_Http
* @subpackage Client_Adapter
* @version $Id: Proxy.php 17059 2009-07-25 11:24:49Z 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 Microsoft_AutoLoader
require_once dirname(__FILE__ ) . '/../../../AutoLoader.php';
* HTTP Proxy-supporting Microsoft_Http_Client adapter class, based on the default
* Should be used if proxy HTTP access is required. If no proxy is set, will
* fall back to Microsoft_Http_Client_Adapter_Socket behavior. Just like the
* default Socket adapter, this adapter does not require any special extensions
* @package Microsoft_Http
* @subpackage Client_Adapter
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
'proxy_auth' => Microsoft_Http_Client::AUTH_BASIC,
* Whether HTTPS CONNECT was already negotiated with the proxy or not
* 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)
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);
// Connect (a non-secure connection) to the proxy server
* Send request to the proxy server
* @param Microsoft_Uri_Http $uri
* @param string $http_ver
* @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
require_once 'Microsoft/Http/Client/Adapter/Exception.php';
$host = $this->config['proxy_host'];
$port = $this->config['proxy_port'];
require_once 'Microsoft/Http/Client/Adapter/Exception.php';
// Add Proxy-Authorization header
if ($this->config['proxy_user'] && ! isset ($headers['proxy-authorization'])) {
// if we are proxying HTTPS, preform CONNECT handshake with the proxy
if ($uri->getScheme() == 'https' && (! $this->negotiated)) {
// Save request method for later
$path .= '?' . $uri->getQuery();
$request = "$method $path HTTP/$http_ver\r\n";
$request = "$method $uri HTTP/$http_ver\r\n";
// Add all headers to the request string
foreach ($headers as $k => $v) {
$request .= "\r\n" . $body;
require_once 'Microsoft/Http/Client/Adapter/Exception.php';
* Preform handshaking with HTTPS proxy using CONNECT method
* @param string $http_ver
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']);
require_once 'Microsoft/Http/Client/Adapter/Exception.php';
// Read response headers only
$gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
// Check that the response from the proxy is 200
require_once 'Microsoft/Http/Client/Adapter/Exception.php';
// If all is good, switch socket to secure mode. We have to fall back
// through the different modes
STREAM_CRYPTO_METHOD_TLS_CLIENT,
STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
STREAM_CRYPTO_METHOD_SSLv2_CLIENT
foreach($modes as $mode) {
require_once 'Microsoft/Http/Client/Adapter/Exception.php';
" HTTPS server through proxy: could not negotiate secure connection.");
* Close the connection to the server
* Destructor: make sure the socket is disconnected
|