-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
net/http: Setting custom "Host" request header doesn't have effect #7682
Labels
Milestone
Comments
Well, I expected `Header.Add` to modify the "Host" header of the request. From the source code it seems that Go ignores this header setting by design, and uses `req.Host`. This is surprising behavior and would continue to be surprising even if it was better documented. To be clear: there are two separate hosts we are specifying. The first is the actual host that the connection is being made to: "127.0.0.1:9292". (This is specified with the full URL of the request.) The second is what we want this server to receive as the `Host` header value. Usually these values are the same, but there are use-cases when you want to specify a custom `Host` header. It's also surprising that changing `req.Host` actually does the trick. I would expect that to change the actual host we're connecting to, not just the header value. So the current behavior is backwards from my perspective. |
This is the Go example to reproduce this: http://play.golang.org/p/5I0i3S_szO |
I'm using a simple http debug proxy like this and am still seeing no `Host` header: https://github.com/jingweno/hdp/blob/master/main.go |
Looks like `Host` doesn't show us in `Request.Header` but is parsed into `Request.Host`. A modification of the above Go example makes it work: http://play.golang.org/p/DEFLb7_WRJ. `Header.Add` still doesn't correctly set the header: http://play.golang.org/p/Kqh7sgX-z1 |
This is documented at http://tip.golang.org/pkg/net/http/#Request.Write I'll add a note to reference that on the Request struct. Owner changed to @bradfitz. Status changed to Accepted. |
I was aware that this behavior is kind of documented (see "Relevant code" link in my original post). The reason I opened this issue, if it isn't clear by now, is twofold: 1. Why isn't `Header.Add` allowed to modify the "Host" header? It's a regular HTTP header like any other, so I've epxected it to work. Why doesn't it at least throw a runtime exception? 2. Setting `req.Host = "example.com"` is ambiguous. It's not clear whether this changes the actual hostname the connection is opened to, or just the "Host" header. Experimentation shows it's the latter, but that wasn't clear to me prior to experimentation. I'm proposing the following fixes: 1. Allow `Header.Add("Host"...)` to change the "Host" header. 2. Make it clear in Request.Host documentation that setting this value only changes the "Host" header but won't change the actual hostname/IP the connection will be opened to. |
Sent https://golang.org/cl/83800043 I don't think we can safely change the behavior at this point. Status changed to Started. |
This issue was closed by revision 9dbb185. Status changed to Fixed. |
This was referenced May 23, 2016
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
by mislav.marohnic:
The text was updated successfully, but these errors were encountered: