Support to proxy in HttpSocket.

This commit is contained in:
Juan Basso 2010-12-01 13:46:13 -02:00
parent 634d686288
commit b9010b05f2
2 changed files with 141 additions and 1 deletions

View file

@ -64,6 +64,13 @@ class HttpSocket extends CakeSocket {
'user' => null,
'pass' => null
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
),
'version' => '1.1',
'body' => '',
'line' => null,
@ -121,6 +128,13 @@ class HttpSocket extends CakeSocket {
'user' => null,
'pass' => null
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
),
'cookies' => array()
)
);
@ -238,6 +252,7 @@ class HttpSocket extends CakeSocket {
}
$this->_setAuth();
$this->_setProxyConfig();
if (is_array($this->request['body'])) {
$this->request['body'] = $this->_httpSerialize($this->request['body']);
@ -463,6 +478,28 @@ class HttpSocket extends CakeSocket {
call_user_func("$authClass::authentication", $this);
}
/**
* Set the proxy configuration and authentication
*
* @return void
*/
protected function _setProxyConfig() {
if (empty($this->request['proxy']['host'])) {
return;
}
$this->config['host'] = $this->request['proxy']['host'];
$this->config['port'] = $this->request['proxy']['port'];
if (empty($this->request['proxy']['method']) || !isset($this->request['proxy']['user'], $this->request['proxy']['pass'])) {
return;
}
$authClass = Inflector::camelize($this->request['proxy']['method']) . 'Authentication';
if (!App::import('Lib', 'http/' . $authClass)) {
throw new Exception(__('Unknown authentication method for proxy.'));
}
call_user_func("$authClass::proxyAuthentication", $this);
}
/**
* Parses the given message and breaks it down in parts.
*
@ -837,7 +874,11 @@ class HttpSocket extends CakeSocket {
$request['uri'] = $this->_parseUri($request['uri']);
$request = array_merge(array('method' => 'GET'), $request);
$request['uri'] = $this->_buildUri($request['uri'], '/%path?%query');
if (!empty($request['proxy']['host'])) {
$request['uri'] = $this->_buildUri($request['uri'], '%scheme://%host:%port/%path?%query');
} else {
$request['uri'] = $this->_buildUri($request['uri'], '/%path?%query');
}
if (!$this->quirksMode && $request['uri'] === '*' && !in_array($request['method'], $asteriskMethods)) {
throw new Exception(sprintf(__('HttpSocket::_buildRequestLine - The "*" asterisk character is only allowed for the following methods: %s. Activate quirks mode to work outside of HTTP/1.1 specs.'), join(',', $asteriskMethods)));

View file

@ -19,6 +19,36 @@
*/
App::import('Core', 'HttpSocket');
/**
* TestAuthentication class
*
* @package cake
* @subpackage cake.tests.cases.libs
*/
class TestAuthentication {
/**
* authentication method
*
* @param HttpSocket $http
* @return void
*/
public static function authentication(HttpSocket $http) {
$http->request['header']['Authorization'] = 'Test ' . $http->request['auth']['user'] . '.' . $http->request['auth']['pass'];
}
/**
* proxyAuthentication method
*
* @param HttpSocket $http
* @return void
*/
public static function proxyAuthentication(HttpSocket $http) {
$http->request['header']['Proxy-Authorization'] = 'Test ' . $http->request['proxy']['user'] . '.' . $http->request['proxy']['pass'];
}
}
class TestHttpSocket extends HttpSocket {
/**
@ -261,6 +291,13 @@ class HttpSocketTest extends CakeTestCase {
, 'user' => 'bob'
, 'pass' => 'secret'
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
),
'cookies' => array(),
)
);
@ -290,6 +327,13 @@ class HttpSocketTest extends CakeTestCase {
, 'user' => null
, 'pass' => null
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
),
'cookies' => array()
)
);
@ -336,6 +380,13 @@ class HttpSocketTest extends CakeTestCase {
,'user' => null
,'pass' => null
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
),
'cookies' => array(),
),
)
@ -355,6 +406,13 @@ class HttpSocketTest extends CakeTestCase {
'method' => 'Basic'
, 'user' => null
, 'pass' => null
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
)
, 'version' => '1.1'
, 'body' => ''
@ -588,6 +646,47 @@ class HttpSocketTest extends CakeTestCase {
$this->assertFalse($this->Socket->connected);
}
/**
* testProxy method
*
* @return void
*/
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
)
);
$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->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->assertEqual($this->Socket->request['raw'], $expected);
$this->assertEqual($this->Socket->config['host'], 'proxy.server');
$this->assertEqual($this->Socket->config['port'], 123);
$request['auth'] = array(
'method' => 'Test',
'user' => 'login',
'pass' => '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->assertEqual($this->Socket->request['raw'], $expected);
}
/**
* testUrl method
*