Skip to content

Commit 7a01845

Browse files
committed
feat: add param $host to constructor
1 parent 10eae04 commit 7a01845

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

system/HTTP/SiteURI.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ class SiteURI extends URI
7373
/**
7474
* @param string $relativePath URI path relative to baseURL. May include
7575
* queries or fragments.
76+
* @param string $host Hostname. If it is not in $allowedHostnames,
77+
* just be ignored.
7678
*/
77-
public function __construct(App $configApp, string $relativePath = '')
79+
public function __construct(App $configApp, string $relativePath = '', string $host = '')
7880
{
7981
$this->baseURL = $this->normalizeBaseURL($configApp);
8082
$this->indexPage = $configApp->indexPage;
@@ -97,21 +99,33 @@ public function __construct(App $configApp, string $relativePath = '')
9799
$tempUri = $this->baseURL . $indexPage . $relativePath;
98100
$uri = new URI($tempUri);
99101

102+
// Update scheme
100103
if ($configApp->forceGlobalSecureRequests) {
101104
$uri->setScheme('https');
102105
}
103106

107+
// Update host
108+
if ($host !== '' && $this->checkHost($host, $configApp->allowedHostnames)) {
109+
$uri->setHost($host);
110+
}
111+
104112
$parts = parse_url((string) $uri);
105113
if ($parts === false) {
106114
throw HTTPException::forUnableToParseURI($uri);
107115
}
108116
$this->applyParts($parts);
109117

118+
// Set routePath
110119
$parts = explode('?', $relativePath);
111120
$routePath = $parts[0];
112121
$this->setRoutePath($routePath);
113122
}
114123

124+
private function checkHost(string $host, array $allowedHostnames): bool
125+
{
126+
return (bool) (in_array($host, $allowedHostnames, true));
127+
}
128+
115129
private function normalizeBaseURL(App $configApp): string
116130
{
117131
// It's possible the user forgot a trailing slash on their

tests/system/HTTP/SiteURITest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ public function testConstructorRelativePathWithQuery()
5858
$this->assertSame('foo=1&bar=2', $uri->getQuery());
5959
}
6060

61+
public function testConstructorHost()
62+
{
63+
$config = new App();
64+
$config->allowedHostnames = ['sub.example.com'];
65+
66+
$uri = new SiteURI($config, '', 'sub.example.com');
67+
68+
$this->assertInstanceOf(SiteURI::class, $uri);
69+
$this->assertSame('http://sub.example.com/index.php/', (string) $uri);
70+
$this->assertSame('/index.php/', $uri->getPath());
71+
}
72+
6173
public function testConstructorSubfolder()
6274
{
6375
$config = new App();

0 commit comments

Comments
 (0)