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

Correctly forward Host header in healthcheck #3203

Merged
merged 1 commit into from
Jun 29, 2017

Conversation

kamaradclimber
Copy link
Contributor

Host header must be set explicitely on http requests

I'll write test a bit later

@kamaradclimber kamaradclimber force-pushed the hostheader branch 2 times, most recently from f4bf066 to af42c1c Compare June 28, 2017 17:29
@@ -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{"customhost"}}, status: api.HealthPassing},
Copy link
Contributor

Choose a reason for hiding this comment

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

s/customhost/a/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

agent/check.go Outdated
@@ -386,6 +386,10 @@ func (c *CheckHTTP) check() {
req.Header = make(http.Header)
}

if host, ok := c.Header["Host"]; ok && len(host) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

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

@@ -234,7 +235,9 @@ func TestCheckHTTP(t *testing.T) {
"User-Agent": []string{"Consul Health Check"},
}
for k, v := range tt.header {
expectedHeader[k] = v
if k != "Host" {
Copy link
Contributor

Choose a reason for hiding this comment

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

This only tests that the Host header is not part of the Header object but it does not test whether the custom value was actually set. I think for this you have to compare the r.Host with the custom host header. Maybe like this:

for k, v := range tt.header {
    expectedHeader[k] = v
}

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks!

@kamaradclimber
Copy link
Contributor Author

thanks @magiconair for your comments (you've simply rewritten my patch :) )

@@ -234,8 +235,17 @@ func TestCheckHTTP(t *testing.T) {
"User-Agent": []string{"Consul Health Check"},
}
for k, v := range tt.header {
expectedHeader[k] = v
expectedHeader[k] = v
Copy link
Contributor

Choose a reason for hiding this comment

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

pls run gofmt. This doesn't look right.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done thanks

Host header must be set explicitely on http requests

Change-Id: I91a32f0fb1ec3fbc713adf0e10869797e91172c7
Signed-off-by: Grégoire Seux <g.seux@criteo.com>
@magiconair
Copy link
Contributor

This LGTM. @slackpad do you want to have a quick peek?

@slackpad slackpad merged commit 940b151 into hashicorp:master Jun 29, 2017
@slackpad
Copy link
Contributor

Looks good - thanks!

@mbrulatout mbrulatout deleted the hostheader branch May 17, 2023 07:52
@mbrulatout mbrulatout restored the hostheader branch May 17, 2023 07:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants