From 9e2f7feec7f4a5a268b106f451430ed73e8fb0f6 Mon Sep 17 00:00:00 2001 From: Mat Evans Date: Thu, 31 Jul 2014 11:40:40 +0100 Subject: [PATCH 1/2] gor issue 74 explicitly set the Host on the request when using the --output-http-header option --- output_http.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/output_http.go b/output_http.go index bd8df64..2cc4a87 100644 --- a/output_http.go +++ b/output_http.go @@ -131,7 +131,14 @@ func (o *HTTPOutput) sendRequest(client *http.Client, data []byte) { request.URL, _ = url.ParseRequestURI(URL) for _, header := range o.headers { - request.Header.Set(header.Name, header.Value) + // Need to check here for the Host header as it needs to be set on the request and not as a separate header + // http.ReadRequest sets it by default to the URL Host of the request being read + if header.Name == "Host" { + request.Host = header.Value + } else { + request.Header.Set(header.Name, header.Value) + } + } resp, err := client.Do(request) From defc017ade56484a02e2a2f503ee9d745f51ee86 Mon Sep 17 00:00:00 2001 From: Mat Evans Date: Thu, 7 Aug 2014 10:22:04 +0100 Subject: [PATCH 2/2] Add a testable SetHeader function with associated tests It seemed easier to pull this bit of functionality out and be able to test it on it's own than rely on the whole http tests --- output_http.go | 23 +++++++++++++++-------- output_http_test.go | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/output_http.go b/output_http.go index 2cc4a87..fc7aa75 100644 --- a/output_http.go +++ b/output_http.go @@ -131,14 +131,7 @@ func (o *HTTPOutput) sendRequest(client *http.Client, data []byte) { request.URL, _ = url.ParseRequestURI(URL) for _, header := range o.headers { - // Need to check here for the Host header as it needs to be set on the request and not as a separate header - // http.ReadRequest sets it by default to the URL Host of the request being read - if header.Name == "Host" { - request.Host = header.Value - } else { - request.Header.Set(header.Name, header.Value) - } - + SetHeader(request, header.Name, header.Value) } resp, err := client.Do(request) @@ -158,6 +151,20 @@ func (o *HTTPOutput) sendRequest(client *http.Client, data []byte) { } +func SetHeader(request *http.Request, name string, value string) { + + // Need to check here for the Host header as it needs to be set on the request and not as a separate header + // http.ReadRequest sets it by default to the URL Host of the request being read + if name == "Host" { + request.Host = value + } else { + request.Header.Set(name, value) + } + + return + +} + func (o *HTTPOutput) String() string { return "HTTP output: " + o.address } diff --git a/output_http_test.go b/output_http_test.go index a951d3f..42c8ed4 100644 --- a/output_http_test.go +++ b/output_http_test.go @@ -21,6 +21,27 @@ func startHTTP(cb func(*http.Request)) net.Listener { return listener } +func TestSetHeader(t *testing.T) { + + req := &http.Request{ + Header: make(map[string][]string), + } + req.Host = "test.com" + + SetHeader(req, "Host", "test2.com") + + if req.Host != "test2.com" { + t.Error("Expected test2.com - got ", req.Host) + } + + SetHeader(req, "test_header", "test_value") + + if req.Header.Get("test_header") != "test_value" { + t.Error("Wrong header value found") + } + +} + func TestHTTPOutput(t *testing.T) { wg := new(sync.WaitGroup) quit := make(chan int)