forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
106 lines (87 loc) · 2.22 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package http
import (
"bytes"
"net/http"
"net/url"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs"
"github.com/elastic/beats/libbeat/outputs/transport"
"github.com/elastic/beats/heartbeat/monitors"
)
func init() {
monitors.RegisterActive("http", create)
}
var debugf = logp.MakeDebug("http")
func create(
info monitors.Info,
cfg *common.Config,
) ([]monitors.Job, error) {
config := defaultConfig
if err := cfg.Unpack(&config); err != nil {
return nil, err
}
tls, err := outputs.LoadTLSConfig(config.TLS)
if err != nil {
return nil, err
}
var body []byte
var enc contentEncoder
if config.Check.Request.SendBody != "" {
var err error
compression := config.Check.Request.Compression
enc, err = getContentEncoder(compression.Type, compression.Level)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(nil)
err = enc.Encode(buf, bytes.NewBufferString(config.Check.Request.SendBody))
if err != nil {
return nil, err
}
body = buf.Bytes()
}
validator := makeValidateResponse(&config.Check.Response)
jobs := make([]monitors.Job, len(config.URLs))
if config.ProxyURL != "" {
transport, err := newRoundTripper(&config, tls)
if err != nil {
return nil, err
}
for i, url := range config.URLs {
jobs[i], err = newHTTPMonitorHostJob(url, &config, transport, enc, body, validator)
if err != nil {
return nil, err
}
}
} else {
for i, url := range config.URLs {
jobs[i], err = newHTTPMonitorIPsJob(&config, url, tls, enc, body, validator)
if err != nil {
return nil, err
}
}
}
return jobs, nil
}
func newRoundTripper(config *Config, tls *transport.TLSConfig) (*http.Transport, error) {
var proxy func(*http.Request) (*url.URL, error)
if config.ProxyURL != "" {
url, err := url.Parse(config.ProxyURL)
if err != nil {
return nil, err
}
proxy = http.ProxyURL(url)
}
dialer := transport.NetDialer(config.Timeout)
tlsDialer, err := transport.TLSDialer(dialer, tls, config.Timeout)
if err != nil {
return nil, err
}
return &http.Transport{
Proxy: proxy,
Dial: dialer.Dial,
DialTLS: tlsDialer.Dial,
DisableKeepAlives: true,
}, nil
}