Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

heartbeat: setup default ports in http monitors #3924

Merged
merged 3 commits into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ https://github.com/elastic/beats/compare/v5.1.1...master[Check the HEAD diff]
- Allow `-` in Apache access log byte count. {pull}3863[3863]

*Heartbeat*
- Add default ports in HTTP monitor. {pull}3924[3924]

*Metricbeat*

Expand Down
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
14 changes: 11 additions & 3 deletions heartbeat/monitors/active/http/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,24 @@ func execPing(
}

func splitHostnamePort(requ *http.Request) (string, uint16, error) {
host, port, err := net.SplitHostPort(requ.URL.Host)
host := requ.URL.Host
// Try to add a default port if needed
if strings.LastIndex(host, ":") == -1 {
switch requ.URL.Scheme {
case urlSchemaHTTP:
host += ":80"
case urlSchemaHTTPS:
host += ":443"
}
}
host, port, err := net.SplitHostPort(host)
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)
}

return host, uint16(p), nil
}

Expand Down
93 changes: 93 additions & 0 deletions heartbeat/monitors/active/http/task_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package http

import (
"net"
"net/http"
"net/url"
"reflect"
"testing"
)

func TestSplitHostnamePort(t *testing.T) {
var urlTests = []struct {
scheme string
host string
expectedHost string
expectedPort uint16
expectedError error
}{
{
"http",
"foo",
"foo",
80,
nil,
},
{
"http",
"www.foo.com",
"www.foo.com",
80,
nil,
},
{
"http",
"www.foo.com:8080",
"www.foo.com",
8080,
nil,
},
{
"https",
"foo",
"foo",
443,
nil,
},
{
"http",
"foo:81",
"foo",
81,
nil,
},
{
"https",
"foo:444",
"foo",
444,
nil,
},
{
"httpz",
"foo",
"foo",
81,
&net.AddrError{},
},
}
for _, test := range urlTests {
url := &url.URL{
Scheme: test.scheme,
Host: test.host,
}
request := &http.Request{
URL: url,
}
host, port, err := splitHostnamePort(request)
if err != nil {
if test.expectedError == nil {
t.Error(err)
} else if reflect.TypeOf(err) != reflect.TypeOf(test.expectedError) {
t.Errorf("Expected %T but got %T", err, test.expectedError)
}
continue
}
if host != test.expectedHost {
t.Errorf("Unexpected host for %#v: expected %q, got %q", request, test.expectedHost, host)
}
if port != test.expectedPort {
t.Errorf("Unexpected port for %#v: expected %q, got %q", request, test.expectedPort, port)
}
}
}