Skip to content

Commit 3163a4a

Browse files
janfejtekdg
authored andcommitted
Improved https recognition behind load balancer (#221)
1 parent 15c9908 commit 3163a4a

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/Http/RequestFactory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ private function useForwardedProxy(Url $url): ?string
316316
: substr($address, 1, strpos($address, ']') - 1); // IPv6
317317
}
318318

319+
if (isset($proxyParams['proto']) && count($proxyParams['proto']) === 1) {
320+
$url->setScheme(strcasecmp($proxyParams['proto'][0], 'https') === 0 ? 'https' : 'http');
321+
$url->setPort($url->getScheme() === 'https' ? 443 : 80);
322+
}
323+
319324
if (isset($proxyParams['host']) && count($proxyParams['host']) === 1) {
320325
$host = $proxyParams['host'][0];
321326
$startingDelimiterPosition = strpos($host, '[');
@@ -334,11 +339,6 @@ private function useForwardedProxy(Url $url): ?string
334339
}
335340
}
336341
}
337-
338-
$scheme = (isset($proxyParams['proto']) && count($proxyParams['proto']) === 1)
339-
? $proxyParams['proto'][0]
340-
: 'http';
341-
$url->setScheme(strcasecmp($scheme, 'https') === 0 ? 'https' : 'http');
342342
return $remoteAddr ?? null;
343343
}
344344

tests/Http/RequestFactory.port.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ class RequestFactoryPortTest extends Tester\TestCase
7474
[8080, ['HTTP_HOST' => 'localhost', 'SERVER_PORT' => '666', 'HTTP_X_FORWARDED_PORT' => '8080']],
7575
[8080, ['SERVER_NAME' => 'localhost', 'SERVER_PORT' => '666', 'HTTP_X_FORWARDED_PORT' => '8080']],
7676
[44443, ['HTTPS' => 'on', 'SERVER_NAME' => 'localhost:666', 'HTTP_X_FORWARDED_PORT' => '44443']],
77+
[443, ['HTTP_FORWARDED' => 'for=192.168.1.1;host=example.com;proto=https']],
78+
[44443, ['HTTP_FORWARDED' => 'for=192.168.1.1;host=example.com:44443;proto=https']],
79+
[80, ['HTTP_FORWARDED' => 'for=192.168.1.1;host=example.com;proto=http']],
80+
[8080, ['HTTP_FORWARDED' => 'for=192.168.1.1;host=example.com:8080;proto=http']],
7781
];
7882
}
7983
}

0 commit comments

Comments
 (0)