Skip to content

Commit

Permalink
Fix missing host header in http check (#15337)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamMousa committed Nov 23, 2022
1 parent 84b79aa commit 5a5c7f0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/15337.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
check: Add support for sending custom host header
```
9 changes: 8 additions & 1 deletion client/serviceregistration/checks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,14 @@ func (c *checker) checkHTTP(ctx context.Context, qc *QueryContext, q *Query) *st
qr.Status = structs.CheckFailure
return qr
}
request.Header = q.Headers
for header, values := range q.Headers {
for _, value := range values {
request.Header.Add(header, value)
}
}

request.Host = request.Header.Get("Host")

request.Body = io.NopCloser(strings.NewReader(q.Body))
request = request.WithContext(ctx)

Expand Down
39 changes: 38 additions & 1 deletion client/serviceregistration/checks/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,14 @@ func TestChecker_Do_HTTP_extras(t *testing.T) {
method string
body []byte
headers map[string][]string
host string
)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
method = r.Method
body, _ = io.ReadAll(r.Body)
headers = maps.Clone(r.Header)
host = r.Host
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
Expand All @@ -244,7 +246,7 @@ func TestChecker_Do_HTTP_extras(t *testing.T) {
name string
method string
body string
headers map[string][]string
headers http.Header
}{
{
name: "method GET",
Expand All @@ -269,6 +271,25 @@ func TestChecker_Do_HTTP_extras(t *testing.T) {
[2]string{"Authorization", "Basic ZWxhc3RpYzpjaGFuZ2VtZQ=="},
),
},
{
name: "host header",
method: "GET",
headers: makeHeaders(encoding, agent,
[2]string{"Host", "hello"},
[2]string{"Test-Abc", "hello"},
),
},
{
name: "host header without normalization",
method: "GET",
body: "",
// This is needed to prevent header normalization by http.Header.Set
headers: func() map[string][]string {
h := makeHeaders(encoding, agent, [2]string{"Test-Abc", "hello"})
h["hoST"] = []string{"heLLO"}
return h
}(),
},
{
name: "with body",
method: "POST",
Expand Down Expand Up @@ -312,9 +333,25 @@ func TestChecker_Do_HTTP_extras(t *testing.T) {
must.Eq(t, http.StatusOK, result.StatusCode,
must.Sprintf("test.URL: %s", ts.URL),
must.Sprintf("headers: %v", tc.headers),
must.Sprintf("received headers: %v", tc.headers),
)
must.Eq(t, tc.method, method)
must.Eq(t, tc.body, string(body))

hostSent := false

for key, values := range tc.headers {
if strings.EqualFold(key, "Host") && len(values) > 0 {
must.Eq(t, values[0], host)
hostSent = true
delete(tc.headers, key)

}
}
if !hostSent {
must.Eq(t, nil, tc.headers["Host"])
}

must.Eq(t, tc.headers, headers)
})
}
Expand Down

0 comments on commit 5a5c7f0

Please sign in to comment.