Skip to content

Commit

Permalink
[feature] Support X-Forwarded-Host (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
elithrar authored Jul 17, 2016
2 parents 66e6c6f + d3ca879 commit af3022d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 5 additions & 1 deletion proxy_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var (
xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
xRealIP = http.CanonicalHeaderKey("X-Real-IP")
xForwardedProto = http.CanonicalHeaderKey("X-Forwarded-Scheme")
xForwardedHost = http.CanonicalHeaderKey("X-Forwarded-Host")
)

var (
Expand Down Expand Up @@ -49,7 +50,10 @@ func ProxyHeaders(h http.Handler) http.Handler {
if scheme := getScheme(r); scheme != "" {
r.URL.Scheme = scheme
}

// Set the host with the value passed by the proxy
if r.Header.Get(xForwardedHost) != "" {
r.Host = r.Header.Get(xForwardedHost)
}
// Call the next handler in the chain.
h.ServeHTTP(w, r)
}
Expand Down
14 changes: 11 additions & 3 deletions proxy_headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,17 @@ func TestProxyHeaders(t *testing.T) {

r.Header.Set(xForwardedFor, "8.8.8.8")
r.Header.Set(xForwardedProto, "https")

var addr string
var proto string
r.Header.Set(xForwardedHost, "google.com")
var (
addr string
proto string
host string
)
ProxyHeaders(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
addr = r.RemoteAddr
proto = r.URL.Scheme
host = r.Host
})).ServeHTTP(rr, r)

if rr.Code != http.StatusOK {
Expand All @@ -96,5 +100,9 @@ func TestProxyHeaders(t *testing.T) {
t.Fatalf("wrong address: got %s want %s", proto,
r.Header.Get(xForwardedProto))
}
if host != r.Header.Get(xForwardedHost) {
t.Fatalf("wrong address: got %s want %s", host,
r.Header.Get(xForwardedHost))
}

}

0 comments on commit af3022d

Please sign in to comment.