From 2f314a94e39886b2216e9a6e197413fb24ddc152 Mon Sep 17 00:00:00 2001 From: Beau Simensen Date: Mon, 2 Jul 2012 06:48:22 -0700 Subject: [PATCH] Standalone query string normalization --- .../Component/HttpFoundation/Request.php | 57 ++++++++++++------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index b868bd912c5e..f50290020916 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -463,6 +463,42 @@ static public function isProxyTrusted() return self::$trustProxy; } + /** + * Normalize a query string. + * + * It builds a normalized query string, where keys/value pairs are alphabetized + * and have consistent escaping. + * + * @param string $qs Query string + * + * @return string|null A normalized query string for the Request + * + * @api + */ + static public function normalizeQueryString($qs = null) + { + if (!$qs) { + return null; + } + + $parts = array(); + $order = array(); + + foreach (explode('&', $qs) as $segment) { + if (false === strpos($segment, '=')) { + $parts[] = $segment; + $order[] = $segment; + } else { + $tmp = explode('=', rawurldecode($segment), 2); + $parts[] = rawurlencode($tmp[0]).'='.rawurlencode($tmp[1]); + $order[] = $tmp[0]; + } + } + array_multisort($order, SORT_ASC, $parts); + + return implode('&', $parts); + } + /** * Gets a "parameter" value. * @@ -809,26 +845,7 @@ public function getUriForPath($path) */ public function getQueryString() { - if (!$qs = $this->server->get('QUERY_STRING')) { - return null; - } - - $parts = array(); - $order = array(); - - foreach (explode('&', $qs) as $segment) { - if (false === strpos($segment, '=')) { - $parts[] = $segment; - $order[] = $segment; - } else { - $tmp = explode('=', rawurldecode($segment), 2); - $parts[] = rawurlencode($tmp[0]).'='.rawurlencode($tmp[1]); - $order[] = $tmp[0]; - } - } - array_multisort($order, SORT_ASC, $parts); - - return implode('&', $parts); + return static::normalizeQueryString($this->server->get('QUERY_STRING')); } /**