]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Fixes #477: support multi reverse proxy with comma syntax 505/head
authorArthurHoaro <arthur@hoa.ro>
Sun, 28 Feb 2016 15:24:18 +0000 (16:24 +0100)
committerArthurHoaro <arthur@hoa.ro>
Sun, 28 Feb 2016 15:24:18 +0000 (16:24 +0100)
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.

application/HttpUtils.php
tests/HttpUtils/ServerUrlTest.php

index e2c1cb470f8a9c776358046839b10d3cdbdd3fd5..af7cb37123fa06a84738a6054c733ab0b38afd23 100644 (file)
@@ -106,11 +106,21 @@ function server_url($server)
     // Shaarli is served behind a proxy
     if (isset($server['HTTP_X_FORWARDED_PROTO'])) {
         // Keep forwarded scheme
-        $scheme = $server['HTTP_X_FORWARDED_PROTO'];
+        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
-            $port = ':'.$server['HTTP_X_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;
index 5096db653bda40498ddb4fcf1006cf347d15b252..8a55a2202be11910e9dcb22c38f60554820efbc3 100644 (file)
@@ -67,6 +67,19 @@ class ServerUrlTest extends PHPUnit_Framework_TestCase
                 )
             )
         );
+
+        $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'
+                )
+            )
+        );
     }
 
     /**