mirror of
https://github.com/brmlab/brmsklad.git
synced 2025-10-30 15:53:59 +01:00
Upgrade CakePHP from 2.2.5 to 2.9.5
This commit is contained in:
parent
5a580df460
commit
235a541597
793 changed files with 60746 additions and 23753 deletions
|
|
@ -2,19 +2,18 @@
|
|||
/**
|
||||
* Basic authentication
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package Cake.Network.Http
|
||||
* @since CakePHP(tm) v 2.0.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
/**
|
||||
|
|
@ -27,36 +26,36 @@ class BasicAuthentication {
|
|||
/**
|
||||
* Authentication
|
||||
*
|
||||
* @param HttpSocket $http
|
||||
* @param array $authInfo
|
||||
* @param HttpSocket $http Http socket instance.
|
||||
* @param array &$authInfo Authentication info.
|
||||
* @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']);
|
||||
$http->request['header']['Authorization'] = static::_generateHeader($authInfo['user'], $authInfo['pass']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Proxy Authentication
|
||||
*
|
||||
* @param HttpSocket $http
|
||||
* @param array $proxyInfo
|
||||
* @param HttpSocket $http Http socket instance.
|
||||
* @param array &$proxyInfo Proxy info.
|
||||
* @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']);
|
||||
$http->request['header']['Proxy-Authorization'] = static::_generateHeader($proxyInfo['user'], $proxyInfo['pass']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate basic [proxy] authentication header
|
||||
*
|
||||
* @param string $user
|
||||
* @param string $pass
|
||||
* @param string $user Username.
|
||||
* @param string $pass Password.
|
||||
* @return string
|
||||
*/
|
||||
protected static function _generateHeader($user, $pass) {
|
||||
|
|
|
|||
|
|
@ -2,19 +2,18 @@
|
|||
/**
|
||||
* Digest authentication
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package Cake.Network.Http
|
||||
* @since CakePHP(tm) v 2.0.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
/**
|
||||
|
|
@ -27,26 +26,26 @@ class DigestAuthentication {
|
|||
/**
|
||||
* Authentication
|
||||
*
|
||||
* @param HttpSocket $http
|
||||
* @param array $authInfo
|
||||
* @param HttpSocket $http Http socket instance.
|
||||
* @param array &$authInfo Authentication info.
|
||||
* @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)) {
|
||||
if (!isset($authInfo['realm']) && !static::_getServerInformation($http, $authInfo)) {
|
||||
return;
|
||||
}
|
||||
$http->request['header']['Authorization'] = self::_generateHeader($http, $authInfo);
|
||||
$http->request['header']['Authorization'] = static::_generateHeader($http, $authInfo);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve information about the authentication
|
||||
*
|
||||
* @param HttpSocket $http
|
||||
* @param array $authInfo
|
||||
* @return boolean
|
||||
* @param HttpSocket $http Http socket instance.
|
||||
* @param array &$authInfo Authentication info.
|
||||
* @return bool
|
||||
*/
|
||||
protected static function _getServerInformation(HttpSocket $http, &$authInfo) {
|
||||
$originalRequest = $http->request;
|
||||
|
|
@ -71,8 +70,8 @@ class DigestAuthentication {
|
|||
/**
|
||||
* Generate the header Authorization
|
||||
*
|
||||
* @param HttpSocket $http
|
||||
* @param array $authInfo
|
||||
* @param HttpSocket $http Http socket instance.
|
||||
* @param array &$authInfo Authentication info.
|
||||
* @return string
|
||||
*/
|
||||
protected static function _generateHeader(HttpSocket $http, &$authInfo) {
|
||||
|
|
|
|||
|
|
@ -2,437 +2,34 @@
|
|||
/**
|
||||
* HTTP Response from HttpSocket.
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package Cake.Network.Http
|
||||
* @since CakePHP(tm) v 2.0.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
App::uses('HttpSocketResponse', 'Network/Http');
|
||||
|
||||
if (class_exists('HttpResponse')) {
|
||||
trigger_error(__d(
|
||||
'cake_dev',
|
||||
"HttpResponse is deprecated due to naming conflicts. Use HttpSocketResponse instead."
|
||||
), E_USER_ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP Response from HttpSocket.
|
||||
*
|
||||
* @package Cake.Network.Http
|
||||
* @deprecated 3.0.0 This class is deprecated as it has naming conflicts with pecl/http
|
||||
*/
|
||||
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 = '';
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $message
|
||||
*/
|
||||
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
|
||||
* @param array $headers
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* If return is a valid 3xx (Redirection)
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isRedirect() {
|
||||
return in_array($this->code, array(301, 302, 303, 307)) && !is_null($this->getHeader('Location'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given message and breaks it down in parts.
|
||||
*
|
||||
* @param string $message Message to parse
|
||||
* @return void
|
||||
* @throws SocketException
|
||||
*/
|
||||
public function parseResponse($message) {
|
||||
if (!is_string($message)) {
|
||||
throw new SocketException(__d('cake_dev', 'Invalid response.'));
|
||||
}
|
||||
|
||||
if (!preg_match("/^(.+\r\n)(.*)(?<=\r\n)\r\n/Us", $message, $match)) {
|
||||
throw new SocketException(__d('cake_dev', '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 containing the body to decode.
|
||||
* @param string|boolean $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 containing the chunked body to decode.
|
||||
* @return mixed Array of response headers and body or false.
|
||||
* @throws SocketException
|
||||
*/
|
||||
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|\n)/iU', $body, $match)) {
|
||||
throw new SocketException(__d('cake_dev', 'HttpSocket::_decodeChunkedBody - Could not parse malformed chunk.'));
|
||||
}
|
||||
|
||||
$chunkSize = 0;
|
||||
$hexLength = 0;
|
||||
$chunkExtensionValue = '';
|
||||
if (isset($match[0])) {
|
||||
$chunkSize = $match[0];
|
||||
}
|
||||
if (isset($match[1])) {
|
||||
$hexLength = $match[1];
|
||||
}
|
||||
if (isset($match[3])) {
|
||||
$chunkExtensionValue = $match[3];
|
||||
}
|
||||
|
||||
$body = substr($body, strlen($chunkSize));
|
||||
$chunkLength = hexdec($hexLength);
|
||||
$chunk = substr($body, 0, $chunkLength);
|
||||
$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 received.
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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;
|
||||
}
|
||||
foreach ($escape as $key => $char) {
|
||||
$escape[$key] = '\\x' . str_pad(dechex(ord($char)), 2, '0', STR_PAD_LEFT);
|
||||
}
|
||||
return $escape;
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess - Offset Exists
|
||||
*
|
||||
* @param string $offset
|
||||
* @return boolean
|
||||
*/
|
||||
public function offsetExists($offset) {
|
||||
return in_array($offset, array('raw', 'status', 'header', 'body', 'cookies'));
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess - Offset Get
|
||||
*
|
||||
* @param string $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 - Offset Set
|
||||
*
|
||||
* @param string $offset
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function offsetSet($offset, $value) {
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess - Offset Unset
|
||||
*
|
||||
* @param string $offset
|
||||
* @return void
|
||||
*/
|
||||
public function offsetUnset($offset) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Instance as string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString() {
|
||||
return $this->body();
|
||||
}
|
||||
class HttpResponse extends HttpSocketResponse {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,25 +2,26 @@
|
|||
/**
|
||||
* HTTP Socket connection class.
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package Cake.Network.Http
|
||||
* @since CakePHP(tm) v 1.2.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
App::uses('CakeSocket', 'Network');
|
||||
App::uses('Router', 'Routing');
|
||||
App::uses('Hash', 'Utility');
|
||||
|
||||
/**
|
||||
* Cake network socket connection class.
|
||||
* CakePHP network socket connection class.
|
||||
*
|
||||
* Core base class for HTTP network communication. HttpSocket can be used as an
|
||||
* Object Oriented replacement for cURL in many places.
|
||||
|
|
@ -34,7 +35,7 @@ class HttpSocket extends CakeSocket {
|
|||
* enforce RFC 2616 (HTTP/1.1 specs).
|
||||
* will be disabled and additional measures to deal with non-standard responses will be enabled.
|
||||
*
|
||||
* @var boolean
|
||||
* @var bool
|
||||
*/
|
||||
public $quirksMode = false;
|
||||
|
||||
|
|
@ -64,7 +65,7 @@ class HttpSocket extends CakeSocket {
|
|||
),
|
||||
'raw' => null,
|
||||
'redirect' => false,
|
||||
'cookies' => array()
|
||||
'cookies' => array(),
|
||||
);
|
||||
|
||||
/**
|
||||
|
|
@ -75,11 +76,11 @@ class HttpSocket extends CakeSocket {
|
|||
public $response = null;
|
||||
|
||||
/**
|
||||
* Response classname
|
||||
* Response class name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $responseClass = 'HttpResponse';
|
||||
public $responseClass = 'HttpSocketResponse';
|
||||
|
||||
/**
|
||||
* Configuration settings for the HttpSocket and the requests
|
||||
|
|
@ -92,6 +93,10 @@ class HttpSocket extends CakeSocket {
|
|||
'protocol' => 'tcp',
|
||||
'port' => 80,
|
||||
'timeout' => 30,
|
||||
'ssl_verify_peer' => true,
|
||||
'ssl_allow_self_signed' => false,
|
||||
'ssl_verify_depth' => 5,
|
||||
'ssl_verify_host' => true,
|
||||
'request' => array(
|
||||
'uri' => array(
|
||||
'scheme' => array('http', 'https'),
|
||||
|
|
@ -99,7 +104,7 @@ class HttpSocket extends CakeSocket {
|
|||
'port' => array(80, 443)
|
||||
),
|
||||
'redirect' => false,
|
||||
'cookies' => array()
|
||||
'cookies' => array(),
|
||||
)
|
||||
);
|
||||
|
||||
|
|
@ -127,23 +132,23 @@ class HttpSocket extends CakeSocket {
|
|||
/**
|
||||
* Build an HTTP Socket using the specified configuration.
|
||||
*
|
||||
* You can use a url string to set the url and use default configurations for
|
||||
* You can use a URL string to set the URL and use default configurations for
|
||||
* all other options:
|
||||
*
|
||||
* `$http = new HttpSocket('http://cakephp.org/');`
|
||||
*
|
||||
* Or use an array to configure multiple options:
|
||||
*
|
||||
* {{{
|
||||
* ```
|
||||
* $http = new HttpSocket(array(
|
||||
* 'host' => 'cakephp.org',
|
||||
* 'timeout' => 20
|
||||
* ));
|
||||
* }}}
|
||||
* ```
|
||||
*
|
||||
* See HttpSocket::$config for options that can be used.
|
||||
*
|
||||
* @param string|array $config Configuration information, either a string url or an array of options.
|
||||
* @param string|array $config Configuration information, either a string URL or an array of options.
|
||||
*/
|
||||
public function __construct($config = array()) {
|
||||
if (is_string($config)) {
|
||||
|
|
@ -161,24 +166,24 @@ class HttpSocket extends CakeSocket {
|
|||
/**
|
||||
* Set authentication settings.
|
||||
*
|
||||
* Accepts two forms of parameters. If all you need is a username + password, as with
|
||||
* Accepts two forms of parameters. If all you need is a username + password, as with
|
||||
* Basic authentication you can do the following:
|
||||
*
|
||||
* {{{
|
||||
* ```
|
||||
* $http->configAuth('Basic', 'mark', 'secret');
|
||||
* }}}
|
||||
* ```
|
||||
*
|
||||
* If you are using an authentication strategy that requires more inputs, like Digest authentication
|
||||
* you can call `configAuth()` with an array of user information.
|
||||
*
|
||||
* {{{
|
||||
* ```
|
||||
* $http->configAuth('Digest', array(
|
||||
* 'user' => 'mark',
|
||||
* 'pass' => 'secret',
|
||||
* 'realm' => 'my-realm',
|
||||
* 'nonce' => 1235
|
||||
* ));
|
||||
* }}}
|
||||
* ```
|
||||
*
|
||||
* To remove any set authentication strategy, call `configAuth()` with no parameters:
|
||||
*
|
||||
|
|
@ -205,7 +210,7 @@ class HttpSocket extends CakeSocket {
|
|||
* Set proxy settings
|
||||
*
|
||||
* @param string|array $host Proxy host. Can be an array with settings to authentication class
|
||||
* @param integer $port Port. Default 3128.
|
||||
* @param int $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
|
||||
|
|
@ -226,7 +231,7 @@ class HttpSocket extends CakeSocket {
|
|||
/**
|
||||
* Set the resource to receive the request content. This resource must support fwrite.
|
||||
*
|
||||
* @param resource|boolean $resource Resource or false to disable the resource use
|
||||
* @param resource|bool $resource Resource or false to disable the resource use
|
||||
* @return void
|
||||
* @throws SocketException
|
||||
*/
|
||||
|
|
@ -246,7 +251,7 @@ class HttpSocket extends CakeSocket {
|
|||
* method and provide a more granular interface.
|
||||
*
|
||||
* @param string|array $request Either an URI string, or an array defining host/uri
|
||||
* @return mixed false on error, HttpResponse on success
|
||||
* @return mixed false on error, HttpSocketResponse on success
|
||||
* @throws SocketException
|
||||
*/
|
||||
public function request($request = array()) {
|
||||
|
|
@ -289,6 +294,7 @@ class HttpSocket extends CakeSocket {
|
|||
if (isset($host)) {
|
||||
$this->config['host'] = $host;
|
||||
}
|
||||
|
||||
$this->_setProxy();
|
||||
$this->request['proxy'] = $this->_proxy;
|
||||
|
||||
|
|
@ -306,8 +312,7 @@ class HttpSocket extends CakeSocket {
|
|||
if (isset($this->request['uri']['port'])) {
|
||||
$port = $this->request['uri']['port'];
|
||||
}
|
||||
if (
|
||||
($scheme === 'http' && $port != 80) ||
|
||||
if (($scheme === 'http' && $port != 80) ||
|
||||
($scheme === 'https' && $port != 443) ||
|
||||
($port != 80 && $port != 443)
|
||||
) {
|
||||
|
|
@ -318,12 +323,17 @@ class HttpSocket extends CakeSocket {
|
|||
|
||||
if (isset($this->request['uri']['user'], $this->request['uri']['pass'])) {
|
||||
$this->configAuth('Basic', $this->request['uri']['user'], $this->request['uri']['pass']);
|
||||
} elseif (isset($this->request['auth'], $this->request['auth']['method'], $this->request['auth']['user'], $this->request['auth']['pass'])) {
|
||||
$this->configAuth($this->request['auth']['method'], $this->request['auth']['user'], $this->request['auth']['pass']);
|
||||
}
|
||||
$authHeader = Hash::get($this->request, 'header.Authorization');
|
||||
if (empty($authHeader)) {
|
||||
$this->_setAuth();
|
||||
$this->request['auth'] = $this->_auth;
|
||||
}
|
||||
$this->_setAuth();
|
||||
$this->request['auth'] = $this->_auth;
|
||||
|
||||
if (is_array($this->request['body'])) {
|
||||
$this->request['body'] = http_build_query($this->request['body']);
|
||||
$this->request['body'] = http_build_query($this->request['body'], '', '&');
|
||||
}
|
||||
|
||||
if (!empty($this->request['body']) && !isset($this->request['header']['Content-Type'])) {
|
||||
|
|
@ -333,6 +343,9 @@ class HttpSocket extends CakeSocket {
|
|||
if (!empty($this->request['body']) && !isset($this->request['header']['Content-Length'])) {
|
||||
$this->request['header']['Content-Length'] = strlen($this->request['body']);
|
||||
}
|
||||
if (isset($this->request['uri']['scheme']) && $this->request['uri']['scheme'] === 'https' && in_array($this->config['protocol'], array(false, 'tcp'))) {
|
||||
$this->config['protocol'] = 'ssl';
|
||||
}
|
||||
|
||||
$connectionType = null;
|
||||
if (isset($this->request['header']['Connection'])) {
|
||||
|
|
@ -359,11 +372,13 @@ class HttpSocket extends CakeSocket {
|
|||
|
||||
$this->request['raw'] .= "\r\n";
|
||||
$this->request['raw'] .= $this->request['body'];
|
||||
|
||||
// SSL context is set during the connect() method.
|
||||
$this->write($this->request['raw']);
|
||||
|
||||
$response = null;
|
||||
$inHeader = true;
|
||||
while ($data = $this->read()) {
|
||||
while (($data = $this->read()) !== false) {
|
||||
if ($this->_contentResource) {
|
||||
if ($inHeader) {
|
||||
$response .= $data;
|
||||
|
|
@ -395,6 +410,7 @@ class HttpSocket extends CakeSocket {
|
|||
throw new SocketException(__d('cake_dev', 'Class %s not found.', $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();
|
||||
|
|
@ -403,7 +419,8 @@ class HttpSocket extends CakeSocket {
|
|||
}
|
||||
|
||||
if ($this->request['redirect'] && $this->response->isRedirect()) {
|
||||
$request['uri'] = trim(urldecode($this->response->getHeader('Location')), '=');
|
||||
$location = trim($this->response->getHeader('Location'), '=');
|
||||
$request['uri'] = str_replace('%2F', '/', $location);
|
||||
$request['redirect'] = is_int($this->request['redirect']) ? $this->request['redirect'] - 1 : $this->request['redirect'];
|
||||
$this->response = $this->request($request);
|
||||
}
|
||||
|
|
@ -422,12 +439,12 @@ class HttpSocket extends CakeSocket {
|
|||
*
|
||||
* You could express the same thing using a uri array and query string parameters:
|
||||
*
|
||||
* {{{
|
||||
* ```
|
||||
* $response = $http->get(
|
||||
* array('host' => 'google.com', 'path' => '/search'),
|
||||
* array('q' => 'cakephp', 'client' => 'safari')
|
||||
* );
|
||||
* }}}
|
||||
* ```
|
||||
*
|
||||
* @param string|array $uri URI to request. Either a string uri, or a uri array, see HttpSocket::_parseUri()
|
||||
* @param array $query Querystring parameters to append to URI
|
||||
|
|
@ -449,20 +466,46 @@ class HttpSocket extends CakeSocket {
|
|||
return $this->request($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Issues a HEAD request to the specified URI, query, and request.
|
||||
*
|
||||
* By definition HEAD request are identical to GET request except they return no response body. This means that all
|
||||
* information and examples relevant to GET also applys to HEAD.
|
||||
*
|
||||
* @param string|array $uri URI to request. Either a string URI, or a URI array, see HttpSocket::_parseUri()
|
||||
* @param array $query Querystring parameters to append to URI
|
||||
* @param array $request An indexed array with indexes such as 'method' or uri
|
||||
* @return mixed Result of request, either false on failure or the response to the request.
|
||||
*/
|
||||
public function head($uri = null, $query = array(), $request = array()) {
|
||||
if (!empty($query)) {
|
||||
$uri = $this->_parseUri($uri, $this->config['request']['uri']);
|
||||
if (isset($uri['query'])) {
|
||||
$uri['query'] = array_merge($uri['query'], $query);
|
||||
} else {
|
||||
$uri['query'] = $query;
|
||||
}
|
||||
$uri = $this->_buildUri($uri);
|
||||
}
|
||||
|
||||
$request = Hash::merge(array('method' => 'HEAD', 'uri' => $uri), $request);
|
||||
return $this->request($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Issues a POST request to the specified URI, query, and request.
|
||||
*
|
||||
* `post()` can be used to post simple data arrays to a url:
|
||||
* `post()` can be used to post simple data arrays to a URL:
|
||||
*
|
||||
* {{{
|
||||
* ```
|
||||
* $response = $http->post('http://example.com', array(
|
||||
* 'username' => 'batman',
|
||||
* 'password' => 'bruce_w4yne'
|
||||
* ));
|
||||
* }}}
|
||||
* ```
|
||||
*
|
||||
* @param string|array $uri URI to request. See HttpSocket::_parseUri()
|
||||
* @param array $data Array of POST data keys and values.
|
||||
* @param array $data Array of request body data keys and values.
|
||||
* @param array $request An indexed array with indexes such as 'method' or uri
|
||||
* @return mixed Result of request, either false on failure or the response to the request.
|
||||
*/
|
||||
|
|
@ -475,7 +518,7 @@ class HttpSocket extends CakeSocket {
|
|||
* Issues a PUT request to the specified URI, query, and request.
|
||||
*
|
||||
* @param string|array $uri URI to request, See HttpSocket::_parseUri()
|
||||
* @param array $data Array of PUT data keys and values.
|
||||
* @param array $data Array of request body data keys and values.
|
||||
* @param array $request An indexed array with indexes such as 'method' or uri
|
||||
* @return mixed Result of request
|
||||
*/
|
||||
|
|
@ -484,11 +527,24 @@ class HttpSocket extends CakeSocket {
|
|||
return $this->request($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Issues a PATCH request to the specified URI, query, and request.
|
||||
*
|
||||
* @param string|array $uri URI to request, See HttpSocket::_parseUri()
|
||||
* @param array $data Array of request body data keys and values.
|
||||
* @param array $request An indexed array with indexes such as 'method' or uri
|
||||
* @return mixed Result of request
|
||||
*/
|
||||
public function patch($uri = null, $data = array(), $request = array()) {
|
||||
$request = Hash::merge(array('method' => 'PATCH', 'uri' => $uri, 'body' => $data), $request);
|
||||
return $this->request($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Issues a DELETE request to the specified URI, query, and request.
|
||||
*
|
||||
* @param string|array $uri URI to request (see {@link _parseUri()})
|
||||
* @param array $data Query to append to URI
|
||||
* @param array $data Array of request body data keys and values.
|
||||
* @param array $request An indexed array with indexes such as 'method' or uri
|
||||
* @return mixed Result of request
|
||||
*/
|
||||
|
|
@ -498,19 +554,19 @@ class HttpSocket extends CakeSocket {
|
|||
}
|
||||
|
||||
/**
|
||||
* Normalizes urls into a $uriTemplate. If no template is provided
|
||||
* a default one will be used. Will generate the url using the
|
||||
* 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.
|
||||
*
|
||||
* ### Usage:
|
||||
*
|
||||
* After configuring part of the request parameters, you can use url() to generate
|
||||
* urls.
|
||||
* URLs.
|
||||
*
|
||||
* {{{
|
||||
* ```
|
||||
* $http = new HttpSocket('http://www.cakephp.org');
|
||||
* $url = $http->url('/search?q=bar');
|
||||
* }}}
|
||||
* ```
|
||||
*
|
||||
* Would return `http://www.cakephp.org/search?q=bar`
|
||||
*
|
||||
|
|
@ -520,12 +576,12 @@ class HttpSocket extends CakeSocket {
|
|||
*
|
||||
* Would return `/search?q=socket`.
|
||||
*
|
||||
* @param string|array Either a string or array of url options to create a url with.
|
||||
* @param string $uriTemplate A template string to use for url formatting.
|
||||
* @return mixed Either false on failure or a string containing the composed url.
|
||||
* @param string|array $url Either a string or array of URL options to create a URL with.
|
||||
* @param string $uriTemplate A template string to use for URL formatting.
|
||||
* @return mixed Either false on failure or a string containing the composed URL.
|
||||
*/
|
||||
public function url($url = null, $uriTemplate = null) {
|
||||
if (is_null($url)) {
|
||||
if ($url === null) {
|
||||
$url = '/';
|
||||
}
|
||||
if (is_string($url)) {
|
||||
|
|
@ -537,7 +593,7 @@ class HttpSocket extends CakeSocket {
|
|||
if (is_array($port)) {
|
||||
$port = $port[0];
|
||||
}
|
||||
if ($url{0} == '/') {
|
||||
if ($url{0} === '/') {
|
||||
$url = $this->config['request']['uri']['host'] . ':' . $port . $url;
|
||||
}
|
||||
if (!preg_match('/^.+:\/\/|\*|^\//', $url)) {
|
||||
|
|
@ -579,7 +635,7 @@ class HttpSocket extends CakeSocket {
|
|||
throw new SocketException(__d('cake_dev', 'Unknown authentication method.'));
|
||||
}
|
||||
if (!method_exists($authClass, 'authentication')) {
|
||||
throw new SocketException(sprintf(__d('cake_dev', 'The %s do not support authentication.'), $authClass));
|
||||
throw new SocketException(__d('cake_dev', 'The %s does not support authentication.', $authClass));
|
||||
}
|
||||
call_user_func_array("$authClass::authentication", array($this, &$this->_auth[$method]));
|
||||
}
|
||||
|
|
@ -596,6 +652,7 @@ class HttpSocket extends CakeSocket {
|
|||
}
|
||||
$this->config['host'] = $this->_proxy['host'];
|
||||
$this->config['port'] = $this->_proxy['port'];
|
||||
$this->config['proxy'] = true;
|
||||
|
||||
if (empty($this->_proxy['method']) || !isset($this->_proxy['user'], $this->_proxy['pass'])) {
|
||||
return;
|
||||
|
|
@ -608,16 +665,23 @@ class HttpSocket extends CakeSocket {
|
|||
throw new SocketException(__d('cake_dev', 'Unknown authentication method for proxy.'));
|
||||
}
|
||||
if (!method_exists($authClass, 'proxyAuthentication')) {
|
||||
throw new SocketException(sprintf(__d('cake_dev', 'The %s do not support proxy authentication.'), $authClass));
|
||||
throw new SocketException(__d('cake_dev', 'The %s does not support proxy authentication.', $authClass));
|
||||
}
|
||||
call_user_func_array("$authClass::proxyAuthentication", array($this, &$this->_proxy));
|
||||
|
||||
if (!empty($this->request['header']['Proxy-Authorization'])) {
|
||||
$this->config['proxyauth'] = $this->request['header']['Proxy-Authorization'];
|
||||
if ($this->request['uri']['scheme'] === 'https') {
|
||||
$this->request['header'] = Hash::remove($this->request['header'], 'Proxy-Authorization');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses and sets the specified URI into current request configuration.
|
||||
*
|
||||
* @param string|array $uri URI, See HttpSocket::_parseUri()
|
||||
* @return boolean If uri has merged in config
|
||||
* @return bool If uri has merged in config
|
||||
*/
|
||||
protected function _configUri($uri = null) {
|
||||
if (empty($uri)) {
|
||||
|
|
@ -661,7 +725,7 @@ class HttpSocket extends CakeSocket {
|
|||
}
|
||||
|
||||
$uri['path'] = preg_replace('/^\//', null, $uri['path']);
|
||||
$uri['query'] = http_build_query($uri['query']);
|
||||
$uri['query'] = http_build_query($uri['query'], '', '&');
|
||||
$uri['query'] = rtrim($uri['query'], '=');
|
||||
$stripIfEmpty = array(
|
||||
'query' => '?%query',
|
||||
|
|
@ -695,7 +759,7 @@ class HttpSocket extends CakeSocket {
|
|||
* such as 'scheme', 'port', 'query'.
|
||||
*
|
||||
* @param string|array $uri URI to parse
|
||||
* @param boolean|array $base If true use default URI config, otherwise indexed array to set 'scheme', 'host', 'port', etc.
|
||||
* @param bool|array $base If true use default URI config, otherwise indexed array to set 'scheme', 'host', 'port', etc.
|
||||
* @return array Parsed URI
|
||||
*/
|
||||
protected function _parseUri($uri = null, $base = array()) {
|
||||
|
|
@ -823,16 +887,15 @@ class HttpSocket extends CakeSocket {
|
|||
* Builds a request line according to HTTP/1.1 specs. Activate quirks mode to work outside specs.
|
||||
*
|
||||
* @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 SocketException
|
||||
*/
|
||||
protected function _buildRequestLine($request = array(), $versionToken = 'HTTP/1.1') {
|
||||
protected function _buildRequestLine($request = array()) {
|
||||
$asteriskMethods = array('OPTIONS');
|
||||
|
||||
if (is_string($request)) {
|
||||
$isValid = preg_match("/(.+) (.+) (.+)\r\n/U", $request, $match);
|
||||
if (!$this->quirksMode && (!$isValid || ($match[2] == '*' && !in_array($match[3], $asteriskMethods)))) {
|
||||
if (!$this->quirksMode && (!$isValid || ($match[2] === '*' && !in_array($match[3], $asteriskMethods)))) {
|
||||
throw new SocketException(__d('cake_dev', 'HttpSocket::_buildRequestLine - Passed an invalid request line string. Activate quirks mode to do this.'));
|
||||
}
|
||||
return $request;
|
||||
|
|
@ -842,9 +905,9 @@ class HttpSocket extends CakeSocket {
|
|||
return false;
|
||||
}
|
||||
|
||||
$request['uri'] = $this->_parseUri($request['uri']);
|
||||
$request = array_merge(array('method' => 'GET'), $request);
|
||||
if (!empty($this->_proxy['host'])) {
|
||||
$request['uri'] = $this->_parseUri($request['uri']);
|
||||
$request += array('method' => 'GET');
|
||||
if (!empty($this->_proxy['host']) && $request['uri']['scheme'] !== 'https') {
|
||||
$request['uri'] = $this->_buildUri($request['uri'], '%scheme://%host:%port/%path?%query');
|
||||
} else {
|
||||
$request['uri'] = $this->_buildUri($request['uri'], '/%path?%query');
|
||||
|
|
@ -853,14 +916,15 @@ class HttpSocket extends CakeSocket {
|
|||
if (!$this->quirksMode && $request['uri'] === '*' && !in_array($request['method'], $asteriskMethods)) {
|
||||
throw new SocketException(__d('cake_dev', '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 . "\r\n";
|
||||
$version = isset($request['version']) ? $request['version'] : '1.1';
|
||||
return $request['method'] . ' ' . $request['uri'] . ' HTTP/' . $version . "\r\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the header.
|
||||
*
|
||||
* @param array $header Header to build
|
||||
* @param string $mode
|
||||
* @param string $mode Mode
|
||||
* @return string Header built from array
|
||||
*/
|
||||
protected function _buildHeader($header, $mode = 'standard') {
|
||||
|
|
@ -883,7 +947,7 @@ class HttpSocket extends CakeSocket {
|
|||
|
||||
$returnHeader = '';
|
||||
foreach ($header as $field => $contents) {
|
||||
if (is_array($contents) && $mode == 'standard') {
|
||||
if (is_array($contents) && $mode === 'standard') {
|
||||
$contents = implode(',', $contents);
|
||||
}
|
||||
foreach ((array)$contents as $content) {
|
||||
|
|
@ -899,13 +963,21 @@ class HttpSocket extends CakeSocket {
|
|||
/**
|
||||
* Builds cookie headers for a request.
|
||||
*
|
||||
* Cookies can either be in the format returned in responses, or
|
||||
* a simple key => value pair.
|
||||
*
|
||||
* @param array $cookies Array of cookies to send with the request.
|
||||
* @return string Cookie header string to be sent with the request.
|
||||
*/
|
||||
public function buildCookies($cookies) {
|
||||
$header = array();
|
||||
foreach ($cookies as $name => $cookie) {
|
||||
$header[] = $name . '=' . $this->_escapeToken($cookie['value'], array(';'));
|
||||
if (is_array($cookie)) {
|
||||
$value = $this->_escapeToken($cookie['value'], array(';'));
|
||||
} else {
|
||||
$value = $this->_escapeToken($cookie, array(';'));
|
||||
}
|
||||
$header[] = $name . '=' . $value;
|
||||
}
|
||||
return $this->_buildHeader(array('Cookie' => implode('; ', $header)), 'pragmatic');
|
||||
}
|
||||
|
|
@ -914,7 +986,7 @@ class HttpSocket extends CakeSocket {
|
|||
* Escapes a given $token according to RFC 2616 (HTTP 1.1 specs)
|
||||
*
|
||||
* @param string $token Token to escape
|
||||
* @param array $chars
|
||||
* @param array $chars Characters to escape
|
||||
* @return string Escaped token
|
||||
*/
|
||||
protected function _escapeToken($token, $chars = null) {
|
||||
|
|
@ -926,8 +998,8 @@ 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
|
||||
* @param bool $hex true to get them as HEX values, false otherwise
|
||||
* @param array $chars Characters to escape
|
||||
* @return array Escape chars
|
||||
*/
|
||||
protected function _tokenEscapeChars($hex = true, $chars = null) {
|
||||
|
|
@ -941,7 +1013,7 @@ class HttpSocket extends CakeSocket {
|
|||
$escape[] = chr(127);
|
||||
}
|
||||
|
||||
if ($hex == false) {
|
||||
if (!$hex) {
|
||||
return $escape;
|
||||
}
|
||||
foreach ($escape as $key => $char) {
|
||||
|
|
@ -951,11 +1023,11 @@ class HttpSocket extends CakeSocket {
|
|||
}
|
||||
|
||||
/**
|
||||
* Resets the state of this HttpSocket instance to it's initial state (before Object::__construct got executed) or does
|
||||
* Resets the state of this HttpSocket instance to it's initial state (before CakeObject::__construct got executed) or does
|
||||
* the same thing partially for the request and the response property only.
|
||||
*
|
||||
* @param boolean $full If set to false only HttpSocket::response and HttpSocket::request are reseted
|
||||
* @return boolean True on success
|
||||
* @param bool $full If set to false only HttpSocket::response and HttpSocket::request are reset
|
||||
* @return bool True on success
|
||||
*/
|
||||
public function reset($full = true) {
|
||||
static $initalState = array();
|
||||
|
|
@ -972,3 +1044,4 @@ class HttpSocket extends CakeSocket {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
461
lib/Cake/Network/Http/HttpSocketResponse.php
Normal file
461
lib/Cake/Network/Http/HttpSocketResponse.php
Normal file
|
|
@ -0,0 +1,461 @@
|
|||
<?php
|
||||
/**
|
||||
* HTTP Response from HttpSocket.
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @since CakePHP(tm) v 2.0.0
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
/**
|
||||
* HTTP Response from HttpSocket.
|
||||
*
|
||||
* @package Cake.Network.Http
|
||||
*/
|
||||
class HttpSocketResponse 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 int
|
||||
*/
|
||||
public $code = 0;
|
||||
|
||||
/**
|
||||
* Reason phrase
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $reasonPhrase = '';
|
||||
|
||||
/**
|
||||
* Pure raw content
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $raw = '';
|
||||
|
||||
/**
|
||||
* Context data in the response.
|
||||
* Contains SSL certificates for example.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $context = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $message Message to parse.
|
||||
*/
|
||||
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.
|
||||
* @param array $headers Headers to format.
|
||||
* @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 bool
|
||||
*/
|
||||
public function isOk() {
|
||||
return in_array($this->code, array(200, 201, 202, 203, 204, 205, 206));
|
||||
}
|
||||
|
||||
/**
|
||||
* If return is a valid 3xx (Redirection)
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isRedirect() {
|
||||
return in_array($this->code, array(301, 302, 303, 307)) && $this->getHeader('Location') !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given message and breaks it down in parts.
|
||||
*
|
||||
* @param string $message Message to parse
|
||||
* @return void
|
||||
* @throws SocketException
|
||||
*/
|
||||
public function parseResponse($message) {
|
||||
if (!is_string($message)) {
|
||||
throw new SocketException(__d('cake_dev', 'Invalid response.'));
|
||||
}
|
||||
|
||||
if (!preg_match("/^(.+\r\n)(.*)(?<=\r\n)\r\n/Us", $message, $match)) {
|
||||
throw new SocketException(__d('cake_dev', 'Invalid HTTP response.'));
|
||||
}
|
||||
|
||||
list(, $statusLine, $header) = $match;
|
||||
$this->raw = $message;
|
||||
$this->body = (string)substr($message, strlen($match[0]));
|
||||
|
||||
if (preg_match("/(.+) ([0-9]{3})(?:\s+(\w.+))?\s*\r\n/DU", $statusLine, $match)) {
|
||||
$this->httpVersion = $match[1];
|
||||
$this->code = $match[2];
|
||||
if (isset($match[3])) {
|
||||
$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 containing the body to decode.
|
||||
* @param string|bool $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 containing the chunked body to decode.
|
||||
* @return mixed Array of response headers and body or false.
|
||||
* @throws SocketException
|
||||
*/
|
||||
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|\n)/iU', $body, $match)) {
|
||||
// Handle remaining invalid data as one big chunk.
|
||||
preg_match('/^(.*?)\r\n/', $body, $invalidMatch);
|
||||
$length = isset($invalidMatch[1]) ? strlen($invalidMatch[1]) : 0;
|
||||
$match = array(
|
||||
0 => '',
|
||||
1 => dechex($length)
|
||||
);
|
||||
}
|
||||
$chunkSize = 0;
|
||||
$hexLength = 0;
|
||||
if (isset($match[0])) {
|
||||
$chunkSize = $match[0];
|
||||
}
|
||||
if (isset($match[1])) {
|
||||
$hexLength = $match[1];
|
||||
}
|
||||
|
||||
$chunkLength = hexdec($hexLength);
|
||||
$body = substr($body, strlen($chunkSize));
|
||||
|
||||
$decodedBody .= substr($body, 0, $chunkLength);
|
||||
if ($chunkLength) {
|
||||
$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("/(.+):(.+)(?:\r\n|\$)/Uis", $header, $matches, PREG_SET_ORDER);
|
||||
$lines = explode("\r\n", $header);
|
||||
|
||||
$header = array();
|
||||
foreach ($lines as $line) {
|
||||
if (strlen($line) === 0) {
|
||||
continue;
|
||||
}
|
||||
$continuation = false;
|
||||
$first = substr($line, 0, 1);
|
||||
|
||||
// Multi-line header
|
||||
if ($first === ' ' || $first === "\t") {
|
||||
$value .= preg_replace("/\s+/", ' ', $line);
|
||||
$continuation = true;
|
||||
} elseif (strpos($line, ':') !== false) {
|
||||
list($field, $value) = explode(':', $line, 2);
|
||||
$field = $this->_unescapeToken($field);
|
||||
}
|
||||
|
||||
$value = trim($value);
|
||||
if (!isset($header[$field]) || $continuation) {
|
||||
$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 received.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
$nameParts = explode('=', array_shift($parts), 2);
|
||||
if (count($nameParts) < 2) {
|
||||
$nameParts = array('', $nameParts[0]);
|
||||
}
|
||||
list($name, $value) = $nameParts;
|
||||
$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 Characters to unescape.
|
||||
* @return string Unescaped token
|
||||
*/
|
||||
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 bool $hex True to get them as HEX values, false otherwise.
|
||||
* @param array $chars Characters to uescape.
|
||||
* @return array Escape chars
|
||||
*/
|
||||
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) {
|
||||
return $escape;
|
||||
}
|
||||
foreach ($escape as $key => $char) {
|
||||
$escape[$key] = '\\x' . str_pad(dechex(ord($char)), 2, '0', STR_PAD_LEFT);
|
||||
}
|
||||
return $escape;
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess - Offset Exists
|
||||
*
|
||||
* @param string $offset Offset to check.
|
||||
* @return bool
|
||||
*/
|
||||
public function offsetExists($offset) {
|
||||
return in_array($offset, array('raw', 'status', 'header', 'body', 'cookies'));
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess - Offset Get
|
||||
*
|
||||
* @param string $offset Offset to get.
|
||||
* @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 - Offset Set
|
||||
*
|
||||
* @param string $offset Offset to set.
|
||||
* @param mixed $value Value.
|
||||
* @return void
|
||||
*/
|
||||
public function offsetSet($offset, $value) {
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess - Offset Unset
|
||||
*
|
||||
* @param string $offset Offset to unset.
|
||||
* @return void
|
||||
*/
|
||||
public function offsetUnset($offset) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Instance as string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString() {
|
||||
return $this->body();
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue