From 430854f6dec770817f0c4ec688df37f7bb6f470e Mon Sep 17 00:00:00 2001 From: Nicolas Filotto Date: Thu, 28 May 2020 20:05:57 +0200 Subject: [PATCH] Fix segmentation violation on connection failed (#7593) --- plugins/inputs/http_response/http_response.go | 16 +++++------ .../http_response/http_response_test.go | 27 +++++++++++++++++++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/plugins/inputs/http_response/http_response.go b/plugins/inputs/http_response/http_response.go index 8382d28ae..bc9452efc 100644 --- a/plugins/inputs/http_response/http_response.go +++ b/plugins/inputs/http_response/http_response.go @@ -271,14 +271,6 @@ func (h *HTTPResponse) httpGather(u string) (map[string]interface{}, map[string] resp, err := h.client.Do(request) response_time := time.Since(start).Seconds() - // Add the response headers - for headerName, tag := range h.HTTPHeaderTags { - headerValues, foundHeader := resp.Header[headerName] - if foundHeader && len(headerValues) > 0 { - tags[tag] = headerValues[0] - } - } - // If an error in returned, it means we are dealing with a network error, as // HTTP error codes do not generate errors in the net/http library if err != nil { @@ -306,6 +298,14 @@ func (h *HTTPResponse) httpGather(u string) (map[string]interface{}, map[string] // required by the net/http library defer resp.Body.Close() + // Add the response headers + for headerName, tag := range h.HTTPHeaderTags { + headerValues, foundHeader := resp.Header[headerName] + if foundHeader && len(headerValues) > 0 { + tags[tag] = headerValues[0] + } + } + // Set log the HTTP response code tags["status_code"] = strconv.Itoa(resp.StatusCode) fields["http_response_code"] = resp.StatusCode diff --git a/plugins/inputs/http_response/http_response_test.go b/plugins/inputs/http_response/http_response_test.go index 4a92f805c..9986ddefc 100644 --- a/plugins/inputs/http_response/http_response_test.go +++ b/plugins/inputs/http_response/http_response_test.go @@ -287,6 +287,33 @@ func TestHTTPHeaderTags(t *testing.T) { "result": "success", } checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil) + + // Connection failed + h = &HTTPResponse{ + Log: testutil.Logger{}, + Address: "https:/nonexistent.nonexistent", // Any non-routable IP works here + Body: "", + Method: "GET", + ResponseTimeout: internal.Duration{Duration: time.Second * 5}, + HTTPHeaderTags: map[string]string{"Server": "my_server", "Content-Type": "content_type"}, + FollowRedirects: false, + } + + acc = testutil.Accumulator{} + err = h.Gather(&acc) + require.NoError(t, err) + + expectedFields = map[string]interface{}{ + "result_type": "connection_failed", + "result_code": 3, + } + expectedTags = map[string]interface{}{ + "server": nil, + "method": "GET", + "result": "connection_failed", + } + absentFields = []string{"http_response_code", "response_time", "content_length", "response_string_match"} + checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil) } func findInterface() (net.Interface, error) {