Skip to content

Commit

Permalink
Work around parse_url() bug (bis)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Nov 13, 2024
1 parent 168b77c commit 3280c9d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
11 changes: 4 additions & 7 deletions Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,7 @@ public static function create(string $uri, string $method = 'GET', array $parame
$server['PATH_INFO'] = '';
$server['REQUEST_METHOD'] = strtoupper($method);

if (false === ($components = parse_url($uri)) && '/' === ($uri[0] ?? '')) {
$components = parse_url($uri.'#');
unset($components['fragment']);
}

if (false === $components) {
if (false === $components = parse_url(\strlen($uri) !== strcspn($uri, '?#') ? $uri : $uri.'#')) {
throw new BadRequestException('Invalid URI.');
}

Expand All @@ -386,9 +381,11 @@ public static function create(string $uri, string $method = 'GET', array $parame
if ('https' === $components['scheme']) {
$server['HTTPS'] = 'on';
$server['SERVER_PORT'] = 443;
} else {
} elseif ('http' === $components['scheme']) {
unset($server['HTTPS']);
$server['SERVER_PORT'] = 80;
} else {
throw new BadRequestException('Invalid URI: http(s) scheme expected.');
}
}

Expand Down
3 changes: 2 additions & 1 deletion Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ public function testCreateWithRequestUri()
* ["foo\u0000"]
* [" foo"]
* ["foo "]
* [":"]
* ["//"]
* ["foo:bar"]
*/
public function testCreateWithBadRequestUri(string $uri)
{
Expand Down

0 comments on commit 3280c9d

Please sign in to comment.