From 075bdebebe7c04ffa6d5d00b9a9cba4bc9809da3 Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Fri, 3 Dec 2010 23:10:07 -0200 Subject: [PATCH] Moved the proxy configuration from request to class attribute. --- cake/libs/http/basic_authentication.php | 7 +-- cake/libs/http_socket.php | 60 ++++++++++++++-------- cake/tests/cases/libs/http_socket.test.php | 52 ++++--------------- 3 files changed, 51 insertions(+), 68 deletions(-) diff --git a/cake/libs/http/basic_authentication.php b/cake/libs/http/basic_authentication.php index 393f4a86718..11124621086 100644 --- a/cake/libs/http/basic_authentication.php +++ b/cake/libs/http/basic_authentication.php @@ -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']); } } diff --git a/cake/libs/http_socket.php b/cake/libs/http_socket.php index d5ed518ada8..06df4ad1f61 100644 --- a/cake/libs/http_socket.php +++ b/cake/libs/http_socket.php @@ -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 @@ 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 @@ -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. @@ -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); } /** @@ -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'); diff --git a/cake/tests/cases/libs/http_socket.test.php b/cake/tests/cases/libs/http_socket.test.php index 06b05592832..834237934c7 100644 --- a/cake/tests/cases/libs/http_socket.test.php +++ b/cake/tests/cases/libs/http_socket.test.php @@ -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']; } } @@ -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(), ) ); @@ -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() ) ); @@ -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(), ), ) @@ -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' => '' @@ -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); }