From d332f0624fbe91afe5524b55d4fa0826b239d710 Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Sat, 11 Dec 2010 16:49:19 -0200 Subject: [PATCH] Making the cookies independent for each host. --- cake/libs/http_socket.php | 19 ++++++++-- cake/tests/cases/libs/http_socket.test.php | 41 +++++++++++++++++++++- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/cake/libs/http_socket.php b/cake/libs/http_socket.php index 0ecc1a60b49..0af827f4477 100644 --- a/cake/libs/http_socket.php +++ b/cake/libs/http_socket.php @@ -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; } @@ -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'])) { @@ -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']; diff --git a/cake/tests/cases/libs/http_socket.test.php b/cake/tests/cases/libs/http_socket.test.php index 32be35f7b5b..895b62e0880 100644 --- a/cake/tests/cases/libs/http_socket.test.php +++ b/cake/tests/cases/libs/http_socket.test.php @@ -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); } @@ -666,6 +666,45 @@ public function testRequestWithResource() { $this->assertEqual($result, '

This is a test!

'); } +/** + * 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

This is a test!

"; + $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

This is a test!

"; + $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

This is a test!

"; + $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

This is a test!

"; + $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 *