Skip to content

Commit

Permalink
Moved the proxy configuration from request to class attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Dec 4, 2010
1 parent f004bef commit 075bdeb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 68 deletions.
7 changes: 4 additions & 3 deletions cake/libs/http/basic_authentication.php
Expand Up @@ -44,12 +44,13 @@ public static function authentication(HttpSocket $http, &$authInfo) {
* 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']);
}
}

Expand Down
60 changes: 38 additions & 22 deletions cake/libs/http_socket.php
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
)
);
Expand All @@ -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.
*
Expand Down Expand Up @@ -182,7 +176,7 @@ public function __construct($config = array()) {
/**
* 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
Expand All @@ -199,6 +193,28 @@ public function setAuthConfig($method, $user = null, $pass = null) {
$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.
Expand Down Expand Up @@ -490,23 +506,23 @@ protected function _setAuth() {
* @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);
}

/**
Expand Down Expand Up @@ -882,7 +898,7 @@ protected function _buildRequestLine($request = array(), $versionToken = 'HTTP/1

$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');
Expand Down
52 changes: 9 additions & 43 deletions cake/tests/cases/libs/http_socket.test.php
Expand Up @@ -42,10 +42,11 @@ public static function authentication(HttpSocket $http, &$authInfo) {
* 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'];
}

}
Expand Down Expand Up @@ -287,13 +288,6 @@ function testConfigUri() {
, 'host' => 'www.cakephp.org'
, 'port' => 23
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
),
'cookies' => array(),
)
);
Expand All @@ -318,13 +312,6 @@ function testConfigUri() {
, 'host' => 'www.foo.com'
, 'port' => 80
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
),
'cookies' => array()
)
);
Expand Down Expand Up @@ -366,13 +353,6 @@ function testRequest() {
, 'host' => 'www.cakephp.org'
, 'port' => 80,
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
),
'cookies' => array(),
),
)
Expand All @@ -387,13 +367,6 @@ function testRequest() {
, 'path' => '/'
, 'query' => array('foo' => 'bar')
, 'fragment' => null
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
)
, 'version' => '1.1'
, 'body' => ''
Expand Down Expand Up @@ -636,31 +609,24 @@ function testProxy() {
$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);
}

Expand Down

0 comments on commit 075bdeb

Please sign in to comment.