Skip to content

Commit 1033461

Browse files
committed
Fixing HttpSocket losing auth credentials when multiple requests are made with the same object. Fixes #893
1 parent 60ab980 commit 1033461

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

cake/libs/http_socket.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,25 @@ function request($request = array()) {
196196
$request['uri'] = null;
197197
}
198198
$uri = $this->_parseUri($request['uri']);
199-
199+
$hadAuth = false;
200+
if (is_array($uri) && array_key_exists('user', $uri)) {
201+
$hadAuth = true;
202+
}
200203
if (!isset($uri['host'])) {
201204
$host = $this->config['host'];
202205
}
203206
if (isset($request['host'])) {
204207
$host = $request['host'];
205208
unset($request['host']);
206209
}
207-
208210
$request['uri'] = $this->url($request['uri']);
209211
$request['uri'] = $this->_parseUri($request['uri'], true);
210212
$this->request = Set::merge($this->request, $this->config['request'], $request);
211213

214+
if (!$hadAuth && !empty($this->config['request']['auth']['user'])) {
215+
$this->request['uri']['user'] = $this->config['request']['auth']['user'];
216+
$this->request['uri']['pass'] = $this->config['request']['auth']['pass'];
217+
}
212218
$this->_configUri($this->request['uri']);
213219

214220
if (isset($host)) {
@@ -605,7 +611,6 @@ function _configUri($uri = null) {
605611
if (!isset($uri['host'])) {
606612
return false;
607613
}
608-
609614
$config = array(
610615
'request' => array(
611616
'uri' => array_intersect_key($uri, $this->config['request']['uri']),
@@ -1049,7 +1054,6 @@ function reset($full = true) {
10491054
if (empty($initalState)) {
10501055
$initalState = get_class_vars(__CLASS__);
10511056
}
1052-
10531057
if ($full == false) {
10541058
$this->request = $initalState['request'];
10551059
$this->response = $initalState['response'];

cake/tests/cases/libs/http_socket.test.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,31 @@ function testGet() {
638638
$this->RequestSocket->get('http://www.google.com/', null, array('auth' => array('user' => 'foo', 'pass' => 'bar')));
639639
}
640640

641+
/**
642+
* test that two consecutive get() calls reset the authentication credentials.
643+
*
644+
* @return void
645+
*/
646+
function testConsecutiveGetResetsAuthCredentials() {
647+
$socket = new MockHttpSocket();
648+
$socket->config['request']['auth'] = array(
649+
'method' => 'Basic',
650+
'user' => 'mark',
651+
'pass' => 'secret'
652+
);
653+
$socket->get('http://mark:secret@example.com/test');
654+
$this->assertEqual($socket->request['uri']['user'], 'mark');
655+
$this->assertEqual($socket->request['uri']['pass'], 'secret');
656+
657+
$socket->get('/test2');
658+
$this->assertEqual($socket->request['auth']['user'], 'mark');
659+
$this->assertEqual($socket->request['auth']['pass'], 'secret');
660+
661+
$socket->get('/test3');
662+
$this->assertEqual($socket->request['auth']['user'], 'mark');
663+
$this->assertEqual($socket->request['auth']['pass'], 'secret');
664+
}
665+
641666
/**
642667
* testPostPutDelete method
643668
*

0 commit comments

Comments
 (0)