Skip to content

Commit

Permalink
Make current_url() function respect forceGlobalSecureRequests config …
Browse files Browse the repository at this point in the history
…setting
  • Loading branch information
michalsn committed Sep 16, 2020
1 parent 7a1623f commit 77e2e82
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
9 changes: 8 additions & 1 deletion system/HTTP/URI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/');
Expand All @@ -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()
Expand Down
31 changes: 31 additions & 0 deletions tests/system/HTTP/URITest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace CodeIgniter\HTTP;

use CodeIgniter\Config\Config;
use CodeIgniter\Config\Services;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use Config\App;
Expand All @@ -17,6 +18,7 @@ protected function setUp(): void

public function tearDown(): void
{
Config::reset();
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -962,6 +964,35 @@ public function testBasedWithIndex()
$this->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';
Expand Down

0 comments on commit 77e2e82

Please sign in to comment.