-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpclient.go
175 lines (148 loc) · 3.93 KB
/
httpclient.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package httpclient
import (
"fmt"
"io"
"net/http"
"time"
)
var traceId = "github.com/jimrobinson/httpclient"
// Client configures a timeout on the reciept of response headers and
// the read of response bodys from an http server. It treats the
// configured timeout as absolute, not as a deadline that resets per
// successful read operation.
type Client struct {
http.Client
Transport *http.Transport
Timeout time.Duration
}
// NewClient returns an Client configured to timeout requests
// that take longer than the specified timeout.
func NewClient(timeout time.Duration) (hr *Client) {
transport := &http.Transport{
ResponseHeaderTimeout: timeout,
}
client := http.Client{
Transport: transport,
}
hr = &Client{
Client: client,
Transport: transport,
Timeout: timeout,
}
return
}
// Do sends an HTTP request and returns an HTTP response, following
// policy (e.g. redirects, cookies, auth) as configured on the client.
// If a non-zero timeout has been set on the Client, the request will
// be cancled if the duration has been reached before the request has
// completed.
func (hr *Client) Do(req *http.Request) (rsp *http.Response, err error) {
rspCh := make(chan *http.Response)
errCh := make(chan error)
var timeoutCh <-chan time.Time
if hr.Timeout > 0 {
timeoutCh = time.After(hr.Timeout)
}
go func(req *http.Request) {
rsp, err := hr.Client.Do(req)
if err != nil {
errCh <- err
rspCh <- rsp
} else {
rspCh <- rsp
}
}(req)
var now time.Time
select {
case err = <-errCh:
rsp = <-rspCh
case now = <-timeoutCh:
go hr.Transport.CancelRequest(req)
err = fmt.Errorf("error requesting %s: read timed out at %s after waiting %s",
req.URL, now.Format(time.RFC3339), hr.Timeout)
case rsp = <-rspCh:
err = nil
}
return
}
// DoAuth performs the same work as Do, but additionally
// attempts to handle WWW-Authenticate requests using
// the provided session. An error is returned if the session
// is nil.
func (hr *Client) DoAuth(req *http.Request, session Session) (rsp *http.Response, err error) {
if session == nil {
return hr.Do(req)
}
auth := session.Authorization(req.URL)
if auth != "" {
req.Header.Set("Authorization", auth)
}
// copy the request body so that we may
// resubmit it if we need to re-authorize
var body io.ReadCloser
if req.Body != nil {
var clone []io.ReadCloser
clone, err = session.Duplicate(req.Body, 2)
if err != nil {
return
}
req.Body, body = clone[0], clone[1]
for i := range clone {
defer clone[i].Close()
}
}
rsp, err = hr.Do(req)
// retry the request w/ Authorization if challenged
if err == nil && rsp.StatusCode == http.StatusUnauthorized {
var challenges Challenges
challenges, err = Authentication(rsp)
if err != nil {
err = fmt.Errorf("unable to parse %s WWW-Authenticate: %v", req.URL.String(), err)
return
}
n := len(challenges)
if n == 0 {
err = fmt.Errorf("unable to parse %s WWW-Authenticate header: %s",
req.URL.String(), rsp.Header.Get("Www-Authenticate"))
return
}
for i, challenge := range challenges {
lastTry := i+1 == n
auth, err = challenge.Authorization(session, req)
if err != nil {
if err == NoCredentialsErr && !lastTry {
continue
}
return
}
if auth != "" {
req.Header.Set("Authorization", auth)
// if the request body is not nil
// and we have additional challenges
// we may need to try, then we have
// to keep cloning the request body.
if body != nil {
if lastTry {
req.Body = body
} else {
var clone []io.ReadCloser
clone, err = session.Duplicate(body, 2)
if err != nil {
return
}
req.Body, body = clone[0], clone[1]
for i := range clone {
defer clone[i].Close()
}
}
}
rsp, err = hr.Do(req)
if err == nil && rsp.StatusCode != http.StatusUnauthorized {
session.SetAuthorization(req.URL, challenge.Domain, auth)
return
}
}
}
}
return
}