Skip to content

Commit ad50637

Browse files
committed
fix: supprot protocol-relative links
1 parent 38c16e8 commit ad50637

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

system/HTTP/SiteURI.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,13 @@ public function baseUrl($relativePath = '', ?string $scheme = null): string
381381

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

384+
// For protocol-relative links
385+
if ($scheme !== '') {
386+
$scheme = $uri->getScheme();
387+
}
388+
384389
return URI::createURIString(
385-
$uri->getScheme(),
390+
$scheme,
386391
$uri->getAuthority(),
387392
$uri->getPath(),
388393
$uri->getQuery(),
@@ -420,8 +425,13 @@ public function siteUrl($relativePath = '', ?string $scheme = null, ?App $config
420425

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

428+
// For protocol-relative links
429+
if ($scheme !== '') {
430+
$scheme = $uri->getScheme();
431+
}
432+
423433
return URI::createURIString(
424-
$uri->getScheme(),
434+
$scheme,
425435
$uri->getAuthority(),
426436
$uri->getPath(),
427437
$uri->getQuery(),

system/HTTP/URI.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ class URI
147147
/**
148148
* Builds a representation of the string from the component parts.
149149
*
150-
* @param string|null $scheme URI scheme. E.g., http, ftp
150+
* @param string|null $scheme URI scheme. E.g., http, ftp. Empty string creates
151+
* protocol-relative URI.
151152
*
152153
* @return string URI string with only passed parts. Maybe incomplete as a URI.
153154
*/
@@ -159,9 +160,14 @@ public static function createURIString(
159160
?string $fragment = null
160161
): string {
161162
$uri = '';
163+
162164
if (! empty($scheme)) {
163165
$uri .= $scheme . '://';
164166
}
167+
// For protocol-relative links
168+
elseif ($scheme === '') {
169+
$uri .= '//';
170+
}
165171

166172
if (! empty($authority)) {
167173
$uri .= $authority;

system/Helpers/url_helper.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ function site_url($relativePath = '', ?string $scheme = null, ?App $config = nul
3333

3434
assert($currentURI instanceof SiteURI);
3535

36-
// @TODO supprot protocol-relative links
3736
return $currentURI->siteUrl($relativePath, $scheme, $config);
3837
}
3938
}

0 commit comments

Comments
 (0)