Skip to content

Commit

Permalink
fix: host in baseURL may be wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Feb 18, 2023
1 parent 598f72e commit 032e3bb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
32 changes: 24 additions & 8 deletions system/HTTP/SiteURI.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@
class SiteURI extends URI
{
/**
* The baseURL.
* The current baseURL.
*/
private string $baseURL;

/**
* The path part of baseURL.
*
* The baseURL "http://example.com/" → '/'
* The baseURL "http://localhost:8888/ci431/public/" → '/ci431/public/'
*/
private string $basePathWithoutIndexPage;

/**
* The Index File.
*/
Expand Down Expand Up @@ -84,10 +92,10 @@ public function __construct(
?string $host = null,
?string $scheme = null
) {
$this->baseURL = $this->normalizeBaseURL($configApp);
$baseURL = $this->normalizeBaseURL($configApp);
$this->indexPage = $configApp->indexPage;

$this->setBaseSegments();
$this->setBasePath($baseURL);

// Check for an index page
$indexPage = '';
Expand All @@ -102,7 +110,7 @@ public function __construct(

$relativePath = URI::removeDotSegments($relativePath);

$tempUri = $this->baseURL . $indexPage . $relativePath;
$tempUri = $baseURL . $indexPage . $relativePath;
$uri = new URI($tempUri);

// Update scheme
Expand All @@ -128,6 +136,13 @@ public function __construct(
$parts = explode('#', $parts[0]);
$routePath = $parts[0];
$this->setRoutePath($routePath);

// Set baseURL
$this->baseURL = URI::createURIString(
$this->getScheme(),
$this->getAuthority(),
$this->basePathWithoutIndexPage,
);
}

private function checkHost(string $host, array $allowedHostnames): bool
Expand All @@ -152,12 +167,13 @@ private function normalizeBaseURL(App $configApp): string
}

/**
* Sets baseSegments.
* Sets basePathWithoutIndexPage and baseSegments.
*/
private function setBaseSegments(): void
private function setBasePath(string $baseURL): void
{
$basePath = (new URI($this->baseURL))->getPath();
$this->baseSegments = $this->convertToSegments($basePath);
$this->basePathWithoutIndexPage = (new URI($baseURL))->getPath();

$this->baseSegments = $this->convertToSegments($this->basePathWithoutIndexPage);

if ($this->indexPage) {
$this->baseSegments[] = $this->indexPage;
Expand Down
6 changes: 6 additions & 0 deletions tests/system/HTTP/SiteURITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function testConstructor()
$this->assertInstanceOf(SiteURI::class, $uri);
$this->assertSame('http://example.com/index.php/', (string) $uri);
$this->assertSame('/index.php/', $uri->getPath());
$this->assertSame('http://example.com/', $uri->getBaseURL());
}

public function testConstructorRelativePath()
Expand Down Expand Up @@ -92,6 +93,7 @@ public function testConstructorHost()
$this->assertInstanceOf(SiteURI::class, $uri);
$this->assertSame('http://sub.example.com/index.php/', (string) $uri);
$this->assertSame('/index.php/', $uri->getPath());
$this->assertSame('http://sub.example.com/', $uri->getBaseURL());
}

public function testConstructorScheme()
Expand All @@ -102,6 +104,7 @@ public function testConstructorScheme()

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

public function testConstructorSubfolder()
Expand All @@ -114,6 +117,7 @@ public function testConstructorSubfolder()
$this->assertInstanceOf(SiteURI::class, $uri);
$this->assertSame('http://example.com/ci4/index.php/', (string) $uri);
$this->assertSame('/ci4/index.php/', $uri->getPath());
$this->assertSame('http://example.com/ci4/', $uri->getBaseURL());
}

public function testConstructorSubfolderRelativePathWithQuery()
Expand All @@ -136,6 +140,7 @@ public function testConstructorForceGlobalSecureRequests()
$uri = new SiteURI($config);

$this->assertSame('https://example.com/index.php/', (string) $uri);
$this->assertSame('https://example.com/', $uri->getBaseURL());
}

public function testConstructorIndexPageEmpty()
Expand All @@ -146,6 +151,7 @@ public function testConstructorIndexPageEmpty()
$uri = new SiteURI($config);

$this->assertSame('http://example.com/', (string) $uri);
$this->assertSame('http://example.com/', $uri->getBaseURL());
}

public function testConstructorInvalidBaseURL()
Expand Down

0 comments on commit 032e3bb

Please sign in to comment.