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

Can't set Host header #38

Closed
grobie opened this issue Jan 16, 2014 · 1 comment
Closed

Can't set Host header #38

grobie opened this issue Jan 16, 2014 · 1 comment

Comments

@grobie
Copy link

grobie commented Jan 16, 2014

golang's net/http library seems to ignore a Host header and only uses the value set in request struct or extracts it from url.Host. In order to be able to set a flag like -header="Host: foo.bar" it's necessary to treat that that header different than others.

This works for me:

diff --git a/lib/targets.go b/lib/targets.go
index 9b9739a..b3d70b2 100644
--- a/lib/targets.go
+++ b/lib/targets.go
@@ -63,8 +63,12 @@ func (t Targets) SetHeader(header http.Header) {
        for _, target := range t {
                target.Header = make(http.Header, len(header))
                for k, vs := range header {
-                       target.Header[k] = make([]string, len(vs))
-                       copy(target.Header[k], vs)
+                       if k == "Host" {
+                               target.Host = vs[0]
+                       } else {
+                               target.Header[k] = make([]string, len(vs))
+                               copy(target.Header[k], vs)
+                       }
                }
        }
 }

No idea why the copy of the headers is necessary, it might be necessary for the Host field as well then.

@tsenart
Copy link
Owner

tsenart commented Jan 16, 2014

We can't set the Host header differently that what the request URL is because that wouldn't be compliant to the HTTP spec. You can check the code that prevents this here:
http://golang.org/src/pkg/net/http/request.go#L61
http://golang.org/src/pkg/net/http/request.go#L330
http://golang.org/src/pkg/net/http/request.go#L385

I agree that this is very useful in certain situations. Please open a PR with your change set and test it if you want brevity on this issue. Otherwise I can do it too, but I can't promise it will be very soon.

The copy of the headers is necessary because the net/http pkg mutates the headers underneath for various reasons (like setting auth headers from the url info, the host header, etc). You can check out this old issue for further reference: #30

tsenart added a commit that referenced this issue Jan 22, 2014
Added special handling of Host header + test. Fixes #38
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

No branches or pull requests

2 participants