Skip to content

Commit

Permalink
Using native methods to query in HttpSocket.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Oct 29, 2011
1 parent bcdf61a commit fcd96bc
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 64 deletions.
64 changes: 3 additions & 61 deletions lib/Cake/Network/Http/HttpSocket.php
Expand Up @@ -298,7 +298,7 @@ public function request($request = array()) {
$this->request['auth'] = $this->_auth;

if (is_array($this->request['body'])) {
$this->request['body'] = $this->_httpSerialize($this->request['body']);
$this->request['body'] = http_build_query($this->request['body']);
}

if (!empty($this->request['body']) && !isset($this->request['header']['Content-Type'])) {
Expand Down Expand Up @@ -623,7 +623,7 @@ protected function _buildUri($uri = array(), $uriTemplate = '%scheme://%user:%pa
}

$uri['path'] = preg_replace('/^\//', null, $uri['path']);
$uri['query'] = $this->_httpSerialize($uri['query']);
$uri['query'] = http_build_query($uri['query']);
$stripIfEmpty = array(
'query' => '?%query',
'fragment' => '#%fragment',
Expand Down Expand Up @@ -728,49 +728,7 @@ protected function _parseQuery($query) {
if (is_array($query)) {
return $query;
}
$parsedQuery = array();

if (is_string($query) && !empty($query)) {
$query = preg_replace('/^\?/', '', $query);
$items = explode('&', $query);

foreach ($items as $item) {
if (strpos($item, '=') !== false) {
list($key, $value) = explode('=', $item, 2);
} else {
$key = $item;
$value = null;
}

$key = urldecode($key);
$value = urldecode($value);

if (preg_match_all('/\[([^\[\]]*)\]/iUs', $key, $matches)) {
$subKeys = $matches[1];
$rootKey = substr($key, 0, strpos($key, '['));
if (!empty($rootKey)) {
array_unshift($subKeys, $rootKey);
}
$queryNode =& $parsedQuery;

foreach ($subKeys as $subKey) {
if (!is_array($queryNode)) {
$queryNode = array();
}

if ($subKey === '') {
$queryNode[] = array();
end($queryNode);
$subKey = key($queryNode);
}
$queryNode =& $queryNode[$subKey];
}
$queryNode = $value;
} else {
$parsedQuery[$key] = $value;
}
}
}
parse_str(ltrim($query, '?'), $parsedQuery);
return $parsedQuery;
}

Expand Down Expand Up @@ -811,22 +769,6 @@ protected function _buildRequestLine($request = array(), $versionToken = 'HTTP/1
return $request['method'] . ' ' . $request['uri'] . ' ' . $versionToken . "\r\n";
}

/**
* Serializes an array for transport.
*
* @param array $data Data to serialize
* @return string Serialized variable
*/
protected function _httpSerialize($data = array()) {
if (is_string($data)) {
return $data;
}
if (empty($data) || !is_array($data)) {
return false;
}
return substr(Router::queryString($data), 1);
}

/**
* Builds the header.
*
Expand Down
3 changes: 0 additions & 3 deletions lib/Cake/Test/Case/Network/Http/HttpSocketTest.php
Expand Up @@ -1284,9 +1284,6 @@ public function testParseQuery() {
$query = $this->Socket->parseQuery('a[]=foo&a[]=bar&a[]=cake');
$this->assertEquals($query, array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake')));

$query = $this->Socket->parseQuery('a]][[=foo&[]=bar&]]][]=cake');
$this->assertEquals($query, array('a]][[' => 'foo', 0 => 'bar', ']]]' => array('cake')));

$query = $this->Socket->parseQuery('a[][]=foo&a[][]=bar&a[][]=cake');
$expectedQuery = array(
'a' => array(
Expand Down

0 comments on commit fcd96bc

Please sign in to comment.