From aa76ae33bd733137c5c8552dd1c686a5edf34564 Mon Sep 17 00:00:00 2001 From: Bob Weinand Date: Wed, 9 Mar 2016 12:33:42 +0100 Subject: [PATCH] Prevent (buggy) rewrite of URI query strings Prepare tag 1.0.3 --- CHANGELOG.md | 5 +++++ lib/Uri.php | 10 +++++----- test/UriTest.php | 9 ++------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f1f0eeb..b6379b90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +v1.0.3 +------ + +- Fix URI query string handling (do not rewrite) + v1.0.2 ------ diff --git a/lib/Uri.php b/lib/Uri.php index 44e9d5b3..f7681447 100644 --- a/lib/Uri.php +++ b/lib/Uri.php @@ -435,13 +435,13 @@ public function getAuthority($hiddenPass = true) { private function parseQueryParameters() { if ($this->query) { - parse_str(rawurldecode($this->query), $parameters); + $parameters = []; + foreach (explode("&", $this->query) as $pair) { + $pair = explode("=", $pair, 2); + $parameters[rawurldecode($pair[0])][] = rawurldecode(isset($pair[1]) ? $pair[1] : ""); + } $this->queryParameters = $parameters; - $this->query = str_replace('+', '%20', http_build_query($parameters, '', '&')); - - // Fix http_build_query adding equals sign to empty keys - $this->query = str_replace('=&', '&', rtrim($this->query, '=')); } } diff --git a/test/UriTest.php b/test/UriTest.php index 9b44955a..2021cb90 100644 --- a/test/UriTest.php +++ b/test/UriTest.php @@ -200,7 +200,7 @@ public function provideUris() { 'host' => 'localhost', 'port' => '', 'path' => '/test.php', - 'query' => http_build_query(array('params'=>array(1,2))), + 'query' => "params[]=1¶ms[]=2", 'fragment' => '' ) ) @@ -223,14 +223,9 @@ public function testUri($rawUri, $expectedVals) { } public function testQueryParams() { - $uri = new Uri('http://localhost/test.php?params[]=1¶ms[]=2'); + $uri = new Uri('http://localhost/test.php?params=1¶ms=2'); $expected = array('params' => array(1, 2)); $actual = $uri->getAllQueryParameters(); $this->assertEquals($expected, $actual); - - $uri = new Uri('http://localhost/test.php?params[1][0]=1¶ms[1][1]=2'); - $expected = array('params' => array(1 => array(1, 2))); - $actual = $uri->getAllQueryParameters(); - $this->assertEquals($expected, $actual); } }