Skip to content

Commit

Permalink
fix: supprot protocol-relative links
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Mar 27, 2023
1 parent f7cda3a commit c1f5b60
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
14 changes: 12 additions & 2 deletions system/HTTP/SiteURI.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,13 @@ public function baseUrl($relativePath = '', ?string $scheme = null): string

$uri = new self($config, $relativePath, $host, $scheme);

// For protocol-relative links
if ($scheme !== '') {
$scheme = $uri->getScheme();
}

return URI::createURIString(
$uri->getScheme(),
$scheme,
$uri->getAuthority(),
$uri->getPath(),
$uri->getQuery(),
Expand Down Expand Up @@ -420,8 +425,13 @@ public function siteUrl($relativePath = '', ?string $scheme = null, ?App $config

$uri = new self($config, $relativePath, $host, $scheme);

// For protocol-relative links
if ($scheme !== '') {
$scheme = $uri->getScheme();
}

return URI::createURIString(
$uri->getScheme(),
$scheme,
$uri->getAuthority(),
$uri->getPath(),
$uri->getQuery(),
Expand Down
8 changes: 7 additions & 1 deletion system/HTTP/URI.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ class URI
/**
* Builds a representation of the string from the component parts.
*
* @param string|null $scheme URI scheme. E.g., http, ftp
* @param string|null $scheme URI scheme. E.g., http, ftp. Empty string creates
* protocol-relative URI.
*
* @return string URI string with only passed parts. Maybe incomplete as a URI.
*/
Expand All @@ -159,9 +160,14 @@ public static function createURIString(
?string $fragment = null
): string {
$uri = '';

if (! empty($scheme)) {
$uri .= $scheme . '://';
}
// For protocol-relative links
elseif ($scheme === '') {
$uri .= '//';
}

if (! empty($authority)) {
$uri .= $authority;
Expand Down
1 change: 0 additions & 1 deletion system/Helpers/url_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ function site_url($relativePath = '', ?string $scheme = null, ?App $config = nul

assert($currentURI instanceof SiteURI);

// @TODO supprot protocol-relative links
return $currentURI->siteUrl($relativePath, $scheme, $config);
}
}
Expand Down

0 comments on commit c1f5b60

Please sign in to comment.