From 9306452456467b5a0059a47e9238051f1451f507 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Tue, 6 Jun 2017 17:10:09 +0200 Subject: [PATCH] Issue #304: Add X-Forwarded-Prefix header This patch adds the X-Forwarded-Prefix header to upstream requests which contains the unmodified url path from the original request. Fixes #304 --- proxy/http_headers.go | 4 ++++ proxy/http_headers_test.go | 12 +++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/proxy/http_headers.go b/proxy/http_headers.go index 003bdf2e4..44c612592 100644 --- a/proxy/http_headers.go +++ b/proxy/http_headers.go @@ -66,6 +66,10 @@ func addHeaders(r *http.Request, cfg config.Proxy) error { r.Header.Set("X-Forwarded-Port", localPort(r)) } + if r.URL != nil && r.Header.Get("X-Forwarded-Prefix") == "" { + r.Header.Set("X-Forwarded-Prefix", r.URL.Path) + } + fwd := r.Header.Get("Forwarded") if fwd == "" { fwd = "for=" + remoteIP + "; proto=" + proto diff --git a/proxy/http_headers_test.go b/proxy/http_headers_test.go index dd2fc2876..e9fe9546a 100644 --- a/proxy/http_headers_test.go +++ b/proxy/http_headers_test.go @@ -3,6 +3,7 @@ package proxy import ( "crypto/tls" "net/http" + "net/url" "testing" "github.com/fabiolb/fabio/config" @@ -25,13 +26,14 @@ func TestAddHeaders(t *testing.T) { }, {"http request", - &http.Request{RemoteAddr: "1.2.3.4:5555"}, + &http.Request{URL: &url.URL{Path: "/foo"}, RemoteAddr: "1.2.3.4:5555"}, config.Proxy{}, http.Header{ - "Forwarded": []string{"for=1.2.3.4; proto=http"}, - "X-Forwarded-Proto": []string{"http"}, - "X-Forwarded-Port": []string{"80"}, - "X-Real-Ip": []string{"1.2.3.4"}, + "Forwarded": []string{"for=1.2.3.4; proto=http"}, + "X-Forwarded-Proto": []string{"http"}, + "X-Forwarded-Port": []string{"80"}, + "X-Forwarded-Prefix": []string{"/foo"}, + "X-Real-Ip": []string{"1.2.3.4"}, }, "", },