/
http.go
56 lines (47 loc) · 1.27 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
package shared
import (
"crypto/tls"
"net/http"
"time"
// http transport/client wrappers
"github.com/hashicorp/go-cleanhttp"
"github.com/nathanejohnson/intransport"
)
// HTTPCode returns the http code of requested url
func HTTPCode(url string, timeout time.Duration, insecure bool) (int, error) {
var client *http.Client
var request *http.Request
var response *http.Response
var err error
// build request
if request, err = http.NewRequest("GET", url, nil); err != nil {
return 0, err
}
if insecure {
// init client using default clean transport
// and disable certificate verification
transport := cleanhttp.DefaultTransport()
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
client = &http.Client{
Transport: transport,
}
} else {
// init client using intransport library to automatically
// fetch intermediate certificates and validate the chain and
// stapled OCSP responses if certificates are marked
// as must staple
client = intransport.NewInTransportHTTPClient(nil)
}
// set timeout
client.Timeout = timeout
// execute request
if response, err = client.Do(request); err != nil {
return 0, err
}
// we don't use the body
response.Body.Close()
// all okay - return code
return response.StatusCode, nil
}