From c558deef274d838aae5c0366ff7dc2ebce27a981 Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 20 Sep 2012 09:41:28 -0400 Subject: [PATCH] Support X-Forwarded-Context for subdomain proxy configs (issue 135) --- docs/01_setup.mkd | 5 +++++ docs/04_releases.mkd | 1 + src/com/gitblit/utils/HttpUtils.java | 16 +++++++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/docs/01_setup.mkd b/docs/01_setup.mkd index eaaf3be5..fa1bcd90 100644 --- a/docs/01_setup.mkd +++ b/docs/01_setup.mkd @@ -161,6 +161,11 @@ ProxyPreserveHost On #RequestHeader set X-Forwarded-Proto https #RequestHeader set X-Forwarded-Port 443 +# If you are using subdomain proxying then you will want to tell Gitblit the appropriate +# context path for your repository url. +# If you are not using subdomain proxying, then ignore this setting. +#RequestHeader set X-Forwarded-Context / + #ProxyPass /gitblit ajp://localhost:8009/gitblit %ENDCODE% **Please** make sure to: diff --git a/docs/04_releases.mkd b/docs/04_releases.mkd index 271e18a7..7f0feece 100644 --- a/docs/04_releases.mkd +++ b/docs/04_releases.mkd @@ -16,6 +16,7 @@ If you are updating from an earlier release AND you have indexed branches with t #### additions +- added support for X-Forwarded-Context for Apache subdomain proxy configurations (issue 135) - delete branch feature (issue 121, Github/ajermakovics) - added line links to blob view at the expense of zebra striping (issue 130) - added RedmineUserService (github/mallowlabs) diff --git a/src/com/gitblit/utils/HttpUtils.java b/src/com/gitblit/utils/HttpUtils.java index 3903f8c7..ad7d58c1 100644 --- a/src/com/gitblit/utils/HttpUtils.java +++ b/src/com/gitblit/utils/HttpUtils.java @@ -67,6 +67,20 @@ public class HttpUtils { } } + String context = request.getContextPath(); + String forwardedContext = request.getHeader("X-Forwarded-Context"); + if (forwardedContext != null) { + forwardedContext = request.getHeader("X_Forwarded_Context"); + } + if (!StringUtils.isEmpty(forwardedContext)) { + context = forwardedContext; + } + + // trim any trailing slash + if (context.length() > 0 && context.charAt(context.length() - 1) == '/') { + context = context.substring(1); + } + StringBuilder sb = new StringBuilder(); sb.append(scheme); sb.append("://"); @@ -75,7 +89,7 @@ public class HttpUtils { || ("https".equals(scheme) && port != 443)) { sb.append(":" + port); } - sb.append(request.getContextPath()); + sb.append(context); return sb.toString(); } } -- 2.39.5