Skip to content

Commit

Permalink
merged branch simensen/normalize-querystring (PR #4711)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fabpot committed Jul 2, 2012
2 parents 67a69ea + 6296a24 commit 736aa21
Showing 1 changed file with 35 additions and 20 deletions.
55 changes: 35 additions & 20 deletions src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -463,6 +463,40 @@ static public function isProxyTrusted()
return self::$trustProxy;
}

/**
* Normalizes 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
*/
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.
*
Expand Down Expand Up @@ -809,26 +843,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'));
}

/**
Expand Down

0 comments on commit 736aa21

Please sign in to comment.