Skip to content

Commit

Permalink
Making the cookies independent for each host.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Dec 11, 2010
1 parent eeafb55 commit d332f06
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
19 changes: 16 additions & 3 deletions cake/libs/http_socket.php
Expand Up @@ -263,10 +263,21 @@ public function &request($request = array()) {
}
$request['uri'] = $this->url($request['uri']);
$request['uri'] = $this->_parseUri($request['uri'], true);
$this->request = Set::merge($this->request, $this->config['request'], $request);
$this->request = Set::merge($this->request, array_diff_key($this->config['request'], array('cookies' => true)), $request);

$this->_configUri($this->request['uri']);

$Host = $this->request['uri']['host'];
if (!empty($this->config['request']['cookies'][$Host])) {
if (!isset($this->request['cookies'])) {
$this->request['cookies'] = array();
}
if (!isset($request['cookies'])) {
$request['cookies'] = array();
}
$this->request['cookies'] = array_merge($this->request['cookies'], $this->config['request']['cookies'][$Host], $request['cookies']);
}

if (isset($host)) {
$this->config['host'] = $host;
}
Expand All @@ -280,7 +291,6 @@ public function &request($request = array()) {
if (!empty($this->request['cookies'])) {
$cookies = $this->buildCookies($this->request['cookies']);
}
$Host = $this->request['uri']['host'];
$schema = '';
$port = 0;
if (isset($this->request['uri']['schema'])) {
Expand Down Expand Up @@ -374,7 +384,10 @@ public function &request($request = array()) {

$this->response = $this->_parseResponse($response);
if (!empty($this->response['cookies'])) {
$this->config['request']['cookies'] = array_merge($this->config['request']['cookies'], $this->response['cookies']);
if (!isset($this->config['request']['cookies'][$Host])) {
$this->config['request']['cookies'][$Host] = array();
}
$this->config['request']['cookies'][$Host] = array_merge($this->config['request']['cookies'][$Host], $this->response['cookies']);
}

return $this->response['body'];
Expand Down
41 changes: 40 additions & 1 deletion cake/tests/cases/libs/http_socket.test.php
Expand Up @@ -618,7 +618,7 @@ public function testRequest3() {
)
);
$this->assertEqual($result, $expect);
$this->assertEqual($this->Socket->config['request']['cookies'], $expect);
$this->assertEqual($this->Socket->config['request']['cookies']['www.cakephp.org'], $expect);
$this->assertFalse($this->Socket->connected);
}

Expand Down Expand Up @@ -666,6 +666,45 @@ public function testRequestWithResource() {
$this->assertEqual($result, '<h1>This is a test!</h1>');
}

/**
* testRequestWithCrossCookie
*
* @return void
*/
public function testRequestWithCrossCookie() {
$this->Socket->connected = true;
$this->Socket->config['request']['cookies'] = array();

$serverResponse = "HTTP/1.x 200 OK\r\nSet-Cookie: foo=bar\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a test!</h1>";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
$expected = array('www.cakephp.org' => array('foo' => array('value' => 'bar')));
$this->Socket->request('http://www.cakephp.org/');
$this->assertEqual($this->Socket->config['request']['cookies'], $expected);

$serverResponse = "HTTP/1.x 200 OK\r\nSet-Cookie: bar=foo\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a test!</h1>";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
$this->Socket->request('http://www.cakephp.org/other');
$this->assertEqual($this->Socket->request['cookies'], array('foo' => array('value' => 'bar')));
$expected['www.cakephp.org'] += array('bar' => array('value' => 'foo'));
$this->assertEqual($this->Socket->config['request']['cookies'], $expected);

$serverResponse = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a test!</h1>";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
$this->Socket->request('/other2');
$this->assertEqual($this->Socket->config['request']['cookies'], $expected);

$serverResponse = "HTTP/1.x 200 OK\r\nSet-Cookie: foobar=ok\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a test!</h1>";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
$this->Socket->request('http://www.cake.com');
$this->assertTrue(empty($this->Socket->request['cookies']));
$expected['www.cake.com'] = array('foobar' => array('value' => 'ok'));
$this->assertEqual($this->Socket->config['request']['cookies'], $expected);
}

/**
* testProxy method
*
Expand Down

0 comments on commit d332f06

Please sign in to comment.