| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
package http |
| 12 |
|
| 13 |
import ( |
| 14 |
"encoding/base64" |
| 15 |
"errors" |
| 16 |
"fmt" |
| 17 |
"io" |
| 18 |
"io/ioutil" |
| 19 |
"log" |
| 20 |
"net/url" |
| 21 |
"strings" |
| 22 |
"sync" |
| 23 |
"time" |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
type Client struct { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
Transport RoundTripper |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
CheckRedirect func(req *Request, via []*Request) error |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
Jar CookieJar |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
Timeout time.Duration |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
var DefaultClient = &Client{} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
type RoundTripper interface { |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
RoundTrip(*Request) (*Response, error) |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") } |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
type readClose struct { |
| 109 |
io.Reader |
| 110 |
io.Closer |
| 111 |
} |
| 112 |
|
| 113 |
func (c *Client) send(req *Request) (*Response, error) { |
| 114 |
if c.Jar != nil { |
| 115 |
for _, cookie := range c.Jar.Cookies(req.URL) { |
| 116 |
req.AddCookie(cookie) |
| 117 |
} |
| 118 |
} |
| 119 |
resp, err := send(req, c.transport()) |
| 120 |
if err != nil { |
| 121 |
return nil, err |
| 122 |
} |
| 123 |
if c.Jar != nil { |
| 124 |
if rc := resp.Cookies(); len(rc) > 0 { |
| 125 |
c.Jar.SetCookies(req.URL, rc) |
| 126 |
} |
| 127 |
} |
| 128 |
return resp, err |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
func (c *Client) Do(req *Request) (resp *Response, err error) { |
| 150 |
if req.Method == "GET" || req.Method == "HEAD" { |
| 151 |
return c.doFollowingRedirects(req, shouldRedirectGet) |
| 152 |
} |
| 153 |
if req.Method == "POST" || req.Method == "PUT" { |
| 154 |
return c.doFollowingRedirects(req, shouldRedirectPost) |
| 155 |
} |
| 156 |
return c.send(req) |
| 157 |
} |
| 158 |
|
| 159 |
func (c *Client) transport() RoundTripper { |
| 160 |
if c.Transport != nil { |
| 161 |
return c.Transport |
| 162 |
} |
| 163 |
return DefaultTransport |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
func send(req *Request, t RoundTripper) (resp *Response, err error) { |
| 169 |
if t == nil { |
| 170 |
req.closeBody() |
| 171 |
return nil, errors.New("http: no Client.Transport or DefaultTransport") |
| 172 |
} |
| 173 |
|
| 174 |
if req.URL == nil { |
| 175 |
req.closeBody() |
| 176 |
return nil, errors.New("http: nil Request.URL") |
| 177 |
} |
| 178 |
|
| 179 |
if req.RequestURI != "" { |
| 180 |
req.closeBody() |
| 181 |
return nil, errors.New("http: Request.RequestURI can't be set in client requests.") |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
if req.Header == nil { |
| 188 |
req.Header = make(Header) |
| 189 |
} |
| 190 |
|
| 191 |
if u := req.URL.User; u != nil { |
| 192 |
username := u.Username() |
| 193 |
password, _ := u.Password() |
| 194 |
req.Header.Set("Authorization", "Basic "+basicAuth(username, password)) |
| 195 |
} |
| 196 |
resp, err = t.RoundTrip(req) |
| 197 |
if err != nil { |
| 198 |
if resp != nil { |
| 199 |
log.Printf("RoundTripper returned a response & error; ignoring response") |
| 200 |
} |
| 201 |
return nil, err |
| 202 |
} |
| 203 |
return resp, nil |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
func basicAuth(username, password string) string { |
| 212 |
auth := username + ":" + password |
| 213 |
return base64.StdEncoding.EncodeToString([]byte(auth)) |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
func shouldRedirectGet(statusCode int) bool { |
| 219 |
switch statusCode { |
| 220 |
case StatusMovedPermanently, StatusFound, StatusSeeOther, StatusTemporaryRedirect: |
| 221 |
return true |
| 222 |
} |
| 223 |
return false |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
func shouldRedirectPost(statusCode int) bool { |
| 229 |
switch statusCode { |
| 230 |
case StatusFound, StatusSeeOther: |
| 231 |
return true |
| 232 |
} |
| 233 |
return false |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
func Get(url string) (resp *Response, err error) { |
| 253 |
return DefaultClient.Get(url) |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
func (c *Client) Get(url string) (resp *Response, err error) { |
| 272 |
req, err := NewRequest("GET", url, nil) |
| 273 |
if err != nil { |
| 274 |
return nil, err |
| 275 |
} |
| 276 |
return c.doFollowingRedirects(req, shouldRedirectGet) |
| 277 |
} |
| 278 |
|
| 279 |
func (c *Client) doFollowingRedirects(ireq *Request, shouldRedirect func(int) bool) (resp *Response, err error) { |
| 280 |
var base *url.URL |
| 281 |
redirectChecker := c.CheckRedirect |
| 282 |
if redirectChecker == nil { |
| 283 |
redirectChecker = defaultCheckRedirect |
| 284 |
} |
| 285 |
var via []*Request |
| 286 |
|
| 287 |
if ireq.URL == nil { |
| 288 |
ireq.closeBody() |
| 289 |
return nil, errors.New("http: nil Request.URL") |
| 290 |
} |
| 291 |
|
| 292 |
var reqmu sync.Mutex |
| 293 |
req := ireq |
| 294 |
|
| 295 |
var timer *time.Timer |
| 296 |
if c.Timeout > 0 { |
| 297 |
type canceler interface { |
| 298 |
CancelRequest(*Request) |
| 299 |
} |
| 300 |
tr, ok := c.transport().(canceler) |
| 301 |
if !ok { |
| 302 |
return nil, fmt.Errorf("net/http: Client Transport of type %T doesn't support CancelRequest; Timeout not supported", c.transport()) |
| 303 |
} |
| 304 |
timer = time.AfterFunc(c.Timeout, func() { |
| 305 |
reqmu.Lock() |
| 306 |
defer reqmu.Unlock() |
| 307 |
tr.CancelRequest(req) |
| 308 |
}) |
| 309 |
} |
| 310 |
|
| 311 |
urlStr := "" |
| 312 |
redirectFailed := false |
| 313 |
for redirect := 0; ; redirect++ { |
| 314 |
if redirect != 0 { |
| 315 |
nreq := new(Request) |
| 316 |
nreq.Method = ireq.Method |
| 317 |
if ireq.Method == "POST" || ireq.Method == "PUT" { |
| 318 |
nreq.Method = "GET" |
| 319 |
} |
| 320 |
nreq.Header = make(Header) |
| 321 |
nreq.URL, err = base.Parse(urlStr) |
| 322 |
if err != nil { |
| 323 |
break |
| 324 |
} |
| 325 |
if len(via) > 0 { |
| 326 |
|
| 327 |
lastReq := via[len(via)-1] |
| 328 |
if lastReq.URL.Scheme != "https" { |
| 329 |
nreq.Header.Set("Referer", lastReq.URL.String()) |
| 330 |
} |
| 331 |
|
| 332 |
err = redirectChecker(nreq, via) |
| 333 |
if err != nil { |
| 334 |
redirectFailed = true |
| 335 |
break |
| 336 |
} |
| 337 |
} |
| 338 |
reqmu.Lock() |
| 339 |
req = nreq |
| 340 |
reqmu.Unlock() |
| 341 |
} |
| 342 |
|
| 343 |
urlStr = req.URL.String() |
| 344 |
if resp, err = c.send(req); err != nil { |
| 345 |
break |
| 346 |
} |
| 347 |
|
| 348 |
if shouldRedirect(resp.StatusCode) { |
| 349 |
|
| 350 |
|
| 351 |
const maxBodySlurpSize = 2 << 10 |
| 352 |
if resp.ContentLength == -1 || resp.ContentLength <= maxBodySlurpSize { |
| 353 |
io.CopyN(ioutil.Discard, resp.Body, maxBodySlurpSize) |
| 354 |
} |
| 355 |
resp.Body.Close() |
| 356 |
if urlStr = resp.Header.Get("Location"); urlStr == "" { |
| 357 |
err = errors.New(fmt.Sprintf("%d response missing Location header", resp.StatusCode)) |
| 358 |
break |
| 359 |
} |
| 360 |
base = req.URL |
| 361 |
via = append(via, req) |
| 362 |
continue |
| 363 |
} |
| 364 |
if timer != nil { |
| 365 |
resp.Body = &cancelTimerBody{timer, resp.Body} |
| 366 |
} |
| 367 |
return resp, nil |
| 368 |
} |
| 369 |
|
| 370 |
method := ireq.Method |
| 371 |
urlErr := &url.Error{ |
| 372 |
Op: method[0:1] + strings.ToLower(method[1:]), |
| 373 |
URL: urlStr, |
| 374 |
Err: err, |
| 375 |
} |
| 376 |
|
| 377 |
if redirectFailed { |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
return resp, urlErr |
| 382 |
} |
| 383 |
|
| 384 |
if resp != nil { |
| 385 |
resp.Body.Close() |
| 386 |
} |
| 387 |
return nil, urlErr |
| 388 |
} |
| 389 |
|
| 390 |
func defaultCheckRedirect(req *Request, via []*Request) error { |
| 391 |
if len(via) >= 10 { |
| 392 |
return errors.New("stopped after 10 redirects") |
| 393 |
} |
| 394 |
return nil |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
func Post(url string, bodyType string, body io.Reader) (resp *Response, err error) { |
| 403 |
return DefaultClient.Post(url, bodyType, body) |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
func (c *Client) Post(url string, bodyType string, body io.Reader) (resp *Response, err error) { |
| 413 |
req, err := NewRequest("POST", url, body) |
| 414 |
if err != nil { |
| 415 |
return nil, err |
| 416 |
} |
| 417 |
req.Header.Set("Content-Type", bodyType) |
| 418 |
return c.doFollowingRedirects(req, shouldRedirectPost) |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
func PostForm(url string, data url.Values) (resp *Response, err error) { |
| 429 |
return DefaultClient.PostForm(url, data) |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
func (c *Client) PostForm(url string, data url.Values) (resp *Response, err error) { |
| 438 |
return c.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode())) |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
func Head(url string) (resp *Response, err error) { |
| 452 |
return DefaultClient.Head(url) |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
func (c *Client) Head(url string) (resp *Response, err error) { |
| 464 |
req, err := NewRequest("HEAD", url, nil) |
| 465 |
if err != nil { |
| 466 |
return nil, err |
| 467 |
} |
| 468 |
return c.doFollowingRedirects(req, shouldRedirectGet) |
| 469 |
} |
| 470 |
|
| 471 |
type cancelTimerBody struct { |
| 472 |
t *time.Timer |
| 473 |
rc io.ReadCloser |
| 474 |
} |
| 475 |
|
| 476 |
func (b *cancelTimerBody) Read(p []byte) (n int, err error) { |
| 477 |
n, err = b.rc.Read(p) |
| 478 |
if err == io.EOF { |
| 479 |
b.t.Stop() |
| 480 |
} |
| 481 |
return |
| 482 |
} |
| 483 |
|
| 484 |
func (b *cancelTimerBody) Close() error { |
| 485 |
err := b.rc.Close() |
| 486 |
b.t.Stop() |
| 487 |
return err |
| 488 |
} |