Skip to content

net/http: more Method=="" vs Method=="GET" inconsistency #13621

@rsc

Description

@rsc

These diffs look like they might be needed. I looked at code mentioning the string "GET".

diff --git a/src/net/http/client.go b/src/net/http/client.go
index c3f849e..3472cd4 100644
--- a/src/net/http/client.go
+++ b/src/net/http/client.go
@@ -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 == "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)
diff --git a/src/net/http/request.go b/src/net/http/request.go
index 9f74042..cb62ab9 100644
--- a/src/net/http/request.go
+++ b/src/net/http/request.go
@@ -1054,9 +1054,11 @@ 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
 }
diff --git a/src/net/http/transfer.go b/src/net/http/transfer.go
index 38b6f67..720c58c 100644
--- a/src/net/http/transfer.go
+++ b/src/net/http/transfer.go
@@ -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

Brad, can you look and pick up the ones that are real bugs, if any? Thanks.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions