Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge 3b27f47 into 6e1e657
Browse files Browse the repository at this point in the history
  • Loading branch information
boesing committed Nov 6, 2019
2 parents 6e1e657 + 3b27f47 commit 19f43fe
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/functions/marshal_uri_from_sapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function marshalUriFromSapi(array $server, array $headers) : Uri
* @return array Array of two items, host and port, in that order (can be
* passed to a list() operation).
*/
$marshalIpv6HostAndPort = function (array $server, string $host, ?int $port) : array {
$marshalIpv6HostAndPort = function (array $server, ?int $port) : array {
$host = '[' . $server['SERVER_ADDR'] . ']';
$port = $port ?: 80;
if ($port . ']' === substr($host, strrpos($host, ':') + 1)) {
Expand All @@ -93,8 +93,14 @@ function marshalUriFromSapi(array $server, array $headers) : Uri

static $defaults = ['', null];

if ($getHeaderFromArray('host', $headers, false)) {
return $marshalHostAndPortFromHeader($getHeaderFromArray('host', $headers));
$forwardedHost = $getHeaderFromArray('x-forwarded-host', $headers, false);
if ($forwardedHost !== false) {
return $marshalHostAndPortFromHeader($forwardedHost);
}

$host = $getHeaderFromArray('host', $headers, false);
if ($host !== false) {
return $marshalHostAndPortFromHeader($host);
}

if (! isset($server['SERVER_NAME'])) {
Expand All @@ -112,7 +118,7 @@ function marshalUriFromSapi(array $server, array $headers) : Uri

// Misinterpreted IPv6-Address
// Reported for Safari on Windows
return $marshalIpv6HostAndPort($server, $host, $port);
return $marshalIpv6HostAndPort($server, $port);
};

/**
Expand Down
32 changes: 32 additions & 0 deletions test/functions/MarshalUriFromSapiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,36 @@ public function returnsUrlWithCorrectHttpSchemeFromArraysProvider() : array
'empty' => ['', 'http'],
];
}

/**
* @dataProvider returnsUrlWithCorrectSchemeAndHostFromArrays
*/
public function testReturnsUrlWithCorrectSchemeAndHostFromArrays(string $expectedScheme, string $expectedHost, array $server, array $headers) : void
{
$uri = marshalUriFromSapi($server, $headers);
self::assertSame($expectedScheme, $uri->getScheme());
self::assertSame($expectedHost, $uri->getHost());
}

public function returnsUrlWithCorrectSchemeAndHostFromArrays() : array
{
return [
'x-forwarded-proto' => [
'https',
'localhost',
[
'SERVER_NAME' => 'localhost',
],
['X-Forwarded-Proto' => 'https'],
],
'x-forwarded-host' => [
'http',
'example.org',
[
'SERVER_NAME' => 'localhost',
],
['X-Forwarded-Host' => 'example.org', 'Host' => 'localhost'],
],
];
}
}

0 comments on commit 19f43fe

Please sign in to comment.