--- client.go.orig 2012-04-11 11:48:18.000000000 +0100 +++ client.go 2012-04-11 12:19:52.000000000 +0100 @@ -99,11 +99,12 @@ if req.Method == "GET" || req.Method == "HEAD" { return c.doFollowingRedirects(req) } - return send(req, c.Transport) + return c.send(req) } // send issues an HTTP request. Caller should close resp.Body when done reading from it. -func send(req *Request, t RoundTripper) (resp *Response, err error) { +func (c *Client) send(req *Request) (resp *Response, err error) { + t := c.Transport if t == nil { t = DefaultTransport if t == nil { @@ -130,7 +131,22 @@ if u := req.URL.User; u != nil { req.Header.Set("Authorization", "Basic "+base64.URLEncoding.EncodeToString([]byte(u.String()))) } - return t.RoundTrip(req) + if c.Jar != nil { + for _, cookie := range c.Jar.Cookies(req.URL) { + req.AddCookie(cookie) + } + } + + if resp, err = t.RoundTrip(req); err != nil { + return + } + + if c.Jar != nil { + if cookies := resp.Cookies(); len(cookies) > 0 { + c.Jar.SetCookies(req.URL, cookies) + } + } + return } // True if the specified HTTP status code is one for which the Get utility should @@ -190,11 +206,6 @@ return nil, errors.New("http: nil Request.URL") } - jar := c.Jar - if jar == nil { - jar = blackHoleJar{} - } - req := ireq urlStr := "" // next relative or absolute URL to fetch (after first request) for redirect := 0; ; redirect++ { @@ -220,16 +231,10 @@ } } - for _, cookie := range jar.Cookies(req.URL) { - req.AddCookie(cookie) - } urlStr = req.URL.String() - if r, err = send(req, c.Transport); err != nil { + if r, err = c.send(req); err != nil { break } - if c := r.Cookies(); len(c) > 0 { - jar.SetCookies(req.URL, c) - } if shouldRedirect(r.StatusCode) { r.Body.Close() @@ -278,11 +283,7 @@ return nil, err } req.Header.Set("Content-Type", bodyType) - r, err = send(req, c.Transport) - if err == nil && c.Jar != nil { - c.Jar.SetCookies(req.URL, r.Cookies()) - } - return r, err + return c.send(req) } // PostForm issues a POST to the specified URL,