From d4de56da714ffc46cfee0b066c723c21be527c79 Mon Sep 17 00:00:00 2001 From: Jaime Perez Crespo <jaime.perez@uninett.no> Date: Thu, 3 Mar 2016 11:48:54 +0100 Subject: [PATCH] Rename Utils\HTTP::getSelfHostWithoutPort() to Utils\HTTP::getSelfHostWithNonStandardPort(), change the logic, and reimplement Utils\HTTP::getSelfHost() to depend on use the former. Complete the tests to include the case of port 443 while using HTTPS. --- .../Metadata/MetaDataStorageHandler.php | 2 +- .../MetaDataStorageHandlerFlatFile.php | 4 +-- .../Metadata/MetaDataStorageHandlerPdo.php | 4 +-- .../Metadata/MetaDataStorageSource.php | 2 +- lib/SimpleSAML/Utils/HTTP.php | 29 ++++++++++--------- tests/lib/SimpleSAML/Utils/HTTPTest.php | 21 ++++++++++---- www/admin/hostnames.php | 2 +- 7 files changed, 38 insertions(+), 26 deletions(-) diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php index 50c3209af..5c1c6e89d 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php @@ -207,7 +207,7 @@ class SimpleSAML_Metadata_MetaDataStorageHandler } // then we look for the hostname - $currenthost = \SimpleSAML\Utils\HTTP::getSelfHostWithoutPort(); // sp.example.org + $currenthost = \SimpleSAML\Utils\HTTP::getSelfHost(); // sp.example.org foreach ($this->sources as $source) { $index = $source->getEntityIdFromHostPath($currenthost, $set, $type); diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php index 80bade1c9..3f8632227 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php @@ -134,9 +134,9 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerFlatFile extends SimpleSAML_Meta } elseif ($set === 'shib13-idp-hosted') { return $baseurl.'shib13/idp/metadata.php'; } elseif ($set === 'wsfed-sp-hosted') { - return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort(); + return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost(); } elseif ($set === 'adfs-idp-hosted') { - return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort().':idp'; + return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost().':idp'; } else { throw new Exception('Can not generate dynamic EntityID for metadata of this type: ['.$set.']'); } diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerPdo.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerPdo.php index 7202a73f9..e8981169d 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerPdo.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerPdo.php @@ -151,9 +151,9 @@ class SimpleSAML_Metadata_MetaDataStorageHandlerPdo extends SimpleSAML_Metadata_ } elseif ($set === 'shib13-sp-hosted') { return $baseurl.'shib13/sp/metadata.php'; } elseif ($set === 'wsfed-sp-hosted') { - return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort(); + return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost(); } elseif ($set === 'adfs-idp-hosted') { - return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort().':idp'; + return 'urn:federation:'.\SimpleSAML\Utils\HTTP::getSelfHost().':idp'; } else { throw new Exception('Can not generate dynamic EntityID for metadata of this type: ['.$set.']'); } diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageSource.php b/lib/SimpleSAML/Metadata/MetaDataStorageSource.php index d442bf782..9d677cda5 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageSource.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageSource.php @@ -198,7 +198,7 @@ abstract class SimpleSAML_Metadata_MetaDataStorageSource $metadataSet = $this->getMetadataSet($set); // check for hostname - $currenthost = \SimpleSAML\Utils\HTTP::getSelfHostWithoutPort(); // sp.example.org + $currenthost = \SimpleSAML\Utils\HTTP::getSelfHost(); // sp.example.org foreach ($metadataSet as $index => $entry) { if ($index === $entityId) { diff --git a/lib/SimpleSAML/Utils/HTTP.php b/lib/SimpleSAML/Utils/HTTP.php index c25bc6d57..00946fc3c 100644 --- a/lib/SimpleSAML/Utils/HTTP.php +++ b/lib/SimpleSAML/Utils/HTTP.php @@ -595,32 +595,35 @@ class HTTP /** * Retrieve our own host. * - * @return string The current host (with non-default ports included). + * E.g. www.example.com * - * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> - * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> + * @return string The current host. + * + * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ public static function getSelfHost() { - $url = self::getBaseURL(); - - $start = strpos($url, '://') + 3; - $length = strcspn($url, '/', $start); - - return substr($url, $start, $length); + return array_shift(explode(':', self::getSelfHostWithNonStandardPort())); } /** - * Retrieve our own host. + * Retrieve our own host, including the port in case the it is not standard for the protocol in use. That is port + * 80 for HTTP and port 443 for HTTPS. + * + * E.g. www.example.com:8080 * - * @return string The current host without port specification. + * @return string The current host, followed by a colon and the port number, in case the port is not standard for + * the protocol. + * + * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> + * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function getSelfHostWithoutPort() + public static function getSelfHostWithNonStandardPort() { $url = self::getBaseURL(); $start = strpos($url, '://') + 3; - $length = strcspn($url, '/:', $start); + $length = strcspn($url, '/', $start); return substr($url, $start, $length); } diff --git a/tests/lib/SimpleSAML/Utils/HTTPTest.php b/tests/lib/SimpleSAML/Utils/HTTPTest.php index b4e9845a7..f67b8832e 100644 --- a/tests/lib/SimpleSAML/Utils/HTTPTest.php +++ b/tests/lib/SimpleSAML/Utils/HTTPTest.php @@ -6,7 +6,7 @@ use SimpleSAML\Utils\HTTP; class HTTPTest extends \PHPUnit_Framework_TestCase { /** - * Test HTTP::getSelfHost with and without custom port + * Test SimpleSAML\Utils\HTTP::getSelfHost() with and without custom port. */ public function testGetSelfHost() { @@ -16,20 +16,29 @@ class HTTPTest extends \PHPUnit_Framework_TestCase $_SERVER['SERVER_PORT'] = '80'; $this->assertEquals('localhost', HTTP::getSelfHost()); $_SERVER['SERVER_PORT'] = '3030'; - $this->assertEquals('localhost:3030', HTTP::getSelfHost()); + $this->assertEquals('localhost', HTTP::getSelfHost()); } /** - * Test HTTP::getSelfHostWithoutPort + * Test SimpleSAML\Utils\HTTP::getSelfHostWithPort(), with and without custom port. */ - public function testGetSelfHostWithoutPort() + public function testGetSelfHostWithPort() { \SimpleSAML_Configuration::loadFromArray(array( 'baseurlpath' => '', ), '[ARRAY]', 'simplesaml'); + + // standard port for HTTP $_SERVER['SERVER_PORT'] = '80'; - $this->assertEquals('localhost', HTTP::getSelfHostWithoutPort()); + $this->assertEquals('localhost', HTTP::getSelfHostWithNonStandardPort()); + + // non-standard port $_SERVER['SERVER_PORT'] = '3030'; - $this->assertEquals('localhost', HTTP::getSelfHostWithoutPort()); + $this->assertEquals('localhost:3030', HTTP::getSelfHostWithNonStandardPort()); + + // standard port for HTTPS + $_SERVER['HTTPS'] = 'on'; + $_SERVER['SERVER_PORT'] = '443'; + $this->assertEquals('localhost', HTTP::getSelfHostWithNonStandardPort()); } } diff --git a/www/admin/hostnames.php b/www/admin/hostnames.php index e77d99edc..4baec753b 100644 --- a/www/admin/hostnames.php +++ b/www/admin/hostnames.php @@ -17,7 +17,7 @@ $attributes['SERVER_PROTOCOL'] = array($_SERVER['SERVER_PROTOCOL']); $attributes['SERVER_PORT'] = array($_SERVER['SERVER_PORT']); $attributes['Utilities_getBaseURL()'] = array(\SimpleSAML\Utils\HTTP::getBaseURL()); -$attributes['Utilities_getSelfHost()'] = array(\SimpleSAML\Utils\HTTP::getSelfHostWithoutPort()); +$attributes['Utilities_getSelfHost()'] = array(\SimpleSAML\Utils\HTTP::getSelfHost()); $attributes['Utilities_selfURLhost()'] = array(\SimpleSAML\Utils\HTTP::getSelfURLHost()); $attributes['Utilities_selfURLNoQuery()'] = array(\SimpleSAML\Utils\HTTP::getSelfURLNoQuery()); $attributes['Utilities_getSelfHostWithPath()'] = array(\SimpleSAML\Utils\HTTP::getSelfHostWithPath()); -- GitLab