Skip to content

Commit

Permalink
Correctly forward Host header in healthcheck
Browse files Browse the repository at this point in the history
Host header must be set explicitely on http requests

Change-Id: I91a32f0fb1ec3fbc713adf0e10869797e91172c7
Signed-off-by: Grégoire Seux <[email protected]>
  • Loading branch information
kamaradclimber committed Jun 29, 2017
1 parent d5d79d0 commit 074b4a9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions agent/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@ func (c *CheckHTTP) check() {
req.Header = make(http.Header)
}

if host := req.Header.Get("Host"); host != "" {
req.Host = host
}

if req.Header.Get("User-Agent") == "" {
req.Header.Set("User-Agent", UserAgent)
}
Expand Down
10 changes: 10 additions & 0 deletions agent/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ func TestCheckHTTP(t *testing.T) {

// custom header
{desc: "custom header", code: 200, header: http.Header{"A": []string{"b", "c"}}, status: api.HealthPassing},
{desc: "host header", code: 200, header: http.Header{"Host": []string{"a"}}, status: api.HealthPassing},
}

for _, tt := range tests {
Expand All @@ -236,6 +237,15 @@ func TestCheckHTTP(t *testing.T) {
for k, v := range tt.header {
expectedHeader[k] = v
}

// the Host header is in r.Host and not in the headers
host := expectedHeader.Get("Host")
if host != "" && host != r.Host {
w.WriteHeader(999)
return
}
expectedHeader.Del("Host")

if !reflect.DeepEqual(expectedHeader, r.Header) {
w.WriteHeader(999)
return
Expand Down

0 comments on commit 074b4a9

Please sign in to comment.