Skip to content

Commit

Permalink
Fix URI RFC3986 parsing and invalid characters
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 2, 2025
1 parent b77529a commit 9efb38c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
7 changes: 6 additions & 1 deletion BaseUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,12 @@ final protected static function formatPathWithEmptyBaseQuery(string $path): stri
#[Deprecated(message:'no longer used by the isSameDocument method', since:'league/uri-interfaces:7.6.0')]
final protected function normalize(Psr7UriInterface|UriInterface $uri): string
{
return UriString::normalize($uri->withScheme($uri instanceof Psr7UriInterface ? '' : null));
$newUri = $uri->withScheme($uri instanceof Psr7UriInterface ? '' : null);
if ('' === $newUri->__toString()) {
return '';
}

return UriString::normalize($newUri);
}


Expand Down
2 changes: 1 addition & 1 deletion Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private function normalizePsr7Uri(UriInterface $uri): UriInterface
*/
public static function new(Stringable|string $uri = ''): self
{
return self::fromComponents(UriString::parse($uri));
return new self(Uri::new($uri));
}

/**
Expand Down
4 changes: 0 additions & 4 deletions HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ public static function validUrlProvider(): array
'http://login:[email protected]/',
'http://login:[email protected]/',
],
'empty string' => [
'',
'',
],
];
}

Expand Down
23 changes: 12 additions & 11 deletions Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,11 @@ public static function tryNew(Stringable|string|null $uri = ''): ?self
*/
public static function new(Stringable|string $uri = ''): self
{
$uri = (string) $uri;
if ('' === $uri) {
return new self(null, null, null, null, null, '', null, null);
}

$components = UriString::parse($uri);

return new self(
Expand Down Expand Up @@ -473,16 +478,7 @@ public static function fromBaseUri(
Stringable|string $uri,
Stringable|string|null $baseUri = null
): self {
$uri = self::new($uri);
$baseUri = self::tryNew($baseUri) ?? $uri;

/** @var self $uri */
$uri = match (true) {
$baseUri->isAbsolute() => $baseUri->resolve($uri),
default => throw new SyntaxError('the URI `'.$baseUri.'` must be absolute.'),
};

return $uri;
return self::new(UriString::resolve($uri, $baseUri));
}

/**
Expand Down Expand Up @@ -1670,7 +1666,12 @@ public function equals(UriInterface|Stringable|string $uri, bool $excludeFragmen
*/
public function normalize(): UriInterface
{
return self::new(UriString::normalize($this->toString()));
$uriString = $this->toString();
if ('' === $uriString) {
return $this;
}

return self::new(UriString::normalize($uriString));
}

/**
Expand Down

0 comments on commit 9efb38c

Please sign in to comment.