From: Christoph Hack Date: Thu, 31 Mar 2016 09:29:18 +0000 (+0200) Subject: configureable http headers for the http probe X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=894ca5c304f76e26e4c7a676b1279dc9cbe54840;p=blackbox_exporter.git configureable http headers for the http probe --- diff --git a/http.go b/http.go index 7253d5d..3f13fd5 100644 --- a/http.go +++ b/http.go @@ -53,9 +53,8 @@ func probeHTTP(target string, w http.ResponseWriter, module Module) (success boo redirects = len(via) if redirects > 10 || config.NoFollowRedirects { return errors.New("Don't follow redirects") - } else { - return nil } + return nil } if !strings.HasPrefix(target, "http://") && !strings.HasPrefix(target, "https://") { @@ -71,6 +70,14 @@ func probeHTTP(target string, w http.ResponseWriter, module Module) (success boo return } + for key, value := range config.Headers { + if strings.Title(key) == "Host" { + request.Host = value + continue + } + request.Header.Set(key, value) + } + resp, err := client.Do(request) // Err won't be nil if redirects were turned off. See https://github.com/golang/go/issues/3795 if err != nil && resp == nil { diff --git a/http_test.go b/http_test.go index ce1b21f..cddfe2f 100644 --- a/http_test.go +++ b/http_test.go @@ -238,3 +238,33 @@ func TestFailIfNotMatchesRegexp(t *testing.T) { t.Fatalf("Regexp test failed unexpectedly, got %s", body) } } + +func TestHTTPHeaders(t *testing.T) { + headers := map[string]string{ + "Host": "my-secret-vhost.com", + "User-Agent": "unsuspicious user", + "Accept-Language": "en-US", + } + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + for key, value := range headers { + if strings.Title(key) == "Host" { + if r.Host != value { + t.Errorf("Unexpected host: expected %q, got %q.", value, r.Host) + } + continue + } + if got := r.Header.Get(key); got != value { + t.Errorf("Unexpected value of header %q: expected %q, got %q", key, value, got) + } + } + w.WriteHeader(http.StatusOK) + })) + defer ts.Close() + recorder := httptest.NewRecorder() + result := probeHTTP(ts.URL, recorder, Module{Timeout: time.Second, HTTP: HTTPProbe{ + Headers: headers, + }}) + if !result { + t.Fatalf("Probe failed unexpectedly.") + } +} diff --git a/main.go b/main.go index b41678b..d22bcc1 100644 --- a/main.go +++ b/main.go @@ -31,13 +31,14 @@ type Module struct { type HTTPProbe struct { // Defaults to 2xx. - ValidStatusCodes []int `yaml:"valid_status_codes"` - NoFollowRedirects bool `yaml:"no_follow_redirects"` - FailIfSSL bool `yaml:"fail_if_ssl"` - FailIfNotSSL bool `yaml:"fail_if_not_ssl"` - Method string `yaml:"method"` - FailIfMatchesRegexp []string `yaml:"fail_if_matches_regexp"` - FailIfNotMatchesRegexp []string `yaml:"fail_if_not_matches_regexp"` + ValidStatusCodes []int `yaml:"valid_status_codes"` + NoFollowRedirects bool `yaml:"no_follow_redirects"` + FailIfSSL bool `yaml:"fail_if_ssl"` + FailIfNotSSL bool `yaml:"fail_if_not_ssl"` + Method string `yaml:"method"` + Headers map[string]string `yaml:"headers"` + FailIfMatchesRegexp []string `yaml:"fail_if_matches_regexp"` + FailIfNotMatchesRegexp []string `yaml:"fail_if_not_matches_regexp"` } type QueryResponse struct {