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

Commit 603d4a8

Browse files
committed
Added X-Forwarded-Host header awareness and prefer it over Host
1 parent c759917 commit 603d4a8

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/functions/marshal_uri_from_sapi.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function marshalUriFromSapi(array $server, array $headers) : Uri
8080
* @return array Array of two items, host and port, in that order (can be
8181
* passed to a list() operation).
8282
*/
83-
$marshalIpv6HostAndPort = function (array $server, string $host, ?int $port) : array {
83+
$marshalIpv6HostAndPort = function (array $server, ?int $port) : array {
8484
$host = '[' . $server['SERVER_ADDR'] . ']';
8585
$port = $port ?: 80;
8686
if ($port . ']' === substr($host, strrpos($host, ':') + 1)) {
@@ -93,8 +93,14 @@ function marshalUriFromSapi(array $server, array $headers) : Uri
9393

9494
static $defaults = ['', null];
9595

96-
if ($getHeaderFromArray('host', $headers, false)) {
97-
return $marshalHostAndPortFromHeader($getHeaderFromArray('host', $headers));
96+
$forwardedHost = $getHeaderFromArray('x-forwarded-host', $headers, false);
97+
if ($forwardedHost !== false) {
98+
return $marshalHostAndPortFromHeader($forwardedHost);
99+
}
100+
101+
$host = $getHeaderFromArray('host', $headers, false);
102+
if ($host !== false) {
103+
return $marshalHostAndPortFromHeader($host);
98104
}
99105

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

113119
// Misinterpreted IPv6-Address
114120
// Reported for Safari on Windows
115-
return $marshalIpv6HostAndPort($server, $host, $port);
121+
return $marshalIpv6HostAndPort($server, $port);
116122
};
117123

118124
/**

0 commit comments

Comments
 (0)