Skip to content

Commit 792f6b4

Browse files
janfejtekdg
authored andcommitted
Improved https recognition behind load balancer (#221)
1 parent 613d8cf commit 792f6b4

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
@@ -315,6 +315,11 @@ private function useForwardedProxy(Url $url): ?string
315315
: substr($address, 1, strpos($address, ']') - 1); // IPv6
316316
}
317317

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

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)