Skip to content

Commit e1d9fcd

Browse files
committed
net/http: clarify client return values in docs
Also, fixes one violation found during testing where both response and error could be non-nil when a CheckRedirect test failed. This is arguably a minor API (behavior, not signature) change, but it wasn't documented either way and was inconsistent & non-Go like. Any code depending on the old behavior was wrong anyway. R=adg, rsc CC=golang-dev https://golang.org/cl/6307088
1 parent ca2ae27 commit e1d9fcd

File tree

2 files changed

+60
-33
lines changed

2 files changed

+60
-33
lines changed

src/pkg/net/http/client.go

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"errors"
1515
"fmt"
1616
"io"
17+
"log"
1718
"net/url"
1819
"strings"
1920
)
@@ -87,9 +88,13 @@ type readClose struct {
8788
// Do sends an HTTP request and returns an HTTP response, following
8889
// policy (e.g. redirects, cookies, auth) as configured on the client.
8990
//
90-
// A non-nil response always contains a non-nil resp.Body.
91+
// An error is returned if caused by client policy (such as
92+
// CheckRedirect), or if there was an HTTP protocol error.
93+
// A non-2xx response doesn't cause an error.
9194
//
92-
// Callers should close resp.Body when done reading from it. If
95+
// When err is nil, resp always contains a non-nil resp.Body.
96+
//
97+
// Callers should close res.Body when done reading from it. If
9398
// resp.Body is not closed, the Client's underlying RoundTripper
9499
// (typically Transport) may not be able to re-use a persistent TCP
95100
// connection to the server for a subsequent "keep-alive" request.
@@ -102,7 +107,8 @@ func (c *Client) Do(req *Request) (resp *Response, err error) {
102107
return send(req, c.Transport)
103108
}
104109

105-
// send issues an HTTP request. Caller should close resp.Body when done reading from it.
110+
// send issues an HTTP request.
111+
// Caller should close resp.Body when done reading from it.
106112
func send(req *Request, t RoundTripper) (resp *Response, err error) {
107113
if t == nil {
108114
t = DefaultTransport
@@ -130,7 +136,14 @@ func send(req *Request, t RoundTripper) (resp *Response, err error) {
130136
if u := req.URL.User; u != nil {
131137
req.Header.Set("Authorization", "Basic "+base64.URLEncoding.EncodeToString([]byte(u.String())))
132138
}
133-
return t.RoundTrip(req)
139+
resp, err = t.RoundTrip(req)
140+
if err != nil {
141+
if resp != nil {
142+
log.Printf("RoundTripper returned a response & error; ignoring response")
143+
}
144+
return nil, err
145+
}
146+
return resp, nil
134147
}
135148

136149
// True if the specified HTTP status code is one for which the Get utility should
@@ -151,10 +164,15 @@ func shouldRedirect(statusCode int) bool {
151164
// 303 (See Other)
152165
// 307 (Temporary Redirect)
153166
//
154-
// Caller should close r.Body when done reading from it.
167+
// An error is returned if there were too many redirects or if there
168+
// was an HTTP protocol error. A non-2xx response doesn't cause an
169+
// error.
170+
//
171+
// When err is nil, resp always contains a non-nil resp.Body.
172+
// Caller should close resp.Body when done reading from it.
155173
//
156174
// Get is a wrapper around DefaultClient.Get.
157-
func Get(url string) (r *Response, err error) {
175+
func Get(url string) (resp *Response, err error) {
158176
return DefaultClient.Get(url)
159177
}
160178

@@ -167,16 +185,21 @@ func Get(url string) (r *Response, err error) {
167185
// 303 (See Other)
168186
// 307 (Temporary Redirect)
169187
//
170-
// Caller should close r.Body when done reading from it.
171-
func (c *Client) Get(url string) (r *Response, err error) {
188+
// An error is returned if the Client's CheckRedirect function fails
189+
// or if there was an HTTP protocol error. A non-2xx response doesn't
190+
// cause an error.
191+
//
192+
// When err is nil, resp always contains a non-nil resp.Body.
193+
// Caller should close resp.Body when done reading from it.
194+
func (c *Client) Get(url string) (resp *Response, err error) {
172195
req, err := NewRequest("GET", url, nil)
173196
if err != nil {
174197
return nil, err
175198
}
176199
return c.doFollowingRedirects(req)
177200
}
178201

179-
func (c *Client) doFollowingRedirects(ireq *Request) (r *Response, err error) {
202+
func (c *Client) doFollowingRedirects(ireq *Request) (resp *Response, err error) {
180203
// TODO: if/when we add cookie support, the redirected request shouldn't
181204
// necessarily supply the same cookies as the original.
182205
var base *url.URL
@@ -224,17 +247,17 @@ func (c *Client) doFollowingRedirects(ireq *Request) (r *Response, err error) {
224247
req.AddCookie(cookie)
225248
}
226249
urlStr = req.URL.String()
227-
if r, err = send(req, c.Transport); err != nil {
250+
if resp, err = send(req, c.Transport); err != nil {
228251
break
229252
}
230-
if c := r.Cookies(); len(c) > 0 {
253+
if c := resp.Cookies(); len(c) > 0 {
231254
jar.SetCookies(req.URL, c)
232255
}
233256

234-
if shouldRedirect(r.StatusCode) {
235-
r.Body.Close()
236-
if urlStr = r.Header.Get("Location"); urlStr == "" {
237-
err = errors.New(fmt.Sprintf("%d response missing Location header", r.StatusCode))
257+
if shouldRedirect(resp.StatusCode) {
258+
resp.Body.Close()
259+
if urlStr = resp.Header.Get("Location"); urlStr == "" {
260+
err = errors.New(fmt.Sprintf("%d response missing Location header", resp.StatusCode))
238261
break
239262
}
240263
base = req.URL
@@ -244,13 +267,16 @@ func (c *Client) doFollowingRedirects(ireq *Request) (r *Response, err error) {
244267
return
245268
}
246269

270+
if resp != nil {
271+
resp.Body.Close()
272+
}
273+
247274
method := ireq.Method
248-
err = &url.Error{
275+
return nil, &url.Error{
249276
Op: method[0:1] + strings.ToLower(method[1:]),
250277
URL: urlStr,
251278
Err: err,
252279
}
253-
return
254280
}
255281

256282
func defaultCheckRedirect(req *Request, via []*Request) error {
@@ -262,17 +288,17 @@ func defaultCheckRedirect(req *Request, via []*Request) error {
262288

263289
// Post issues a POST to the specified URL.
264290
//
265-
// Caller should close r.Body when done reading from it.
291+
// Caller should close resp.Body when done reading from it.
266292
//
267293
// Post is a wrapper around DefaultClient.Post
268-
func Post(url string, bodyType string, body io.Reader) (r *Response, err error) {
294+
func Post(url string, bodyType string, body io.Reader) (resp *Response, err error) {
269295
return DefaultClient.Post(url, bodyType, body)
270296
}
271297

272298
// Post issues a POST to the specified URL.
273299
//
274-
// Caller should close r.Body when done reading from it.
275-
func (c *Client) Post(url string, bodyType string, body io.Reader) (r *Response, err error) {
300+
// Caller should close resp.Body when done reading from it.
301+
func (c *Client) Post(url string, bodyType string, body io.Reader) (resp *Response, err error) {
276302
req, err := NewRequest("POST", url, body)
277303
if err != nil {
278304
return nil, err
@@ -283,28 +309,30 @@ func (c *Client) Post(url string, bodyType string, body io.Reader) (r *Response,
283309
req.AddCookie(cookie)
284310
}
285311
}
286-
r, err = send(req, c.Transport)
312+
resp, err = send(req, c.Transport)
287313
if err == nil && c.Jar != nil {
288-
c.Jar.SetCookies(req.URL, r.Cookies())
314+
c.Jar.SetCookies(req.URL, resp.Cookies())
289315
}
290-
return r, err
316+
return
291317
}
292318

293-
// PostForm issues a POST to the specified URL,
294-
// with data's keys and values urlencoded as the request body.
319+
// PostForm issues a POST to the specified URL, with data's keys and
320+
// values URL-encoded as the request body.
295321
//
296-
// Caller should close r.Body when done reading from it.
322+
// When err is nil, resp always contains a non-nil resp.Body.
323+
// Caller should close resp.Body when done reading from it.
297324
//
298325
// PostForm is a wrapper around DefaultClient.PostForm
299-
func PostForm(url string, data url.Values) (r *Response, err error) {
326+
func PostForm(url string, data url.Values) (resp *Response, err error) {
300327
return DefaultClient.PostForm(url, data)
301328
}
302329

303330
// PostForm issues a POST to the specified URL,
304331
// with data's keys and values urlencoded as the request body.
305332
//
306-
// Caller should close r.Body when done reading from it.
307-
func (c *Client) PostForm(url string, data url.Values) (r *Response, err error) {
333+
// When err is nil, resp always contains a non-nil resp.Body.
334+
// Caller should close resp.Body when done reading from it.
335+
func (c *Client) PostForm(url string, data url.Values) (resp *Response, err error) {
308336
return c.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
309337
}
310338

@@ -318,7 +346,7 @@ func (c *Client) PostForm(url string, data url.Values) (r *Response, err error)
318346
// 307 (Temporary Redirect)
319347
//
320348
// Head is a wrapper around DefaultClient.Head
321-
func Head(url string) (r *Response, err error) {
349+
func Head(url string) (resp *Response, err error) {
322350
return DefaultClient.Head(url)
323351
}
324352

@@ -330,7 +358,7 @@ func Head(url string) (r *Response, err error) {
330358
// 302 (Found)
331359
// 303 (See Other)
332360
// 307 (Temporary Redirect)
333-
func (c *Client) Head(url string) (r *Response, err error) {
361+
func (c *Client) Head(url string) (resp *Response, err error) {
334362
req, err := NewRequest("HEAD", url, nil)
335363
if err != nil {
336364
return nil, err

src/pkg/net/http/client_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ func TestRedirects(t *testing.T) {
231231

232232
checkErr = errors.New("no redirects allowed")
233233
res, err = c.Get(ts.URL)
234-
finalUrl = res.Request.URL.String()
235234
if e, g := "Get /?n=1: no redirects allowed", fmt.Sprintf("%v", err); e != g {
236235
t.Errorf("with redirects forbidden, expected error %q, got %q", e, g)
237236
}

0 commit comments

Comments
 (0)