From: Brad Fitzpatrick Date: Tue, 20 Dec 2016 17:59:37 +0000 (+0000) Subject: net/http, doc: more redirect documentation X-Git-Tag: go1.8rc1~72 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=2eae691d56125c62e0e5f0e4a3bd42e67e423f01;p=gostls13.git net/http, doc: more redirect documentation Updates #18347 Updates #9348 Change-Id: I115203b0be3eb2e7e269ff28e2f3c47eeca86038 Reviewed-on: https://go-review.googlesource.com/34657 Reviewed-by: Russ Cox --- diff --git a/doc/go1.8.html b/doc/go1.8.html index 5d9e9f5b39..83556521fa 100644 --- a/doc/go1.8.html +++ b/doc/go1.8.html @@ -1283,9 +1283,17 @@ crypto/x509: return error for missing SerialNumber (CL 27238)
  • - The Client now supports 307 and 308 redirects. - If the redirect requires resending the request body, - the request must have the new + The Client now supports 301, 307, and 308 redirects. + + For example, Client.Post now follows 301 + redirects, converting them to GET requests + without bodies, like it did for 302 and 303 redirect responses + previously. + + The Client now also follows 307 and 308 + redirects, preserving the original request method and body, if + any. If the redirect requires resending the request body, the + request must have the new Request.GetBody field defined. NewRequest diff --git a/src/net/http/client.go b/src/net/http/client.go index 9308c5c968..7eb87c6d10 100644 --- a/src/net/http/client.go +++ b/src/net/http/client.go @@ -470,6 +470,15 @@ func redirectBehavior(reqMethod string, resp *Response, via []*Request) (redirec // the returned Response.Body is already closed. // // Generally Get, Post, or PostForm will be used instead of Do. +// +// If the server replies with a redirect, the Client first uses the +// CheckRedirect function to determine whether the redirect should be +// followed. If permitted, a 301, 302, or 303 redirect causes +// subsequent requests to use HTTP method "GET", with no body. +// A 307 or 308 redirect preserves the original HTTP method and body, +// provided that the Request.GetBody function is defined. +// The NewRequest function automatically sets GetBody for common +// standard library body types. func (c *Client) Do(req *Request) (*Response, error) { if req.URL == nil { req.closeBody() @@ -673,6 +682,9 @@ func defaultCheckRedirect(req *Request, via []*Request) error { // Post is a wrapper around DefaultClient.Post. // // To set custom headers, use NewRequest and DefaultClient.Do. +// +// See the Client.Do method documentation for details on how redirects +// are handled. func Post(url string, contentType string, body io.Reader) (resp *Response, err error) { return DefaultClient.Post(url, contentType, body) } @@ -685,6 +697,9 @@ func Post(url string, contentType string, body io.Reader) (resp *Response, err e // request. // // To set custom headers, use NewRequest and Client.Do. +// +// See the Client.Do method documentation for details on how redirects +// are handled. func (c *Client) Post(url string, contentType string, body io.Reader) (resp *Response, err error) { req, err := NewRequest("POST", url, body) if err != nil { @@ -704,6 +719,9 @@ func (c *Client) Post(url string, contentType string, body io.Reader) (resp *Res // Caller should close resp.Body when done reading from it. // // PostForm is a wrapper around DefaultClient.PostForm. +// +// See the Client.Do method documentation for details on how redirects +// are handled. func PostForm(url string, data url.Values) (resp *Response, err error) { return DefaultClient.PostForm(url, data) } @@ -716,6 +734,9 @@ func PostForm(url string, data url.Values) (resp *Response, err error) { // // When err is nil, resp always contains a non-nil resp.Body. // Caller should close resp.Body when done reading from it. +// +// See the Client.Do method documentation for details on how redirects +// are handled. func (c *Client) PostForm(url string, data url.Values) (resp *Response, err error) { return c.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode())) } diff --git a/src/net/http/request.go b/src/net/http/request.go index 96fa619683..283595f462 100644 --- a/src/net/http/request.go +++ b/src/net/http/request.go @@ -731,6 +731,12 @@ func validMethod(method string) bool { // net/http/httptest package, use ReadRequest, or manually update the // Request fields. See the Request type's documentation for the // difference between inbound and outbound request fields. +// +// If body is of type *bytes.Buffer, *bytes.Reader, or +// *strings.Reader, the returned request's ContentLength is set to its +// exact value (instead of -1), GetBody is populated (so 307 and 308 +// redirects can replay the body), and Body is set to NoBody if the +// ContentLength is 0. func NewRequest(method, urlStr string, body io.Reader) (*Request, error) { if method == "" { // We document that "" means "GET" for Request.Method, and people have