mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Added initial cookie jar support to HttpSocket
Added test coverage for it Fixed bug in HttpSocket::buildCookies() Fixed 'Connection: close' not closing the connection bug Fixed some issues with HTTPS connections git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6031 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
be493341fa
commit
effc9d4029
2 changed files with 109 additions and 35 deletions
|
@ -82,7 +82,8 @@ class HttpSocket extends CakeSocket {
|
|||
'Connection' => 'close',
|
||||
'User-Agent' => 'CakePHP'
|
||||
),
|
||||
'raw' => null
|
||||
'raw' => null,
|
||||
'cookies' => array()
|
||||
);
|
||||
/**
|
||||
* The default structure for storing the response
|
||||
|
@ -103,7 +104,8 @@ class HttpSocket extends CakeSocket {
|
|||
'reason-phrase' => null
|
||||
),
|
||||
'header' => array(),
|
||||
'body' => ''
|
||||
'body' => '',
|
||||
'cookies' => array(),
|
||||
);
|
||||
|
||||
/**
|
||||
|
@ -128,7 +130,8 @@ class HttpSocket extends CakeSocket {
|
|||
'method' => 'basic',
|
||||
'user' => null,
|
||||
'pass' => null
|
||||
)
|
||||
),
|
||||
'cookies' => array(),
|
||||
)
|
||||
);
|
||||
|
||||
|
@ -191,8 +194,12 @@ class HttpSocket extends CakeSocket {
|
|||
$this->config['host'] = $host;
|
||||
}
|
||||
|
||||
$cookies = null;
|
||||
if (is_array($this->request['header'])) {
|
||||
$this->request['header'] = $this->parseHeader($this->request['header']);
|
||||
if (!empty($this->request['cookies'])) {
|
||||
$cookies = $this->buildCookies($this->request['cookies']);
|
||||
}
|
||||
$this->request['header'] = am(array('Host' => $this->request['uri']['host']), $this->request['header']);
|
||||
}
|
||||
|
||||
|
@ -208,7 +215,8 @@ class HttpSocket extends CakeSocket {
|
|||
$this->request['header']['Content-Length'] = strlen($this->request['body']);
|
||||
}
|
||||
|
||||
$this->request['header'] = $this->buildHeader($this->request['header']);
|
||||
$connectionType = @$this->request['header']['Connection'];
|
||||
$this->request['header'] = $this->buildHeader($this->request['header']).$cookies;
|
||||
|
||||
if (empty($this->request['line'])) {
|
||||
$this->request['line'] = $this->buildRequestLine($this->request);
|
||||
|
@ -234,7 +242,15 @@ class HttpSocket extends CakeSocket {
|
|||
while ($data = $this->read()) {
|
||||
$response .= $data;
|
||||
}
|
||||
|
||||
if ($connectionType == 'close') {
|
||||
$this->disconnect();
|
||||
}
|
||||
|
||||
$this->response = $this->parseResponse($response);
|
||||
if (!empty($this->response['cookies'])) {
|
||||
$this->config['request']['cookies'] = am($this->config['request']['cookies'], $this->response['cookies']);
|
||||
}
|
||||
return $this->response['body'];
|
||||
}
|
||||
|
||||
|
@ -359,28 +375,31 @@ class HttpSocket extends CakeSocket {
|
|||
}
|
||||
|
||||
$response = $responseTemplate;
|
||||
if (preg_match("/^(.+\r\n)(.*)(?<=\r\n)\r\n/Us", $message, $match)) {
|
||||
list(, $response['raw']['status-line'], $response['raw']['header']) = $match;
|
||||
$response['raw']['response'] = $message;
|
||||
$response['raw']['body'] = substr($message, strlen($match[0]));
|
||||
|
||||
if (preg_match("/(.+) ([0-9]{3}) (.+)\r\n/DU", $response['raw']['status-line'], $match)) {
|
||||
$response['status']['http-version'] = $match[1];
|
||||
$response['status']['code'] = (int)$match[2];
|
||||
$response['status']['reason-phrase'] = $match[3];
|
||||
}
|
||||
|
||||
$response['header'] = $this->parseHeader($response['raw']['header']);
|
||||
$decoded = $this->decodeBody($response['raw']['body'], @$response['header']['Transfer-Encoding']);
|
||||
$response['body'] = $decoded['body'];
|
||||
if (!empty($decoded['header'])) {
|
||||
$response['header'] = $this->parseHeader($this->buildHeader($response['header']).$this->buildHeader($decoded['header']));
|
||||
}
|
||||
|
||||
} else {
|
||||
if (!preg_match("/^(.+\r\n)(.*)(?<=\r\n)\r\n/Us", $message, $match)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
list(, $response['raw']['status-line'], $response['raw']['header']) = $match;
|
||||
$response['raw']['response'] = $message;
|
||||
$response['raw']['body'] = substr($message, strlen($match[0]));
|
||||
|
||||
if (preg_match("/(.+) ([0-9]{3}) (.+)\r\n/DU", $response['raw']['status-line'], $match)) {
|
||||
$response['status']['http-version'] = $match[1];
|
||||
$response['status']['code'] = (int)$match[2];
|
||||
$response['status']['reason-phrase'] = $match[3];
|
||||
}
|
||||
|
||||
$response['header'] = $this->parseHeader($response['raw']['header']);
|
||||
$decoded = $this->decodeBody($response['raw']['body'], @$response['header']['Transfer-Encoding']);
|
||||
$response['body'] = $decoded['body'];
|
||||
if (!empty($decoded['header'])) {
|
||||
$response['header'] = $this->parseHeader($this->buildHeader($response['header']).$this->buildHeader($decoded['header']));
|
||||
}
|
||||
|
||||
if (!empty($response['header'])) {
|
||||
$response['cookies'] = $this->parseCookies($response['header']);
|
||||
}
|
||||
|
||||
foreach ($response['raw'] as $field => $val) {
|
||||
if ($val === '') {
|
||||
$response['raw'][$field] = null;
|
||||
|
@ -809,7 +828,7 @@ class HttpSocket extends CakeSocket {
|
|||
}
|
||||
|
||||
$cookies = array();
|
||||
foreach ($header['Set-Cookie'] as $cookie) {
|
||||
foreach ((array)$header['Set-Cookie'] as $cookie) {
|
||||
$parts = preg_split('/(?<![^;]");[ \t]*/', $cookie);
|
||||
list($name, $value) = explode('=', array_shift($parts));
|
||||
$cookies[$name] = compact('value');
|
||||
|
@ -837,16 +856,29 @@ class HttpSocket extends CakeSocket {
|
|||
function buildCookies($cookies) {
|
||||
$header = array();
|
||||
foreach ($cookies as $name => $cookie) {
|
||||
$serialized = $name.'='.$this->escapeToken($cookie['value'], array(';'));
|
||||
unset($cookie['value']);
|
||||
foreach ($cookie as $key => $val) {
|
||||
$serialized .= ';'.Inflector::camelize($key).'='.$this->escapeToken($val, array(';'));
|
||||
}
|
||||
$header[] = $serialized;
|
||||
$header[] = $name.'='.$this->escapeToken($cookie['value'], array(';'));
|
||||
}
|
||||
$header = $this->buildHeader(array('Cookie' => $header), 'pragmatic');
|
||||
return $header;
|
||||
}
|
||||
/**
|
||||
* undocumented function
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function saveCookies() {
|
||||
|
||||
}
|
||||
/**
|
||||
* undocumented function
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
function loadCookies() {
|
||||
|
||||
}
|
||||
/**
|
||||
* Unescapes a given $token according to RFC 2616 (HTTP 1.1 specs)
|
||||
*
|
||||
|
|
|
@ -104,7 +104,8 @@ class HttpSocketTest extends UnitTestCase {
|
|||
'method' => 'basic'
|
||||
, 'user' => 'bob'
|
||||
, 'pass' => 'secret'
|
||||
)
|
||||
),
|
||||
'cookies' => array(),
|
||||
)
|
||||
);
|
||||
$this->assertIdentical($this->Socket->config, $expected);
|
||||
|
@ -132,7 +133,8 @@ class HttpSocketTest extends UnitTestCase {
|
|||
'method' => 'basic'
|
||||
, 'user' => null
|
||||
, 'pass' => null
|
||||
)
|
||||
),
|
||||
'cookies' => array()
|
||||
)
|
||||
);
|
||||
$this->assertIdentical($this->Socket->config, $expected);
|
||||
|
@ -177,6 +179,7 @@ class HttpSocketTest extends UnitTestCase {
|
|||
,'user' => null
|
||||
,'pass' => null
|
||||
),
|
||||
'cookies' => array(),
|
||||
),
|
||||
)
|
||||
, 'request' => array(
|
||||
|
@ -201,6 +204,7 @@ class HttpSocketTest extends UnitTestCase {
|
|||
, 'line' => "GET /?foo=bar HTTP/1.1\r\n"
|
||||
, 'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n"
|
||||
, 'raw' => ""
|
||||
, 'cookies' => array(),
|
||||
)
|
||||
)
|
||||
)
|
||||
|
@ -312,6 +316,22 @@ class HttpSocketTest extends UnitTestCase {
|
|||
)
|
||||
)
|
||||
)
|
||||
, 10 => array(
|
||||
'request' => array(
|
||||
'method' => 'POST',
|
||||
'uri' => 'https://www.cakephp.org/posts/add',
|
||||
'body' => array('name' => 'HttpSocket-is-released', 'date' => 'today'),
|
||||
'cookies' => array('foo' => array('value' => 'bar'))
|
||||
)
|
||||
, 'expectation' => array(
|
||||
'request' => array(
|
||||
'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 38\r\nCookie: foo=bar\r\n",
|
||||
'cookies' => array(
|
||||
'foo' => array('value' => 'bar'),
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$expectation = array();
|
||||
|
@ -365,6 +385,23 @@ class HttpSocketTest extends UnitTestCase {
|
|||
$this->Socket->expectCallCount('read', 2);
|
||||
$response = $this->Socket->request($request);
|
||||
$this->assertIdentical($response, "<h1>Hello, your lucky number is ".$number."</h1>");
|
||||
|
||||
$this->Socket->reset();
|
||||
$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 cookie test!</h1>";
|
||||
unset($this->Socket->_mock->_return_sequence['read']);
|
||||
$this->Socket->_mock->_call_counts['read'] = 0;
|
||||
$this->Socket->setReturnValueAt(0, 'read', $serverResponse);
|
||||
$this->Socket->connected = true;
|
||||
$this->Socket->request($request);
|
||||
$result = $this->Socket->response['cookies'];
|
||||
$expect = array(
|
||||
'foo' => array(
|
||||
'value' => 'bar'
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expect);
|
||||
$this->assertEqual($this->Socket->config['request']['cookies'], $expect);
|
||||
$this->assertFalse($this->Socket->connected);
|
||||
}
|
||||
|
||||
function testUrl() {
|
||||
|
@ -1086,6 +1123,11 @@ class HttpSocketTest extends UnitTestCase {
|
|||
$expected['cakephp'] = array('value' => 'great', 'secure' => true);
|
||||
$cookies = $this->Socket->parseCookies($header);
|
||||
$this->assertEqual($cookies, $expected);
|
||||
|
||||
$header['Set-Cookie'] = 'foo=bar';
|
||||
unset($expected['people'], $expected['cakephp']);
|
||||
$cookies = $this->Socket->parseCookies($header);
|
||||
$this->assertEqual($cookies, $expected);
|
||||
}
|
||||
/**
|
||||
* undocumented function
|
||||
|
@ -1104,7 +1146,7 @@ class HttpSocketTest extends UnitTestCase {
|
|||
'path' => '/accounts'
|
||||
)
|
||||
);
|
||||
$expect = "Cookie: foo=bar\r\nCookie: people=jim,jack,johnny\";\";Path=/accounts\r\n";
|
||||
$expect = "Cookie: foo=bar\r\nCookie: people=jim,jack,johnny\";\"\r\n";
|
||||
$result = $this->Socket->buildCookies($cookies);
|
||||
$this->assertEqual($result, $expect);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue