Commit 18227bb7 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http: be more consistent about Request.Method "" vs "GET"

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/17981Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent 761ac75a
...@@ -179,10 +179,11 @@ func (c *Client) send(req *Request) (*Response, error) { ...@@ -179,10 +179,11 @@ func (c *Client) send(req *Request) (*Response, error) {
// //
// Generally Get, Post, or PostForm will be used instead of Do. // Generally Get, Post, or PostForm will be used instead of Do.
func (c *Client) Do(req *Request) (resp *Response, err error) { 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) return c.doFollowingRedirects(req, shouldRedirectGet)
} }
if req.Method == "POST" || req.Method == "PUT" { if method == "POST" || method == "PUT" {
return c.doFollowingRedirects(req, shouldRedirectPost) return c.doFollowingRedirects(req, shouldRedirectPost)
} }
return c.send(req) return c.send(req)
......
...@@ -1057,11 +1057,13 @@ func (r *Request) closeBody() { ...@@ -1057,11 +1057,13 @@ func (r *Request) closeBody() {
} }
func (r *Request) isReplayable() bool { func (r *Request) isReplayable() bool {
return r.Body == nil && if r.Body == nil {
(r.Method == "GET" || switch valueOrDefault(r.Method, "GET") {
r.Method == "HEAD" || case "GET", "HEAD", "OPTIONS", "TRACE":
r.Method == "OPTIONS" || return true
r.Method == "TRACE") }
}
return false
} }
func validHostHeader(h string) bool { func validHostHeader(h string) bool {
......
...@@ -56,7 +56,7 @@ func newTransferWriter(r interface{}) (t *transferWriter, err error) { ...@@ -56,7 +56,7 @@ func newTransferWriter(r interface{}) (t *transferWriter, err error) {
if rr.ContentLength != 0 && rr.Body == nil { if rr.ContentLength != 0 && rr.Body == nil {
return nil, fmt.Errorf("http: Request.ContentLength=%d with nil Body", rr.ContentLength) 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.Body = rr.Body
t.BodyCloser = rr.Body t.BodyCloser = rr.Body
t.ContentLength = rr.ContentLength t.ContentLength = rr.ContentLength
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment