Skip to content

Commit fcd96bc

Browse files
committed
Using native methods to query in HttpSocket.
1 parent bcdf61a commit fcd96bc

File tree

2 files changed

+3
-64
lines changed

2 files changed

+3
-64
lines changed

lib/Cake/Network/Http/HttpSocket.php

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public function request($request = array()) {
298298
$this->request['auth'] = $this->_auth;
299299

300300
if (is_array($this->request['body'])) {
301-
$this->request['body'] = $this->_httpSerialize($this->request['body']);
301+
$this->request['body'] = http_build_query($this->request['body']);
302302
}
303303

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

625625
$uri['path'] = preg_replace('/^\//', null, $uri['path']);
626-
$uri['query'] = $this->_httpSerialize($uri['query']);
626+
$uri['query'] = http_build_query($uri['query']);
627627
$stripIfEmpty = array(
628628
'query' => '?%query',
629629
'fragment' => '#%fragment',
@@ -728,49 +728,7 @@ protected function _parseQuery($query) {
728728
if (is_array($query)) {
729729
return $query;
730730
}
731-
$parsedQuery = array();
732-
733-
if (is_string($query) && !empty($query)) {
734-
$query = preg_replace('/^\?/', '', $query);
735-
$items = explode('&', $query);
736-
737-
foreach ($items as $item) {
738-
if (strpos($item, '=') !== false) {
739-
list($key, $value) = explode('=', $item, 2);
740-
} else {
741-
$key = $item;
742-
$value = null;
743-
}
744-
745-
$key = urldecode($key);
746-
$value = urldecode($value);
747-
748-
if (preg_match_all('/\[([^\[\]]*)\]/iUs', $key, $matches)) {
749-
$subKeys = $matches[1];
750-
$rootKey = substr($key, 0, strpos($key, '['));
751-
if (!empty($rootKey)) {
752-
array_unshift($subKeys, $rootKey);
753-
}
754-
$queryNode =& $parsedQuery;
755-
756-
foreach ($subKeys as $subKey) {
757-
if (!is_array($queryNode)) {
758-
$queryNode = array();
759-
}
760-
761-
if ($subKey === '') {
762-
$queryNode[] = array();
763-
end($queryNode);
764-
$subKey = key($queryNode);
765-
}
766-
$queryNode =& $queryNode[$subKey];
767-
}
768-
$queryNode = $value;
769-
} else {
770-
$parsedQuery[$key] = $value;
771-
}
772-
}
773-
}
731+
parse_str(ltrim($query, '?'), $parsedQuery);
774732
return $parsedQuery;
775733
}
776734

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

814-
/**
815-
* Serializes an array for transport.
816-
*
817-
* @param array $data Data to serialize
818-
* @return string Serialized variable
819-
*/
820-
protected function _httpSerialize($data = array()) {
821-
if (is_string($data)) {
822-
return $data;
823-
}
824-
if (empty($data) || !is_array($data)) {
825-
return false;
826-
}
827-
return substr(Router::queryString($data), 1);
828-
}
829-
830772
/**
831773
* Builds the header.
832774
*

lib/Cake/Test/Case/Network/Http/HttpSocketTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,9 +1284,6 @@ public function testParseQuery() {
12841284
$query = $this->Socket->parseQuery('a[]=foo&a[]=bar&a[]=cake');
12851285
$this->assertEquals($query, array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake')));
12861286

1287-
$query = $this->Socket->parseQuery('a]][[=foo&[]=bar&]]][]=cake');
1288-
$this->assertEquals($query, array('a]][[' => 'foo', 0 => 'bar', ']]]' => array('cake')));
1289-
12901287
$query = $this->Socket->parseQuery('a[][]=foo&a[][]=bar&a[][]=cake');
12911288
$expectedQuery = array(
12921289
'a' => array(

0 commit comments

Comments
 (0)