Skip to content

Commit

Permalink
Fixing HttpSocket losing auth credentials when multiple requests are …
Browse files Browse the repository at this point in the history
…made with the same object. Fixes #893
  • Loading branch information
markstory committed Jul 7, 2010
1 parent 60ab980 commit 1033461
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cake/libs/http_socket.php
Expand Up @@ -196,19 +196,25 @@ function request($request = array()) {
$request['uri'] = null; $request['uri'] = null;
} }
$uri = $this->_parseUri($request['uri']); $uri = $this->_parseUri($request['uri']);

$hadAuth = false;
if (is_array($uri) && array_key_exists('user', $uri)) {
$hadAuth = true;
}
if (!isset($uri['host'])) { if (!isset($uri['host'])) {
$host = $this->config['host']; $host = $this->config['host'];
} }
if (isset($request['host'])) { if (isset($request['host'])) {
$host = $request['host']; $host = $request['host'];
unset($request['host']); unset($request['host']);
} }

$request['uri'] = $this->url($request['uri']); $request['uri'] = $this->url($request['uri']);
$request['uri'] = $this->_parseUri($request['uri'], true); $request['uri'] = $this->_parseUri($request['uri'], true);
$this->request = Set::merge($this->request, $this->config['request'], $request); $this->request = Set::merge($this->request, $this->config['request'], $request);


if (!$hadAuth && !empty($this->config['request']['auth']['user'])) {
$this->request['uri']['user'] = $this->config['request']['auth']['user'];
$this->request['uri']['pass'] = $this->config['request']['auth']['pass'];
}
$this->_configUri($this->request['uri']); $this->_configUri($this->request['uri']);


if (isset($host)) { if (isset($host)) {
Expand Down Expand Up @@ -605,7 +611,6 @@ function _configUri($uri = null) {
if (!isset($uri['host'])) { if (!isset($uri['host'])) {
return false; return false;
} }

$config = array( $config = array(
'request' => array( 'request' => array(
'uri' => array_intersect_key($uri, $this->config['request']['uri']), 'uri' => array_intersect_key($uri, $this->config['request']['uri']),
Expand Down Expand Up @@ -1049,7 +1054,6 @@ function reset($full = true) {
if (empty($initalState)) { if (empty($initalState)) {
$initalState = get_class_vars(__CLASS__); $initalState = get_class_vars(__CLASS__);
} }

if ($full == false) { if ($full == false) {
$this->request = $initalState['request']; $this->request = $initalState['request'];
$this->response = $initalState['response']; $this->response = $initalState['response'];
Expand Down
25 changes: 25 additions & 0 deletions cake/tests/cases/libs/http_socket.test.php
Expand Up @@ -638,6 +638,31 @@ function testGet() {
$this->RequestSocket->get('http://www.google.com/', null, array('auth' => array('user' => 'foo', 'pass' => 'bar'))); $this->RequestSocket->get('http://www.google.com/', null, array('auth' => array('user' => 'foo', 'pass' => 'bar')));
} }


/**
* test that two consecutive get() calls reset the authentication credentials.
*
* @return void
*/
function testConsecutiveGetResetsAuthCredentials() {
$socket = new MockHttpSocket();
$socket->config['request']['auth'] = array(
'method' => 'Basic',
'user' => 'mark',
'pass' => 'secret'
);
$socket->get('http://mark:secret@example.com/test');
$this->assertEqual($socket->request['uri']['user'], 'mark');
$this->assertEqual($socket->request['uri']['pass'], 'secret');

$socket->get('/test2');
$this->assertEqual($socket->request['auth']['user'], 'mark');
$this->assertEqual($socket->request['auth']['pass'], 'secret');

$socket->get('/test3');
$this->assertEqual($socket->request['auth']['user'], 'mark');
$this->assertEqual($socket->request['auth']['pass'], 'secret');
}

/** /**
* testPostPutDelete method * testPostPutDelete method
* *
Expand Down

0 comments on commit 1033461

Please sign in to comment.