Skip to content

Commit

Permalink
Fix new Uri() for relative URLs with param colon
Browse files Browse the repository at this point in the history
Fixes #6331
  • Loading branch information
distantnative committed Jul 22, 2024
1 parent 744a004 commit cce19f2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
8 changes: 1 addition & 7 deletions src/Http/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,13 +602,7 @@ protected function detectPortInHost(string|null $host = null): array
*/
protected function detectRequestUri(string|null $requestUri = null): Uri
{
// make sure the URL parser works properly when there's a
// colon in the request URI but the URI is relative
if (Url::isAbsolute($requestUri) === false) {
$requestUri = 'https://getkirby.com' . $requestUri;
}

$uri = new Uri($requestUri);
$uri = new Uri($requestUri ?? '');

// create the URI object as a combination of base uri parts
// and the parts from REQUEST_URI
Expand Down
11 changes: 10 additions & 1 deletion src/Http/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,16 @@ class Uri
public function __construct(array|string $props = [], array $inject = [])
{
if (is_string($props) === true) {
$props = parse_url($props);
// make sure the URL parser works properly when there's a
// colon in the string but the string is a relative URL
if (Url::isAbsolute($props) === false) {
$props = 'https://getkirby.com/' . $props;
$props = parse_url($props);
unset($props['scheme'], $props['host']);
} else {
$props = parse_url($props);
}

$props['username'] = $props['user'] ?? null;
$props['password'] = $props['pass'] ?? null;

Expand Down
9 changes: 9 additions & 0 deletions tests/Http/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,15 @@ public static function buildProvider(): array
'/search/page:2?q=something'
],

// relative path with colon + adding query
[
'/search/page:2',
[
'query' => ['q' => 'something']
],
'/search/page:2?q=something'
],

// path + adding params + query
[
'https://getkirby.com/search',
Expand Down

0 comments on commit cce19f2

Please sign in to comment.