Skip to content

Commit

Permalink
Issue #304: Add X-Forwarded-Prefix header
Browse files Browse the repository at this point in the history
This patch adds the X-Forwarded-Prefix header to
upstream requests which contains the unmodified
url path from the original request.

Fixes #304
  • Loading branch information
magiconair committed Jun 6, 2017
1 parent 716c337 commit 9306452
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
4 changes: 4 additions & 0 deletions proxy/http_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions proxy/http_headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package proxy
import (
"crypto/tls"
"net/http"
"net/url"
"testing"

"github.com/fabiolb/fabio/config"
Expand All @@ -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"},
},
"",
},
Expand Down

0 comments on commit 9306452

Please sign in to comment.