-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpclient.go
More file actions
116 lines (95 loc) · 2.44 KB
/
Copy pathhttpclient.go
File metadata and controls
116 lines (95 loc) · 2.44 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package inet
import (
"net/http"
"net/http/httputil"
"strings"
"time"
)
// HTTPClient is a simple wrapper around net/http that adds the
// Jar(http.CookieJar) method for the Client interface.
type HTTPClient struct {
http.Client
debug bool
ua string
}
// Debug will enable debugging/logging of Requests/Responses.
func (c *HTTPClient) Debug(enable bool) Client {
c.debug = enable
return c
}
// Do is a wrapper around net/http.Client.Do which allows for
// debugging of Requests/Responses.
func (c *HTTPClient) Do(req *http.Request) (*http.Response, error) {
var b []byte
var cookies string
var e error
var res *http.Response
var skip bool
if (c.ua != "") && (req.Header.Get("User-Agent") == "") {
req.Header.Set("User-Agent", c.ua)
}
if c.debug {
if b, e = httputil.DumpRequestOut(req, true); e == nil {
if c.Client.Jar != nil {
for _, cookie := range c.Client.Jar.Cookies(req.URL) {
if cookies == "" {
cookies = cookie.String()
} else {
cookies += "; " + cookie.String()
}
}
}
skip = cookies == ""
for _, line := range strings.Split(string(b), "\n") {
println(line)
if skip {
continue
}
if strings.HasPrefix(line, "Content-Length:") {
println("Cookie: " + cookies)
skip = true
}
}
}
}
//nolint:gosec // G704 - huh?
if res, e = c.Client.Do(req); e != nil {
//nolint:wrapcheck // Intentionally not wrapping
return nil, e
}
if c.debug {
if b, e = httputil.DumpResponse(res, true); e == nil {
println()
println(string(b))
}
}
return res, nil
}
// Jar will return the Client's cookiejar.
func (c *HTTPClient) Jar() http.CookieJar {
return c.Client.Jar
}
// SetJar will set the cookiejar for the underlying http.Client.
func (c *HTTPClient) SetJar(jar http.CookieJar) Client {
c.Client.Jar = jar
return c
}
// SetTimeout will set the timeout for the underlying http.Client.
func (c *HTTPClient) SetTimeout(timeout time.Duration) Client {
c.Client.Timeout = timeout
return c
}
// SetTransport will set the transport implementation for the
// underlying http.Client.
func (c *HTTPClient) SetTransport(trans http.RoundTripper) Client {
c.Client.Transport = trans
return c
}
// Timeout will return the Client's configured timeout.
func (c *HTTPClient) Timeout() time.Duration {
return c.Client.Timeout
}
// Transport will return the Client's transport implementation.
func (c *HTTPClient) Transport() http.RoundTripper {
return c.Client.Transport
}