Skip to content

Commit 9bd18f8

Browse files
committed
fix: host in baseURL may be wrong
1 parent 562c683 commit 9bd18f8

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

system/HTTP/SiteURI.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,18 @@
2222
class SiteURI extends URI
2323
{
2424
/**
25-
* The baseURL.
25+
* The current baseURL.
2626
*/
2727
private string $baseURL;
2828

29+
/**
30+
* The path part of baseURL.
31+
*
32+
* The baseURL "http://example.com/" → '/'
33+
* The baseURL "http://localhost:8888/ci431/public/" → '/ci431/public/'
34+
*/
35+
private string $basePathWithoutIndexPage;
36+
2937
/**
3038
* The Index File.
3139
*/
@@ -84,10 +92,10 @@ public function __construct(
8492
?string $host = null,
8593
?string $scheme = null
8694
) {
87-
$this->baseURL = $this->normalizeBaseURL($configApp);
95+
$baseURL = $this->normalizeBaseURL($configApp);
8896
$this->indexPage = $configApp->indexPage;
8997

90-
$this->setBaseSegments();
98+
$this->setBasePath($baseURL);
9199

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

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

105-
$tempUri = $this->baseURL . $indexPage . $relativePath;
113+
$tempUri = $baseURL . $indexPage . $relativePath;
106114
$uri = new URI($tempUri);
107115

108116
// Update scheme
@@ -128,6 +136,13 @@ public function __construct(
128136
$parts = explode('#', $parts[0]);
129137
$routePath = $parts[0];
130138
$this->setRoutePath($routePath);
139+
140+
// Set baseURL
141+
$this->baseURL = URI::createURIString(
142+
$this->getScheme(),
143+
$this->getAuthority(),
144+
$this->basePathWithoutIndexPage,
145+
);
131146
}
132147

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

154169
/**
155-
* Sets baseSegments.
170+
* Sets basePathWithoutIndexPage and baseSegments.
156171
*/
157-
private function setBaseSegments(): void
172+
private function setBasePath(string $baseURL): void
158173
{
159-
$basePath = (new URI($this->baseURL))->getPath();
160-
$this->baseSegments = $this->convertToSegments($basePath);
174+
$this->basePathWithoutIndexPage = (new URI($baseURL))->getPath();
175+
176+
$this->baseSegments = $this->convertToSegments($this->basePathWithoutIndexPage);
161177

162178
if ($this->indexPage) {
163179
$this->baseSegments[] = $this->indexPage;

tests/system/HTTP/SiteURITest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function testConstructor()
3535
$this->assertInstanceOf(SiteURI::class, $uri);
3636
$this->assertSame('http://example.com/index.php/', (string) $uri);
3737
$this->assertSame('/index.php/', $uri->getPath());
38+
$this->assertSame('http://example.com/', $uri->getBaseURL());
3839
}
3940

4041
public function testConstructorRelativePath()
@@ -92,6 +93,7 @@ public function testConstructorHost()
9293
$this->assertInstanceOf(SiteURI::class, $uri);
9394
$this->assertSame('http://sub.example.com/index.php/', (string) $uri);
9495
$this->assertSame('/index.php/', $uri->getPath());
96+
$this->assertSame('http://sub.example.com/', $uri->getBaseURL());
9597
}
9698

9799
public function testConstructorScheme()
@@ -102,6 +104,7 @@ public function testConstructorScheme()
102104

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

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

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

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

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

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

151157
public function testConstructorInvalidBaseURL()

0 commit comments

Comments
 (0)