summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/SimpleSAML/Metadata/MetaDataStorageHandler.php4
-rw-r--r--lib/SimpleSAML/Metadata/MetaDataStorageSource.php4
-rw-r--r--lib/SimpleSAML/Utils/HTTP.php27
-rw-r--r--tests/lib/SimpleSAML/Utils/HTTPTest.php44
4 files changed, 66 insertions, 13 deletions
diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php
index 3903e72..5c1c6e8 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 bc7e896..9d677cd 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 5f791de..00946fc 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 0000000..f67b883
--- /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());
+ }
+}