Skip to content

Commit

Permalink
Issue #294: Use upstream hostname for request
Browse files Browse the repository at this point in the history
WIP WIP WIP WIP WIP WIP WIP WIP WIP WIP WIP WIP
-----------------------------------------------

This patch is work in progress and demonstrates the
desired behavior and adds a test to verify it. The
feature needs to be made configurable per route.

Fabio does not modify the Host header when forwarding
the request to the upstream server. This patch enables
a mode where for a specific route fabio will use the
hostname of the upstream server as the host header.

-----------------------------------------------
WIP WIP WIP WIP WIP WIP WIP WIP WIP WIP WIP WIP

Fixes #294
  • Loading branch information
magiconair committed May 18, 2017
1 parent b90b44c commit d3e73f9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
28 changes: 28 additions & 0 deletions proxy/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,34 @@ func TestProxyStripsPath(t *testing.T) {
}
}

func TestProxyUseUpstreamHostname(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, r.Host)
}))

proxy := httptest.NewServer(&HTTPProxy{
Transport: &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
addr = server.URL[len("http://"):]
return net.Dial(network, addr)
},
},
Lookup: func(r *http.Request) *route.Target {
tbl, _ := route.NewTable("route add mock / http://a.com/")
return tbl.Lookup(r, "", route.Picker["rr"], route.Matcher["prefix"])
},
})
defer proxy.Close()

resp, body := mustGet(proxy.URL + "/")
if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Fatalf("got status %d want %d", got, want)
}
if got, want := string(body), "a.com"; got != want {
t.Fatalf("got body %q want %q", got, want)
}
}

func TestProxyLogOutput(t *testing.T) {
// build a format string from all log fields and one header field
fields := []string{"header.X-Foo:$header.X-Foo"}
Expand Down
1 change: 1 addition & 0 deletions proxy/http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (p *HTTPProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else {
targetURL.RawQuery = t.URL.RawQuery + "&" + r.URL.RawQuery
}
r.Host = targetURL.Host

// TODO(fs): The HasPrefix check seems redundant since the lookup function should
// TODO(fs): have found the target based on the prefix but there may be other
Expand Down

0 comments on commit d3e73f9

Please sign in to comment.