From b4311ca022a0abec9c02e4f19c5803a6fc7659d3 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Thu, 16 Jun 2011 05:52:16 +0000 Subject: [PATCH] Fixes for r90105, r90193: * Actually removed $wgProto. * Per Aryeh's suggestions on the future of $wgServer: made $wgServer detection in DefaultSettings.php more permanent by merging it with the new code from r90105. This means that bug 14977 is properly fixed now. * Require entry points to set up the autoloader before including DefaultSettings.php. Comments on bug 14977 indicate that at some point in the past, this may have broken something. Anything that breaks now should just be fixed, we need the autoloader. Tested the most common entry points. * Since the detection code has moved from Installer to WebRequest, I also moved the relevant test file and updated the test. The function under test is now public static, so r90154 is superseded. --- RELEASE-NOTES-1.18 | 2 + includes/DefaultSettings.php | 40 +++---------------- includes/WebRequest.php | 39 ++++++++++++++++++ includes/installer/Installer.php | 33 +-------------- .../InstallerTest.php => WebRequestTest.php} | 32 ++++----------- 5 files changed, 55 insertions(+), 91 deletions(-) rename tests/phpunit/includes/{installer/InstallerTest.php => WebRequestTest.php} (59%) diff --git a/RELEASE-NOTES-1.18 b/RELEASE-NOTES-1.18 index a6f20c730c..7b86b211c4 100644 --- a/RELEASE-NOTES-1.18 +++ b/RELEASE-NOTES-1.18 @@ -255,6 +255,8 @@ production. * (bug 29263) Add LTR class to the shared CSS to be used for left-to-right text such as SQL queries shown in dberrortext and similar messages in RTL environments +* (bug 14977) Fixed $wgServer detection in cases where an IPv6 address is used + as the server name. === API changes in 1.18 === * (bug 26339) Throw warning when truncating an overlarge API result. diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index d255994674..b41a7f55f1 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -26,10 +26,9 @@ if( !defined( 'MEDIAWIKI' ) ) { die( 1 ); } -# Create a site configuration object. Not used for much in a default install -if ( !defined( 'MW_COMPILED' ) ) { - require_once( "$IP/includes/SiteConfiguration.php" ); -} +# Create a site configuration object. Not used for much in a default install. +# Note: this (and other things) will break if the autoloader is not enabled. +# Please include includes/AutoLoader.php before including this file. $wgConf = new SiteConfiguration; /** @endcond */ @@ -51,36 +50,7 @@ $wgSitename = 'MediaWiki'; * wrong server, it will redirect incorrectly after you save a page. In that * case, set this variable to fix it. */ -$wgServer = ''; - -/** @cond file_level_code */ -if( isset( $_SERVER['SERVER_NAME'] ) - # KLUGE: lighttpd 1.4.28 truncates IPv6 addresses at the first colon, - # giving bogus hostnames like "[2001"; check for presence of both - # brackets to detect this. - && ($_SERVER['SERVER_NAME'][0] !== '[' || substr($_SERVER['SERVER_NAME'], -1) === ']') - ) { - $serverName = $_SERVER['SERVER_NAME']; -} elseif( isset( $_SERVER['HOSTNAME'] ) ) { - $serverName = $_SERVER['HOSTNAME']; -} elseif( isset( $_SERVER['SERVER_ADDR'] ) ) { - $serverName = $_SERVER['SERVER_ADDR']; -} else { - $serverName = 'localhost'; -} - -$wgProto = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http'; - -$wgServer = $wgProto.'://' . $serverName; -# If the port is a non-standard one, add it to the URL -if( isset( $_SERVER['SERVER_PORT'] ) - && !strpos( $serverName, ':' ) - && ( ( $wgProto == 'http' && $_SERVER['SERVER_PORT'] != 80 ) - || ( $wgProto == 'https' && $_SERVER['SERVER_PORT'] != 443 ) ) ) { - - $wgServer .= ":" . $_SERVER['SERVER_PORT']; -} -/** @endcond */ +$wgServer = WebRequest::detectServer(); /************************************************************************//** * @name Script path settings @@ -986,6 +956,8 @@ $wgDjvuOutputExtension = 'jpg'; * @{ */ +$serverName = substr( $wgServer, strrpos( $wgServer, '/' ) + 1 ); + /** * Site admin email address. */ diff --git a/includes/WebRequest.php b/includes/WebRequest.php index a7e4c7c48b..5a883eb7e5 100644 --- a/includes/WebRequest.php +++ b/includes/WebRequest.php @@ -124,6 +124,45 @@ class WebRequest { return $matches; } + /** + * Work out an appropriate URL prefix containing scheme and host, based on + * information detected from $_SERVER + */ + public static function detectServer() { + if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on') { + $proto = 'https'; + $stdPort = 443; + } else { + $proto = 'http'; + $stdPort = 80; + } + + $varNames = array( 'HTTP_HOST', 'SERVER_NAME', 'HOSTNAME', 'SERVER_ADDR' ); + $host = 'localhost'; + $port = $stdPort; + foreach ( $varNames as $varName ) { + if ( !isset( $_SERVER[$varName] ) ) { + continue; + } + $parts = IP::splitHostAndPort( $_SERVER[$varName] ); + if ( !$parts ) { + // Invalid, do not use + continue; + } + $host = $parts[0]; + if ( $parts[1] === false ) { + if ( isset( $_SERVER['SERVER_PORT'] ) ) { + $port = $_SERVER['SERVER_PORT']; + } // else leave it as $stdPort + } else { + $port = $parts[1]; + } + break; + } + + return $proto . '://' . IP::combineHostAndPort( $host, $port, $stdPort ); + } + /** * Check for title, action, and/or variant data in the URL * and interpolate it into the GET variables. diff --git a/includes/installer/Installer.php b/includes/installer/Installer.php index 1d2a9387b8..4fd9389030 100644 --- a/includes/installer/Installer.php +++ b/includes/installer/Installer.php @@ -843,38 +843,7 @@ abstract class Installer { * Environment check for the server hostname. */ protected function envCheckServer() { - if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on') { - $proto = 'https'; - $stdPort = 443; - } else { - $proto = 'http'; - $stdPort = 80; - } - - $varNames = array( 'HTTP_HOST', 'SERVER_NAME', 'HOSTNAME', 'SERVER_ADDR' ); - $host = 'localhost'; - $port = $stdPort; - foreach ( $varNames as $varName ) { - if ( !isset( $_SERVER[$varName] ) ) { - continue; - } - $parts = IP::splitHostAndPort( $_SERVER[$varName] ); - if ( !$parts ) { - // Invalid, do not use - continue; - } - $host = $parts[0]; - if ( $parts[1] === false ) { - if ( isset( $_SERVER['SERVER_PORT'] ) ) { - $port = $_SERVER['SERVER_PORT']; - } // else leave it as $stdPort - } else { - $port = $parts[1]; - } - break; - } - - $server = $proto . '://' . IP::combineHostAndPort( $host, $port, $stdPort ); + $server = WebRequest::detectServer(); $this->showMessage( 'config-using-server', $server ); $this->setVar( 'wgServer', $server ); } diff --git a/tests/phpunit/includes/installer/InstallerTest.php b/tests/phpunit/includes/WebRequestTest.php similarity index 59% rename from tests/phpunit/includes/installer/InstallerTest.php rename to tests/phpunit/includes/WebRequestTest.php index bcba7521af..1cfbd3fc3d 100644 --- a/tests/phpunit/includes/installer/InstallerTest.php +++ b/tests/phpunit/includes/WebRequestTest.php @@ -1,36 +1,18 @@ settings = array(); - } - -} - -class InstallerTest extends MediaWikiTestCase { +class WebRequestTest extends MediaWikiTestCase { /** - * @dataProvider provideEnvCheckServer + * @dataProvider provideDetectServer */ - function testEnvCheckServer( $expected, $input, $description ) { - $installer = new Installer_TestHelper; + function testDetectServer( $expected, $input, $description ) { $oldServer = $_SERVER; $_SERVER = $input; - $rm = new ReflectionMethod( 'Installer_TestHelper', 'envCheckServer' ); - if( !method_exists( $rm, 'setAccessible' ) ) { - $this->markTestIncomplete( "Test requires PHP 5.3.2 or above for ReflectionMethod::setAccessible" ); - } else { - $rm->setAccessible( true ); - $rm->invoke( $installer ); - $_SERVER = $oldServer; - $this->assertEquals( $expected, $installer->getVar( 'wgServer' ), $description ); - } + $result = WebRequest::detectServer(); + $_SERVER = $oldServer; + $this->assertEquals( $expected, $result, $description ); } - function provideEnvCheckServer() { + function provideDetectServer() { return array( array( 'http://x', -- 2.20.1