From f176c28a568606dd5f81bf0c683daff6b885c039 Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Thu, 19 May 2016 12:08:25 +0100 Subject: [PATCH] http_response: override req.Host header properly closes #1198 --- CHANGELOG.md | 1 + plugins/inputs/http_response/http_response.go | 18 ++++---- .../http_response/http_response_test.go | 42 ++++++++++++------- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c4088dd5..46b46a5d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - [#1195](https://github.com/influxdata/telegraf/pull/1195): Docker panic on timeout. Thanks @zstyblik! - [#1211](https://github.com/influxdata/telegraf/pull/1211): mongodb input. Fix possible panic. Thanks @kols! - [#1228](https://github.com/influxdata/telegraf/pull/1228): Fix service plugin host tag overwrite. +- [#1198](https://github.com/influxdata/telegraf/pull/1198): http_response: override request Host header properly ## v0.13 [2016-05-11] diff --git a/plugins/inputs/http_response/http_response.go b/plugins/inputs/http_response/http_response.go index 69c8fcd06..4cdd253fc 100644 --- a/plugins/inputs/http_response/http_response.go +++ b/plugins/inputs/http_response/http_response.go @@ -68,16 +68,6 @@ func CreateHttpClient(followRedirects bool, ResponseTimeout time.Duration) *http return client } -// CreateHeaders takes a map of header strings and puts them -// into a http.Header Object -func CreateHeaders(headers map[string]string) http.Header { - httpHeaders := make(http.Header) - for key := range headers { - httpHeaders.Add(key, headers[key]) - } - return httpHeaders -} - // HTTPGather gathers all fields and returns any errors it encounters func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) { // Prepare fields @@ -93,7 +83,13 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) { if err != nil { return nil, err } - request.Header = CreateHeaders(h.Headers) + + for key, val := range h.Headers { + request.Header.Add(key, val) + if key == "Host" { + request.Host = val + } + } // Start Timer start := time.Now() diff --git a/plugins/inputs/http_response/http_response_test.go b/plugins/inputs/http_response/http_response_test.go index acdfeac75..c9027f049 100644 --- a/plugins/inputs/http_response/http_response_test.go +++ b/plugins/inputs/http_response/http_response_test.go @@ -11,20 +11,6 @@ import ( "time" ) -func TestCreateHeaders(t *testing.T) { - fakeHeaders := map[string]string{ - "Accept": "text/plain", - "Content-Type": "application/json", - "Cache-Control": "no-cache", - } - headers := CreateHeaders(fakeHeaders) - testHeaders := make(http.Header) - testHeaders.Add("Accept", "text/plain") - testHeaders.Add("Content-Type", "application/json") - testHeaders.Add("Cache-Control", "no-cache") - assert.Equal(t, testHeaders, headers) -} - func setUpTestMux() http.Handler { mux := http.NewServeMux() mux.HandleFunc("/redirect", func(w http.ResponseWriter, req *http.Request) { @@ -63,6 +49,33 @@ func setUpTestMux() http.Handler { return mux } +func TestHeaders(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + cHeader := r.Header.Get("Content-Type") + assert.Equal(t, "Hello", r.Host) + assert.Equal(t, "application/json", cHeader) + w.WriteHeader(http.StatusOK) + })) + defer ts.Close() + + h := &HTTPResponse{ + Address: ts.URL, + Method: "GET", + ResponseTimeout: 2, + Headers: map[string]string{ + "Content-Type": "application/json", + "Host": "Hello", + }, + } + fields, err := h.HTTPGather() + require.NoError(t, err) + assert.NotEmpty(t, fields) + if assert.NotNil(t, fields["http_response_code"]) { + assert.Equal(t, http.StatusOK, fields["http_response_code"]) + } + assert.NotNil(t, fields["response_time"]) +} + func TestFields(t *testing.T) { mux := setUpTestMux() ts := httptest.NewServer(mux) @@ -85,7 +98,6 @@ func TestFields(t *testing.T) { assert.Equal(t, http.StatusOK, fields["http_response_code"]) } assert.NotNil(t, fields["response_time"]) - } func TestRedirects(t *testing.T) {