]> source.dussan.org Git - gitblit.git/commitdiff
fix baseURL handling in HttpUtils, fixes #1083 1086/head
authorDirk Steinkopf <dsteinkopf@ecube.de>
Sun, 12 Jun 2016 17:27:39 +0000 (19:27 +0200)
committerDirk Steinkopf <dsteinkopf@ecube.de>
Sun, 12 Jun 2016 17:27:39 +0000 (19:27 +0200)
src/main/java/com/gitblit/utils/HttpUtils.java

index 818ed492beb463d263c1fc185359b6da0c1f0e50..2fd8d898ef9412a9fb17cefc4d5599e3824c50b7 100644 (file)
@@ -78,6 +78,7 @@ public class HttpUtils {
                }\r
         }\r
 \r
+               // try to use reverse-proxy's context\r
         String context = request.getContextPath();\r
         String forwardedContext = request.getHeader("X-Forwarded-Context");\r
         if (StringUtils.isEmpty(forwardedContext)) {\r
@@ -92,13 +93,24 @@ public class HttpUtils {
                context = context.substring(1);\r
         }\r
 \r
+               // try to use reverse-proxy's hostname\r
+               String host = request.getServerName();\r
+               String forwardedHost = request.getHeader("X-Forwarded-Host");\r
+               if (StringUtils.isEmpty(forwardedHost)) {\r
+                       forwardedHost = request.getHeader("X_Forwarded_Host");\r
+               }\r
+               if (!StringUtils.isEmpty(forwardedHost)) {\r
+                       host = forwardedHost;\r
+               }\r
+\r
+               // build result\r
                StringBuilder sb = new StringBuilder();\r
                sb.append(scheme);\r
                sb.append("://");\r
-               sb.append(request.getServerName());\r
+               sb.append(host);\r
                if (("http".equals(scheme) && port != 80)\r
                                || ("https".equals(scheme) && port != 443)) {\r
-                       sb.append(":" + port);\r
+                       sb.append(":").append(port);\r
                }\r
                sb.append(context);\r
                return sb.toString();\r