]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: make Client follow redirects even if Request.Method is empty
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 3 Dec 2015 18:58:05 +0000 (18:58 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 3 Dec 2015 19:53:44 +0000 (19:53 +0000)
Fixes #12705

Change-Id: I69639d2b03777835b2697ff349a00ccab410aa49
Reviewed-on: https://go-review.googlesource.com/17318
Reviewed-by: Burcu Dogan <jbd@google.com>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/http/client.go
src/net/http/client_test.go

index 47f145a0cae6f6fbedff286df30864252e2362b6..3a8b2848599d40abcc611206a4fa9864e91f5bc0 100644 (file)
@@ -172,7 +172,7 @@ func (c *Client) send(req *Request) (*Response, error) {
 //
 // Generally Get, Post, or PostForm will be used instead of Do.
 func (c *Client) Do(req *Request) (resp *Response, err error) {
-       if req.Method == "GET" || req.Method == "HEAD" {
+       if req.Method == "" || req.Method == "GET" || req.Method == "HEAD" {
                return c.doFollowingRedirects(req, shouldRedirectGet)
        }
        if req.Method == "POST" || req.Method == "PUT" {
@@ -423,6 +423,9 @@ func (c *Client) doFollowingRedirects(ireq *Request, shouldRedirect func(int) bo
        }
 
        method := ireq.Method
+       if method == "" {
+               method = "GET"
+       }
        urlErr := &url.Error{
                Op:  method[0:1] + strings.ToLower(method[1:]),
                URL: urlStr,
index 40d5109862fe14b66734db035d278d47aa26c2af..e59ab2cd0e06d2bdb3697d639cd7b98c760974eb 100644 (file)
@@ -230,6 +230,13 @@ func TestClientRedirects(t *testing.T) {
                t.Errorf("with default client Do, expected error %q, got %q", e, g)
        }
 
+       // Requests with an empty Method should also redirect (Issue 12705)
+       greq.Method = ""
+       _, err = c.Do(greq)
+       if e, g := "Get /?n=10: stopped after 10 redirects", fmt.Sprintf("%v", err); e != g {
+               t.Errorf("with default client Do and empty Method, expected error %q, got %q", e, g)
+       }
+
        var checkErr error
        var lastVia []*Request
        c = &Client{CheckRedirect: func(_ *Request, via []*Request) error {