From dfd51cb0379936a5cc90b980ddf46b170f3b365b Mon Sep 17 00:00:00 2001 From: vlakoff Date: Mon, 31 Mar 2014 20:41:07 +0200 Subject: Refactoring in Config->__construct() Put the $_SERVER['HTTP_HOST'] fallback in a more logical place. --- system/core/Config.php | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/system/core/Config.php b/system/core/Config.php index f630d1709..56259bd06 100644 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -76,16 +76,9 @@ class CI_Config { // Set the base_url automatically if none was provided if (empty($this->config['base_url'])) { - if (isset($_SERVER['HTTP_HOST'])) - { - $base_url = is_https() ? 'https' : 'http'; - $base_url .= '://'.$_SERVER['HTTP_HOST'] - .str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); - } - else - { - $base_url = 'http://localhost/'; - } + $base_url = (is_https() ? 'https' : 'http') . '://' + . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost') + . str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); $this->set_item('base_url', $base_url); } -- cgit v1.2.3-24-g4f1b From 2516f3b3945106aeebda87bd6a35454b7d72bdba Mon Sep 17 00:00:00 2001 From: vlakoff Date: Mon, 31 Mar 2014 20:46:04 +0200 Subject: Robuster determination of "base_url" config item Remove the basename only at the end, to avoid edge cases. --- system/core/Config.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/system/core/Config.php b/system/core/Config.php index 56259bd06..728e28dd8 100644 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -76,9 +76,11 @@ class CI_Config { // Set the base_url automatically if none was provided if (empty($this->config['base_url'])) { + $script_basename = basename($_SERVER['SCRIPT_NAME']); + $base_url = (is_https() ? 'https' : 'http') . '://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost') - . str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); + . preg_replace('/'.preg_quote($script_basename).'$/', '', $_SERVER['SCRIPT_NAME']); $this->set_item('base_url', $base_url); } -- cgit v1.2.3-24-g4f1b From 93836ff351e3035bf368ed56da09ac27e354989f Mon Sep 17 00:00:00 2001 From: vlakoff Date: Mon, 31 Mar 2014 20:46:14 +0200 Subject: Different method for determining "base_url" Better performance by not using regex. --- system/core/Config.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/system/core/Config.php b/system/core/Config.php index 728e28dd8..ec852b133 100644 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -76,11 +76,16 @@ class CI_Config { // Set the base_url automatically if none was provided if (empty($this->config['base_url'])) { - $script_basename = basename($_SERVER['SCRIPT_NAME']); + $script_dir = strtr(dirname($_SERVER['SCRIPT_NAME']), '\\', '/'); + + if ($script_dir !== '/') + { + $script_dir .= '/'; + } $base_url = (is_https() ? 'https' : 'http') . '://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost') - . preg_replace('/'.preg_quote($script_basename).'$/', '', $_SERVER['SCRIPT_NAME']); + . $script_dir; $this->set_item('base_url', $base_url); } -- cgit v1.2.3-24-g4f1b From e803b5edb173cf91a4afd5f2ee6330e60426ffa8 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Mon, 31 Mar 2014 22:11:30 +0200 Subject: Remove test that has become unfit --- tests/codeigniter/core/Config_test.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/codeigniter/core/Config_test.php b/tests/codeigniter/core/Config_test.php index 6a0a7a35f..9c39df71d 100644 --- a/tests/codeigniter/core/Config_test.php +++ b/tests/codeigniter/core/Config_test.php @@ -67,13 +67,6 @@ class Config_test extends CI_TestCase { // Clear base_url $this->ci_set_config('base_url', ''); - // Rerun constructor - $cls =& $this->ci_core_class('cfg'); - $this->config = new $cls; - - // Test default base - $this->assertEquals('http://localhost/', $this->config->base_url()); - // Capture server vars $old_host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : NULL; $old_script = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : NULL; @@ -87,6 +80,7 @@ class Config_test extends CI_TestCase { $_SERVER['SCRIPT_NAME'] = $path.$script; // Rerun constructor + $cls =& $this->ci_core_class('cfg'); $this->config = new $cls; // Test plain detected -- cgit v1.2.3-24-g4f1b From 8bec33f37bd0e4542d5f963a446283c10a2b2ffe Mon Sep 17 00:00:00 2001 From: vlakoff Date: Tue, 1 Apr 2014 01:03:48 +0200 Subject: Remove spaces around concatenations per request --- system/core/Config.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/system/core/Config.php b/system/core/Config.php index ec852b133..b2689a6bb 100644 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -83,9 +83,9 @@ class CI_Config { $script_dir .= '/'; } - $base_url = (is_https() ? 'https' : 'http') . '://' - . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost') - . $script_dir; + $base_url = (is_https() ? 'https' : 'http').'://' + .(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost') + .$script_dir; $this->set_item('base_url', $base_url); } -- cgit v1.2.3-24-g4f1b From b208f6502917b42a07b4bb7e5ffe2ef4b1359267 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Tue, 1 Apr 2014 01:21:51 +0200 Subject: Yet another method for determining "base_url" This one is great because we don't have to deal with the special cases: * in Windows, dirname('/foo/index.php') gives "/foo", but dirname('/index.php') gives "\" instead of "/" * dirname() doesn't include the trailing slash, with the expection of "/" (root) props @narfbg --- system/core/Config.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/system/core/Config.php b/system/core/Config.php index b2689a6bb..41367d353 100644 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -76,16 +76,9 @@ class CI_Config { // Set the base_url automatically if none was provided if (empty($this->config['base_url'])) { - $script_dir = strtr(dirname($_SERVER['SCRIPT_NAME']), '\\', '/'); - - if ($script_dir !== '/') - { - $script_dir .= '/'; - } - $base_url = (is_https() ? 'https' : 'http').'://' .(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost') - .$script_dir; + .substr($_SERVER['SCRIPT_NAME'], 0, -strlen(basename($_SERVER['SCRIPT_NAME']))); $this->set_item('base_url', $base_url); } -- cgit v1.2.3-24-g4f1b From 3802d7006726451362c9ebd31302d8d6992dd4d5 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Tue, 1 Apr 2014 01:32:38 +0200 Subject: Add test for "base_url" determination on server root --- tests/codeigniter/core/Config_test.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/codeigniter/core/Config_test.php b/tests/codeigniter/core/Config_test.php index 9c39df71d..81401e3ce 100644 --- a/tests/codeigniter/core/Config_test.php +++ b/tests/codeigniter/core/Config_test.php @@ -74,7 +74,7 @@ class Config_test extends CI_TestCase { // Setup server vars for detection $host = 'test.com'; - $path = '/path/'; + $path = '/'; $script = 'base_test.php'; $_SERVER['HTTP_HOST'] = $host; $_SERVER['SCRIPT_NAME'] = $path.$script; @@ -83,7 +83,15 @@ class Config_test extends CI_TestCase { $cls =& $this->ci_core_class('cfg'); $this->config = new $cls; - // Test plain detected + // Test plain detected (root) + $this->assertEquals('http://'.$host.$path, $this->config->base_url()); + + // Rerun constructor + $path = '/path/'; + $_SERVER['SCRIPT_NAME'] = $path.$script; + $this->config = new $cls; + + // Test plain detected (subfolder) $this->assertEquals('http://'.$host.$path, $this->config->base_url()); // Rerun constructor -- cgit v1.2.3-24-g4f1b From 7ead65b5dc48c477dc3dfe3b969b1e14bb9a1b79 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Tue, 1 Apr 2014 02:09:25 +0200 Subject: Discard the unwanted changes --- system/core/Config.php | 13 ++++++++++--- tests/codeigniter/core/Config_test.php | 8 +++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/system/core/Config.php b/system/core/Config.php index 41367d353..ad0e5f981 100644 --- a/system/core/Config.php +++ b/system/core/Config.php @@ -76,9 +76,16 @@ class CI_Config { // Set the base_url automatically if none was provided if (empty($this->config['base_url'])) { - $base_url = (is_https() ? 'https' : 'http').'://' - .(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost') - .substr($_SERVER['SCRIPT_NAME'], 0, -strlen(basename($_SERVER['SCRIPT_NAME']))); + if (isset($_SERVER['HTTP_HOST'])) + { + $base_url = (is_https() ? 'https' : 'http') + .'://'.$_SERVER['HTTP_HOST'] + .substr($_SERVER['SCRIPT_NAME'], 0, -strlen(basename($_SERVER['SCRIPT_NAME']))); + } + else + { + $base_url = 'http://localhost/'; + } $this->set_item('base_url', $base_url); } diff --git a/tests/codeigniter/core/Config_test.php b/tests/codeigniter/core/Config_test.php index 81401e3ce..2c28a639c 100644 --- a/tests/codeigniter/core/Config_test.php +++ b/tests/codeigniter/core/Config_test.php @@ -67,6 +67,13 @@ class Config_test extends CI_TestCase { // Clear base_url $this->ci_set_config('base_url', ''); + // Rerun constructor + $cls =& $this->ci_core_class('cfg'); + $this->config = new $cls; + + // Test default base + $this->assertEquals('http://localhost/', $this->config->base_url()); + // Capture server vars $old_host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : NULL; $old_script = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : NULL; @@ -80,7 +87,6 @@ class Config_test extends CI_TestCase { $_SERVER['SCRIPT_NAME'] = $path.$script; // Rerun constructor - $cls =& $this->ci_core_class('cfg'); $this->config = new $cls; // Test plain detected (root) -- cgit v1.2.3-24-g4f1b