forked from djui/await
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
59 lines (45 loc) · 1.16 KB
/
http.go
File metadata and controls
59 lines (45 loc) · 1.16 KB
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
package main
import (
"context"
"crypto/tls"
"errors"
"net/http"
"net/url"
)
type httpResource struct {
url.URL
}
func (r *httpResource) Await(ctx context.Context) error {
var client *http.Client
if skipTLSVerification(r) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client = &http.Client{Transport: tr}
} else {
client = &http.Client{}
}
// IDEA(uwe): Use fragment to set method
req, err := http.NewRequest("GET", r.URL.String(), nil)
if err != nil {
return err
}
// IDEA(uwe): Use k/v pairs in fragment to set headers
req = req.WithContext(ctx)
req.Header.Set("User-Agent", "await/"+version)
resp, err := client.Do(req)
if err != nil {
return &unavailabilityError{err}
}
defer func() { _ = resp.Body.Close() }()
// IDEA(uwe): Use fragment to set tolerated status code
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return nil
}
return &unavailabilityError{errors.New(resp.Status)}
}
func skipTLSVerification(r *httpResource) bool {
opts := parseFragment(r.URL.Fragment)
vals, ok := opts["tls"]
return ok && r.URL.Scheme == "https" && len(vals) == 1 && vals[0] == "skip-verify"
}