diff --git a/system/HTTP/URI.php b/system/HTTP/URI.php index 4d7b6c388250..df57a86fcda2 100644 --- a/system/HTTP/URI.php +++ b/system/HTTP/URI.php @@ -596,7 +596,8 @@ public function __toString(): string // If hosted in a sub-folder, we will have additional // segments that show up prior to the URI path we just // grabbed from the request, so add it on if necessary. - $baseUri = new self(config(\Config\App::class)->baseURL); + $config = config(\Config\App::class); + $baseUri = new self($config->baseURL); $basePath = trim($baseUri->getPath(), '/') . '/'; $path = $this->getPath(); $trimPath = ltrim($path, '/'); @@ -606,6 +607,12 @@ public function __toString(): string $path = $basePath . $trimPath; } + // force https if needed + if ($config->forceGlobalSecureRequests) + { + $this->setScheme('https'); + } + return static::createURIString( $this->getScheme(), $this->getAuthority(), $path, // Absolute URIs should use a "/" for an empty path $this->getQuery(), $this->getFragment() diff --git a/tests/system/HTTP/URITest.php b/tests/system/HTTP/URITest.php index d492ab66a6d8..b96bf8c01fa4 100644 --- a/tests/system/HTTP/URITest.php +++ b/tests/system/HTTP/URITest.php @@ -1,6 +1,7 @@ assertEquals($uri->getPath(), $request->uri->getPath()); } + public function testForceGlobalSecureRequests() + { + Services::reset(); + + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/ci/v4/controller/method'; + + $config = new App(); + $config->baseURL = 'http://example.com/ci/v4'; + $config->indexPage = 'index.php'; + $config->forceGlobalSecureRequests = true; + + Config::injectMock('App', $config); + + $request = Services::request($config); + $request->uri = new URI('http://example.com/ci/v4/controller/method'); + + Services::injectMock('request', $request); + + // going through request + $this->assertEquals('https://example.com/ci/v4/controller/method', (string) $request->uri); + + // standalone + $uri = new URI('http://example.com/ci/v4/controller/method'); + $this->assertEquals('https://example.com/ci/v4/controller/method', (string) $uri); + + $this->assertEquals($uri->getPath(), $request->uri->getPath()); + } + public function testZeroAsURIPath() { $url = 'http://example.com/0';