/
http.go
499 lines (435 loc) · 13.3 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
* Copyright (c) 2020-present unTill Pro, Ltd.
* @author Denis Gribanov
*/
package coreutils
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/voedger/voedger/pkg/goutils/logger"
"golang.org/x/exp/slices"
ibus "github.com/voedger/voedger/staging/src/github.com/untillpro/airs-ibus"
)
func NewHTTPErrorf(httpStatus int, args ...interface{}) SysError {
return SysError{
HTTPStatus: httpStatus,
Message: fmt.Sprint(args...),
}
}
func NewHTTPError(httpStatus int, err error) SysError {
return NewHTTPErrorf(httpStatus, err.Error())
}
func ReplyErrf(sender ibus.ISender, status int, args ...interface{}) {
ReplyErrDef(sender, NewHTTPErrorf(status, args...), http.StatusInternalServerError)
}
//nolint:errorlint
func ReplyErrDef(sender ibus.ISender, err error, defaultStatusCode int) {
res := WrapSysError(err, defaultStatusCode).(SysError)
ReplyJSON(sender, res.HTTPStatus, res.ToJSON())
}
func ReplyErr(sender ibus.ISender, err error) {
ReplyErrDef(sender, err, http.StatusInternalServerError)
}
func ReplyJSON(sender ibus.ISender, httpCode int, body string) {
sender.SendResponse(ibus.Response{
ContentType: ApplicationJSON,
StatusCode: httpCode,
Data: []byte(body),
})
}
func ReplyBadRequest(sender ibus.ISender, message string) {
ReplyErrf(sender, http.StatusBadRequest, message)
}
func replyAccessDenied(sender ibus.ISender, code int, message string) {
msg := "access denied"
if len(message) > 0 {
msg += ": " + message
}
ReplyErrf(sender, code, msg)
}
func ReplyAccessDeniedUnauthorized(sender ibus.ISender, message string) {
replyAccessDenied(sender, http.StatusUnauthorized, message)
}
func ReplyAccessDeniedForbidden(sender ibus.ISender, message string) {
replyAccessDenied(sender, http.StatusForbidden, message)
}
func ReplyUnauthorized(sender ibus.ISender, message string) {
ReplyErrf(sender, http.StatusUnauthorized, message)
}
func ReplyInternalServerError(sender ibus.ISender, message string, err error) {
ReplyErrf(sender, http.StatusInternalServerError, message, ": ", err)
}
// WithResponseHandler, WithLongPolling and WithDiscardResponse are mutual exclusive
func WithResponseHandler(responseHandler func(httpResp *http.Response)) ReqOptFunc {
return func(ro *reqOpts) {
ro.responseHandler = responseHandler
}
}
// WithLongPolling, WithResponseHandler and WithDiscardResponse are mutual exclusive
func WithLongPolling() ReqOptFunc {
return func(ro *reqOpts) {
ro.responseHandler = func(resp *http.Response) {
if !slices.Contains(ro.expectedHTTPCodes, resp.StatusCode) {
body, err := readBody(resp)
if err != nil {
panic("failed to read response body in custom response handler: " + err.Error())
}
panic(fmt.Sprintf("actual status code %d, expected %v. Body: %s", resp.StatusCode, ro.expectedHTTPCodes, body))
}
}
}
}
// WithDiscardResponse, WithResponseHandler and WithLongPolling are mutual exclusive
// causes FederationReq() to return nil for *HTTPResponse
func WithDiscardResponse() ReqOptFunc {
return func(opts *reqOpts) {
opts.discardResp = true
}
}
func WithCookies(cookiesPairs ...string) ReqOptFunc {
return func(po *reqOpts) {
for i := 0; i < len(cookiesPairs); i += 2 {
po.cookies[cookiesPairs[i]] = cookiesPairs[i+1]
}
}
}
func WithHeaders(headersPairs ...string) ReqOptFunc {
return func(po *reqOpts) {
for i := 0; i < len(headersPairs); i += 2 {
po.headers[headersPairs[i]] = headersPairs[i+1]
}
}
}
func WithExpectedCode(expectedHTTPCode int, expectErrorContains ...string) ReqOptFunc {
return func(po *reqOpts) {
po.expectedHTTPCodes = append(po.expectedHTTPCodes, expectedHTTPCode)
po.expectedErrorContains = append(po.expectedErrorContains, expectErrorContains...)
}
}
// has priority over WithAuthorizeByIfNot
func WithAuthorizeBy(principalToken string) ReqOptFunc {
return func(po *reqOpts) {
po.headers[Authorization] = BearerPrefix + principalToken
}
}
func WithRetryOnCertainError(errMatcher func(err error) bool, timeout time.Duration, retryDelay time.Duration) ReqOptFunc {
return func(opts *reqOpts) {
opts.retriersOnErrors = append(opts.retriersOnErrors, retrier{
macther: errMatcher,
timeout: timeout,
delay: retryDelay,
})
}
}
func WithRetryOnAnyError(timeout time.Duration, retryDelay time.Duration) ReqOptFunc {
return WithRetryOnCertainError(func(error) bool { return true }, timeout, retryDelay)
}
func WithAuthorizeByIfNot(principalToken string) ReqOptFunc {
return func(po *reqOpts) {
if _, ok := po.headers[Authorization]; !ok {
po.headers[Authorization] = BearerPrefix + principalToken
}
}
}
func WithRelativeURL(relativeURL string) ReqOptFunc {
return func(ro *reqOpts) {
ro.relativeURL = relativeURL
}
}
func WithMethod(m string) ReqOptFunc {
return func(po *reqOpts) {
po.method = m
}
}
func Expect409(expected ...string) ReqOptFunc {
return WithExpectedCode(http.StatusConflict, expected...)
}
func Expect404() ReqOptFunc {
return WithExpectedCode(http.StatusNotFound)
}
func Expect401() ReqOptFunc {
return WithExpectedCode(http.StatusUnauthorized)
}
func Expect403(expectedMessages ...string) ReqOptFunc {
return WithExpectedCode(http.StatusForbidden, expectedMessages...)
}
func Expect400(expectErrorContains ...string) ReqOptFunc {
return WithExpectedCode(http.StatusBadRequest, expectErrorContains...)
}
func Expect400RefIntegrity_Existence() ReqOptFunc {
return WithExpectedCode(http.StatusBadRequest, "referential integrity violation", "does not exist")
}
func Expect400RefIntegrity_QName() ReqOptFunc {
return WithExpectedCode(http.StatusBadRequest, "referential integrity violation", "QNames are only allowed")
}
func Expect429() ReqOptFunc {
return WithExpectedCode(http.StatusTooManyRequests)
}
func Expect500() ReqOptFunc {
return WithExpectedCode(http.StatusInternalServerError)
}
func Expect503() ReqOptFunc {
return WithExpectedCode(http.StatusServiceUnavailable)
}
func Expect410() ReqOptFunc {
return WithExpectedCode(http.StatusGone)
}
func ExpectSysError500() ReqOptFunc {
return func(opts *reqOpts) {
opts.expectedSysErrorCode = http.StatusInternalServerError
}
}
type reqOpts struct {
method string
headers map[string]string
cookies map[string]string
expectedHTTPCodes []int
expectedErrorContains []string
responseHandler func(httpResp *http.Response) // used if no errors and an expected status code is received
relativeURL string
discardResp bool
expectedSysErrorCode int
retriersOnErrors []retrier
}
func req(method, url, body string, headers, cookies map[string]string) (*http.Request, error) {
req, err := http.NewRequest(method, url, bytes.NewReader([]byte(body)))
if err != nil {
return nil, fmt.Errorf("NewRequest() failed: %w", err)
}
req.Close = true
for k, v := range headers {
req.Header.Add(k, v)
}
for k, v := range cookies {
req.AddCookie(&http.Cookie{
Name: k,
Value: v,
})
}
return req, nil
}
// status code expected -> DiscardBody, ResponseHandler are used
// status code is unexpected -> DiscardBody, ResponseHandler are ignored, body is read out, wrapped ErrUnexpectedStatusCode is returned
func (c *implIHTTPClient) Req(urlStr string, body string, optFuncs ...ReqOptFunc) (*HTTPResponse, error) {
opts := &reqOpts{
headers: map[string]string{},
cookies: map[string]string{},
method: http.MethodGet,
}
optFuncs = append(optFuncs, WithRetryOnCertainError(func(err error) bool {
// https://github.com/voedger/voedger/issues/1694
return IsWSAEError(err, WSAECONNREFUSED)
}, retryOn_WSAECONNREFUSED_Timeout, retryOn_WSAECONNREFUSED_Delay))
for _, optFunc := range optFuncs {
optFunc(opts)
}
mutualExclusiveOpts := 0
if opts.discardResp {
mutualExclusiveOpts++
}
if opts.expectedSysErrorCode > 0 {
mutualExclusiveOpts++
}
if opts.responseHandler != nil {
mutualExclusiveOpts++
}
if mutualExclusiveOpts > 1 {
panic("request options conflict")
}
if len(opts.expectedHTTPCodes) == 0 {
opts.expectedHTTPCodes = append(opts.expectedHTTPCodes, http.StatusOK)
}
if len(opts.relativeURL) > 0 {
netURL, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
netURL.Path = opts.relativeURL
urlStr = netURL.String()
}
var resp *http.Response
var err error
tryNum := 0
startTime := time.Now()
reqLoop:
for time.Since(startTime) < maxHTTPRequestTimeout {
req, err := req(opts.method, urlStr, body, opts.headers, opts.cookies)
if err != nil {
return nil, err
}
resp, err = c.client.Do(req)
if err != nil {
for _, retrier := range opts.retriersOnErrors {
if retrier.macther(err) {
if time.Since(startTime) < retrier.timeout {
time.Sleep(retrier.delay)
continue reqLoop
}
}
}
return nil, fmt.Errorf("request do() failed: %w", err)
}
if opts.responseHandler == nil {
defer resp.Body.Close()
}
if resp.StatusCode == http.StatusServiceUnavailable && !slices.Contains(opts.expectedHTTPCodes, http.StatusServiceUnavailable) {
if err := discardRespBody(resp); err != nil {
return nil, err
}
if tryNum > shortRetriesOn503Amount {
time.Sleep(longRetryOn503Delay)
} else {
time.Sleep(shortRetryOn503Delay)
}
logger.Verbose("503. retrying...")
tryNum++
continue
}
break
}
httpResponse := &HTTPResponse{
HTTPResp: resp,
expectedSysErrorCode: opts.expectedSysErrorCode,
expectedHTTPCodes: opts.expectedHTTPCodes,
}
isCodeExpected := slices.Contains(opts.expectedHTTPCodes, resp.StatusCode)
if isCodeExpected {
if opts.responseHandler != nil {
opts.responseHandler(resp)
return httpResponse, nil
}
if opts.discardResp {
err := discardRespBody(resp)
return nil, err
}
}
respBody, err := readBody(resp)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
httpResponse.Body = respBody
var statusErr error
if !isCodeExpected {
statusErr = fmt.Errorf("%w: %d, %s", ErrUnexpectedStatusCode, resp.StatusCode, respBody)
}
if resp.StatusCode != http.StatusOK && len(opts.expectedErrorContains) > 0 {
sysError := map[string]interface{}{}
if err := json.Unmarshal([]byte(respBody), &sysError); err != nil {
return nil, err
}
actualError := sysError["sys.Error"].(map[string]interface{})["Message"].(string)
if !containsAllMessages(opts.expectedErrorContains, actualError) {
return nil, fmt.Errorf(`actual error message "%s" does not contain the expected messages %v`, actualError, opts.expectedErrorContains)
}
}
return httpResponse, statusErr
}
func (c *implIHTTPClient) CloseIdleConnections() {
c.client.CloseIdleConnections()
}
func containsAllMessages(strs []string, toFind string) bool {
for _, str := range strs {
if !strings.Contains(toFind, str) {
return false
}
}
return true
}
func (resp *HTTPResponse) ExpectedSysErrorCode() int {
return resp.expectedSysErrorCode
}
func (resp *HTTPResponse) ExpectedHTTPCodes() []int {
return resp.expectedHTTPCodes
}
func (resp *HTTPResponse) Println() {
log.Println(resp.Body)
}
func (resp *HTTPResponse) getError(t *testing.T) map[string]interface{} {
t.Helper()
m := map[string]interface{}{}
err := json.Unmarshal([]byte(resp.Body), &m)
require.NoError(t, err)
return m["sys.Error"].(map[string]interface{})
}
func (resp *HTTPResponse) RequireError(t *testing.T, message string) {
t.Helper()
m := resp.getError(t)
require.Equal(t, message, m["Message"])
}
func (resp *HTTPResponse) RequireContainsError(t *testing.T, messagePart string) {
t.Helper()
m := resp.getError(t)
require.Contains(t, m["Message"], messagePart)
}
func readBody(resp *http.Response) (string, error) {
respBody, err := io.ReadAll(resp.Body)
return string(respBody), err
}
func discardRespBody(resp *http.Response) error {
_, err := io.Copy(io.Discard, resp.Body)
if err != nil {
// https://github.com/voedger/voedger/issues/1694
if !IsWSAEError(err, WSAECONNRESET) {
return fmt.Errorf("failed to discard response body: %w", err)
}
}
return nil
}
func (resp *FuncResponse) SectionRow(rowIdx ...int) []interface{} {
if len(rowIdx) > 1 {
panic("must be 0 or 1 rowIdx'es")
}
if len(resp.Sections) == 0 {
panic("empty response")
}
i := 0
if len(rowIdx) == 1 {
i = rowIdx[0]
}
return resp.Sections[0].Elements[i][0][0]
}
// returns a new ID for raw ID 1
func (resp *FuncResponse) NewID() int64 {
return resp.NewIDs["1"]
}
func (resp *FuncResponse) IsEmpty() bool {
return len(resp.Sections) == 0
}
func (fe FuncError) Error() string {
if len(fe.ExpectedHTTPCodes) == 1 && fe.ExpectedHTTPCodes[0] == http.StatusOK {
return fmt.Sprintf("status %d: %s", fe.HTTPStatus, fe.Message)
}
return fmt.Sprintf("status %d, expected %v: %s", fe.HTTPStatus, fe.ExpectedHTTPCodes, fe.Message)
}
func (fe FuncError) Unwrap() error {
return fe.SysError
}
type implIHTTPClient struct {
client *http.Client
}
func NewIHTTPClient() (client IHTTPClient, clenup func()) {
// set linger - see https://github.com/voedger/voedger/issues/415
tr := http.DefaultTransport.(*http.Transport).Clone()
tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
dialer := net.Dialer{}
conn, err := dialer.DialContext(ctx, network, addr)
if err != nil {
return nil, err
}
err = conn.(*net.TCPConn).SetLinger(0)
return conn, err
}
client = &implIHTTPClient{client: &http.Client{Transport: tr}}
return client, client.CloseIdleConnections
}