mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
Merge with 2.0-http.
This commit is contained in:
commit
c0e2f63f7d
11 changed files with 2120 additions and 1002 deletions
|
@ -100,6 +100,7 @@ class CakeSocket {
|
|||
* Connect the socket to the given host and port.
|
||||
*
|
||||
* @return boolean Success
|
||||
* @throws Exception
|
||||
*/
|
||||
public function connect() {
|
||||
if ($this->connection != null) {
|
||||
|
@ -112,7 +113,6 @@ class CakeSocket {
|
|||
}
|
||||
|
||||
if ($this->config['persistent'] == true) {
|
||||
$tmp = null;
|
||||
$this->connection = @pfsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
|
||||
} else {
|
||||
$this->connection = @fsockopen($scheme.$this->config['host'], $this->config['port'], $errNum, $errStr, $this->config['timeout']);
|
||||
|
@ -120,6 +120,7 @@ class CakeSocket {
|
|||
|
||||
if (!empty($errNum) || !empty($errStr)) {
|
||||
$this->setLastError($errStr, $errNum);
|
||||
throw new Exception($errStr, $errNum);
|
||||
}
|
||||
|
||||
$this->connected = is_resource($this->connection);
|
||||
|
@ -137,9 +138,8 @@ class CakeSocket {
|
|||
public function host() {
|
||||
if (Validation::ip($this->config['host'])) {
|
||||
return gethostbyaddr($this->config['host']);
|
||||
} else {
|
||||
return gethostbyaddr($this->address());
|
||||
}
|
||||
return gethostbyaddr($this->address());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -150,9 +150,8 @@ class CakeSocket {
|
|||
public function address() {
|
||||
if (Validation::ip($this->config['host'])) {
|
||||
return $this->config['host'];
|
||||
} else {
|
||||
return gethostbyname($this->config['host']);
|
||||
}
|
||||
return gethostbyname($this->config['host']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -163,9 +162,8 @@ class CakeSocket {
|
|||
public function addresses() {
|
||||
if (Validation::ip($this->config['host'])) {
|
||||
return array($this->config['host']);
|
||||
} else {
|
||||
return gethostbynamel($this->config['host']);
|
||||
}
|
||||
return gethostbynamel($this->config['host']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -176,9 +174,8 @@ class CakeSocket {
|
|||
public function lastError() {
|
||||
if (!empty($this->lastError)) {
|
||||
return $this->lastError['num'] . ': ' . $this->lastError['str'];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -186,6 +183,7 @@ class CakeSocket {
|
|||
*
|
||||
* @param integer $errNum Error code
|
||||
* @param string $errStr Error string
|
||||
* @return void
|
||||
*/
|
||||
public function setLastError($errNum, $errStr) {
|
||||
$this->lastError = array('num' => $errNum, 'str' => $errStr);
|
||||
|
@ -229,17 +227,8 @@ class CakeSocket {
|
|||
return false;
|
||||
}
|
||||
return $buffer;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Abort socket operation.
|
||||
*
|
||||
* @return boolean Success
|
||||
*/
|
||||
public function abort() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -272,6 +261,7 @@ class CakeSocket {
|
|||
/**
|
||||
* Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)
|
||||
*
|
||||
* @param array $state Array with key and values to reset
|
||||
* @return boolean True on success
|
||||
*/
|
||||
public function reset($state = null) {
|
||||
|
|
68
cake/libs/http/basic_authentication.php
Normal file
68
cake/libs/http/basic_authentication.php
Normal file
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
/**
|
||||
* Basic authentication
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.http
|
||||
* @since CakePHP(tm) v 2.0.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Basic authentication
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.http
|
||||
*/
|
||||
class BasicAuthentication {
|
||||
|
||||
/**
|
||||
* Authentication
|
||||
*
|
||||
* @param HttpSocket $http
|
||||
* @param array $authInfo
|
||||
* @return void
|
||||
* @see http://www.ietf.org/rfc/rfc2617.txt
|
||||
*/
|
||||
public static function authentication(HttpSocket $http, &$authInfo) {
|
||||
if (isset($authInfo['user'], $authInfo['pass'])) {
|
||||
$http->request['header']['Authorization'] = self::_generateHeader($authInfo['user'], $authInfo['pass']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Proxy Authentication
|
||||
*
|
||||
* @param HttpSocket $http
|
||||
* @param array $proxyInfo
|
||||
* @return void
|
||||
* @see http://www.ietf.org/rfc/rfc2617.txt
|
||||
*/
|
||||
public static function proxyAuthentication(HttpSocket $http, &$proxyInfo) {
|
||||
if (isset($proxyInfo['user'], $proxyInfo['pass'])) {
|
||||
$http->request['header']['Proxy-Authorization'] = self::_generateHeader($proxyInfo['user'], $proxyInfo['pass']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate basic [proxy] authentication header
|
||||
*
|
||||
* @param string $user
|
||||
* @param string $pass
|
||||
* @return string
|
||||
*/
|
||||
protected static function _generateHeader($user, $pass) {
|
||||
return 'Basic ' . base64_encode($user . ':' . $pass);
|
||||
}
|
||||
|
||||
}
|
106
cake/libs/http/digest_authentication.php
Normal file
106
cake/libs/http/digest_authentication.php
Normal file
|
@ -0,0 +1,106 @@
|
|||
<?php
|
||||
/**
|
||||
* Digest authentication
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.http
|
||||
* @since CakePHP(tm) v 2.0.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Digest authentication
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.http
|
||||
*/
|
||||
class DigestAuthentication {
|
||||
|
||||
/**
|
||||
* Authentication
|
||||
*
|
||||
* @param HttpSocket $http
|
||||
* @param array $authInfo
|
||||
* @return void
|
||||
* @link http://www.ietf.org/rfc/rfc2617.txt
|
||||
*/
|
||||
public static function authentication(HttpSocket $http, &$authInfo) {
|
||||
if (isset($authInfo['user'], $authInfo['pass'])) {
|
||||
if (!isset($authInfo['realm']) && !self::_getServerInformation($http, $authInfo)) {
|
||||
return;
|
||||
}
|
||||
$http->request['header']['Authorization'] = self::_generateHeader($http, $authInfo);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrive information about the authetication
|
||||
*
|
||||
* @param HttpSocket $http
|
||||
* @parma array $authInfo
|
||||
* @return boolean
|
||||
*/
|
||||
protected static function _getServerInformation(HttpSocket $http, &$authInfo) {
|
||||
$originalRequest = $http->request;
|
||||
$http->configAuth(false);
|
||||
$http->request($http->request);
|
||||
$http->request = $originalRequest;
|
||||
$http->configAuth('Digest', $authInfo);
|
||||
|
||||
if (empty($http->response['header']['WWW-Authenticate'])) {
|
||||
return false;
|
||||
}
|
||||
preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $http->response['header']['WWW-Authenticate'], $matches, PREG_SET_ORDER);
|
||||
foreach ($matches as $match) {
|
||||
$authInfo[$match[1]] = $match[2];
|
||||
}
|
||||
if (!empty($authInfo['qop']) && empty($authInfo['nc'])) {
|
||||
$authInfo['nc'] = 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the header Authorization
|
||||
*
|
||||
* @param HttpSocket $http
|
||||
* @param array $authInfo
|
||||
* @return string
|
||||
*/
|
||||
protected static function _generateHeader(HttpSocket $http, &$authInfo) {
|
||||
$a1 = md5($authInfo['user'] . ':' . $authInfo['realm'] . ':' . $authInfo['pass']);
|
||||
$a2 = md5($http->request['method'] . ':' . $http->request['uri']['path']);
|
||||
|
||||
if (empty($authInfo['qop'])) {
|
||||
$response = md5($a1 . ':' . $authInfo['nonce'] . ':' . $a2);
|
||||
} else {
|
||||
$authInfo['cnonce'] = uniqid();
|
||||
$nc = sprintf('%08x', $authInfo['nc']++);
|
||||
$response = md5($a1 . ':' . $authInfo['nonce'] . ':' . $nc . ':' . $authInfo['cnonce'] . ':auth:' . $a2);
|
||||
}
|
||||
|
||||
$authHeader = 'Digest ';
|
||||
$authHeader .= 'username="' . str_replace(array('\\', '"'), array('\\\\', '\\"'), $authInfo['user']) . '", ';
|
||||
$authHeader .= 'realm="' . $authInfo['realm'] . '", ';
|
||||
$authHeader .= 'nonce="' . $authInfo['nonce'] . '", ';
|
||||
$authHeader .= 'uri="' . $http->request['uri']['path'] . '", ';
|
||||
$authHeader .= 'response="' . $response . '"';
|
||||
if (!empty($authInfo['opaque'])) {
|
||||
$authHeader .= ', opaque="' . $authInfo['opaque'] . '"';
|
||||
}
|
||||
if (!empty($authInfo['qop'])) {
|
||||
$authHeader .= ', qop="auth", nc=' . $nc . ', cnonce="' . $authInfo['cnonce'] . '"';
|
||||
}
|
||||
return $authHeader;
|
||||
}
|
||||
}
|
438
cake/libs/http_response.php
Normal file
438
cake/libs/http_response.php
Normal file
|
@ -0,0 +1,438 @@
|
|||
<?php
|
||||
/**
|
||||
* HTTP Response from HttpSocket.
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs
|
||||
* @since CakePHP(tm) v 2.0.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
class HttpResponse implements ArrayAccess {
|
||||
|
||||
/**
|
||||
* Body content
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $body = '';
|
||||
|
||||
/**
|
||||
* Headers
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $headers = array();
|
||||
|
||||
/**
|
||||
* Cookies
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $cookies = array();
|
||||
|
||||
/**
|
||||
* HTTP version
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $httpVersion = 'HTTP/1.1';
|
||||
|
||||
/**
|
||||
* Response code
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
public $code = 0;
|
||||
|
||||
/**
|
||||
* Reason phrase
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $reasonPhrase = '';
|
||||
|
||||
/**
|
||||
* Pure raw content
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $raw = '';
|
||||
|
||||
/**
|
||||
* Contructor
|
||||
*
|
||||
*/
|
||||
public function __construct($message = null) {
|
||||
if ($message !== null) {
|
||||
$this->parseResponse($message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Body content
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function body() {
|
||||
return (string)$this->body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get header in case insensitive
|
||||
*
|
||||
* @param string $name Header name
|
||||
* @return mixed String if header exists or null
|
||||
*/
|
||||
public function getHeader($name, $headers = null) {
|
||||
if (!is_array($headers)) {
|
||||
$headers =& $this->headers;
|
||||
}
|
||||
if (isset($headers[$name])) {
|
||||
return $headers[$name];
|
||||
}
|
||||
foreach ($headers as $key => $value) {
|
||||
if (strcasecmp($key, $name) == 0) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* If return is 200 (OK)
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isOk() {
|
||||
return $this->code == 200;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given message and breaks it down in parts.
|
||||
*
|
||||
* @param string $message Message to parse
|
||||
* @return void
|
||||
* @throw Exception
|
||||
*/
|
||||
public function parseResponse($message) {
|
||||
if (!is_string($message)) {
|
||||
throw new Exception(__('Invalid response.'));
|
||||
}
|
||||
|
||||
if (!preg_match("/^(.+\r\n)(.*)(?<=\r\n)\r\n/Us", $message, $match)) {
|
||||
throw new Exception(__('Invalid HTTP response.'));
|
||||
}
|
||||
|
||||
list(, $statusLine, $header) = $match;
|
||||
$this->raw = $message;
|
||||
$this->body = (string)substr($message, strlen($match[0]));
|
||||
|
||||
if (preg_match("/(.+) ([0-9]{3}) (.+)\r\n/DU", $statusLine, $match)) {
|
||||
$this->httpVersion = $match[1];
|
||||
$this->code = $match[2];
|
||||
$this->reasonPhrase = $match[3];
|
||||
}
|
||||
|
||||
$this->headers = $this->_parseHeader($header);
|
||||
$transferEncoding = $this->getHeader('Transfer-Encoding');
|
||||
$decoded = $this->_decodeBody($this->body, $transferEncoding);
|
||||
$this->body = $decoded['body'];
|
||||
|
||||
if (!empty($decoded['header'])) {
|
||||
$this->headers = $this->_parseHeader($this->_buildHeader($this->headers) . $this->_buildHeader($decoded['header']));
|
||||
}
|
||||
|
||||
if (!empty($this->headers)) {
|
||||
$this->cookies = $this->parseCookies($this->headers);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic function to decode a $body with a given $encoding. Returns either an array with the keys
|
||||
* 'body' and 'header' or false on failure.
|
||||
*
|
||||
* @param string $body A string continaing the body to decode.
|
||||
* @param mixed $encoding Can be false in case no encoding is being used, or a string representing the encoding.
|
||||
* @return mixed Array of response headers and body or false.
|
||||
*/
|
||||
protected function _decodeBody($body, $encoding = 'chunked') {
|
||||
if (!is_string($body)) {
|
||||
return false;
|
||||
}
|
||||
if (empty($encoding)) {
|
||||
return array('body' => $body, 'header' => false);
|
||||
}
|
||||
$decodeMethod = '_decode' . Inflector::camelize(str_replace('-', '_', $encoding)) . 'Body';
|
||||
|
||||
if (!is_callable(array(&$this, $decodeMethod))) {
|
||||
return array('body' => $body, 'header' => false);
|
||||
}
|
||||
return $this->{$decodeMethod}($body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a chunked message $body and returns either an array with the keys 'body' and 'header' or false as
|
||||
* a result.
|
||||
*
|
||||
* @param string $body A string continaing the chunked body to decode.
|
||||
* @return mixed Array of response headers and body or false.
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function _decodeChunkedBody($body) {
|
||||
if (!is_string($body)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$decodedBody = null;
|
||||
$chunkLength = null;
|
||||
|
||||
while ($chunkLength !== 0) {
|
||||
if (!preg_match("/^([0-9a-f]+) *(?:;(.+)=(.+))?\r\n/iU", $body, $match)) {
|
||||
throw new Exception(__('HttpSocket::_decodeChunkedBody - Could not parse malformed chunk.'));
|
||||
}
|
||||
|
||||
$chunkSize = 0;
|
||||
$hexLength = 0;
|
||||
$chunkExtensionName = '';
|
||||
$chunkExtensionValue = '';
|
||||
if (isset($match[0])) {
|
||||
$chunkSize = $match[0];
|
||||
}
|
||||
if (isset($match[1])) {
|
||||
$hexLength = $match[1];
|
||||
}
|
||||
if (isset($match[2])) {
|
||||
$chunkExtensionName = $match[2];
|
||||
}
|
||||
if (isset($match[3])) {
|
||||
$chunkExtensionValue = $match[3];
|
||||
}
|
||||
|
||||
$body = substr($body, strlen($chunkSize));
|
||||
$chunkLength = hexdec($hexLength);
|
||||
$chunk = substr($body, 0, $chunkLength);
|
||||
if (!empty($chunkExtensionName)) {
|
||||
/**
|
||||
* @todo See if there are popular chunk extensions we should implement
|
||||
*/
|
||||
}
|
||||
$decodedBody .= $chunk;
|
||||
if ($chunkLength !== 0) {
|
||||
$body = substr($body, $chunkLength + strlen("\r\n"));
|
||||
}
|
||||
}
|
||||
|
||||
$entityHeader = false;
|
||||
if (!empty($body)) {
|
||||
$entityHeader = $this->_parseHeader($body);
|
||||
}
|
||||
return array('body' => $decodedBody, 'header' => $entityHeader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an array based header.
|
||||
*
|
||||
* @param array $header Header as an indexed array (field => value)
|
||||
* @return array Parsed header
|
||||
*/
|
||||
protected function _parseHeader($header) {
|
||||
if (is_array($header)) {
|
||||
return $header;
|
||||
} elseif (!is_string($header)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
preg_match_all("/(.+):(.+)(?:(?<![\t ])\r\n|\$)/Uis", $header, $matches, PREG_SET_ORDER);
|
||||
|
||||
$header = array();
|
||||
foreach ($matches as $match) {
|
||||
list(, $field, $value) = $match;
|
||||
|
||||
$value = trim($value);
|
||||
$value = preg_replace("/[\t ]\r\n/", "\r\n", $value);
|
||||
|
||||
$field = $this->_unescapeToken($field);
|
||||
|
||||
if (!isset($header[$field])) {
|
||||
$header[$field] = $value;
|
||||
} else {
|
||||
$header[$field] = array_merge((array)$header[$field], (array)$value);
|
||||
}
|
||||
}
|
||||
return $header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses cookies in response headers.
|
||||
*
|
||||
* @param array $header Header array containing one ore more 'Set-Cookie' headers.
|
||||
* @return mixed Either false on no cookies, or an array of cookies recieved.
|
||||
* @todo Make this 100% RFC 2965 confirm
|
||||
*/
|
||||
public function parseCookies($header) {
|
||||
$cookieHeader = $this->getHeader('Set-Cookie', $header);
|
||||
if (!$cookieHeader) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$cookies = array();
|
||||
foreach ((array)$cookieHeader as $cookie) {
|
||||
if (strpos($cookie, '";"') !== false) {
|
||||
$cookie = str_replace('";"', "{__cookie_replace__}", $cookie);
|
||||
$parts = str_replace("{__cookie_replace__}", '";"', explode(';', $cookie));
|
||||
} else {
|
||||
$parts = preg_split('/\;[ \t]*/', $cookie);
|
||||
}
|
||||
|
||||
list($name, $value) = explode('=', array_shift($parts), 2);
|
||||
$cookies[$name] = compact('value');
|
||||
|
||||
foreach ($parts as $part) {
|
||||
if (strpos($part, '=') !== false) {
|
||||
list($key, $value) = explode('=', $part);
|
||||
} else {
|
||||
$key = $part;
|
||||
$value = true;
|
||||
}
|
||||
|
||||
$key = strtolower($key);
|
||||
if (!isset($cookies[$name][$key])) {
|
||||
$cookies[$name][$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $cookies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unescapes a given $token according to RFC 2616 (HTTP 1.1 specs)
|
||||
*
|
||||
* @param string $token Token to unescape
|
||||
* @param array $chars
|
||||
* @return string Unescaped token
|
||||
* @todo Test $chars parameter
|
||||
*/
|
||||
protected function _unescapeToken($token, $chars = null) {
|
||||
$regex = '/"([' . implode('', $this->_tokenEscapeChars(true, $chars)) . '])"/';
|
||||
$token = preg_replace($regex, '\\1', $token);
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets escape chars according to RFC 2616 (HTTP 1.1 specs).
|
||||
*
|
||||
* @param boolean $hex true to get them as HEX values, false otherwise
|
||||
* @param array $chars
|
||||
* @return array Escape chars
|
||||
* @todo Test $chars parameter
|
||||
*/
|
||||
protected function _tokenEscapeChars($hex = true, $chars = null) {
|
||||
if (!empty($chars)) {
|
||||
$escape = $chars;
|
||||
} else {
|
||||
$escape = array('"', "(", ")", "<", ">", "@", ",", ";", ":", "\\", "/", "[", "]", "?", "=", "{", "}", " ");
|
||||
for ($i = 0; $i <= 31; $i++) {
|
||||
$escape[] = chr($i);
|
||||
}
|
||||
$escape[] = chr(127);
|
||||
}
|
||||
|
||||
if ($hex == false) {
|
||||
return $escape;
|
||||
}
|
||||
$regexChars = '';
|
||||
foreach ($escape as $key => $char) {
|
||||
$escape[$key] = '\\x' . str_pad(dechex(ord($char)), 2, '0', STR_PAD_LEFT);
|
||||
}
|
||||
return $escape;
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess - Offset Exists
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @return boolean
|
||||
*/
|
||||
public function offsetExists($offset) {
|
||||
return in_array($offset, array('raw', 'status', 'header', 'body', 'cookies'));
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess - Offset Get
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @return mixed
|
||||
*/
|
||||
public function offsetGet($offset) {
|
||||
switch ($offset) {
|
||||
case 'raw':
|
||||
$firstLineLength = strpos($this->raw, "\r\n") + 2;
|
||||
if ($this->raw[$firstLineLength] === "\r") {
|
||||
$header = null;
|
||||
} else {
|
||||
$header = substr($this->raw, $firstLineLength, strpos($this->raw, "\r\n\r\n") - $firstLineLength) . "\r\n";
|
||||
}
|
||||
return array(
|
||||
'status-line' => $this->httpVersion . ' ' . $this->code . ' ' . $this->reasonPhrase . "\r\n",
|
||||
'header' => $header,
|
||||
'body' => $this->body,
|
||||
'response' => $this->raw
|
||||
);
|
||||
case 'status':
|
||||
return array(
|
||||
'http-version' => $this->httpVersion,
|
||||
'code' => $this->code,
|
||||
'reason-phrase' => $this->reasonPhrase
|
||||
);
|
||||
case 'header':
|
||||
return $this->headers;
|
||||
case 'body':
|
||||
return $this->body;
|
||||
case 'cookies':
|
||||
return $this->cookies;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess - 0ffset Set
|
||||
*
|
||||
* @param mixed $offset
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function offsetSet($offset, $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess - Offset Unset
|
||||
*
|
||||
* @param mixed @offset
|
||||
* @return void
|
||||
*/
|
||||
public function offsetUnset($offset) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instance as string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString() {
|
||||
return $this->body();
|
||||
}
|
||||
|
||||
}
|
|
@ -31,29 +31,19 @@ App::import('Core', 'Router');
|
|||
*/
|
||||
class HttpSocket extends CakeSocket {
|
||||
|
||||
/**
|
||||
* Object description
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
*/
|
||||
public $description = 'HTTP-based DataSource Interface';
|
||||
|
||||
/**
|
||||
* When one activates the $quirksMode by setting it to true, all checks meant to
|
||||
* enforce RFC 2616 (HTTP/1.1 specs).
|
||||
* will be disabled and additional measures to deal with non-standard responses will be enabled.
|
||||
*
|
||||
* @var boolean
|
||||
* @access public
|
||||
*/
|
||||
public $quirksMode = false;
|
||||
|
||||
/**
|
||||
* The default values to use for a request
|
||||
* Contain information about the last request (read only)
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
public $request = array(
|
||||
'method' => 'GET',
|
||||
|
@ -67,11 +57,6 @@ class HttpSocket extends CakeSocket {
|
|||
'query' => null,
|
||||
'fragment' => null
|
||||
),
|
||||
'auth' => array(
|
||||
'method' => 'Basic',
|
||||
'user' => null,
|
||||
'pass' => null
|
||||
),
|
||||
'version' => '1.1',
|
||||
'body' => '',
|
||||
'line' => null,
|
||||
|
@ -84,33 +69,23 @@ class HttpSocket extends CakeSocket {
|
|||
);
|
||||
|
||||
/**
|
||||
* The default structure for storing the response
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
public $response = array(
|
||||
'raw' => array(
|
||||
'status-line' => null,
|
||||
'header' => null,
|
||||
'body' => null,
|
||||
'response' => null
|
||||
),
|
||||
'status' => array(
|
||||
'http-version' => null,
|
||||
'code' => null,
|
||||
'reason-phrase' => null
|
||||
),
|
||||
'header' => array(),
|
||||
'body' => '',
|
||||
'cookies' => array()
|
||||
);
|
||||
|
||||
/**
|
||||
* Default configuration settings for the HttpSocket
|
||||
* Contain information about the last response (read only)
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $response = null;
|
||||
|
||||
/**
|
||||
* Response classname
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $responseClass = 'HttpResponse';
|
||||
|
||||
/**
|
||||
* Configuration settings for the HttpSocket and the requests
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
*/
|
||||
public $config = array(
|
||||
'persistent' => false,
|
||||
|
@ -124,22 +99,30 @@ class HttpSocket extends CakeSocket {
|
|||
'host' => 'localhost',
|
||||
'port' => 80
|
||||
),
|
||||
'auth' => array(
|
||||
'method' => 'Basic',
|
||||
'user' => null,
|
||||
'pass' => null
|
||||
),
|
||||
'cookies' => array()
|
||||
)
|
||||
);
|
||||
|
||||
/**
|
||||
* String that represents a line break.
|
||||
* Authentication settings
|
||||
*
|
||||
* @var string
|
||||
* @access public
|
||||
* @var array
|
||||
*/
|
||||
public $lineBreak = "\r\n";
|
||||
protected $_auth = array();
|
||||
|
||||
/**
|
||||
* Proxy settings
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_proxy = array();
|
||||
|
||||
/**
|
||||
* Resource to receive the content of request
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected $_contentResource = null;
|
||||
|
||||
/**
|
||||
* Build an HTTP Socket using the specified configuration.
|
||||
|
@ -175,12 +158,72 @@ class HttpSocket extends CakeSocket {
|
|||
parent::__construct($this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set authentication settings
|
||||
*
|
||||
* @param string $method Authentication method (ie. Basic, Digest). If empty, disable authentication
|
||||
* @param mixed $user Username for authentication. Can be an array with settings to authentication class
|
||||
* @param string $pass Password for authentication
|
||||
* @return void
|
||||
*/
|
||||
public function configAuth($method, $user = null, $pass = null) {
|
||||
if (empty($method)) {
|
||||
$this->_auth = array();
|
||||
return;
|
||||
}
|
||||
if (is_array($user)) {
|
||||
$this->_auth = array($method => $user);
|
||||
return;
|
||||
}
|
||||
$this->_auth = array($method => compact('user', 'pass'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set proxy settings
|
||||
*
|
||||
* @param mixed $host Proxy host. Can be an array with settings to authentication class
|
||||
* @param integer $port Port. Default 3128.
|
||||
* @param string $method Proxy method (ie, Basic, Digest). If empty, disable proxy authentication
|
||||
* @param string $user Username if your proxy need authentication
|
||||
* @param string $pass Password to proxy authentication
|
||||
* @return void
|
||||
*/
|
||||
public function configProxy($host, $port = 3128, $method = null, $user = null, $pass = null) {
|
||||
if (empty($host)) {
|
||||
$this->_proxy = array();
|
||||
return;
|
||||
}
|
||||
if (is_array($host)) {
|
||||
$this->_proxy = $host + array('host' => null);
|
||||
return;
|
||||
}
|
||||
$this->_proxy = compact('host', 'port', 'method', 'user', 'pass');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the resource to receive the request content. This resource must support fwrite.
|
||||
*
|
||||
* @param mixed $resource Resource or false to disable the resource use
|
||||
* @return void
|
||||
* @throw Exception
|
||||
*/
|
||||
public function setContentResource($resource) {
|
||||
if ($resource === false) {
|
||||
$this->_contentResource = null;
|
||||
return;
|
||||
}
|
||||
if (!is_resource($resource)) {
|
||||
throw new Exception(__('Invalid resource.'));
|
||||
}
|
||||
$this->_contentResource = $resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Issue the specified request. HttpSocket::get() and HttpSocket::post() wrap this
|
||||
* method and provide a more granular interface.
|
||||
*
|
||||
* @param mixed $request Either an URI string, or an array defining host/uri
|
||||
* @return mixed false on error, request body on success
|
||||
* @return mixed false on error, HttpResponse on success
|
||||
*/
|
||||
public function request($request = array()) {
|
||||
$this->reset(false);
|
||||
|
@ -195,10 +238,6 @@ class HttpSocket extends CakeSocket {
|
|||
$request['uri'] = null;
|
||||
}
|
||||
$uri = $this->_parseUri($request['uri']);
|
||||
$hadAuth = false;
|
||||
if (is_array($uri) && array_key_exists('user', $uri)) {
|
||||
$hadAuth = true;
|
||||
}
|
||||
if (!isset($uri['host'])) {
|
||||
$host = $this->config['host'];
|
||||
}
|
||||
|
@ -208,25 +247,33 @@ class HttpSocket extends CakeSocket {
|
|||
}
|
||||
$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);
|
||||
|
||||
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']);
|
||||
|
||||
$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;
|
||||
}
|
||||
$this->_setProxy();
|
||||
$this->request['proxy'] = $this->_proxy;
|
||||
|
||||
$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']);
|
||||
}
|
||||
$Host = $this->request['uri']['host'];
|
||||
$schema = '';
|
||||
$port = 0;
|
||||
if (isset($this->request['uri']['schema'])) {
|
||||
|
@ -245,12 +292,11 @@ class HttpSocket extends CakeSocket {
|
|||
$this->request['header'] = array_merge(compact('Host'), $this->request['header']);
|
||||
}
|
||||
|
||||
if (isset($this->request['auth']['user']) && isset($this->request['auth']['pass'])) {
|
||||
$this->request['header']['Authorization'] = $this->request['auth']['method'] . " " . base64_encode($this->request['auth']['user'] . ":" . $this->request['auth']['pass']);
|
||||
}
|
||||
if (isset($this->request['uri']['user']) && isset($this->request['uri']['pass'])) {
|
||||
$this->request['header']['Authorization'] = $this->request['auth']['method'] . " " . base64_encode($this->request['uri']['user'] . ":" . $this->request['uri']['pass']);
|
||||
if (isset($this->request['uri']['user'], $this->request['uri']['pass'])) {
|
||||
$this->configAuth('Basic', $this->request['uri']['user'], $this->request['uri']['pass']);
|
||||
}
|
||||
$this->_setAuth();
|
||||
$this->request['auth'] = $this->_auth;
|
||||
|
||||
if (is_array($this->request['body'])) {
|
||||
$this->request['body'] = $this->_httpSerialize($this->request['body']);
|
||||
|
@ -275,9 +321,10 @@ class HttpSocket extends CakeSocket {
|
|||
}
|
||||
|
||||
if ($this->quirksMode === false && $this->request['line'] === false) {
|
||||
return $this->response = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->request['raw'] = '';
|
||||
if ($this->request['line'] !== false) {
|
||||
$this->request['raw'] = $this->request['line'];
|
||||
}
|
||||
|
@ -291,20 +338,46 @@ class HttpSocket extends CakeSocket {
|
|||
$this->write($this->request['raw']);
|
||||
|
||||
$response = null;
|
||||
$inHeader = true;
|
||||
while ($data = $this->read()) {
|
||||
$response .= $data;
|
||||
if ($this->_contentResource) {
|
||||
if ($inHeader) {
|
||||
$response .= $data;
|
||||
$pos = strpos($response, "\r\n\r\n");
|
||||
if ($pos !== false) {
|
||||
$pos += 4;
|
||||
$data = substr($response, $pos);
|
||||
fwrite($this->_contentResource, $data);
|
||||
|
||||
$response = substr($response, 0, $pos);
|
||||
$inHeader = false;
|
||||
}
|
||||
} else {
|
||||
fwrite($this->_contentResource, $data);
|
||||
fflush($this->_contentResource);
|
||||
}
|
||||
} else {
|
||||
$response .= $data;
|
||||
}
|
||||
}
|
||||
|
||||
if ($connectionType == 'close') {
|
||||
if ($connectionType === 'close') {
|
||||
$this->disconnect();
|
||||
}
|
||||
|
||||
$this->response = $this->_parseResponse($response);
|
||||
if (!empty($this->response['cookies'])) {
|
||||
$this->config['request']['cookies'] = array_merge($this->config['request']['cookies'], $this->response['cookies']);
|
||||
if (!App::import('Lib', $this->responseClass)) {
|
||||
throw new Exception(__('Class %s not found.', $this->responseClass));
|
||||
}
|
||||
$responseClass = $this->responseClass;
|
||||
$this->response = new $responseClass($response);
|
||||
if (!empty($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'];
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -394,7 +467,7 @@ class HttpSocket extends CakeSocket {
|
|||
}
|
||||
|
||||
/**
|
||||
* Normalizes urls into a $uriTemplate. If no template is provided
|
||||
* Normalizes urls into a $uriTemplate. If no template is provided
|
||||
* a default one will be used. Will generate the url using the
|
||||
* current config information.
|
||||
*
|
||||
|
@ -426,10 +499,10 @@ class HttpSocket extends CakeSocket {
|
|||
}
|
||||
if (is_string($url)) {
|
||||
if ($url{0} == '/') {
|
||||
$url = $this->config['request']['uri']['host'].':'.$this->config['request']['uri']['port'] . $url;
|
||||
$url = $this->config['request']['uri']['host'] . ':' . $this->config['request']['uri']['port'] . $url;
|
||||
}
|
||||
if (!preg_match('/^.+:\/\/|\*|^\//', $url)) {
|
||||
$url = $this->config['request']['uri']['scheme'].'://'.$url;
|
||||
$url = $this->config['request']['uri']['scheme'] . '://' . $url;
|
||||
}
|
||||
} elseif (!is_array($url) && !empty($url)) {
|
||||
return false;
|
||||
|
@ -449,159 +522,57 @@ class HttpSocket extends CakeSocket {
|
|||
}
|
||||
|
||||
/**
|
||||
* Parses the given message and breaks it down in parts.
|
||||
* Set authentication in request
|
||||
*
|
||||
* @param string $message Message to parse
|
||||
* @return array Parsed message (with indexed elements such as raw, status, header, body)
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function _parseResponse($message) {
|
||||
if (is_array($message)) {
|
||||
return $message;
|
||||
} elseif (!is_string($message)) {
|
||||
return false;
|
||||
protected function _setAuth() {
|
||||
if (empty($this->_auth)) {
|
||||
return;
|
||||
}
|
||||
|
||||
static $responseTemplate;
|
||||
|
||||
if (empty($responseTemplate)) {
|
||||
$classVars = get_class_vars(__CLASS__);
|
||||
$responseTemplate = $classVars['response'];
|
||||
$method = key($this->_auth);
|
||||
$authClass = Inflector::camelize($method) . 'Authentication';
|
||||
if (!App::import('Lib', 'http/' . $authClass)) {
|
||||
throw new Exception(__('Unknown authentication method.'));
|
||||
}
|
||||
|
||||
$response = $responseTemplate;
|
||||
|
||||
if (!preg_match("/^(.+\r\n)(.*)(?<=\r\n)\r\n/Us", $message, $match)) {
|
||||
return false;
|
||||
if (!method_exists($authClass, 'authentication')) {
|
||||
throw new Exception(sprintf(__('The %s do not support authentication.'), $authClass));
|
||||
}
|
||||
|
||||
list($null, $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']);
|
||||
$transferEncoding = null;
|
||||
if (isset($response['header']['Transfer-Encoding'])) {
|
||||
$transferEncoding = $response['header']['Transfer-Encoding'];
|
||||
}
|
||||
$decoded = $this->_decodeBody($response['raw']['body'], $transferEncoding);
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
call_user_func("$authClass::authentication", $this, &$this->_auth[$method]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic function to decode a $body with a given $encoding. Returns either an array with the keys
|
||||
* 'body' and 'header' or false on failure.
|
||||
* Set the proxy configuration and authentication
|
||||
*
|
||||
* @param string $body A string continaing the body to decode.
|
||||
* @param mixed $encoding Can be false in case no encoding is being used, or a string representing the encoding.
|
||||
* @return mixed Array of response headers and body or false.
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function _decodeBody($body, $encoding = 'chunked') {
|
||||
if (!is_string($body)) {
|
||||
return false;
|
||||
protected function _setProxy() {
|
||||
if (empty($this->_proxy) || !isset($this->_proxy['host'], $this->_proxy['port'])) {
|
||||
return;
|
||||
}
|
||||
if (empty($encoding)) {
|
||||
return array('body' => $body, 'header' => false);
|
||||
$this->config['host'] = $this->_proxy['host'];
|
||||
$this->config['port'] = $this->_proxy['port'];
|
||||
|
||||
if (empty($this->_proxy['method']) || !isset($this->_proxy['user'], $this->_proxy['pass'])) {
|
||||
return;
|
||||
}
|
||||
$decodeMethod = '_decode'.Inflector::camelize(str_replace('-', '_', $encoding)).'Body';
|
||||
|
||||
if (!is_callable(array(&$this, $decodeMethod))) {
|
||||
if (!$this->quirksMode) {
|
||||
trigger_error(__('HttpSocket::_decodeBody - Unknown encoding: %s. Activate quirks mode to surpress error.', h($encoding)), E_USER_WARNING);
|
||||
}
|
||||
return array('body' => $body, 'header' => false);
|
||||
$authClass = Inflector::camelize($this->_proxy['method']) . 'Authentication';
|
||||
if (!App::import('Lib', 'http/' . $authClass)) {
|
||||
throw new Exception(__('Unknown authentication method for proxy.'));
|
||||
}
|
||||
return $this->{$decodeMethod}($body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a chunked message $body and returns either an array with the keys 'body' and 'header' or false as
|
||||
* a result.
|
||||
*
|
||||
* @param string $body A string continaing the chunked body to decode.
|
||||
* @return mixed Array of response headers and body or false.
|
||||
*/
|
||||
protected function _decodeChunkedBody($body) {
|
||||
if (!is_string($body)) {
|
||||
return false;
|
||||
if (!method_exists($authClass, 'proxyAuthentication')) {
|
||||
throw new Exception(sprintf(__('The %s do not support proxy authentication.'), $authClass));
|
||||
}
|
||||
|
||||
$decodedBody = null;
|
||||
$chunkLength = null;
|
||||
|
||||
while ($chunkLength !== 0) {
|
||||
if (!preg_match("/^([0-9a-f]+) *(?:;(.+)=(.+))?\r\n/iU", $body, $match)) {
|
||||
if (!$this->quirksMode) {
|
||||
trigger_error(__('HttpSocket::_decodeChunkedBody - Could not parse malformed chunk. Activate quirks mode to do this.'), E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
$chunkSize = 0;
|
||||
$hexLength = 0;
|
||||
$chunkExtensionName = '';
|
||||
$chunkExtensionValue = '';
|
||||
if (isset($match[0])) {
|
||||
$chunkSize = $match[0];
|
||||
}
|
||||
if (isset($match[1])) {
|
||||
$hexLength = $match[1];
|
||||
}
|
||||
if (isset($match[2])) {
|
||||
$chunkExtensionName = $match[2];
|
||||
}
|
||||
if (isset($match[3])) {
|
||||
$chunkExtensionValue = $match[3];
|
||||
}
|
||||
|
||||
$body = substr($body, strlen($chunkSize));
|
||||
$chunkLength = hexdec($hexLength);
|
||||
$chunk = substr($body, 0, $chunkLength);
|
||||
if (!empty($chunkExtensionName)) {
|
||||
/**
|
||||
* @todo See if there are popular chunk extensions we should implement
|
||||
*/
|
||||
}
|
||||
$decodedBody .= $chunk;
|
||||
if ($chunkLength !== 0) {
|
||||
$body = substr($body, $chunkLength+strlen("\r\n"));
|
||||
}
|
||||
}
|
||||
|
||||
$entityHeader = false;
|
||||
if (!empty($body)) {
|
||||
$entityHeader = $this->_parseHeader($body);
|
||||
}
|
||||
return array('body' => $decodedBody, 'header' => $entityHeader);
|
||||
call_user_func("$authClass::proxyAuthentication", $this, &$this->_proxy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses and sets the specified URI into current request configuration.
|
||||
*
|
||||
* @param mixed $uri URI, See HttpSocket::_parseUri()
|
||||
* @return array Current configuration settings
|
||||
* @return boolean If uri has merged in config
|
||||
*/
|
||||
protected function _configUri($uri = null) {
|
||||
if (empty($uri)) {
|
||||
|
@ -619,19 +590,18 @@ class HttpSocket extends CakeSocket {
|
|||
}
|
||||
$config = array(
|
||||
'request' => array(
|
||||
'uri' => array_intersect_key($uri, $this->config['request']['uri']),
|
||||
'auth' => array_intersect_key($uri, $this->config['request']['auth'])
|
||||
'uri' => array_intersect_key($uri, $this->config['request']['uri'])
|
||||
)
|
||||
);
|
||||
$this->config = Set::merge($this->config, $config);
|
||||
$this->config = Set::merge($this->config, array_intersect_key($this->config['request']['uri'], $this->config));
|
||||
return $this->config;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a $uri array and turns it into a fully qualified URL string
|
||||
*
|
||||
* @param mixed $uri Either A $uri array, or a request string. Will use $this->config if left empty.
|
||||
* @param mixed $uri Either A $uri array, or a request string. Will use $this->config if left empty.
|
||||
* @param string $uriTemplate The Uri template/format to use.
|
||||
* @return mixed A fully qualified URL formated according to $uriTemplate, or false on failure
|
||||
*/
|
||||
|
@ -665,7 +635,7 @@ class HttpSocket extends CakeSocket {
|
|||
$uriTemplate = str_replace(':%port', null, $uriTemplate);
|
||||
}
|
||||
foreach ($uri as $property => $value) {
|
||||
$uriTemplate = str_replace('%'.$property, $value, $uriTemplate);
|
||||
$uriTemplate = str_replace('%' . $property, $value, $uriTemplate);
|
||||
}
|
||||
|
||||
if ($uriTemplate === '/*') {
|
||||
|
@ -740,7 +710,7 @@ class HttpSocket extends CakeSocket {
|
|||
* - ?key[subKey]=value
|
||||
* - ?key[]=value1&key[]=value2
|
||||
*
|
||||
* A leading '?' mark in $query is optional and does not effect the outcome of this function.
|
||||
* A leading '?' mark in $query is optional and does not effect the outcome of this function.
|
||||
* For the complete capabilities of this implementation take a look at HttpSocketTest::testparseQuery()
|
||||
*
|
||||
* @param mixed $query A query string to parse into an array or an array to return directly "as is"
|
||||
|
@ -803,6 +773,7 @@ class HttpSocket extends CakeSocket {
|
|||
* @param array $request Needs to contain a 'uri' key. Should also contain a 'method' key, otherwise defaults to GET.
|
||||
* @param string $versionToken The version token to use, defaults to HTTP/1.1
|
||||
* @return string Request line
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function _buildRequestLine($request = array(), $versionToken = 'HTTP/1.1') {
|
||||
$asteriskMethods = array('OPTIONS');
|
||||
|
@ -810,8 +781,7 @@ class HttpSocket extends CakeSocket {
|
|||
if (is_string($request)) {
|
||||
$isValid = preg_match("/(.+) (.+) (.+)\r\n/U", $request, $match);
|
||||
if (!$this->quirksMode && (!$isValid || ($match[2] == '*' && !in_array($match[3], $asteriskMethods)))) {
|
||||
trigger_error(__('HttpSocket::_buildRequestLine - Passed an invalid request line string. Activate quirks mode to do this.'), E_USER_WARNING);
|
||||
return false;
|
||||
throw new Exception(__('HttpSocket::_buildRequestLine - Passed an invalid request line string. Activate quirks mode to do this.'));
|
||||
}
|
||||
return $request;
|
||||
} elseif (!is_array($request)) {
|
||||
|
@ -822,13 +792,16 @@ class HttpSocket extends CakeSocket {
|
|||
|
||||
$request['uri'] = $this->_parseUri($request['uri']);
|
||||
$request = array_merge(array('method' => 'GET'), $request);
|
||||
$request['uri'] = $this->_buildUri($request['uri'], '/%path?%query');
|
||||
if (!empty($this->_proxy['host'])) {
|
||||
$request['uri'] = $this->_buildUri($request['uri'], '%scheme://%host:%port/%path?%query');
|
||||
} else {
|
||||
$request['uri'] = $this->_buildUri($request['uri'], '/%path?%query');
|
||||
}
|
||||
|
||||
if (!$this->quirksMode && $request['uri'] === '*' && !in_array($request['method'], $asteriskMethods)) {
|
||||
trigger_error(__('HttpSocket::_buildRequestLine - The "*" asterisk character is only allowed for the following methods: %s. Activate quirks mode to work outside of HTTP/1.1 specs.', implode(',', $asteriskMethods)), E_USER_WARNING);
|
||||
return false;
|
||||
throw new Exception(__('HttpSocket::_buildRequestLine - The "*" asterisk character is only allowed for the following methods: %s. Activate quirks mode to work outside of HTTP/1.1 specs.', implode(',', $asteriskMethods)));
|
||||
}
|
||||
return $request['method'].' '.$request['uri'].' '.$versionToken.$this->lineBreak;
|
||||
return $request['method'] . ' ' . $request['uri'] . ' ' . $versionToken . "\r\n";
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -851,6 +824,7 @@ class HttpSocket extends CakeSocket {
|
|||
* Builds the header.
|
||||
*
|
||||
* @param array $header Header to build
|
||||
* @param string $mode
|
||||
* @return string Header built from array
|
||||
*/
|
||||
protected function _buildHeader($header, $mode = 'standard') {
|
||||
|
@ -860,6 +834,17 @@ class HttpSocket extends CakeSocket {
|
|||
return false;
|
||||
}
|
||||
|
||||
$fieldsInHeader = array();
|
||||
foreach ($header as $key => $value) {
|
||||
$lowKey = strtolower($key);
|
||||
if (array_key_exists($lowKey, $fieldsInHeader)) {
|
||||
$header[$fieldsInHeader[$lowKey]] = $value;
|
||||
unset($header[$key]);
|
||||
} else {
|
||||
$fieldsInHeader[$lowKey] = $key;
|
||||
}
|
||||
}
|
||||
|
||||
$returnHeader = '';
|
||||
foreach ($header as $field => $contents) {
|
||||
if (is_array($contents) && $mode == 'standard') {
|
||||
|
@ -869,144 +854,37 @@ class HttpSocket extends CakeSocket {
|
|||
$contents = preg_replace("/\r\n(?![\t ])/", "\r\n ", $content);
|
||||
$field = $this->_escapeToken($field);
|
||||
|
||||
$returnHeader .= $field.': '.$contents.$this->lineBreak;
|
||||
$returnHeader .= $field . ': ' . $contents . "\r\n";
|
||||
}
|
||||
}
|
||||
return $returnHeader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an array based header.
|
||||
*
|
||||
* @param array $header Header as an indexed array (field => value)
|
||||
* @return array Parsed header
|
||||
*/
|
||||
protected function _parseHeader($header) {
|
||||
if (is_array($header)) {
|
||||
foreach ($header as $field => $value) {
|
||||
unset($header[$field]);
|
||||
$field = strtolower($field);
|
||||
preg_match_all('/(?:^|(?<=-))[a-z]/U', $field, $offsets, PREG_OFFSET_CAPTURE);
|
||||
|
||||
foreach ($offsets[0] as $offset) {
|
||||
$field = substr_replace($field, strtoupper($offset[0]), $offset[1], 1);
|
||||
}
|
||||
$header[$field] = $value;
|
||||
}
|
||||
return $header;
|
||||
} elseif (!is_string($header)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
preg_match_all("/(.+):(.+)(?:(?<![\t ])" . $this->lineBreak . "|\$)/Uis", $header, $matches, PREG_SET_ORDER);
|
||||
|
||||
$header = array();
|
||||
foreach ($matches as $match) {
|
||||
list(, $field, $value) = $match;
|
||||
|
||||
$value = trim($value);
|
||||
$value = preg_replace("/[\t ]\r\n/", "\r\n", $value);
|
||||
|
||||
$field = $this->_unescapeToken($field);
|
||||
|
||||
$field = strtolower($field);
|
||||
preg_match_all('/(?:^|(?<=-))[a-z]/U', $field, $offsets, PREG_OFFSET_CAPTURE);
|
||||
foreach ($offsets[0] as $offset) {
|
||||
$field = substr_replace($field, strtoupper($offset[0]), $offset[1], 1);
|
||||
}
|
||||
|
||||
if (!isset($header[$field])) {
|
||||
$header[$field] = $value;
|
||||
} else {
|
||||
$header[$field] = array_merge((array)$header[$field], (array)$value);
|
||||
}
|
||||
}
|
||||
return $header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses cookies in response headers.
|
||||
*
|
||||
* @param array $header Header array containing one ore more 'Set-Cookie' headers.
|
||||
* @return mixed Either false on no cookies, or an array of cookies recieved.
|
||||
* @access public
|
||||
* @todo Make this 100% RFC 2965 confirm
|
||||
*/
|
||||
function parseCookies($header) {
|
||||
if (!isset($header['Set-Cookie'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$cookies = array();
|
||||
foreach ((array)$header['Set-Cookie'] as $cookie) {
|
||||
if (strpos($cookie, '";"') !== false) {
|
||||
$cookie = str_replace('";"', "{__cookie_replace__}", $cookie);
|
||||
$parts = str_replace("{__cookie_replace__}", '";"', explode(';', $cookie));
|
||||
} else {
|
||||
$parts = preg_split('/\;[ \t]*/', $cookie);
|
||||
}
|
||||
|
||||
list($name, $value) = explode('=', array_shift($parts), 2);
|
||||
$cookies[$name] = compact('value');
|
||||
|
||||
foreach ($parts as $part) {
|
||||
if (strpos($part, '=') !== false) {
|
||||
list($key, $value) = explode('=', $part);
|
||||
} else {
|
||||
$key = $part;
|
||||
$value = true;
|
||||
}
|
||||
|
||||
$key = strtolower($key);
|
||||
if (!isset($cookies[$name][$key])) {
|
||||
$cookies[$name][$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $cookies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds cookie headers for a request.
|
||||
*
|
||||
* @param array $cookies Array of cookies to send with the request.
|
||||
* @return string Cookie header string to be sent with the request.
|
||||
* @access public
|
||||
* @todo Refactor token escape mechanism to be configurable
|
||||
*/
|
||||
function buildCookies($cookies) {
|
||||
public function buildCookies($cookies) {
|
||||
$header = array();
|
||||
foreach ($cookies as $name => $cookie) {
|
||||
$header[] = $name.'='.$this->_escapeToken($cookie['value'], array(';'));
|
||||
$header[] = $name . '=' . $this->_escapeToken($cookie['value'], array(';'));
|
||||
}
|
||||
$header = $this->_buildHeader(array('Cookie' => implode('; ', $header)), 'pragmatic');
|
||||
return $header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unescapes a given $token according to RFC 2616 (HTTP 1.1 specs)
|
||||
*
|
||||
* @param string $token Token to unescape
|
||||
* @return string Unescaped token
|
||||
* @access protected
|
||||
* @todo Test $chars parameter
|
||||
*/
|
||||
function _unescapeToken($token, $chars = null) {
|
||||
$regex = '/"(['.join('', $this->_tokenEscapeChars(true, $chars)).'])"/';
|
||||
$token = preg_replace($regex, '\\1', $token);
|
||||
return $token;
|
||||
return $this->_buildHeader(array('Cookie' => implode('; ', $header)), 'pragmatic');
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes a given $token according to RFC 2616 (HTTP 1.1 specs)
|
||||
*
|
||||
* @param string $token Token to escape
|
||||
* @param array $chars
|
||||
* @return string Escaped token
|
||||
* @access protected
|
||||
* @todo Test $chars parameter
|
||||
*/
|
||||
function _escapeToken($token, $chars = null) {
|
||||
$regex = '/(['.join('', $this->_tokenEscapeChars(true, $chars)).'])/';
|
||||
protected function _escapeToken($token, $chars = null) {
|
||||
$regex = '/([' . implode('', $this->_tokenEscapeChars(true, $chars)) . '])/';
|
||||
$token = preg_replace($regex, '"\\1"', $token);
|
||||
return $token;
|
||||
}
|
||||
|
@ -1015,11 +893,11 @@ class HttpSocket extends CakeSocket {
|
|||
* Gets escape chars according to RFC 2616 (HTTP 1.1 specs).
|
||||
*
|
||||
* @param boolean $hex true to get them as HEX values, false otherwise
|
||||
* @param array $chars
|
||||
* @return array Escape chars
|
||||
* @access protected
|
||||
* @todo Test $chars parameter
|
||||
*/
|
||||
function _tokenEscapeChars($hex = true, $chars = null) {
|
||||
protected function _tokenEscapeChars($hex = true, $chars = null) {
|
||||
if (!empty($chars)) {
|
||||
$escape = $chars;
|
||||
} else {
|
||||
|
@ -1035,7 +913,7 @@ class HttpSocket extends CakeSocket {
|
|||
}
|
||||
$regexChars = '';
|
||||
foreach ($escape as $key => $char) {
|
||||
$escape[$key] = '\\x'.str_pad(dechex(ord($char)), 2, '0', STR_PAD_LEFT);
|
||||
$escape[$key] = '\\x' . str_pad(dechex(ord($char)), 2, '0', STR_PAD_LEFT);
|
||||
}
|
||||
return $escape;
|
||||
}
|
||||
|
@ -1052,7 +930,7 @@ class HttpSocket extends CakeSocket {
|
|||
if (empty($initalState)) {
|
||||
$initalState = get_class_vars(__CLASS__);
|
||||
}
|
||||
if ($full == false) {
|
||||
if (!$full) {
|
||||
$this->request = $initalState['request'];
|
||||
$this->response = $initalState['response'];
|
||||
return true;
|
||||
|
|
|
@ -34,10 +34,12 @@ class AllSocketTest extends PHPUnit_Framework_TestSuite {
|
|||
* @return void
|
||||
*/
|
||||
public static function suite() {
|
||||
$suite = new PHPUnit_Framework_TestSuite('All Socket related class tests');
|
||||
$suite = new CakeTestSuite('All Socket related class tests');
|
||||
|
||||
$suite->addTestFile(CORE_TEST_CASES . DS . 'libs' . DS . 'cake_socket.test.php');
|
||||
$suite->addTestFile(CORE_TEST_CASES . DS . 'libs' . DS . 'http_socket.test.php');
|
||||
$suite->addTestFile(CAKE_TEST_CASES . DS . 'libs' . DS . 'http_response.test.php');
|
||||
$suite->addTestDirectory(CORE_TEST_CASES . DS . 'libs' . DS . 'http');
|
||||
return $suite;
|
||||
}
|
||||
}
|
|
@ -101,6 +101,30 @@ class CakeSocketTest extends CakeTestCase {
|
|||
$this->assertTrue($this->Socket->connected);
|
||||
}
|
||||
|
||||
/**
|
||||
* data provider function for testInvalidConnection
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function invalidConnections() {
|
||||
return array(
|
||||
array(array('host' => 'invalid.host')),
|
||||
array(array('host' => '127.0.0.1', 'port' => '70000'))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* testInvalidConnection method
|
||||
*
|
||||
* @dataProvider invalidConnections
|
||||
* @expectedException Exception
|
||||
* return void
|
||||
*/
|
||||
public function testInvalidConnection($data) {
|
||||
$this->Socket->config = array_merge($this->Socket->config, $data);
|
||||
$this->Socket->connect();
|
||||
}
|
||||
|
||||
/**
|
||||
* testSocketHost method
|
||||
*
|
||||
|
|
66
cake/tests/cases/libs/http/basic_authentication.test.php
Normal file
66
cake/tests/cases/libs/http/basic_authentication.test.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
/**
|
||||
* BasicMethodTest file
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.http
|
||||
* @since CakePHP(tm) v 2.0.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
App::import('Core', 'HttpSocket');
|
||||
App::import('Lib', 'http/BasicAuthentication');
|
||||
|
||||
/**
|
||||
* BasicMethodTest class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.http
|
||||
*/
|
||||
class BasicMethodTest extends CakeTestCase {
|
||||
|
||||
/**
|
||||
* testAuthentication method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testAuthentication() {
|
||||
$http = new HttpSocket();
|
||||
$auth = array(
|
||||
'method' => 'Basic',
|
||||
'user' => 'mark',
|
||||
'pass' => 'secret'
|
||||
);
|
||||
|
||||
BasicAuthentication::authentication($http, $auth);
|
||||
$this->assertEqual($http->request['header']['Authorization'], 'Basic bWFyazpzZWNyZXQ=');
|
||||
}
|
||||
|
||||
/**
|
||||
* testProxyAuthentication method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testProxyAuthentication() {
|
||||
$http = new HttpSocket();
|
||||
$proxy = array(
|
||||
'method' => 'Basic',
|
||||
'user' => 'mark',
|
||||
'pass' => 'secret'
|
||||
);
|
||||
|
||||
BasicAuthentication::proxyAuthentication($http, $proxy);
|
||||
$this->assertEqual($http->request['header']['Proxy-Authorization'], 'Basic bWFyazpzZWNyZXQ=');
|
||||
}
|
||||
|
||||
}
|
197
cake/tests/cases/libs/http/digest_authentication.test.php
Normal file
197
cake/tests/cases/libs/http/digest_authentication.test.php
Normal file
|
@ -0,0 +1,197 @@
|
|||
<?php
|
||||
/**
|
||||
* DigestAuthenticationTest file
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.http
|
||||
* @since CakePHP(tm) v 2.0.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
App::import('Core', 'HttpSocket');
|
||||
App::import('Lib', 'http/DigestAuthentication');
|
||||
|
||||
class DigestHttpSocket extends HttpSocket {
|
||||
|
||||
/**
|
||||
* nextHeader attribute
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $nextHeader = '';
|
||||
|
||||
/**
|
||||
* request method
|
||||
*
|
||||
* @param mixed $request
|
||||
* @return void
|
||||
*/
|
||||
public function request($request) {
|
||||
if ($request === false) {
|
||||
if (isset($this->response['header']['WWW-Authenticate'])) {
|
||||
unset($this->response['header']['WWW-Authenticate']);
|
||||
}
|
||||
return;
|
||||
}
|
||||
$this->response['header']['WWW-Authenticate'] = $this->nextHeader;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* DigestAuthenticationTest class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs.http
|
||||
*/
|
||||
class DigestAuthenticationTest extends CakeTestCase {
|
||||
|
||||
/**
|
||||
* Socket property
|
||||
*
|
||||
* @var mixed null
|
||||
*/
|
||||
public $HttpSocket = null;
|
||||
|
||||
/**
|
||||
* This function sets up a HttpSocket instance we are going to use for testing
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->HttpSocket = new DigestHttpSocket();
|
||||
$this->HttpSocket->request['method'] = 'GET';
|
||||
$this->HttpSocket->request['uri']['path'] = '/';
|
||||
}
|
||||
|
||||
/**
|
||||
* We use this function to clean up after the test case was executed
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function tearDown() {
|
||||
unset($this->HttpSocket);
|
||||
}
|
||||
|
||||
/**
|
||||
* testBasic method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBasic() {
|
||||
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
|
||||
$this->assertFalse(isset($this->HttpSocket->request['header']['Authorization']));
|
||||
|
||||
$auth = array('user' => 'admin', 'pass' => '1234');
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$this->assertTrue(isset($this->HttpSocket->request['header']['Authorization']));
|
||||
$this->assertEqual($auth['realm'], 'The batcave');
|
||||
$this->assertEqual($auth['nonce'], '4cded326c6c51');
|
||||
}
|
||||
|
||||
/**
|
||||
* testQop method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testQop() {
|
||||
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
|
||||
$auth = array('user' => 'admin', 'pass' => '1234');
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$expected = 'Digest username="admin", realm="The batcave", nonce="4cded326c6c51", uri="/", response="da7e2a46b471d77f70a9bb3698c8902b"';
|
||||
$this->assertEqual($expected, $this->HttpSocket->request['header']['Authorization']);
|
||||
$this->assertFalse(isset($auth['qop']));
|
||||
$this->assertFalse(isset($auth['nc']));
|
||||
|
||||
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",qop="auth"';
|
||||
$auth = array('user' => 'admin', 'pass' => '1234');
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$expected = '@Digest username="admin", realm="The batcave", nonce="4cded326c6c51", uri="/", response="[a-z0-9]{32}", qop="auth", nc=00000001, cnonce="[a-z0-9]+"@';
|
||||
$this->assertPattern($expected, $this->HttpSocket->request['header']['Authorization']);
|
||||
$this->assertEqual($auth['qop'], 'auth');
|
||||
$this->assertEqual($auth['nc'], 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* testOpaque method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testOpaque() {
|
||||
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
|
||||
$auth = array('user' => 'admin', 'pass' => '1234');
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$this->assertFalse(strpos($this->HttpSocket->request['header']['Authorization'], 'opaque="d8ea7aa61a1693024c4cc3a516f49b3c"'));
|
||||
|
||||
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",opaque="d8ea7aa61a1693024c4cc3a516f49b3c"';
|
||||
$auth = array('user' => 'admin', 'pass' => '1234');
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'opaque="d8ea7aa61a1693024c4cc3a516f49b3c"') > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* testMultipleRequest method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMultipleRequest() {
|
||||
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51",qop="auth"';
|
||||
$auth = array('user' => 'admin', 'pass' => '1234');
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000001') > 0);
|
||||
$this->assertEqual($auth['nc'], 2);
|
||||
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000002') > 0);
|
||||
$this->assertEqual($auth['nc'], 3);
|
||||
$responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
|
||||
$response = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
|
||||
|
||||
$this->HttpSocket->nextHeader = '';
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$this->assertTrue(strpos($this->HttpSocket->request['header']['Authorization'], 'nc=00000003') > 0);
|
||||
$this->assertEqual($auth['nc'], 4);
|
||||
$responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
|
||||
$response2 = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
|
||||
$this->assertNotEqual($response, $response2);
|
||||
}
|
||||
|
||||
/**
|
||||
* testPathChanged method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testPathChanged() {
|
||||
$this->HttpSocket->nextHeader = 'Digest realm="The batcave",nonce="4cded326c6c51"';
|
||||
$this->HttpSocket->request['uri']['path'] = '/admin';
|
||||
$auth = array('user' => 'admin', 'pass' => '1234');
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$responsePos = strpos($this->HttpSocket->request['header']['Authorization'], 'response=');
|
||||
$response = substr($this->HttpSocket->request['header']['Authorization'], $responsePos + 10, 32);
|
||||
$this->assertNotEqual($response, 'da7e2a46b471d77f70a9bb3698c8902b');
|
||||
}
|
||||
|
||||
/**
|
||||
* testNoDigestResponse method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNoDigestResponse() {
|
||||
$this->HttpSocket->nextHeader = false;
|
||||
$this->HttpSocket->request['uri']['path'] = '/admin';
|
||||
$auth = array('user' => 'admin', 'pass' => '1234');
|
||||
DigestAuthentication::authentication($this->HttpSocket, $auth);
|
||||
$this->assertFalse(isset($this->HttpSocket->request['header']['Authorization']));
|
||||
}
|
||||
|
||||
}
|
509
cake/tests/cases/libs/http_response.test.php
Normal file
509
cake/tests/cases/libs/http_response.test.php
Normal file
|
@ -0,0 +1,509 @@
|
|||
<?php
|
||||
/**
|
||||
* HttpSocketTest file
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs
|
||||
* @since CakePHP(tm) v 1.2.0.4206
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
App::import('Core', 'HttpResponse');
|
||||
|
||||
/**
|
||||
* TestHttpResponse class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs
|
||||
*/
|
||||
class TestHttpResponse extends HttpResponse {
|
||||
|
||||
/**
|
||||
* Convenience method for testing protected method
|
||||
*
|
||||
* @param array $header Header as an indexed array (field => value)
|
||||
* @return array Parsed header
|
||||
*/
|
||||
public function parseHeader($header) {
|
||||
return parent::_parseHeader($header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for testing protected method
|
||||
*
|
||||
* @param string $body A string continaing the body to decode
|
||||
* @param mixed $encoding Can be false in case no encoding is being used, or a string representing the encoding
|
||||
* @return mixed Array or false
|
||||
*/
|
||||
public function decodeBody($body, $encoding = 'chunked') {
|
||||
return parent::_decodeBody($body, $encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for testing protected method
|
||||
*
|
||||
* @param string $body A string continaing the chunked body to decode
|
||||
* @return mixed Array or false
|
||||
*/
|
||||
public function decodeChunkedBody($body) {
|
||||
return parent::_decodeChunkedBody($body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for testing protected method
|
||||
*
|
||||
* @param string $token Token to unescape
|
||||
* @return string Unescaped token
|
||||
*/
|
||||
public function unescapeToken($token, $chars = null) {
|
||||
return parent::_unescapeToken($token, $chars);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method for testing protected method
|
||||
*
|
||||
* @param boolean $hex true to get them as HEX values, false otherwise
|
||||
* @return array Escape chars
|
||||
*/
|
||||
public function tokenEscapeChars($hex = true, $chars = null) {
|
||||
return parent::_tokenEscapeChars($hex, $chars);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* HttpResponseTest class
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.libs
|
||||
*/
|
||||
class HttpResponseTest extends CakeTestCase {
|
||||
/**
|
||||
* This function sets up a HttpResponse
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUp() {
|
||||
$this->HttpResponse = new TestHttpResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* testBody
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBody() {
|
||||
$this->HttpResponse->body = 'testing';
|
||||
$this->assertEqual($this->HttpResponse->body(), 'testing');
|
||||
|
||||
$this->HttpResponse->body = null;
|
||||
$this->assertIdentical($this->HttpResponse->body(), '');
|
||||
}
|
||||
|
||||
/**
|
||||
* testToString
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testToString() {
|
||||
$this->HttpResponse->body = 'other test';
|
||||
$this->assertEqual($this->HttpResponse->body(), 'other test');
|
||||
$this->assertEqual((string)$this->HttpResponse, 'other test');
|
||||
$this->assertTrue(strpos($this->HttpResponse, 'test') > 0);
|
||||
|
||||
$this->HttpResponse->body = null;
|
||||
$this->assertEqual((string)$this->HttpResponse, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* testGetHeadr
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetHeader() {
|
||||
$this->HttpResponse->headers = array(
|
||||
'foo' => 'Bar',
|
||||
'Some' => 'ok',
|
||||
'HeAdEr' => 'value',
|
||||
'content-Type' => 'text/plain'
|
||||
);
|
||||
|
||||
$this->assertEqual($this->HttpResponse->getHeader('foo'), 'Bar');
|
||||
$this->assertEqual($this->HttpResponse->getHeader('Foo'), 'Bar');
|
||||
$this->assertEqual($this->HttpResponse->getHeader('FOO'), 'Bar');
|
||||
$this->assertEqual($this->HttpResponse->getHeader('header'), 'value');
|
||||
$this->assertEqual($this->HttpResponse->getHeader('Content-Type'), 'text/plain');
|
||||
$this->assertIdentical($this->HttpResponse->getHeader(0), null);
|
||||
|
||||
$this->assertEqual($this->HttpResponse->getHeader('foo', false), 'Bar');
|
||||
$this->assertEqual($this->HttpResponse->getHeader('foo', array('foo' => 'not from class')), 'not from class');
|
||||
}
|
||||
|
||||
/**
|
||||
* testIsOk
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testIsOk() {
|
||||
$this->HttpResponse->code = 0;
|
||||
$this->assertFalse($this->HttpResponse->isOk());
|
||||
$this->HttpResponse->code = -1;
|
||||
$this->assertFalse($this->HttpResponse->isOk());
|
||||
$this->HttpResponse->code = 201;
|
||||
$this->assertFalse($this->HttpResponse->isOk());
|
||||
$this->HttpResponse->code = 'what?';
|
||||
$this->assertFalse($this->HttpResponse->isOk());
|
||||
$this->HttpResponse->code = 200;
|
||||
$this->assertTrue($this->HttpResponse->isOk());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that HttpSocket::parseHeader can take apart a given (and valid) $header string and turn it into an array.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testParseHeader() {
|
||||
$r = $this->HttpResponse->parseHeader(array('foo' => 'Bar', 'fOO-bAr' => 'quux'));
|
||||
$this->assertEquals($r, array('foo' => 'Bar', 'fOO-bAr' => 'quux'));
|
||||
|
||||
$r = $this->HttpResponse->parseHeader(true);
|
||||
$this->assertEquals($r, false);
|
||||
|
||||
$header = "Host: cakephp.org\t\r\n";
|
||||
$r = $this->HttpResponse->parseHeader($header);
|
||||
$expected = array(
|
||||
'Host' => 'cakephp.org'
|
||||
);
|
||||
$this->assertEquals($r, $expected);
|
||||
|
||||
$header = "Date:Sat, 07 Apr 2007 10:10:25 GMT\r\nX-Powered-By: PHP/5.1.2\r\n";
|
||||
$r = $this->HttpResponse->parseHeader($header);
|
||||
$expected = array(
|
||||
'Date' => 'Sat, 07 Apr 2007 10:10:25 GMT',
|
||||
'X-Powered-By' => 'PHP/5.1.2'
|
||||
);
|
||||
$this->assertEquals($r, $expected);
|
||||
|
||||
$header = "people: Jim,John\r\nfoo-LAND: Bar\r\ncAKe-PHP: rocks\r\n";
|
||||
$r = $this->HttpResponse->parseHeader($header);
|
||||
$expected = array(
|
||||
'people' => 'Jim,John',
|
||||
'foo-LAND' => 'Bar',
|
||||
'cAKe-PHP' => 'rocks'
|
||||
);
|
||||
$this->assertEquals($r, $expected);
|
||||
|
||||
$header = "People: Jim,John,Tim\r\nPeople: Lisa,Tina,Chelsea\r\n";
|
||||
$r = $this->HttpResponse->parseHeader($header);
|
||||
$expected = array(
|
||||
'People' => array('Jim,John,Tim', 'Lisa,Tina,Chelsea')
|
||||
);
|
||||
$this->assertEquals($r, $expected);
|
||||
|
||||
$header = "Multi-Line: I am a \r\nmulti line\t\r\nfield value.\r\nSingle-Line: I am not\r\n";
|
||||
$r = $this->HttpResponse->parseHeader($header);
|
||||
$expected = array(
|
||||
'Multi-Line' => "I am a\r\nmulti line\r\nfield value.",
|
||||
'Single-Line' => 'I am not'
|
||||
);
|
||||
$this->assertEquals($r, $expected);
|
||||
|
||||
$header = "Esc\"@\"ped: value\r\n";
|
||||
$r = $this->HttpResponse->parseHeader($header);
|
||||
$expected = array(
|
||||
'Esc@ped' => 'value'
|
||||
);
|
||||
$this->assertEquals($r, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* testParseResponse method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testParseResponse() {
|
||||
$tests = array(
|
||||
'simple-request' => array(
|
||||
'response' => array(
|
||||
'status-line' => "HTTP/1.x 200 OK\r\n",
|
||||
'header' => "Date: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\n",
|
||||
'body' => "<h1>Hello World</h1>\r\n<p>It's good to be html</p>"
|
||||
),
|
||||
'expectations' => array(
|
||||
'httpVersion' => 'HTTP/1.x',
|
||||
'code' => 200,
|
||||
'reasonPhrase' => 'OK',
|
||||
'headers' => array('Date' => 'Mon, 16 Apr 2007 04:14:16 GMT', 'Server' => 'CakeHttp Server'),
|
||||
'body' => "<h1>Hello World</h1>\r\n<p>It's good to be html</p>"
|
||||
)
|
||||
),
|
||||
'no-header' => array(
|
||||
'response' => array(
|
||||
'status-line' => "HTTP/1.x 404 OK\r\n",
|
||||
'header' => null
|
||||
),
|
||||
'expectations' => array(
|
||||
'code' => 404,
|
||||
'headers' => array()
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$testResponse = array();
|
||||
$expectations = array();
|
||||
|
||||
foreach ($tests as $name => $test) {
|
||||
$testResponse = array_merge($testResponse, $test['response']);
|
||||
$testResponse['response'] = $testResponse['status-line'] . $testResponse['header'] . "\r\n" . $testResponse['body'];
|
||||
$this->HttpResponse->parseResponse($testResponse['response']);
|
||||
$expectations = array_merge($expectations, $test['expectations']);
|
||||
|
||||
foreach ($expectations as $property => $expectedVal) {
|
||||
$this->assertEquals($this->HttpResponse->{$property}, $expectedVal, 'Test "' . $name . '": response.' . $property . ' - %s');
|
||||
}
|
||||
|
||||
foreach (array('status-line', 'header', 'body', 'response') as $field) {
|
||||
$this->assertEquals($this->HttpResponse['raw'][$field], $testResponse[$field], 'Test response.raw.' . $field . ': %s');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* data provider function for testInvalidParseResponseData
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function invalidParseResponseDataProvider() {
|
||||
return array(
|
||||
array(array('foo' => 'bar')),
|
||||
array(true),
|
||||
array("HTTP Foo\r\nBar: La"),
|
||||
array('HTTP/1.1 TEST ERROR')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* testInvalidParseResponseData
|
||||
*
|
||||
* @dataProvider invalidParseResponseDataProvider
|
||||
* @expectedException Exception
|
||||
* return void
|
||||
*/
|
||||
public function testInvalidParseResponseData($value) {
|
||||
$this->HttpResponse->parseResponse($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* testDecodeBody method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDecodeBody() {
|
||||
$r = $this->HttpResponse->decodeBody(true);
|
||||
$this->assertEquals($r, false);
|
||||
|
||||
$r = $this->HttpResponse->decodeBody('Foobar', false);
|
||||
$this->assertEquals($r, array('body' => 'Foobar', 'header' => false));
|
||||
|
||||
$encoding = 'chunked';
|
||||
$sample = array(
|
||||
'encoded' => "19\r\nThis is a chunked message\r\n0\r\n",
|
||||
'decoded' => array('body' => "This is a chunked message", 'header' => false)
|
||||
);
|
||||
|
||||
$r = $this->HttpResponse->decodeBody($sample['encoded'], $encoding);
|
||||
$this->assertEquals($r, $sample['decoded']);
|
||||
}
|
||||
|
||||
/**
|
||||
* testDecodeFooCoded
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDecodeFooCoded() {
|
||||
$r = $this->HttpResponse->decodeBody(true);
|
||||
$this->assertEquals($r, false);
|
||||
|
||||
$r = $this->HttpResponse->decodeBody('Foobar', false);
|
||||
$this->assertEquals($r, array('body' => 'Foobar', 'header' => false));
|
||||
|
||||
$encoding = 'foo-bar';
|
||||
$sample = array(
|
||||
'encoded' => '!Foobar!',
|
||||
'decoded' => array('body' => '!Foobar!', 'header' => false),
|
||||
);
|
||||
|
||||
$r = $this->HttpResponse->decodeBody($sample['encoded'], $encoding);
|
||||
$this->assertEquals($r, $sample['decoded']);
|
||||
}
|
||||
|
||||
/**
|
||||
* testDecodeChunkedBody method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDecodeChunkedBody() {
|
||||
$r = $this->HttpResponse->decodeChunkedBody(true);
|
||||
$this->assertEquals($r, false);
|
||||
|
||||
$encoded = "19\r\nThis is a chunked message\r\n0\r\n";
|
||||
$decoded = "This is a chunked message";
|
||||
$r = $this->HttpResponse->decodeChunkedBody($encoded);
|
||||
$this->assertEquals($r['body'], $decoded);
|
||||
$this->assertEquals($r['header'], false);
|
||||
|
||||
$encoded = "19 \r\nThis is a chunked message\r\n0\r\n";
|
||||
$r = $this->HttpResponse->decodeChunkedBody($encoded);
|
||||
$this->assertEquals($r['body'], $decoded);
|
||||
|
||||
$encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n0\r\n";
|
||||
$decoded = "This is a chunked message\nThat is cool\n";
|
||||
$r = $this->HttpResponse->decodeChunkedBody($encoded);
|
||||
$this->assertEquals($r['body'], $decoded);
|
||||
$this->assertEquals($r['header'], false);
|
||||
|
||||
$encoded = "19\r\nThis is a chunked message\r\nE;foo-chunk=5\r\n\nThat is cool\n\r\n0\r\n";
|
||||
$r = $this->HttpResponse->decodeChunkedBody($encoded);
|
||||
$this->assertEquals($r['body'], $decoded);
|
||||
$this->assertEquals($r['header'], false);
|
||||
|
||||
$encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n0\r\nfoo-header: bar\r\ncake: PHP\r\n\r\n";
|
||||
$r = $this->HttpResponse->decodeChunkedBody($encoded);
|
||||
$this->assertEquals($r['body'], $decoded);
|
||||
$this->assertEquals($r['header'], array('foo-header' => 'bar', 'cake' => 'PHP'));
|
||||
|
||||
$encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n";
|
||||
$this->expectError();
|
||||
$r = $this->HttpResponse->decodeChunkedBody($encoded);
|
||||
$this->assertEquals($r, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* testParseCookies method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testParseCookies() {
|
||||
$header = array(
|
||||
'Set-Cookie' => array(
|
||||
'foo=bar',
|
||||
'people=jim,jack,johnny";";Path=/accounts',
|
||||
'google=not=nice'
|
||||
),
|
||||
'Transfer-Encoding' => 'chunked',
|
||||
'Date' => 'Sun, 18 Nov 2007 18:57:42 GMT',
|
||||
);
|
||||
$cookies = $this->HttpResponse->parseCookies($header);
|
||||
$expected = array(
|
||||
'foo' => array(
|
||||
'value' => 'bar'
|
||||
),
|
||||
'people' => array(
|
||||
'value' => 'jim,jack,johnny";"',
|
||||
'path' => '/accounts',
|
||||
),
|
||||
'google' => array(
|
||||
'value' => 'not=nice',
|
||||
)
|
||||
);
|
||||
$this->assertEqual($cookies, $expected);
|
||||
|
||||
$header['Set-Cookie'][] = 'cakephp=great; Secure';
|
||||
$expected['cakephp'] = array('value' => 'great', 'secure' => true);
|
||||
$cookies = $this->HttpResponse->parseCookies($header);
|
||||
$this->assertEqual($cookies, $expected);
|
||||
|
||||
$header['Set-Cookie'] = 'foo=bar';
|
||||
unset($expected['people'], $expected['cakephp'], $expected['google']);
|
||||
$cookies = $this->HttpResponse->parseCookies($header);
|
||||
$this->assertEqual($cookies, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that escaped token strings are properly unescaped by HttpSocket::unescapeToken
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testUnescapeToken() {
|
||||
$this->assertEquals($this->HttpResponse->unescapeToken('Foo'), 'Foo');
|
||||
|
||||
$escape = $this->HttpResponse->tokenEscapeChars(false);
|
||||
foreach ($escape as $char) {
|
||||
$token = 'My-special-"' . $char . '"-Token';
|
||||
$unescapedToken = $this->HttpResponse->unescapeToken($token);
|
||||
$expectedToken = 'My-special-' . $char . '-Token';
|
||||
|
||||
$this->assertEquals($unescapedToken, $expectedToken, 'Test token unescaping for ASCII '.ord($char));
|
||||
}
|
||||
|
||||
$token = 'Extreme-":"Token-" "-""""@"-test';
|
||||
$escapedToken = $this->HttpResponse->unescapeToken($token);
|
||||
$expectedToken = 'Extreme-:Token- -"@-test';
|
||||
$this->assertEquals($expectedToken, $escapedToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* testArrayAccess
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testArrayAccess() {
|
||||
$this->HttpResponse->httpVersion = 'HTTP/1.1';
|
||||
$this->HttpResponse->code = 200;
|
||||
$this->HttpResponse->reasonPhrase = 'OK';
|
||||
$this->HttpResponse->headers = array(
|
||||
'Server' => 'CakePHP',
|
||||
'ContEnt-Type' => 'text/plain'
|
||||
);
|
||||
$this->HttpResponse->cookies = array(
|
||||
'foo' => array('value' => 'bar'),
|
||||
'bar' => array('value' => 'foo')
|
||||
);
|
||||
$this->HttpResponse->body = 'This is a test!';
|
||||
$this->HttpResponse->raw = "HTTP/1.1 200 OK\r\nServer: CakePHP\r\nContEnt-Type: text/plain\r\n\r\nThis is a test!";
|
||||
|
||||
$expected1 = "HTTP/1.1 200 OK\r\n";
|
||||
$this->assertEqual($this->HttpResponse['raw']['status-line'], $expected1);
|
||||
$expected2 = "Server: CakePHP\r\nContEnt-Type: text/plain\r\n";
|
||||
$this->assertEqual($this->HttpResponse['raw']['header'], $expected2);
|
||||
$expected3 = 'This is a test!';
|
||||
$this->assertEqual($this->HttpResponse['raw']['body'], $expected3);
|
||||
$expected = $expected1 . $expected2 . "\r\n" . $expected3;
|
||||
$this->assertEqual($this->HttpResponse['raw']['response'], $expected);
|
||||
|
||||
$expected = 'HTTP/1.1';
|
||||
$this->assertEqual($this->HttpResponse['status']['http-version'], $expected);
|
||||
$expected = 200;
|
||||
$this->assertEqual($this->HttpResponse['status']['code'], $expected);
|
||||
$expected = 'OK';
|
||||
$this->assertEqual($this->HttpResponse['status']['reason-phrase'], $expected);
|
||||
|
||||
$expected = array(
|
||||
'Server' => 'CakePHP',
|
||||
'ContEnt-Type' => 'text/plain'
|
||||
);
|
||||
$this->assertEqual($this->HttpResponse['header'], $expected);
|
||||
|
||||
$expected = 'This is a test!';
|
||||
$this->assertEqual($this->HttpResponse['body'], $expected);
|
||||
|
||||
$expected = array(
|
||||
'foo' => array('value' => 'bar'),
|
||||
'bar' => array('value' => 'foo')
|
||||
);
|
||||
$this->assertEqual($this->HttpResponse['cookies'], $expected);
|
||||
|
||||
$this->HttpResponse->raw = "HTTP/1.1 200 OK\r\n\r\nThis is a test!";
|
||||
$this->assertIdentical($this->HttpResponse['raw']['header'], null);
|
||||
}
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue