Skip to content

Commit

Permalink
Merge branch 'uri-port-defaults' into 3.x
Browse files Browse the repository at this point in the history
Closes #2487
  • Loading branch information
akrabat committed Sep 16, 2018
2 parents 79051ab + 77bd8c9 commit f9db2e6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
8 changes: 5 additions & 3 deletions Slim/Http/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,17 @@ public static function createFromEnvironment(Environment $env)
$username = $env->get('PHP_AUTH_USER', '');
$password = $env->get('PHP_AUTH_PW', '');

// Authority: Host
// Authority: Host and Port
if ($env->has('HTTP_HOST')) {
$host = $env->get('HTTP_HOST');
// set a port default
$port = null;
} else {
$host = $env->get('SERVER_NAME');
// set a port default
$port = (int)$env->get('SERVER_PORT', 80);
}

// Authority: Port
$port = (int)$env->get('SERVER_PORT', 80);
if (preg_match('/^(\[[a-fA-F0-9:.]+\])(:\d+)?\z/', $host, $matches)) {
$host = $matches[1];

Expand Down
52 changes: 49 additions & 3 deletions tests/Http/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,27 @@ public function testCreateEnvironment()
$this->assertEquals('', $uri->getFragment());
}

public function testCreateFromEnvironmentSetsDefaultPortWhenHostHeaderDoesntHaveAPort()
{
$environment = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo/bar',
'QUERY_STRING' => 'abc=123',
'HTTP_HOST' => 'example.com',
'HTTPS' => 'on',
]);

$uri = Uri::createFromEnvironment($environment);

$this->assertEquals('example.com', $uri->getHost());
$this->assertEquals(null, $uri->getPort());
$this->assertEquals('/foo/bar', $uri->getPath());
$this->assertEquals('abc=123', $uri->getQuery());
$this->assertEquals('', $uri->getFragment());

$this->assertEquals('https://example.com/foo/bar?abc=123', (string)$uri);
}

public function testCreateEnvironmentWithIPv6HostNoPort()
{
$environment = Environment::mock([
Expand All @@ -556,14 +577,13 @@ public function testCreateEnvironmentWithIPv6HostNoPort()
'QUERY_STRING' => 'abc=123',
'HTTP_HOST' => '[2001:db8::1]',
'REMOTE_ADDR' => '2001:db8::1',
'SERVER_PORT' => 8080,
]);

$uri = Uri::createFromEnvironment($environment);

$this->assertEquals('josh:sekrit', $uri->getUserInfo());
$this->assertEquals('[2001:db8::1]', $uri->getHost());
$this->assertEquals('8080', $uri->getPort());
$this->assertNull($uri->getPort());
$this->assertEquals('/foo/bar', $uri->getPath());
$this->assertEquals('abc=123', $uri->getQuery());
$this->assertEquals('', $uri->getFragment());
Expand All @@ -579,7 +599,6 @@ public function testCreateEnvironmentWithIPv6HostWithPort()
'QUERY_STRING' => 'abc=123',
'HTTP_HOST' => '[2001:db8::1]:8080',
'REMOTE_ADDR' => '2001:db8::1',
'SERVER_PORT' => 8080,
]);

$uri = Uri::createFromEnvironment($environment);
Expand All @@ -592,6 +611,33 @@ public function testCreateEnvironmentWithIPv6HostWithPort()
$this->assertEquals('', $uri->getFragment());
}

/**
* @group one
*/
public function testCreateEnvironmentWithNoHostHeader()
{
$environment = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo/bar',
'PHP_AUTH_USER' => 'josh',
'PHP_AUTH_PW' => 'sekrit',
'QUERY_STRING' => 'abc=123',
'REMOTE_ADDR' => '2001:db8::1',
'SERVER_NAME' => '[2001:db8::1]',
'SERVER_PORT' => '8080',
]);
$environment->remove('HTTP_HOST');

$uri = Uri::createFromEnvironment($environment);

$this->assertEquals('josh:sekrit', $uri->getUserInfo());
$this->assertEquals('[2001:db8::1]', $uri->getHost());
$this->assertEquals('8080', $uri->getPort());
$this->assertEquals('/foo/bar', $uri->getPath());
$this->assertEquals('abc=123', $uri->getQuery());
$this->assertEquals('', $uri->getFragment());
}

/**
* @covers Slim\Http\Uri::createFromEnvironment
* @ticket 1375
Expand Down

0 comments on commit f9db2e6

Please sign in to comment.