Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 736aa21

Browse files
committed
merged branch simensen/normalize-querystring (PR #4711)
Commits ------- 6296a24 Standalone query string normalization Discussion ---------- [HttpFoundation] Standalone query string normalization I wanted to leverage query string normalization in a test. I considered copying this code to my own library but I noticed that the only instance data `getQueryString` needed to know from the `Request` was `QUERY_STRING` so I broke the rest out into a standalone static function. --------------------------------------------------------------------------- by simensen at 2012-07-02T20:05:59Z I made the requested changes. --------------------------------------------------------------------------- by fabpot at 2012-07-02T20:10:27Z Can you squash your commits? Thanks. --------------------------------------------------------------------------- by simensen at 2012-07-02T20:16:52Z Sure, no problem. Hopefully I did it correctly.
2 parents 67a69ea + 6296a24 commit 736aa21

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,40 @@ static public function isProxyTrusted()
463463
return self::$trustProxy;
464464
}
465465

466+
/**
467+
* Normalizes a query string.
468+
*
469+
* It builds a normalized query string, where keys/value pairs are alphabetized
470+
* and have consistent escaping.
471+
*
472+
* @param string $qs Query string
473+
*
474+
* @return string|null A normalized query string for the Request
475+
*/
476+
static public function normalizeQueryString($qs = null)
477+
{
478+
if (!$qs) {
479+
return null;
480+
}
481+
482+
$parts = array();
483+
$order = array();
484+
485+
foreach (explode('&', $qs) as $segment) {
486+
if (false === strpos($segment, '=')) {
487+
$parts[] = $segment;
488+
$order[] = $segment;
489+
} else {
490+
$tmp = explode('=', rawurldecode($segment), 2);
491+
$parts[] = rawurlencode($tmp[0]).'='.rawurlencode($tmp[1]);
492+
$order[] = $tmp[0];
493+
}
494+
}
495+
array_multisort($order, SORT_ASC, $parts);
496+
497+
return implode('&', $parts);
498+
}
499+
466500
/**
467501
* Gets a "parameter" value.
468502
*
@@ -809,26 +843,7 @@ public function getUriForPath($path)
809843
*/
810844
public function getQueryString()
811845
{
812-
if (!$qs = $this->server->get('QUERY_STRING')) {
813-
return null;
814-
}
815-
816-
$parts = array();
817-
$order = array();
818-
819-
foreach (explode('&', $qs) as $segment) {
820-
if (false === strpos($segment, '=')) {
821-
$parts[] = $segment;
822-
$order[] = $segment;
823-
} else {
824-
$tmp = explode('=', rawurldecode($segment), 2);
825-
$parts[] = rawurlencode($tmp[0]).'='.rawurlencode($tmp[1]);
826-
$order[] = $tmp[0];
827-
}
828-
}
829-
array_multisort($order, SORT_ASC, $parts);
830-
831-
return implode('&', $parts);
846+
return static::normalizeQueryString($this->server->get('QUERY_STRING'));
832847
}
833848

834849
/**

0 commit comments

Comments
 (0)