Skip to content

Commit

Permalink
Merge pull request #148 from adriansuter/patch-https-port
Browse files Browse the repository at this point in the history
Fix the default port if not defined (for https)
  • Loading branch information
l0gicgate authored Jan 24, 2020
2 parents 495595f + 991ae75 commit e4be5e0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/Factory/UriFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public function createFromGlobals(array $globals): Uri
}

// Authority: Port
$port = !empty($globals['SERVER_PORT']) ? (int) $globals['SERVER_PORT'] : 80;
if (preg_match('/^(\[[a-fA-F0-9:.]+\])(:\d+)?\z/', $host, $matches)) {
$port = !empty($globals['SERVER_PORT']) ? (int)$globals['SERVER_PORT'] : ($scheme === 'https' ? 443 : 80);
if (preg_match('/^(\[[a-fA-F0-9:.]+])(:\d+)?\z/', $host, $matches)) {
$host = $matches[1];

if (isset($matches[2])) {
Expand Down Expand Up @@ -110,9 +110,7 @@ public function createFromGlobals(array $globals): Uri
}
}

// Build Uri
$uri = new Uri($scheme, $host, $port, $requestUri, $queryString, '', $username, $password);

return $uri;
// Build Uri and return
return new Uri($scheme, $host, $port, $requestUri, $queryString, '', $username, $password);
}
}
21 changes: 21 additions & 0 deletions tests/Factory/UriFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@ public function testCreateFromGlobals()
$this->assertEquals('', $uri->getFragment());
}

public function testCreateFromGlobalsWithHttps()
{
$globals = Environment::mock(
[
'HTTPS' => 'on',
'HTTP_HOST' => 'example.com'
]
);

// Make the 'SERVER_PORT' empty as we want to test if the default server port gets set correctly.
$globals['SERVER_PORT'] = '';

$uri = $this->createUriFactory()->createFromGlobals($globals);

$this->assertEquals('https', $uri->getScheme());
$this->assertEquals('example.com', $uri->getHost());

// The port is expected to be NULL as the server port is the default standard (443 in case of https).
$this->assertNull($uri->getPort());
}

public function testCreateFromGlobalsUsesServerNameAsHostIfHostHeaderIsNotPresent()
{
$globals = Environment::mock([
Expand Down

0 comments on commit e4be5e0

Please sign in to comment.