Skip to content

Commit

Permalink
heartbeat: setup default ports in http monitors
Browse files Browse the repository at this point in the history
Default http to 80 and https to 443

Resolves #3915
  • Loading branch information
Giuseppe Valente committed Apr 5, 2017
1 parent 30d9baa commit c888f3f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
14 changes: 11 additions & 3 deletions heartbeat/monitors/active/http/simple_transp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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
Expand All @@ -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")
}

Expand Down Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions heartbeat/monitors/active/http/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c888f3f

Please sign in to comment.