Fixes #477: support multi reverse proxy with comma syntax

Going through multiple reverse proxy will store multiple scheme and port in HTTP header separated by a comma. Shaarli will use the first one to generate server_url.
This commit is contained in:
ArthurHoaro 2016-02-28 16:24:18 +01:00
parent 8710d4da8e
commit 85244fa0d0
2 changed files with 25 additions and 2 deletions

View file

@ -106,12 +106,22 @@ function server_url($server)
// Shaarli is served behind a proxy
if (isset($server['HTTP_X_FORWARDED_PROTO'])) {
// Keep forwarded scheme
if (strpos($server['HTTP_X_FORWARDED_PROTO'], ',') !== false) {
$schemes = explode(',', $server['HTTP_X_FORWARDED_PROTO']);
$scheme = trim($schemes[0]);
} else {
$scheme = $server['HTTP_X_FORWARDED_PROTO'];
}
if (isset($server['HTTP_X_FORWARDED_PORT'])) {
// Keep forwarded port
if (strpos($server['HTTP_X_FORWARDED_PORT'], ',') !== false) {
$ports = explode(',', $server['HTTP_X_FORWARDED_PORT']);
$port = ':' . trim($ports[0]);
} else {
$port = ':' . $server['HTTP_X_FORWARDED_PORT'];
}
}
return $scheme.'://'.$server['SERVER_NAME'].$port;
}

View file

@ -67,6 +67,19 @@ public function testHttpsProxyForward()
)
)
);
$this->assertEquals(
'https://host.tld:4974',
server_url(
array(
'HTTPS' => 'Off',
'SERVER_NAME' => 'host.tld',
'SERVER_PORT' => '80',
'HTTP_X_FORWARDED_PROTO' => 'https, https',
'HTTP_X_FORWARDED_PORT' => '4974, 80'
)
)
);
}
/**