Skip to content
Snippets Groups Projects
Commit cbd20f6d authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Merge branch 'bug/issue337'

* bug/issue337:
  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.
  Fixes issue 337
parents b96ddbfc d4de56da
No related branches found
No related tags found
No related merge requests found
......@@ -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);
......
......@@ -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) {
......
......@@ -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.
......
<?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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment