Reviewers: Emmanuel Odeke
Brad Fitzpatrick uploaded a change:
https://go-review.googlesource.com/31733
net/http: add Request.GetBody func for 307/308 redirects
DO NOT SUBMIT -- finish this commit message
Updates #NNN
Change-Id: I197535f71bc2dc45e783f38d8031aa717d50fd80
---
M src/net/http/client.go
M src/net/http/request.go
M src/net/http/request_test.go
3 files changed, 72 insertions(+), 0 deletions(-)
diff --git a/src/net/http/client.go b/src/net/http/client.go
index 39c38bd..9b60f35 100644
--- a/src/net/http/client.go
+++ b/src/net/http/client.go
@@ -485,8 +485,15 @@
Cancel: ireq.Cancel,
ctx: ireq.ctx,
}
+ if ireq.GetBody != nil {
+ req.Body, err = ireq.GetBody()
+ if err != nil {
+ return nil, uerr(err)
+ }
+ }
if ireq.Method == "POST" || ireq.Method == "PUT" {
req.Method = "GET"
+ req.Body = nil // TODO: fix this when 307/308 support happens
}
// Copy the initial request's Header values
// (at least the safe ones). Do this before
diff --git a/src/net/http/request.go b/src/net/http/request.go
index 83d6c81..8332510 100644
--- a/src/net/http/request.go
+++ b/src/net/http/request.go
@@ -151,6 +151,14 @@
// Handler does not need to.
Body io.ReadCloser
+ // GetBody optionally defines a func to return a new copy of
+ // Body. It used for client requests when a redirect requires
+ // reading the body more than once. Use of GetBody still
+ // requires setting Body.
+ //
+ // For server requests it is unused.
+ GetBody func() (io.ReadCloser, error)
+
// ContentLength records the length of the associated content.
// The value -1 indicates that the length is unknown.
// Values >= 0 indicate that the given number of bytes may
@@ -738,10 +746,25 @@
switch v := body.(type) {
case *bytes.Buffer:
req.ContentLength = int64(v.Len())
+ buf := v.Bytes()
+ req.GetBody = func() (io.ReadCloser, error) {
+ r := bytes.NewReader(buf)
+ return ioutil.NopCloser(r), nil
+ }
case *bytes.Reader:
req.ContentLength = int64(v.Len())
+ snapshot := *v
+ req.GetBody = func() (io.ReadCloser, error) {
+ r := snapshot
+ return ioutil.NopCloser(&r), nil
+ }
case *strings.Reader:
req.ContentLength = int64(v.Len())
+ snapshot := *v
+ req.GetBody = func() (io.ReadCloser, error) {
+ r := snapshot
+ return ioutil.NopCloser(&r), nil
+ }
default:
req.ContentLength = -1 // unknown
}
@@ -751,6 +774,7 @@
// to set the Body to nil.
if req.ContentLength == 0 {
req.Body = nil
+ req.GetBody = nil
}
}
diff --git a/src/net/http/request_test.go b/src/net/http/request_test.go
index f12b41c..e463d79 100644
--- a/src/net/http/request_test.go
+++ b/src/net/http/request_test.go
@@ -784,6 +784,47 @@
}
}
+// verify that NewRequest sets Request.GetBody and that it works
+func TestNewRequestGetBody(t *testing.T) {
+ tests := []struct {
+ r io.Reader
+ }{
+ {r: strings.NewReader("hello")},
+ {r: bytes.NewReader([]byte("hello"))},
+ {r: bytes.NewBuffer([]byte("hello"))},
+ }
+ for i, tt := range tests {
+ req, err := NewRequest("POST", "
http://foo.tld/", tt.r)
+ if err != nil {
+ t.Errorf("test[%d]: %v", i, err)
+ continue
+ }
+ if req.Body == nil {
+ t.Errorf("test[%d]: Body = nil", i)
+ continue
+ }
+ if req.GetBody == nil {
+ t.Errorf("test[%d]: GetBody = nil", i)
+ continue
+ }
+ slurp1, err := ioutil.ReadAll(req.Body)
+ if err != nil {
+ t.Errorf("test[%d]: ReadAll(Body) = %v", i, err)
+ }
+ newBody, err := req.GetBody()
+ if err != nil {
+ t.Errorf("test[%d]: GetBody = %v", i, err)
+ }
+ slurp2, err := ioutil.ReadAll(newBody)
+ if err != nil {
+ t.Errorf("test[%d]: ReadAll(GetBody()) = %v", i, err)
+ }
+ if string(slurp1) != string(slurp2) {
+ t.Errorf("test[%d]: Body %q != GetBody %q", i, slurp1, slurp2)
+ }
+ }
+}
+
func testMissingFile(t *testing.T, req *Request) {
f, fh, err := req.FormFile("missing")
if f != nil {
--
https://go-review.googlesource.com/31733
Gerrit-Reviewer: Emmanuel Odeke <
emm....@gmail.com>