Skip to content
This repository
Browse code

Using native methods to query in HttpSocket.

  • Loading branch information...
commit fcd96bcc60fbf91dcf9e77fcd72c0a414b9465d2 1 parent bcdf61a
Juan Basso authored October 29, 2011
64  lib/Cake/Network/Http/HttpSocket.php
@@ -298,7 +298,7 @@ public function request($request = array()) {
298 298
 		$this->request['auth'] = $this->_auth;
299 299
 
300 300
 		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']);
302 302
 		}
303 303
 
304 304
 		if (!empty($this->request['body']) && !isset($this->request['header']['Content-Type'])) {
@@ -623,7 +623,7 @@ protected function _buildUri($uri = array(), $uriTemplate = '%scheme://%user:%pa
623 623
 		}
624 624
 
625 625
 		$uri['path'] = preg_replace('/^\//', null, $uri['path']);
626  
-		$uri['query'] = $this->_httpSerialize($uri['query']);
  626
+		$uri['query'] = http_build_query($uri['query']);
627 627
 		$stripIfEmpty = array(
628 628
 			'query' => '?%query',
629 629
 			'fragment' => '#%fragment',
@@ -728,49 +728,7 @@ protected function _parseQuery($query) {
728 728
 		if (is_array($query)) {
729 729
 			return $query;
730 730
 		}
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);
774 732
 		return $parsedQuery;
775 733
 	}
776 734
 
@@ -812,22 +770,6 @@ protected function _buildRequestLine($request = array(), $versionToken = 'HTTP/1
812 770
 	}
813 771
 
814 772
 /**
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  
-
830  
-/**
831 773
  * Builds the header.
832 774
  *
833 775
  * @param array $header Header to build
3  lib/Cake/Test/Case/Network/Http/HttpSocketTest.php
@@ -1284,9 +1284,6 @@ public function testParseQuery() {
1284 1284
 		$query = $this->Socket->parseQuery('a[]=foo&a[]=bar&a[]=cake');
1285 1285
 		$this->assertEquals($query, array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake')));
1286 1286
 
1287  
-		$query = $this->Socket->parseQuery('a]][[=foo&[]=bar&]]][]=cake');
1288  
-		$this->assertEquals($query, array('a]][[' => 'foo', 0 => 'bar', ']]]' => array('cake')));
1289  
-
1290 1287
 		$query = $this->Socket->parseQuery('a[][]=foo&a[][]=bar&a[][]=cake');
1291 1288
 		$expectedQuery = array(
1292 1289
 			'a' => array(

0 notes on commit fcd96bc

Please sign in to comment.
Something went wrong with that request. Please try again.