-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
56 lines (47 loc) · 1.22 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 profiler
import (
"context"
"net"
"net/http"
"strings"
)
func NewHTTPClient(protocol, address, serverId, serverToken string) *http.Client {
t := &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (conn net.Conn, err error) {
if protocol == "unix" {
return net.Dial("unix", address)
}
return net.Dial(network, addr)
},
}
return &http.Client{
Transport: &bfTransport{
Transport: t,
serverId: serverId,
serverToken: serverToken,
},
}
}
type bfTransport struct {
Transport http.RoundTripper
serverId string
serverToken string
}
func (t *bfTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if t.serverId != "" && t.serverToken != "" {
req.SetBasicAuth(t.serverId, t.serverToken)
}
response, err := t.Transport.RoundTrip(req)
if err != nil {
if strings.Contains(err.Error(), "malformed HTTP version") {
log.Error().Str("endpoint", req.URL.String()).Err(err).Msg("failed to send request")
return response, errOldAgent
}
return response, err
}
if response.StatusCode == 404 {
log.Error().Str("endpoint", req.URL.String()).Msg("failed to send request - got 404 response")
return response, errOldAgent
}
return response, err
}