Skip to content

Commit c888f3f

Browse files
author
Giuseppe Valente
committed
heartbeat: setup default ports in http monitors
Default http to 80 and https to 443 Resolves #3915
1 parent 30d9baa commit c888f3f

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

heartbeat/monitors/active/http/simple_transp.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ import (
1414
"github.com/elastic/beats/libbeat/outputs/transport"
1515
)
1616

17+
const (
18+
gzipEncoding = "gzip"
19+
urlSchemaHTTP = "http"
20+
urlSchemaHTTPS = "https"
21+
)
22+
23+
// SimpleTransport contains the dialer and read/write callbacks
1724
type SimpleTransport struct {
1825
Dialer transport.Dialer
1926
DisableCompression bool
@@ -32,7 +39,7 @@ func (t *SimpleTransport) checkRequest(req *http.Request) error {
3239
}
3340

3441
scheme := req.URL.Scheme
35-
isHTTP := scheme == "http" || scheme == "https"
42+
isHTTP := scheme == urlSchemaHTTP || scheme == urlSchemaHTTPS
3643
if !isHTTP {
3744
return fmt.Errorf("http: unsupported scheme %v", scheme)
3845
}
@@ -43,6 +50,7 @@ func (t *SimpleTransport) checkRequest(req *http.Request) error {
4350
return nil
4451
}
4552

53+
// RoundTrip sets up goroutines to write the request and read the responses
4654
func (t *SimpleTransport) RoundTrip(req *http.Request) (*http.Response, error) {
4755
type readReturn struct {
4856
resp *http.Response
@@ -68,7 +76,7 @@ func (t *SimpleTransport) RoundTrip(req *http.Request) (*http.Response, error) {
6876
req.Method != "HEAD" {
6977

7078
requestedGzip = true
71-
req.Header.Add("Accept-Encoding", "gzip")
79+
req.Header.Add("Accept-Encoding", gzipEncoding)
7280
defer req.Header.Del("Accept-Encoding")
7381
}
7482

@@ -132,7 +140,7 @@ func (t *SimpleTransport) readResponse(
132140
}
133141
t.sigStartRead()
134142

135-
if requestedGzip && resp.Header.Get("Content-Encoding") == "gzip" {
143+
if requestedGzip && resp.Header.Get("Content-Encoding") == gzipEncoding {
136144
resp.Header.Del("Content-Encoding")
137145
resp.Header.Del("Content-Length")
138146
resp.ContentLength = -1

heartbeat/monitors/active/http/task.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,19 @@ func splitHostnamePort(requ *http.Request) (string, uint16, error) {
217217
if err != nil {
218218
return "", 0, err
219219
}
220-
221-
p, err := strconv.ParseUint(port, 10, 16)
222-
if err != nil {
223-
return "", 0, fmt.Errorf("'%v' is no valid port number in '%v'", port, requ.URL.Host)
220+
var p uint64
221+
if port == "" {
222+
switch requ.URL.Scheme {
223+
case urlSchemaHTTP:
224+
p = 80
225+
case urlSchemaHTTPS:
226+
p = 443
227+
}
228+
} else {
229+
p, err = strconv.ParseUint(port, 10, 16)
230+
if err != nil {
231+
return "", 0, fmt.Errorf("'%v' is no valid port number in '%v'", port, requ.URL.Host)
232+
}
224233
}
225234

226235
return host, uint16(p), nil

0 commit comments

Comments
 (0)