Skip to content

Commit 8a5932e

Browse files
committed
refactor: remove $_SERVER['HTTP_HOST']
1 parent d554e20 commit 8a5932e

2 files changed

Lines changed: 36 additions & 27 deletions

File tree

system/Router/RouteCollection.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,20 @@ class RouteCollection implements RouteCollectionInterface
218218
*/
219219
protected $prioritizeDetected = false;
220220

221+
/**
222+
* The current hostname from $_SERVER['HTTP_HOST']
223+
*/
224+
private ?string $httpHost = null;
225+
221226
/**
222227
* Constructor
223228
*/
224229
public function __construct(FileLocator $locator, Modules $moduleConfig)
225230
{
226231
$this->fileLocator = $locator;
227232
$this->moduleConfig = $moduleConfig;
233+
234+
$this->httpHost = Services::request()->getServer('HTTP_HOST');
228235
}
229236

230237
/**
@@ -1176,7 +1183,7 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
11761183
// Hostname limiting?
11771184
if (! empty($options['hostname'])) {
11781185
// @todo determine if there's a way to whitelist hosts?
1179-
if (isset($_SERVER['HTTP_HOST']) && strtolower($_SERVER['HTTP_HOST']) !== strtolower($options['hostname'])) {
1186+
if (isset($this->httpHost) && strtolower($this->httpHost) !== strtolower($options['hostname'])) {
11801187
return;
11811188
}
11821189

@@ -1302,7 +1309,7 @@ private function getMethodParams(string $from): string
13021309
private function checkSubdomains($subdomains): bool
13031310
{
13041311
// CLI calls can't be on subdomain.
1305-
if (! isset($_SERVER['HTTP_HOST'])) {
1312+
if (! isset($this->httpHost)) {
13061313
return false;
13071314
}
13081315

@@ -1337,7 +1344,7 @@ private function determineCurrentSubdomain()
13371344
// We have to ensure that a scheme exists
13381345
// on the URL else parse_url will mis-interpret
13391346
// 'host' as the 'path'.
1340-
$url = $_SERVER['HTTP_HOST'];
1347+
$url = $this->httpHost;
13411348
if (strpos($url, 'http') !== 0) {
13421349
$url = 'http://' . $url;
13431350
}

tests/system/Router/RouteCollectionTest.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818
use Tests\Support\Controllers\Hello;
1919

2020
/**
21-
* @backupGlobals enabled
22-
*
2321
* @internal
2422
*/
2523
final class RouteCollectionTest extends CIUnitTestCase
2624
{
25+
protected function setUp(): void
26+
{
27+
parent::setUp();
28+
29+
$this->resetServices();
30+
}
31+
2732
protected function getCollector(array $config = [], array $files = [], $moduleConfig = null)
2833
{
2934
$defaults = [
@@ -1062,49 +1067,46 @@ public function testAddRedirectGetMethod()
10621067
*/
10631068
public function testWithSubdomain()
10641069
{
1065-
$routes = $this->getCollector();
1066-
10671070
$_SERVER['HTTP_HOST'] = 'adm.example.com';
10681071

1072+
$routes = $this->getCollector();
1073+
10691074
$routes->add('/objects/(:alphanum)', 'Admin::objectsList/$1', ['subdomain' => 'adm']);
10701075
$routes->add('/objects/(:alphanum)', 'App::objectsList/$1');
10711076

10721077
$expects = [
10731078
'objects/([a-zA-Z0-9]+)' => '\Admin::objectsList/$1',
10741079
];
1075-
10761080
$this->assertSame($expects, $routes->getRoutes());
10771081
}
10781082

10791083
public function testWithSubdomainMissing()
10801084
{
1081-
$routes = $this->getCollector();
1085+
$_SERVER['HTTP_HOST'] = 'www.example.com';
10821086

1083-
// $_SERVER['HTTP_HOST'] = 'adm.example.com';
1087+
$routes = $this->getCollector();
10841088

10851089
$routes->add('/objects/(:alphanum)', 'Admin::objectsList/$1', ['subdomain' => 'adm']);
10861090
$routes->add('/objects/(:alphanum)', 'App::objectsList/$1');
10871091

10881092
$expects = [
10891093
'objects/([a-zA-Z0-9]+)' => '\App::objectsList/$1',
10901094
];
1091-
10921095
$this->assertSame($expects, $routes->getRoutes());
10931096
}
10941097

10951098
public function testWithDifferentSubdomain()
10961099
{
1097-
$routes = $this->getCollector();
1098-
10991100
$_SERVER['HTTP_HOST'] = 'adm.example.com';
11001101

1102+
$routes = $this->getCollector();
1103+
11011104
$routes->add('/objects/(:alphanum)', 'Admin::objectsList/$1', ['subdomain' => 'sales']);
11021105
$routes->add('/objects/(:alphanum)', 'App::objectsList/$1');
11031106

11041107
$expects = [
11051108
'objects/([a-zA-Z0-9]+)' => '\App::objectsList/$1',
11061109
];
1107-
11081110
$this->assertSame($expects, $routes->getRoutes());
11091111
}
11101112

@@ -1381,10 +1383,10 @@ public function testOffsetParameters()
13811383
*/
13821384
public function testRouteToWithSubdomainMatch()
13831385
{
1384-
$routes = $this->getCollector();
1385-
1386-
Services::request()->setMethod('get');
13871386
$_SERVER['HTTP_HOST'] = 'doc.example.com';
1387+
Services::request()->setMethod('get');
1388+
1389+
$routes = $this->getCollector();
13881390

13891391
$routes->get('i/(:any)', 'App\Controllers\Site\CDoc::item/$1', ['subdomain' => 'doc', 'as' => 'doc_item']);
13901392

@@ -1393,10 +1395,10 @@ public function testRouteToWithSubdomainMatch()
13931395

13941396
public function testRouteToWithSubdomainMismatch()
13951397
{
1396-
$routes = $this->getCollector();
1397-
1398-
Services::request()->setMethod('get');
13991398
$_SERVER['HTTP_HOST'] = 'dev.example.com';
1399+
Services::request()->setMethod('get');
1400+
1401+
$routes = $this->getCollector();
14001402

14011403
$routes->get('i/(:any)', 'App\Controllers\Site\CDoc::item/$1', ['subdomain' => 'doc', 'as' => 'doc_item']);
14021404

@@ -1453,10 +1455,10 @@ public function testRouteToWithGenericSubdomainNot()
14531455

14541456
public function testRouteToWithoutSubdomainMatch()
14551457
{
1456-
$routes = $this->getCollector();
1457-
1458-
Services::request()->setMethod('get');
14591458
$_SERVER['HTTP_HOST'] = 'doc.example.com';
1459+
Services::request()->setMethod('get');
1460+
1461+
$routes = $this->getCollector();
14601462

14611463
$routes->get('i/(:any)', 'App\Controllers\Site\CDoc::item/$1', ['hostname' => 'example.com', 'as' => 'doc_item']);
14621464

@@ -1477,10 +1479,10 @@ public function testRouteToWithoutSubdomainMismatch()
14771479

14781480
public function testRouteToWithoutSubdomainNot()
14791481
{
1480-
$routes = $this->getCollector();
1481-
1482-
Services::request()->setMethod('get');
14831482
$_SERVER['HTTP_HOST'] = 'example.com';
1483+
Services::request()->setMethod('get');
1484+
1485+
$routes = $this->getCollector();
14841486

14851487
$routes->get('i/(:any)', 'App\Controllers\Site\CDoc::item/$1', ['hostname' => 'example.com', 'as' => 'doc_item']);
14861488

@@ -1494,8 +1496,8 @@ public function testRouteToWithoutSubdomainNot()
14941496
*/
14951497
public function testRouteOverwritingDifferentSubdomains()
14961498
{
1497-
Services::request()->setMethod('get');
14981499
$_SERVER['HTTP_HOST'] = 'doc.domain.com';
1500+
Services::request()->setMethod('get');
14991501

15001502
$routes = $this->getCollector();
15011503
$router = new Router($routes, Services::request());

0 commit comments

Comments
 (0)