Skip to content
Permalink
Browse files

Support to proxy in HttpSocket.

  • Loading branch information...
jrbasso committed Dec 1, 2010
1 parent 634d686 commit b9010b05f2f272c3ba2c7632223f6f1f2837090a
Showing with 141 additions and 1 deletion.
  1. +42 −1 cake/libs/http_socket.php
  2. +99 −0 cake/tests/cases/libs/http_socket.test.php
@@ -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 @@ public function request($request = array()) {
}
$this->_setAuth();
$this->_setProxyConfig();
if (is_array($this->request['body'])) {
$this->request['body'] = $this->_httpSerialize($this->request['body']);
@@ -463,6 +478,28 @@ protected function _setAuth() {
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 @@ protected function _buildRequestLine($request = array(), $versionToken = 'HTTP/1
$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)));
@@ -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 @@ function testConfigUri() {
, 'user' => 'bob'
, 'pass' => 'secret'
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
),
'cookies' => array(),
)
);
@@ -290,6 +327,13 @@ function testConfigUri() {
, 'user' => null
, 'pass' => null
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
),
'cookies' => array()
)
);
@@ -336,6 +380,13 @@ function testRequest() {
,'user' => null
,'pass' => null
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
'port' => 3128,
'user' => null,
'pass' => null
),
'cookies' => array(),
),
)
@@ -355,6 +406,13 @@ function testRequest() {
'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 @@ function testRequest3() {
$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
*

0 comments on commit b9010b0

Please sign in to comment.
You can’t perform that action at this time.