/
common.go
81 lines (73 loc) · 2.42 KB
/
common.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
package http
import (
"errors"
"fmt"
"net/http"
"net/url"
api "kmodules.xyz/prober/api"
"k8s.io/klog/v2"
utilio "k8s.io/utils/io"
)
const (
ContentType = "Content-Type"
ContentUrlEncodedForm = "application/x-www-form-urlencoded"
ContentJson = "application/json"
ContentPlainText = "text/plain"
)
// HTTPInterface is an interface for making HTTP requests, that returns a response and error.
type HTTPInterface interface {
Do(req *http.Request) (*http.Response, error)
}
func doHTTPProbe(req *http.Request, url *url.URL, headers http.Header, client HTTPInterface) (api.Result, string, error) {
if _, ok := headers["User-Agent"]; !ok {
if headers == nil {
headers = http.Header{}
}
// explicitly set User-Agent so it's not set to default Go value
headers.Set("User-Agent", "kmodules.xyz/client-go/release-11.0")
}
req.Header = headers
if headers.Get("Host") != "" {
req.Host = headers.Get("Host")
}
res, err := client.Do(req)
if err != nil {
// Convert errors into failures to catch timeouts.
return api.Failure, err.Error(), nil
}
defer res.Body.Close()
b, err := utilio.ReadAtMost(res.Body, maxRespBodyLength)
if err != nil {
if err == utilio.ErrLimitReached {
klog.V(5).Infof("Non fatal body truncation for %s, Response: %v", url.String(), *res)
} else {
return api.Failure, "", err
}
}
respBody := string(b)
if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusBadRequest {
if res.StatusCode >= http.StatusMultipleChoices { // Redirect
klog.V(5).Infof("Probe terminated redirects for %s, Response: %v", url.String(), *res)
return api.Warning, respBody, nil
}
klog.V(5).Infof("Probe succeeded for %s, Response: %v", url.String(), *res)
return api.Success, respBody, nil
}
klog.V(5).Infof("Probe failed for %s with request headers %v, response body: %v", url.String(), headers, respBody)
return api.Failure, fmt.Sprintf("HTTP probe failed with statuscode: %d", res.StatusCode), nil
}
func redirectChecker(followNonLocalRedirects bool) func(*http.Request, []*http.Request) error {
if followNonLocalRedirects {
return nil // Use the default http client checker.
}
return func(req *http.Request, via []*http.Request) error {
if req.URL.Hostname() != via[0].URL.Hostname() {
return http.ErrUseLastResponse
}
// Default behavior: stop after 10 redirects.
if len(via) >= 10 {
return errors.New("stopped after 10 redirects")
}
return nil
}
}