Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Router/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,21 @@ public static function buildUrl($url, $replace = [], $flags = HTTP_URL_REPLACE,
| HTTP_URL_STRIP_PASS;
}

// Decode query parameters before parsing the URL
$decodeQueryParams = function (string $url): string {
if (Str::contains($url, '?')) {
list($urlWithoutQuery, $queryArgs) = explode('?', $url, 2);
$url = $urlWithoutQuery . '?' . urldecode($queryArgs);
}
return $url;
};

// Parse input
if (is_string($url)) {
$url = parse_url(urldecode($url));
$url = parse_url($decodeQueryParams($url));
}
if (is_string($replace)) {
$replace = parse_url(urldecode($replace));
$replace = parse_url($decodeQueryParams($replace));
}

// Prepare input data
Expand Down
28 changes: 28 additions & 0 deletions tests/Router/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,4 +537,32 @@ public function testQueryArgsArrayMatchLaravel()
);
}
}

public function testEncodedUrlInPathMatchLaravel()
{
$urlInPath = 'https://testUrlInPath/path?k1=v1&k2';
$url = 'https://testdomain/' . rawurlencode($urlInPath);

$generator = new \Winter\Storm\Router\UrlGenerator(new RouteCollection, Request::create($url));
$baseGenerator = new \Illuminate\Routing\UrlGenerator(new RouteCollection, Request::create($url));

$this->assertEquals(
urldecode($baseGenerator->to($url)),
urldecode($generator->to($url))
);
}

public function testDoublyEncodedUrlInPathMatchLaravel()
{
$urlInPath = 'https://testUrlInPath/path?k1=v1&k2';
$url = 'https://testdomain/' . rawurlencode(rawurlencode($urlInPath));

$generator = new \Winter\Storm\Router\UrlGenerator(new RouteCollection, Request::create($url));
$baseGenerator = new \Illuminate\Routing\UrlGenerator(new RouteCollection, Request::create($url));

$this->assertEquals(
urldecode($baseGenerator->to($url)),
urldecode($generator->to($url))
);
}
}