forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
155 lines (128 loc) · 3.46 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package helper
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
"github.com/elastic/beats/libbeat/outputs"
"github.com/elastic/beats/libbeat/outputs/transport"
"github.com/elastic/beats/metricbeat/mb"
)
type HTTP struct {
base mb.BaseMetricSet
client *http.Client // HTTP client that is reused across requests.
headers map[string]string
uri string
method string
body []byte
}
// NewHTTP creates new http helper
func NewHTTP(base mb.BaseMetricSet) *HTTP {
config := struct {
TLS *outputs.TLSConfig `config:"ssl"`
Timeout time.Duration `config:"timeout"`
Headers map[string]string `config:"headers"`
}{}
if err := base.Module().UnpackConfig(&config); err != nil {
return nil
}
if config.Headers == nil {
config.Headers = map[string]string{}
}
tlsConfig, err := outputs.LoadTLSConfig(config.TLS)
if err != nil {
return nil
}
var dialer, tlsDialer transport.Dialer
dialer = transport.NetDialer(config.Timeout)
tlsDialer, err = transport.TLSDialer(dialer, tlsConfig, config.Timeout)
if err != nil {
return nil
}
return &HTTP{
base: base,
client: &http.Client{
Transport: &http.Transport{
Dial: dialer.Dial,
DialTLS: tlsDialer.Dial,
},
Timeout: config.Timeout,
},
headers: config.Headers,
method: "GET",
uri: base.HostData().SanitizedURI,
body: nil,
}
}
// FetchResponse fetches a response for the http metricset.
// It's important that resp.Body has to be closed if this method is used. Before using this method
// check if one of the other Fetch* methods could be used as they ensure that the Body is properly closed.
func (h *HTTP) FetchResponse() (*http.Response, error) {
// Create a fresh reader every time
var reader io.Reader
if h.body != nil {
reader = bytes.NewReader(h.body)
}
req, err := http.NewRequest(h.method, h.uri, reader)
if h.base.HostData().User != "" || h.base.HostData().Password != "" {
req.SetBasicAuth(h.base.HostData().User, h.base.HostData().Password)
}
for k, v := range h.headers {
req.Header.Set(k, v)
}
resp, err := h.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error making http request: %v", err)
}
return resp, nil
}
func (h *HTTP) SetHeader(key, value string) {
h.headers[key] = value
}
func (h *HTTP) SetMethod(method string) {
h.method = method
}
func (h *HTTP) SetURI(uri string) {
h.uri = uri
}
func (h *HTTP) SetBody(body []byte) {
h.body = body
}
// FetchContent makes an HTTP request to the configured url and returns the body content.
func (h *HTTP) FetchContent() ([]byte, error) {
resp, err := h.FetchResponse()
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("HTTP error %d in %s: %s", resp.StatusCode, h.base.Name(), resp.Status)
}
return ioutil.ReadAll(resp.Body)
}
// FetchScanner returns a Scanner for the content.
func (h *HTTP) FetchScanner() (*bufio.Scanner, error) {
content, err := h.FetchContent()
if err != nil {
return nil, err
}
return bufio.NewScanner(bytes.NewReader(content)), nil
}
// FetchJSON makes an HTTP request to the configured url and returns the JSON content.
// This only works if the JSON output needed is in map[string]interface format.
func (h *HTTP) FetchJSON() (map[string]interface{}, error) {
body, err := h.FetchContent()
if err != nil {
return nil, err
}
var data map[string]interface{}
err = json.Unmarshal(body, &data)
if err != nil {
return nil, err
}
return data, nil
}