Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing host header in http check #15337

Merged
merged 7 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
SamMousa marked this conversation as resolved.
Show resolved Hide resolved

request.Host = request.Header.Get("Host")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for future readers of this PR: this is safe without checking for it being empty because:

// For client requests, Host optionally overrides the Host
// header to send. If empty, the Request.Write method uses
// the value of URL.Host. Host may contain an international
// domain name.


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
}(),
},
Comment on lines +287 to +292
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still test sending in weird data although we don't expect our code to ever send us this data since everything is normalized at the edge.

{
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