diff --git a/heartbeat/monitors/active/http/simple_transp.go b/heartbeat/monitors/active/http/simple_transp.go index 87f970f2937..ca0a7d0fe34 100644 --- a/heartbeat/monitors/active/http/simple_transp.go +++ b/heartbeat/monitors/active/http/simple_transp.go @@ -14,6 +14,13 @@ import ( "github.com/elastic/beats/libbeat/outputs/transport" ) +const ( + gzipEncoding = "gzip" + urlSchemaHTTP = "http" + urlSchemaHTTPS = "https" +) + +// SimpleTransport contains the dialer and read/write callbacks type SimpleTransport struct { Dialer transport.Dialer DisableCompression bool @@ -32,7 +39,7 @@ func (t *SimpleTransport) checkRequest(req *http.Request) error { } scheme := req.URL.Scheme - isHTTP := scheme == "http" || scheme == "https" + isHTTP := scheme == urlSchemaHTTP || scheme == urlSchemaHTTPS if !isHTTP { return fmt.Errorf("http: unsupported scheme %v", scheme) } @@ -43,6 +50,7 @@ func (t *SimpleTransport) checkRequest(req *http.Request) error { return nil } +// RoundTrip sets up goroutines to write the request and read the responses func (t *SimpleTransport) RoundTrip(req *http.Request) (*http.Response, error) { type readReturn struct { resp *http.Response @@ -68,7 +76,7 @@ func (t *SimpleTransport) RoundTrip(req *http.Request) (*http.Response, error) { req.Method != "HEAD" { requestedGzip = true - req.Header.Add("Accept-Encoding", "gzip") + req.Header.Add("Accept-Encoding", gzipEncoding) defer req.Header.Del("Accept-Encoding") } @@ -132,7 +140,7 @@ func (t *SimpleTransport) readResponse( } t.sigStartRead() - if requestedGzip && resp.Header.Get("Content-Encoding") == "gzip" { + if requestedGzip && resp.Header.Get("Content-Encoding") == gzipEncoding { resp.Header.Del("Content-Encoding") resp.Header.Del("Content-Length") resp.ContentLength = -1 diff --git a/heartbeat/monitors/active/http/task.go b/heartbeat/monitors/active/http/task.go index b3dcd60484a..7af45242ee8 100644 --- a/heartbeat/monitors/active/http/task.go +++ b/heartbeat/monitors/active/http/task.go @@ -217,10 +217,19 @@ func splitHostnamePort(requ *http.Request) (string, uint16, error) { if err != nil { return "", 0, err } - - p, err := strconv.ParseUint(port, 10, 16) - if err != nil { - return "", 0, fmt.Errorf("'%v' is no valid port number in '%v'", port, requ.URL.Host) + var p uint64 + if port == "" { + switch requ.URL.Scheme { + case urlSchemaHTTP: + p = 80 + case urlSchemaHTTPS: + p = 443 + } + } else { + p, err = strconv.ParseUint(port, 10, 16) + if err != nil { + return "", 0, fmt.Errorf("'%v' is no valid port number in '%v'", port, requ.URL.Host) + } } return host, uint16(p), nil