Skip to content

Commit

Permalink
Improved https recognition behind load balancer (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
janfejtek authored and dg committed Mar 18, 2023
1 parent 150e949 commit 4bea77f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ private function useForwardedProxy(Url $url): ?string
: substr($address, 1, strpos($address, ']') - 1); // IPv6
}

if (isset($proxyParams['proto']) && count($proxyParams['proto']) === 1) {
$url->setScheme(strcasecmp($proxyParams['proto'][0], 'https') === 0 ? 'https' : 'http');
$url->setPort($url->getScheme() === 'https' ? 443 : 80);
}

if (isset($proxyParams['host']) && count($proxyParams['host']) === 1) {
$host = $proxyParams['host'][0];
$startingDelimiterPosition = strpos($host, '[');
Expand All @@ -334,11 +339,6 @@ private function useForwardedProxy(Url $url): ?string
}
}
}

$scheme = (isset($proxyParams['proto']) && count($proxyParams['proto']) === 1)
? $proxyParams['proto'][0]
: 'http';
$url->setScheme(strcasecmp($scheme, 'https') === 0 ? 'https' : 'http');
return $remoteAddr ?? null;
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Http/RequestFactory.port.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class RequestFactoryPortTest extends Tester\TestCase
[8080, ['HTTP_HOST' => 'localhost', 'SERVER_PORT' => '666', 'HTTP_X_FORWARDED_PORT' => '8080']],
[8080, ['SERVER_NAME' => 'localhost', 'SERVER_PORT' => '666', 'HTTP_X_FORWARDED_PORT' => '8080']],
[44443, ['HTTPS' => 'on', 'SERVER_NAME' => 'localhost:666', 'HTTP_X_FORWARDED_PORT' => '44443']],
[443, ['HTTP_FORWARDED' => 'for=192.168.1.1;host=example.com;proto=https']],
[44443, ['HTTP_FORWARDED' => 'for=192.168.1.1;host=example.com:44443;proto=https']],
[80, ['HTTP_FORWARDED' => 'for=192.168.1.1;host=example.com;proto=http']],
[8080, ['HTTP_FORWARDED' => 'for=192.168.1.1;host=example.com:8080;proto=http']],
];
}
}
Expand Down

0 comments on commit 4bea77f

Please sign in to comment.