Skip to content

Commit

Permalink
feat: add param $host to constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Feb 16, 2023
1 parent 10eae04 commit 7a01845
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
16 changes: 15 additions & 1 deletion system/HTTP/SiteURI.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ class SiteURI extends URI
/**
* @param string $relativePath URI path relative to baseURL. May include
* queries or fragments.
* @param string $host Hostname. If it is not in $allowedHostnames,
* just be ignored.
*/
public function __construct(App $configApp, string $relativePath = '')
public function __construct(App $configApp, string $relativePath = '', string $host = '')
{
$this->baseURL = $this->normalizeBaseURL($configApp);
$this->indexPage = $configApp->indexPage;
Expand All @@ -97,21 +99,33 @@ public function __construct(App $configApp, string $relativePath = '')
$tempUri = $this->baseURL . $indexPage . $relativePath;
$uri = new URI($tempUri);

// Update scheme
if ($configApp->forceGlobalSecureRequests) {
$uri->setScheme('https');
}

// Update host
if ($host !== '' && $this->checkHost($host, $configApp->allowedHostnames)) {
$uri->setHost($host);
}

$parts = parse_url((string) $uri);
if ($parts === false) {
throw HTTPException::forUnableToParseURI($uri);
}
$this->applyParts($parts);

// Set routePath
$parts = explode('?', $relativePath);
$routePath = $parts[0];
$this->setRoutePath($routePath);
}

private function checkHost(string $host, array $allowedHostnames): bool
{
return (bool) (in_array($host, $allowedHostnames, true));
}

private function normalizeBaseURL(App $configApp): string
{
// It's possible the user forgot a trailing slash on their
Expand Down
12 changes: 12 additions & 0 deletions tests/system/HTTP/SiteURITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ public function testConstructorRelativePathWithQuery()
$this->assertSame('foo=1&bar=2', $uri->getQuery());
}

public function testConstructorHost()
{
$config = new App();
$config->allowedHostnames = ['sub.example.com'];

$uri = new SiteURI($config, '', 'sub.example.com');

$this->assertInstanceOf(SiteURI::class, $uri);
$this->assertSame('http://sub.example.com/index.php/', (string) $uri);
$this->assertSame('/index.php/', $uri->getPath());
}

public function testConstructorSubfolder()
{
$config = new App();
Expand Down

0 comments on commit 7a01845

Please sign in to comment.