Fix http requests to actually use the resolved ip
authorBrian Brazil <brian.brazil@robustperception.io>
Tue, 5 Sep 2017 12:45:58 +0000 (13:45 +0100)
committerBrian Brazil <brian.brazil@robustperception.io>
Tue, 12 Sep 2017 11:01:48 +0000 (12:01 +0100)
prober/http.go

index d03566061ec96c5254ab396e26356f875493961f..dcd81e0f7354db7a51f1233fae5a6e7e68965bfb 100644 (file)
@@ -156,15 +156,16 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
                httpConfig.Method = "GET"
        }
 
-       request, err := http.NewRequest(httpConfig.Method, target, nil)
-       request.Host = targetURL.Host
-       request = request.WithContext(ctx)
+       // Replace the host field in the URL with the IP we resolved.
+       origHost := targetURL.Host
        if targetPort == "" {
-               targetURL.Host = ip.String()
+               targetURL.Host = "[" + ip.String() + "]"
        } else {
                targetURL.Host = net.JoinHostPort(ip.String(), targetPort)
        }
-
+       request, err := http.NewRequest(httpConfig.Method, targetURL.String(), nil)
+       request.Host = origHost
+       request = request.WithContext(ctx)
        if err != nil {
                level.Error(logger).Log("msg", "Error creating request", "err", err)
                return
@@ -182,7 +183,7 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
        if httpConfig.Body != "" {
                request.Body = ioutil.NopCloser(strings.NewReader(httpConfig.Body))
        }
-       level.Info(logger).Log("msg", "Making HTTP request", "url", request.URL.String())
+       level.Info(logger).Log("msg", "Making HTTP request", "url", request.URL.String(), "host", request.Host)
        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 {