From b992c391d4aae64e147fc64c77ad41d61be8e2e7 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sat, 22 Oct 2016 09:47:05 -0700 Subject: net/http: add NoBody, don't return nil from NewRequest on zero bodies This is an alternate solution to https://golang.org/cl/31445 Instead of making NewRequest return a request with Request.Body == nil to signal a zero byte body, add a well-known variable that means explicitly zero. Too many tests inside Google (and presumably the outside world) broke. Change-Id: I78f6ecca8e8aa1e12179c234ccfb6bcf0ee29ba8 Reviewed-on: https://go-review.googlesource.com/31726 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot Reviewed-by: Joe Tsai --- src/net/http/request.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/net/http/request.go') diff --git a/src/net/http/request.go b/src/net/http/request.go index 551310cab0..5b0bbe2170 100644 --- a/src/net/http/request.go +++ b/src/net/http/request.go @@ -771,10 +771,14 @@ func NewRequest(method, urlStr string, body io.Reader) (*Request, error) { // For client requests, Request.ContentLength of 0 // means either actually 0, or unknown. The only way // to explicitly say that the ContentLength is zero is - // to set the Body to nil. + // to set the Body to nil. But turns out too much code + // depends on NewRequest returning a non-nil Body, + // so we use a well-known ReadCloser variable instead + // and have the http package also treat that sentinel + // variable to mean explicitly zero. if req.ContentLength == 0 { - req.Body = nil - req.GetBody = nil + req.Body = NoBody + req.GetBody = func() (io.ReadCloser, error) { return NoBody, nil } } } @@ -1252,7 +1256,7 @@ func (r *Request) isReplayable() bool { // outgoingLength reports the Content-Length of this outgoing (Client) request. // It maps 0 into -1 (unknown) when the Body is non-nil. func (r *Request) outgoingLength() int64 { - if r.Body == nil { + if r.Body == nil || r.Body == NoBody { return 0 } if r.ContentLength != 0 { -- cgit v1.3-5-g9baa