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/http.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/net/http/http.go') diff --git a/src/net/http/http.go b/src/net/http/http.go index 7e0b77506b..40018453c6 100644 --- a/src/net/http/http.go +++ b/src/net/http/http.go @@ -5,6 +5,7 @@ package http import ( + "io" "strconv" "strings" "time" @@ -81,3 +82,21 @@ func hexEscapeNonASCII(s string) string { } return string(b) } + +// NoBody is an io.ReadCloser with no bytes. Read always returns EOF +// and Close always returns nil. It can be used in an outgoing client +// request to explicitly signal that a request has zero bytes. +// An alternative, however, is to simply set Request.Body to nil. +var NoBody = noBody{} + +type noBody struct{} + +func (noBody) Read([]byte) (int, error) { return 0, io.EOF } +func (noBody) Close() error { return nil } +func (noBody) WriteTo(io.Writer) (int64, error) { return 0, nil } + +var ( + // verify that an io.Copy from NoBody won't require a buffer: + _ io.WriterTo = NoBody + _ io.ReadCloser = NoBody +) -- cgit v1.3