From a1ef8e93062c58d4eace2c6598953e1d350d3fcc Mon Sep 17 00:00:00 2001 From: Dirk Steinkopf Date: Sun, 12 Jun 2016 19:27:39 +0200 Subject: [PATCH] fix baseURL handling in HttpUtils, fixes #1083 --- src/main/java/com/gitblit/utils/HttpUtils.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/gitblit/utils/HttpUtils.java b/src/main/java/com/gitblit/utils/HttpUtils.java index 818ed492..2fd8d898 100644 --- a/src/main/java/com/gitblit/utils/HttpUtils.java +++ b/src/main/java/com/gitblit/utils/HttpUtils.java @@ -78,6 +78,7 @@ public class HttpUtils { } } + // try to use reverse-proxy's context String context = request.getContextPath(); String forwardedContext = request.getHeader("X-Forwarded-Context"); if (StringUtils.isEmpty(forwardedContext)) { @@ -92,13 +93,24 @@ public class HttpUtils { context = context.substring(1); } + // try to use reverse-proxy's hostname + String host = request.getServerName(); + String forwardedHost = request.getHeader("X-Forwarded-Host"); + if (StringUtils.isEmpty(forwardedHost)) { + forwardedHost = request.getHeader("X_Forwarded_Host"); + } + if (!StringUtils.isEmpty(forwardedHost)) { + host = forwardedHost; + } + + // build result StringBuilder sb = new StringBuilder(); sb.append(scheme); sb.append("://"); - sb.append(request.getServerName()); + sb.append(host); if (("http".equals(scheme) && port != 80) || ("https".equals(scheme) && port != 443)) { - sb.append(":" + port); + sb.append(":").append(port); } sb.append(context); return sb.toString(); -- 2.39.5