forked from goss-org/goss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
101 lines (84 loc) · 1.83 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
package system
import (
"crypto/tls"
"io"
"net/http"
"time"
"github.com/aelsabbahy/goss/util"
)
type HTTP interface {
HTTP() string
Status() (int, error)
Body() (io.Reader, error)
Exists() (bool, error)
SetAllowInsecure(bool)
SetNoFollowRedirects(bool)
}
type DefHTTP struct {
http string
allowInsecure bool
noFollowRedirects bool
resp *http.Response
Timeout int
loaded bool
err error
}
func NewDefHTTP(http string, system *System, config util.Config) HTTP {
return &DefHTTP{
http: http,
allowInsecure: config.AllowInsecure,
noFollowRedirects: config.NoFollowRedirects,
Timeout: config.Timeout,
}
}
func (u *DefHTTP) setup() error {
if u.loaded {
return u.err
}
u.loaded = true
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: u.allowInsecure},
DisableKeepAlives: true,
}
client := &http.Client{
Transport: tr,
Timeout: time.Duration(u.Timeout) * time.Millisecond,
}
if u.noFollowRedirects {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
u.resp, u.err = client.Get(u.http)
return u.err
}
func (u *DefHTTP) Exists() (bool, error) {
if _, err := u.Status(); err != nil {
return false, err
}
return true, nil
}
func (u *DefHTTP) SetNoFollowRedirects(t bool) {
u.noFollowRedirects = t
}
func (u *DefHTTP) SetAllowInsecure(t bool) {
u.allowInsecure = t
}
func (u *DefHTTP) ID() string {
return u.http
}
func (u *DefHTTP) HTTP() string {
return u.http
}
func (u *DefHTTP) Status() (int, error) {
if err := u.setup(); err != nil {
return 0, err
}
return u.resp.StatusCode, nil
}
func (u *DefHTTP) Body() (io.Reader, error) {
if err := u.setup(); err != nil {
return nil, err
}
return u.resp.Body, nil
}