-
Notifications
You must be signed in to change notification settings - Fork 19
/
http.go
246 lines (198 loc) · 6.17 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package utils
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"strings"
"sync"
"time"
)
// RequestResponseStatus represents the status of a WebhookRequeset
type RequestResponseStatus string
const (
// RRSuccess represents that the webhook was successful
RRSuccess RequestResponseStatus = "success"
// RRConnectionError represents that the webhook had a connection error
RRConnectionError RequestResponseStatus = "connection_error"
// RRResponseError represents that the webhook response had a non 2xx status code
RRResponseError RequestResponseStatus = "response_error"
)
func (r RequestResponseStatus) String() string {
return string(r)
}
// RequestResponse represents both the outgoing request and response for a particular URL/method/body
type RequestResponse struct {
url string
status RequestResponseStatus
statusCode int
request string
response string
body string
}
// MakeHTTPRequest fires the passed in http request, returning any errors encountered. RequestResponse is always set
// regardless of any errors being set
func MakeHTTPRequest(req *http.Request) (*RequestResponse, error) {
requestTrace, err := httputil.DumpRequestOut(req, true)
if err != nil {
rr, _ := newRRFromRequestAndError(req, string(requestTrace), err)
return rr, err
}
resp, err := getClient().Do(req)
if err != nil {
rr, _ := newRRFromRequestAndError(req, string(requestTrace), err)
return rr, err
}
defer resp.Body.Close()
rr, err := newRRFromResponse(string(requestTrace), resp)
return rr, err
}
func (r *RequestResponse) URL() string { return r.url }
func (r *RequestResponse) Status() RequestResponseStatus { return r.status }
func (r *RequestResponse) StatusCode() int { return r.statusCode }
func (r *RequestResponse) Request() string { return r.request }
func (r *RequestResponse) Response() string { return r.response }
func (r *RequestResponse) Body() string { return r.body }
func (r *RequestResponse) JSON() JSONFragment { return JSONFragment([]byte(r.body)) }
func (r *RequestResponse) Resolve(key string) interface{} {
switch key {
case "body":
return r.Body()
case "json":
return r.JSON()
case "url":
return r.URL()
case "request":
return r.Request()
case "response":
return r.Response()
case "status":
return r.Status()
case "status_code":
return r.StatusCode()
}
return fmt.Errorf("No field '%s' on webhook", key)
}
func (r *RequestResponse) Default() interface{} {
return r
}
func (r *RequestResponse) String() string {
return r.body
}
var _ VariableResolver = (*RequestResponse)(nil)
// newRRFromResponse creates a new RequestResponse based on the passed in http request and error (when we received no response)
func newRRFromRequestAndError(r *http.Request, requestTrace string, requestError error) (*RequestResponse, error) {
rr := RequestResponse{}
rr.url = r.URL.String()
rr.request = requestTrace
rr.status = RRConnectionError
rr.body = requestError.Error()
return &rr, nil
}
// newRRFromResponse creates a new RequestResponse based on the passed in http Response
func newRRFromResponse(requestTrace string, r *http.Response) (*RequestResponse, error) {
var err error
rr := RequestResponse{}
rr.url = r.Request.URL.String()
rr.statusCode = r.StatusCode
// set our status based on our status code
if rr.statusCode/100 == 2 {
rr.status = RRSuccess
} else {
rr.status = RRResponseError
}
rr.request = requestTrace
// figure out if our Response is something that looks like text from our headers
isText := false
contentType := r.Header.Get("Content-Type")
if strings.Contains(contentType, "text") ||
strings.Contains(contentType, "json") ||
strings.Contains(contentType, "utf") ||
strings.Contains(contentType, "javascript") ||
strings.Contains(contentType, "xml") {
isText = true
}
// only dump the whole body if this looks like text
response, err := httputil.DumpResponse(r, isText)
if err != nil {
return &rr, err
}
rr.response = string(response)
if isText {
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
return &rr, err
}
rr.body = strings.TrimSpace(string(bodyBytes))
} else {
// no body for non-text responses but add it to our Response log so users know why
rr.response = rr.response + "\nNon-text body, ignoring"
}
return &rr, nil
}
var (
transport *http.Transport
client *http.Client
once sync.Once
)
func getClient() *http.Client {
once.Do(func() {
timeout := time.Duration(15 * time.Second)
transport = &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
}
client = &http.Client{Transport: transport, Timeout: timeout}
})
return client
}
func getInsecureClient() *http.Client {
once.Do(func() {
timeout := time.Duration(15 * time.Second)
transport = &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client = &http.Client{Transport: transport, Timeout: timeout}
})
return client
}
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type rrEnvelope struct {
URL string `json:"url"`
Status RequestResponseStatus `json:"status"`
StatusCode int `json:"status_code"`
Body string `json:"body"`
Request string `json:"request"`
Response string `json:"response"`
}
func (r *RequestResponse) UnmarshalJSON(data []byte) error {
var envelope rrEnvelope
var err error
err = json.Unmarshal(data, &envelope)
if err != nil {
return err
}
r.url = envelope.URL
r.status = envelope.Status
r.statusCode = envelope.StatusCode
r.request = envelope.Request
r.response = envelope.Response
r.body = envelope.Body
return nil
}
func (r *RequestResponse) MarshalJSON() ([]byte, error) {
var re rrEnvelope
re.URL = r.url
re.Status = r.status
re.StatusCode = r.statusCode
re.Request = r.request
re.Response = r.response
re.Body = r.body
return json.Marshal(re)
}