Skip to content
Permalink
Browse files

Formatting

  • Loading branch information...
jrbasso committed Dec 4, 2010
1 parent 874a117 commit 3f910dc6c1581594131cad74bf70414390400226
Showing with 30 additions and 39 deletions.
  1. +30 −39 cake/libs/http_socket.php
@@ -37,15 +37,13 @@ class HttpSocket extends CakeSocket {
* 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
*
* @var array
* @access public
*/
public $request = array(
'method' => 'GET',
@@ -71,11 +69,10 @@ class HttpSocket extends CakeSocket {
);
/**
* The default structure for storing the response
*
* @var array
* @access public
*/
* The default structure for storing the response
*
* @var array
*/
public $response = array(
'raw' => array(
'status-line' => null,
@@ -97,7 +94,6 @@ class HttpSocket extends CakeSocket {
* Default configuration settings for the HttpSocket
*
* @var array
* @access public
*/
public $config = array(
'persistent' => false,
@@ -119,23 +115,20 @@ class HttpSocket extends CakeSocket {
* String that represents a line break.
*
* @var string
* @access public
*/
public $lineBreak = "\r\n";
/**
* Authentication settings
*
* @var array
* @access protected
*/
protected $_auth = array();
/**
* Proxy settings
*
* @var array
* @access protected
*/
protected $_proxy = array();
@@ -425,7 +418,7 @@ public function delete($uri = null, $data = array(), $request = array()) {
}
/**
* 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.
*
@@ -457,10 +450,10 @@ public function url($url = null, $uriTemplate = null) {
}
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;
@@ -504,6 +497,7 @@ protected function _setAuth() {
* Set the proxy configuration and authentication
*
* @return void
* @throws Exception
*/
protected function _setProxyConfig() {
if (empty($this->_proxy) || !isset($this->_proxy['host'], $this->_proxy['port'])) {
@@ -570,7 +564,7 @@ protected function _parseResponse($message) {
$response['body'] = $decoded['body'];
if (!empty($decoded['header'])) {
$response['header'] = $this->_parseHeader($this->_buildHeader($response['header']).$this->_buildHeader($decoded['header']));
$response['header'] = $this->_parseHeader($this->_buildHeader($response['header']) . $this->_buildHeader($decoded['header']));
}
if (!empty($response['header'])) {
@@ -601,7 +595,7 @@ protected function _decodeBody($body, $encoding = 'chunked') {
if (empty($encoding)) {
return array('body' => $body, 'header' => false);
}
$decodeMethod = '_decode'.Inflector::camelize(str_replace('-', '_', $encoding)).'Body';
$decodeMethod = '_decode'.Inflector::camelize(str_replace('-', '_', $encoding)) . 'Body';
if (!is_callable(array(&$this, $decodeMethod))) {
if (!$this->quirksMode) {
@@ -663,7 +657,7 @@ protected function _decodeChunkedBody($body) {
}
$decodedBody .= $chunk;
if ($chunkLength !== 0) {
$body = substr($body, $chunkLength+strlen("\r\n"));
$body = substr($body, $chunkLength + strlen("\r\n"));
}
}
@@ -707,7 +701,7 @@ protected function _configUri($uri = null) {
/**
* 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
*/
@@ -741,7 +735,7 @@ protected function _buildUri($uri = array(), $uriTemplate = '%scheme://%user:%pa
$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 === '/*') {
@@ -816,7 +810,7 @@ protected function _parseUri($uri = null, $base = array()) {
* - ?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"
@@ -907,7 +901,7 @@ protected function _buildRequestLine($request = array(), $versionToken = 'HTTP/1
if (!$this->quirksMode && $request['uri'] === '*' && !in_array($request['method'], $asteriskMethods)) {
throw new Exception(sprintf(__('HttpSocket::_buildRequestLine - The "*" asterisk character is only allowed for the following methods: %s. Activate quirks mode to work outside of HTTP/1.1 specs.'), join(',', $asteriskMethods)));
}
return $request['method'].' '.$request['uri'].' '.$versionToken.$this->lineBreak;
return $request['method'] . ' ' . $request['uri'] . ' ' . $versionToken . $this->lineBreak;
}
/**
@@ -960,7 +954,7 @@ protected function _buildHeader($header, $mode = 'standard') {
$contents = preg_replace("/\r\n(?![\t ])/", "\r\n ", $content);
$field = $this->_escapeToken($field);
$returnHeader .= $field.': '.$contents.$this->lineBreak;
$returnHeader .= $field . ': ' . $contents . $this->lineBreak;
}
}
return $returnHeader;
@@ -1004,10 +998,9 @@ protected function _parseHeader($header) {
*
* @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) {
public function parseCookies($header) {
if (!isset($header['Set-Cookie'])) {
return false;
}
@@ -1016,7 +1009,7 @@ function parseCookies($header) {
foreach ((array)$header['Set-Cookie'] as $cookie) {
if (strpos($cookie, '";"') !== false) {
$cookie = str_replace('";"', "{__cookie_replace__}", $cookie);
$parts = str_replace("{__cookie_replace__}", '";"', explode(';', $cookie));
$parts = str_replace("{__cookie_replace__}", '";"', explode(';', $cookie));
} else {
$parts = preg_split('/\;[ \t]*/', $cookie);
}
@@ -1046,28 +1039,26 @@ function parseCookies($header) {
*
* @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;
return $this->_buildHeader(array('Cookie' => implode('; ', $header)), 'pragmatic');
}
/**
* 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
* @access protected
* @todo Test $chars parameter
*/
function _unescapeToken($token, $chars = null) {
$regex = '/"(['.join('', $this->_tokenEscapeChars(true, $chars)).'])"/';
protected function _unescapeToken($token, $chars = null) {
$regex = '/"([' . implode('', $this->_tokenEscapeChars(true, $chars)) . '])"/';
$token = preg_replace($regex, '\\1', $token);
return $token;
}
@@ -1076,12 +1067,12 @@ function _unescapeToken($token, $chars = null) {
* 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;
}
@@ -1090,11 +1081,11 @@ function _escapeToken($token, $chars = null) {
* 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 {
@@ -1110,7 +1101,7 @@ function _tokenEscapeChars($hex = true, $chars = null) {
}
$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;
}

0 comments on commit 3f910dc

Please sign in to comment.
You can’t perform that action at this time.