]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: be more consistent about Request.Method "" vs "GET"
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 17 Dec 2015 19:36:43 +0000 (19:36 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 17 Dec 2015 20:21:44 +0000 (20:21 +0000)
Patch from Russ.

No bug identified, but I didn't search exhaustively. The new code is
easier to read.

Fixes #13621

Change-Id: Ifda936e4101116fa254ead950b5fe06adb14e977
Reviewed-on: https://go-review.googlesource.com/17981
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/net/http/client.go
src/net/http/request.go
src/net/http/transfer.go

index c3f849e962bdf2e860fe54af2e1df9c47475740a..dd099bb3165c03a4438a83cf9c4d5070eb9ab0f1 100644 (file)
@@ -179,10 +179,11 @@ 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 == "" || req.Method == "GET" || req.Method == "HEAD" {
+       method := valueOrDefault(req.Method, "GET")
+       if method == "" || method == "GET" || method == "HEAD" {
                return c.doFollowingRedirects(req, shouldRedirectGet)
        }
-       if req.Method == "POST" || req.Method == "PUT" {
+       if method == "POST" || method == "PUT" {
                return c.doFollowingRedirects(req, shouldRedirectPost)
        }
        return c.send(req)
index 01575f33a5ae9f016031a373a65a693298062c10..d706d8e1b600abb10d16c89bedc25a99ea53b4ef 100644 (file)
@@ -1057,11 +1057,13 @@ func (r *Request) closeBody() {
 }
 
 func (r *Request) isReplayable() bool {
-       return r.Body == nil &&
-               (r.Method == "GET" ||
-                       r.Method == "HEAD" ||
-                       r.Method == "OPTIONS" ||
-                       r.Method == "TRACE")
+       if r.Body == nil {
+               switch valueOrDefault(r.Method, "GET") {
+               case "GET", "HEAD", "OPTIONS", "TRACE":
+                       return true
+               }
+       }
+       return false
 }
 
 func validHostHeader(h string) bool {
index b452f33ad6caefc6aa3057381ddd3244fd6cd93e..480226af824c64564cb0228cb90485043af3b1fa 100644 (file)
@@ -56,7 +56,7 @@ func newTransferWriter(r interface{}) (t *transferWriter, err error) {
                if rr.ContentLength != 0 && rr.Body == nil {
                        return nil, fmt.Errorf("http: Request.ContentLength=%d with nil Body", rr.ContentLength)
                }
-               t.Method = rr.Method
+               t.Method = valueOrDefault(rr.Method, "GET")
                t.Body = rr.Body
                t.BodyCloser = rr.Body
                t.ContentLength = rr.ContentLength