diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php index 3903e726e5dbe32bc170d8b561ef77ce9a5319b3..5c1c6e89d9255e540e67275ad4d631b707f22757 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php @@ -208,10 +208,6 @@ class SimpleSAML_Metadata_MetaDataStorageHandler // then we look for the hostname $currenthost = \SimpleSAML\Utils\HTTP::getSelfHost(); // sp.example.org - if (strpos($currenthost, ":") !== false) { - $currenthostdecomposed = explode(":", $currenthost); - $currenthost = $currenthostdecomposed[0]; - } foreach ($this->sources as $source) { $index = $source->getEntityIdFromHostPath($currenthost, $set, $type); diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageSource.php b/lib/SimpleSAML/Metadata/MetaDataStorageSource.php index bc7e896eaa68eb426ee64486942282a98a04a763..9d677cda5ee9ace272e1df5324b012640f9b2354 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageSource.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageSource.php @@ -199,10 +199,6 @@ abstract class SimpleSAML_Metadata_MetaDataStorageSource // check for hostname $currenthost = \SimpleSAML\Utils\HTTP::getSelfHost(); // sp.example.org - if (strpos($currenthost, ":") !== false) { - $currenthostdecomposed = explode(":", $currenthost); - $currenthost = $currenthostdecomposed[0]; - } foreach ($metadataSet as $index => $entry) { if ($index === $entityId) { diff --git a/lib/SimpleSAML/Utils/HTTP.php b/lib/SimpleSAML/Utils/HTTP.php index 5f791def704e11d185b31e51df5bab82dfd2192c..00946fc3c83d2ab1d0072bb54d343eb2762453b8 100644 --- a/lib/SimpleSAML/Utils/HTTP.php +++ b/lib/SimpleSAML/Utils/HTTP.php @@ -336,7 +336,7 @@ class HTTP /** - * Helper function to retrieve a file or URL with proxy support, also + * Helper function to retrieve a file or URL with proxy support, also * supporting proxy basic authorization.. * * An exception will be thrown if we are unable to retrieve the data. @@ -595,22 +595,39 @@ class HTTP /** * Retrieve our own host. * - * @return string The current host (with non-default ports included). + * E.g. www.example.com + * + * @return string The current host. + * + * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> + */ + public static function getSelfHost() + { + return array_shift(explode(':', self::getSelfHostWithNonStandardPort())); + } + + /** + * 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, 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 getSelfHost() + public static function getSelfHostWithNonStandardPort() { $url = self::getBaseURL(); $start = strpos($url, '://') + 3; - $length = strcspn($url, '/:', $start); + $length = strcspn($url, '/', $start); return substr($url, $start, $length); } - /** * Retrieve our own host together with the URL path. Please note this function will return the base URL for the * current SP, as defined in the global configuration. diff --git a/tests/lib/SimpleSAML/Utils/HTTPTest.php b/tests/lib/SimpleSAML/Utils/HTTPTest.php new file mode 100644 index 0000000000000000000000000000000000000000..f67b8832e2925c5cfee07ea4c9c0046c489af1c2 --- /dev/null +++ b/tests/lib/SimpleSAML/Utils/HTTPTest.php @@ -0,0 +1,44 @@ +<?php +namespace SimpleSAML\Test\Utils; + +use SimpleSAML\Utils\HTTP; + +class HTTPTest extends \PHPUnit_Framework_TestCase +{ + /** + * Test SimpleSAML\Utils\HTTP::getSelfHost() with and without custom port. + */ + public function testGetSelfHost() + { + \SimpleSAML_Configuration::loadFromArray(array( + 'baseurlpath' => '', + ), '[ARRAY]', 'simplesaml'); + $_SERVER['SERVER_PORT'] = '80'; + $this->assertEquals('localhost', HTTP::getSelfHost()); + $_SERVER['SERVER_PORT'] = '3030'; + $this->assertEquals('localhost', HTTP::getSelfHost()); + } + + /** + * Test SimpleSAML\Utils\HTTP::getSelfHostWithPort(), with and without custom port. + */ + public function testGetSelfHostWithPort() + { + \SimpleSAML_Configuration::loadFromArray(array( + 'baseurlpath' => '', + ), '[ARRAY]', 'simplesaml'); + + // standard port for HTTP + $_SERVER['SERVER_PORT'] = '80'; + $this->assertEquals('localhost', HTTP::getSelfHostWithNonStandardPort()); + + // non-standard port + $_SERVER['SERVER_PORT'] = '3030'; + $this->assertEquals('localhost:3030', HTTP::getSelfHostWithNonStandardPort()); + + // standard port for HTTPS + $_SERVER['HTTPS'] = 'on'; + $_SERVER['SERVER_PORT'] = '443'; + $this->assertEquals('localhost', HTTP::getSelfHostWithNonStandardPort()); + } +}