forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
107 lines (86 loc) · 2.66 KB
/
config.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
107
package http
import (
"fmt"
"strings"
"time"
"github.com/elastic/beats/libbeat/outputs"
"github.com/elastic/beats/heartbeat/monitors"
)
type Config struct {
Name string `config:"name"`
URLs []string `config:"urls" validate:"required"`
ProxyURL string `config:"proxy_url"`
Timeout time.Duration `config:"timeout"`
MaxRedirects int `config:"max_redirects"`
Mode monitors.IPSettings `config:",inline"`
// authentication
Username string `config:"username"`
Password string `config:"password"`
// configure tls (if not configured HTTPS will use system defaults)
TLS *outputs.TLSConfig `config:"ssl"`
// http(s) ping validation
Check checkConfig `config:"check"`
}
type checkConfig struct {
Request requestParameters `config:"request"`
Response responseParameters `config:"response"`
}
type requestParameters struct {
// HTTP request configuration
Method string `config:"method"` // http request method
SendHeaders map[string]string `config:"headers"` // http request headers
SendBody string `config:"body"` // send body payload
Compression compressionConfig `config:"compression"` // optionally compress payload
// TODO:
// - add support for cookies
// - select HTTP version. golang lib will either use 1.1 or 2.0 if HTTPS is used, otherwise HTTP 1.1 . => implement/use specific http.RoundTripper implementation to change wire protocol/version being used
}
type responseParameters struct {
// expected HTTP response configuration
Status uint16 `config:"status" verify:"min=0, max=699"`
RecvHeaders map[string]string `config:"headers"`
RecvBody string `config:"body"`
}
type compressionConfig struct {
Type string `config:"type"`
Level int `config:"level"`
}
var defaultConfig = Config{
Name: "http",
Timeout: 16 * time.Second,
MaxRedirects: 10,
Mode: monitors.DefaultIPSettings,
Check: checkConfig{
Request: requestParameters{
Method: "GET",
SendHeaders: nil,
SendBody: "",
},
Response: responseParameters{
Status: 0,
RecvHeaders: nil,
RecvBody: "",
},
},
}
func (r *requestParameters) Validate() error {
switch strings.ToUpper(r.Method) {
case "HEAD", "GET", "POST":
default:
return fmt.Errorf("HTTP method '%v' not supported", r.Method)
}
return nil
}
func (c *compressionConfig) Validate() error {
t := strings.ToLower(c.Type)
if t != "" && t != "gzip" {
return fmt.Errorf("compression type '%v' not supported", c.Type)
}
if t == "" {
return nil
}
if !(0 <= c.Level && c.Level <= 9) {
return fmt.Errorf("compression level %v invalid", c.Level)
}
return nil
}