Skip to content

Commit

Permalink
Prevent (buggy) rewrite of URI query strings
Browse files Browse the repository at this point in the history
Prepare tag 2.0.3
  • Loading branch information
bwoebi committed Mar 9, 2016
1 parent 6679833 commit cc665ac
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v2.0.3
------

- Fix URI query string handling (do not rewrite)

v2.0.2
------

Expand Down
10 changes: 5 additions & 5 deletions lib/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,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, '='));
}
}

Expand Down
9 changes: 2 additions & 7 deletions test/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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&params[]=2",
'fragment' => ''
)
)
Expand All @@ -223,14 +223,9 @@ public function testUri($rawUri, $expectedVals) {
}

public function testQueryParams() {
$uri = new Uri('http://localhost/test.php?params[]=1&params[]=2');
$uri = new Uri('http://localhost/test.php?params=1&params=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&params[1][1]=2');
$expected = array('params' => array(1 => array(1, 2)));
$actual = $uri->getAllQueryParameters();
$this->assertEquals($expected, $actual);
}
}

0 comments on commit cc665ac

Please sign in to comment.