From c558deef274d838aae5c0366ff7dc2ebce27a981 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
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 +++++
 src/com/gitblit/utils/HttpUtils.java |   16 +++++++++++++++-
 docs/04_releases.mkd                 |    1 +
 3 files changed, 21 insertions(+), 1 deletions(-)

diff --git a/docs/01_setup.mkd b/docs/01_setup.mkd
index eaaf3be..fa1bcd9 100644
--- a/docs/01_setup.mkd
+++ b/docs/01_setup.mkd
@@ -161,6 +161,11 @@
 #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 271e18a..7f0feec 100644
--- a/docs/04_releases.mkd
+++ b/docs/04_releases.mkd
@@ -16,6 +16,7 @@
 
 #### 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 3903f8c..ad7d58c 100644
--- a/src/com/gitblit/utils/HttpUtils.java
+++ b/src/com/gitblit/utils/HttpUtils.java
@@ -67,6 +67,20 @@
         	}
         }
         
+        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 @@
 				|| ("https".equals(scheme) && port != 443)) {
 			sb.append(":" + port);
 		}
-		sb.append(request.getContextPath());
+		sb.append(context);
 		return sb.toString();
 	}
 }

--
Gitblit v1.9.1