Use default ports for the schemes when dialing (#7960)

Fixes #7946
This commit is contained in:
Praveen raj Mani 2019-08-13 16:43:11 +05:30 committed by Nitish Tiwari
parent aaef18b1a3
commit e96f19c867
3 changed files with 57 additions and 20 deletions

View File

@ -19,7 +19,6 @@ package target
import (
"context"
"fmt"
"net"
"net/url"
"os"
"path/filepath"
@ -83,8 +82,14 @@ func (target *ElasticsearchTarget) Save(eventData event.Event) error {
if target.store != nil {
return target.store.Put(eventData)
}
if _, err := net.Dial("tcp", target.args.URL.Host); err != nil {
return errNotConnected
if dErr := target.args.URL.DialHTTP(); dErr != nil {
if urlErr, ok := dErr.(*url.Error); ok {
// To treat "connection refused" errors as errNotConnected.
if IsConnRefusedErr(urlErr.Err) {
return errNotConnected
}
}
return dErr
}
return target.send(eventData)
}
@ -151,8 +156,14 @@ func (target *ElasticsearchTarget) Send(eventKey string) error {
}
}
if _, err := net.Dial("tcp", target.args.URL.Host); err != nil {
return errNotConnected
if dErr := target.args.URL.DialHTTP(); dErr != nil {
if urlErr, ok := dErr.(*url.Error); ok {
// To treat "connection refused" errors as errNotConnected.
if IsConnRefusedErr(urlErr.Err) {
return errNotConnected
}
}
return dErr
}
eventData, eErr := target.store.Get(eventKey)
@ -227,10 +238,10 @@ func NewElasticsearchTarget(id string, args ElasticsearchArgs, doneCh <-chan str
}
}
_, derr := net.Dial("tcp", args.URL.Host)
if derr != nil {
dErr := args.URL.DialHTTP()
if dErr != nil {
if store == nil {
return nil, derr
return nil, dErr
}
} else {
client, err = newClient(args)

View File

@ -82,15 +82,16 @@ func (target *WebhookTarget) Save(eventData event.Event) error {
if target.store != nil {
return target.store.Put(eventData)
}
urlStr, pErr := xnet.ParseURL(target.args.Endpoint.String())
u, pErr := xnet.ParseURL(target.args.Endpoint.String())
if pErr != nil {
return pErr
}
_, dErr := net.Dial("tcp", urlStr.Host)
if dErr != nil {
// To treat "connection refused" errors as errNotConnected.
if IsConnRefusedErr(dErr) {
return errNotConnected
if dErr := u.DialHTTP(); dErr != nil {
if urlErr, ok := dErr.(*url.Error); ok {
// To treat "connection refused" errors as errNotConnected.
if IsConnRefusedErr(urlErr.Err) {
return errNotConnected
}
}
return dErr
}
@ -136,15 +137,16 @@ func (target *WebhookTarget) send(eventData event.Event) error {
// Send - reads an event from store and sends it to webhook.
func (target *WebhookTarget) Send(eventKey string) error {
urlStr, pErr := xnet.ParseURL(target.args.Endpoint.String())
u, pErr := xnet.ParseURL(target.args.Endpoint.String())
if pErr != nil {
return pErr
}
_, dErr := net.Dial("tcp", urlStr.Host)
if dErr != nil {
// To treat "connection refused" errors as errNotConnected.
if IsConnRefusedErr(dErr) {
return errNotConnected
if dErr := u.DialHTTP(); dErr != nil {
if urlErr, ok := dErr.(*url.Error); ok {
// To treat "connection refused" errors as errNotConnected.
if IsConnRefusedErr(urlErr.Err) {
return errNotConnected
}
}
return dErr
}

View File

@ -19,9 +19,12 @@ package net
import (
"encoding/json"
"errors"
"net"
"net/http"
"net/url"
"path"
"strings"
"time"
)
// URL - improved JSON friendly url.URL.
@ -79,6 +82,27 @@ func (u *URL) UnmarshalJSON(data []byte) (err error) {
return nil
}
// DialHTTP - dials the url to check the connection.
func (u URL) DialHTTP() error {
var client = &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 2 * time.Second,
}).DialContext,
},
}
req, err := http.NewRequest("POST", u.String(), nil)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
resp.Body.Close()
return nil
}
// ParseURL - parses string into URL.
func ParseURL(s string) (u *URL, err error) {
var uu *url.URL