Skip to content

Commit fc6c3a6

Browse files
committed
fix: support protocol-relative links
1 parent 078cf5d commit fc6c3a6

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

system/HTTP/SiteURI.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private function determineBaseURL(
145145
$uri = new URI($baseURL);
146146

147147
// Update scheme
148-
if ($scheme !== null) {
148+
if ($scheme !== null && $scheme !== '') {
149149
$uri->setScheme($scheme);
150150
} elseif ($configApp->forceGlobalSecureRequests) {
151151
$uri->setScheme('https');
@@ -381,13 +381,12 @@ public function baseUrl($relativePath = '', ?string $scheme = null): string
381381

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

384-
return URI::createURIString(
385-
$uri->getScheme(),
386-
$uri->getAuthority(),
387-
$uri->getPath(),
388-
$uri->getQuery(),
389-
$uri->getFragment()
390-
);
384+
// Support protocol-relative links
385+
if ($scheme === '') {
386+
return substr((string) $uri, strlen($uri->getScheme()) + 1);
387+
}
388+
389+
return (string) $uri;
391390
}
392391

393392
/**
@@ -420,12 +419,11 @@ public function siteUrl($relativePath = '', ?string $scheme = null, ?App $config
420419

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

423-
return URI::createURIString(
424-
$uri->getScheme(),
425-
$uri->getAuthority(),
426-
$uri->getPath(),
427-
$uri->getQuery(),
428-
$uri->getFragment()
429-
);
422+
// Support protocol-relative links
423+
if ($scheme === '') {
424+
return substr((string) $uri, strlen($uri->getScheme()) + 1);
425+
}
426+
427+
return (string) $uri;
430428
}
431429
}

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
}

tests/system/HTTP/SiteURITest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,17 @@ public function testConstructorScheme()
293293
$this->assertSame('https://example.com/', $uri->getBaseURL());
294294
}
295295

296+
public function testConstructorEmptyScheme()
297+
{
298+
$config = new App();
299+
300+
$uri = new SiteURI($config, '', null, '');
301+
302+
$this->assertInstanceOf(SiteURI::class, $uri);
303+
$this->assertSame('http://example.com/index.php', (string) $uri);
304+
$this->assertSame('http://example.com/', $uri->getBaseURL());
305+
}
306+
296307
public function testConstructorForceGlobalSecureRequests()
297308
{
298309
$config = new App();

0 commit comments

Comments
 (0)