Moved the proxy configuration from request to class attribute.

This commit is contained in:
Juan Basso 2010-12-03 23:10:07 -02:00
parent f004bef74f
commit 075bdebebe
3 changed files with 51 additions and 68 deletions

View file

@ -44,12 +44,13 @@ class BasicAuthentication {
* Proxy Authentication
*
* @param HttpSocket $http
* @param array $proxyInfo
* @return void
* @see http://www.ietf.org/rfc/rfc2617.txt
*/
public static function proxyAuthentication(HttpSocket $http) {
if (isset($http->request['proxy']['user'], $http->request['proxy']['pass'])) {
$http->request['header']['Proxy-Authorization'] = self::_generateHeader($http->request['proxy']['user'], $http->request['proxy']['pass']);
public static function proxyAuthentication(HttpSocket $http, &$proxyInfo) {
if (isset($proxyInfo['user'], $proxyInfo['pass'])) {
$http->request['header']['Proxy-Authorization'] = self::_generateHeader($proxyInfo['user'], $proxyInfo['pass']);
}
}

View file

@ -59,13 +59,6 @@ class HttpSocket extends CakeSocket {
'query' => null,
'fragment' => null
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
),
'version' => '1.1',
'body' => '',
'line' => null,
@ -118,13 +111,6 @@ class HttpSocket extends CakeSocket {
'host' => 'localhost',
'port' => 80
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
),
'cookies' => array()
)
);
@ -145,6 +131,14 @@ class HttpSocket extends CakeSocket {
*/
protected $_auth = array();
/**
* Proxy settings
*
* @var array
* @access protected
*/
protected $_proxy = array();
/**
* Build an HTTP Socket using the specified configuration.
*
@ -182,7 +176,7 @@ class HttpSocket extends CakeSocket {
/**
* Set authentication settings
*
* @param string $method Authentication method (ex. Basic, Digest). If empty, disable authentication
* @param string $method Authentication method (ie. Basic, Digest). If empty, disable authentication
* @param mixed $user Username for authentication. Can be an array with settings to authentication class
* @param string $pass Password for authentication
* @return void
@ -199,6 +193,28 @@ class HttpSocket extends CakeSocket {
$this->_auth = array($method => compact('user', 'pass'));
}
/**
* Set proxy settings
*
* @param mixed $host Proxy host. Can be an array with settings to authentication class
* @param integer $port Port. Default 3128.
* @param string $method Proxy method (ie, Basic, Digest). If empty, disable proxy authentication
* @param string $user Username if your proxy need authentication
* @param string $pass Password to proxy authentication
* @return void
*/
public function setProxyConfig($host, $port = 3128, $method = null, $user = null, $pass = null) {
if (empty($host)) {
$this->_proxy = array();
return;
}
if (is_array($host)) {
$this->_proxy = $host + array('host' => null);
return;
}
$this->_proxy = compact('host', 'port', 'method', 'user', 'pass');
}
/**
* Issue the specified request. HttpSocket::get() and HttpSocket::post() wrap this
* method and provide a more granular interface.
@ -490,23 +506,23 @@ class HttpSocket extends CakeSocket {
* @return void
*/
protected function _setProxyConfig() {
if (empty($this->request['proxy']['host'])) {
if (empty($this->_proxy) || !isset($this->_proxy['host'], $this->_proxy['port'])) {
return;
}
$this->config['host'] = $this->request['proxy']['host'];
$this->config['port'] = $this->request['proxy']['port'];
$this->config['host'] = $this->_proxy['host'];
$this->config['port'] = $this->_proxy['port'];
if (empty($this->request['proxy']['method']) || !isset($this->request['proxy']['user'], $this->request['proxy']['pass'])) {
if (empty($this->_proxy['method']) || !isset($this->_proxy['user'], $this->_proxy['pass'])) {
return;
}
$authClass = Inflector::camelize($this->request['proxy']['method']) . 'Authentication';
$authClass = Inflector::camelize($this->_proxy['method']) . 'Authentication';
if (!App::import('Lib', 'http/' . $authClass)) {
throw new Exception(__('Unknown authentication method for proxy.'));
}
if (!method_exists($authClass, 'proxyAuthentication')) {
throw new Exception(sprintf(__('The %s do not support proxy authentication.'), $authClass));
}
call_user_func("$authClass::proxyAuthentication", $this);
call_user_func("$authClass::proxyAuthentication", $this, &$this->_proxy);
}
/**
@ -882,7 +898,7 @@ class HttpSocket extends CakeSocket {
$request['uri'] = $this->_parseUri($request['uri']);
$request = array_merge(array('method' => 'GET'), $request);
if (!empty($request['proxy']['host'])) {
if (!empty($this->_proxy['host'])) {
$request['uri'] = $this->_buildUri($request['uri'], '%scheme://%host:%port/%path?%query');
} else {
$request['uri'] = $this->_buildUri($request['uri'], '/%path?%query');

View file

@ -42,10 +42,11 @@ class TestAuthentication {
* proxyAuthentication method
*
* @param HttpSocket $http
* @param array $proxyInfo
* @return void
*/
public static function proxyAuthentication(HttpSocket $http) {
$http->request['header']['Proxy-Authorization'] = 'Test ' . $http->request['proxy']['user'] . '.' . $http->request['proxy']['pass'];
public static function proxyAuthentication(HttpSocket $http, &$proxyInfo) {
$http->request['header']['Proxy-Authorization'] = 'Test ' . $proxyInfo['user'] . '.' . $proxyInfo['pass'];
}
}
@ -287,13 +288,6 @@ class HttpSocketTest extends CakeTestCase {
, 'host' => 'www.cakephp.org'
, 'port' => 23
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
),
'cookies' => array(),
)
);
@ -318,13 +312,6 @@ class HttpSocketTest extends CakeTestCase {
, 'host' => 'www.foo.com'
, 'port' => 80
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
),
'cookies' => array()
)
);
@ -366,13 +353,6 @@ class HttpSocketTest extends CakeTestCase {
, 'host' => 'www.cakephp.org'
, 'port' => 80,
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
),
'cookies' => array(),
),
)
@ -387,13 +367,6 @@ class HttpSocketTest extends CakeTestCase {
, 'path' => '/'
, 'query' => array('foo' => 'bar')
, 'fragment' => null
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
)
, 'version' => '1.1'
, 'body' => ''
@ -636,31 +609,24 @@ class HttpSocketTest extends CakeTestCase {
$this->Socket->reset();
$this->Socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->Socket->expects($this->any())->method('read')->will($this->returnValue(false));
$request = array(
'uri' => 'http://www.cakephp.org/',
'proxy' => array(
'host' => 'proxy.server',
'port' => 123
)
);
$this->Socket->setProxyConfig('proxy.server', 123);
$expected = "GET http://www.cakephp.org/ HTTP/1.1\r\nHost: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n\r\n";
$this->Socket->request($request);
$this->Socket->request('http://www.cakephp.org/');
$this->assertEqual($this->Socket->request['raw'], $expected);
$this->assertEqual($this->Socket->config['host'], 'proxy.server');
$this->assertEqual($this->Socket->config['port'], 123);
$request['proxy']['method'] = 'Test';
$request['proxy']['user'] = 'mark';
$request['proxy']['pass'] = 'secret';
$expected = "GET http://www.cakephp.org/ HTTP/1.1\r\nHost: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nProxy-Authorization: Test mark.secret\r\n\r\n";
$this->Socket->request($request);
$this->Socket->setProxyConfig('proxy.server', 123, 'Test', 'mark', 'secret');
$this->Socket->request('http://www.cakephp.org/');
$this->assertEqual($this->Socket->request['raw'], $expected);
$this->assertEqual($this->Socket->config['host'], 'proxy.server');
$this->assertEqual($this->Socket->config['port'], 123);
$this->Socket->setAuthConfig('Test', 'login', 'passwd');
$expected = "GET http://www.cakephp.org/ HTTP/1.1\r\nHost: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nAuthorization: Test login.passwd\r\nProxy-Authorization: Test mark.secret\r\n\r\n";
$this->Socket->request($request);
$this->Socket->request('http://www.cakephp.org/');
$this->assertEqual($this->Socket->request['raw'], $expected);
}