Skip to content

Commit 1300aea

Browse files
committed
fix: supprot protocol-relative links
1 parent 68e8d43 commit 1300aea

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
@@ -148,7 +148,8 @@ class URI
148148
/**
149149
* Builds a representation of the string from the component parts.
150150
*
151-
* @param string|null $scheme URI scheme. E.g., http, ftp
151+
* @param string|null $scheme URI scheme. E.g., http, ftp. Empty string creates
152+
* protocol-relative URI.
152153
*
153154
* @return string URI string with only passed parts. Maybe incomplete as a URI.
154155
*/
@@ -160,9 +161,14 @@ public static function createURIString(
160161
?string $fragment = null
161162
): string {
162163
$uri = '';
164+
163165
if (! empty($scheme)) {
164166
$uri .= $scheme . '://';
165167
}
168+
// For protocol-relative links
169+
elseif ($scheme === '') {
170+
$uri .= '//';
171+
}
166172

167173
if (! empty($authority)) {
168174
$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)