[go] net/http: add Request.GetBody func for 307/308 redirects

425 views
Skip to first unread message

Brad Fitzpatrick (Gerrit)

unread,
Oct 21, 2016, 4:48:10 PM10/21/16
to Emmanuel Odeke, Ian Lance Taylor, Brad Fitzpatrick, golang-co...@googlegroups.com
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>

Brad Fitzpatrick (Gerrit)

unread,
Oct 21, 2016, 4:49:10 PM10/21/16
to Brad Fitzpatrick, Emmanuel Odeke, golang-co...@googlegroups.com
Brad Fitzpatrick has posted comments on this change.

net/http: add Request.GetBody func for 307/308 redirects

Patch Set 1:

Thoughts?

--
https://go-review.googlesource.com/31733
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Emmanuel Odeke <emm....@gmail.com>
Gerrit-HasComments: No

Brad Fitzpatrick (Gerrit)

unread,
Oct 21, 2016, 5:01:17 PM10/21/16
to Brad Fitzpatrick, Emmanuel Odeke, golang-co...@googlegroups.com
Reviewers: Emmanuel Odeke

Brad Fitzpatrick uploaded a new patch set:
https://go-review.googlesource.com/31733

net/http: add Request.GetBody func for 307/308 redirects

Updates #10767

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(-)


Emmanuel Odeke (Gerrit)

unread,
Oct 21, 2016, 6:17:35 PM10/21/16
to Brad Fitzpatrick, golang-co...@googlegroups.com
Emmanuel Odeke has posted comments on this change.

net/http: add Request.GetBody func for 307/308 redirects

Patch Set 2:

(1 comment)

documentation nit

https://go-review.googlesource.com/#/c/31733/2/src/net/http/request.go
File src/net/http/request.go:

Line 154: // GetBody optionally defines a func to return a new copy of
defines an optional function


--
https://go-review.googlesource.com/31733
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Emmanuel Odeke <emm....@gmail.com>
Gerrit-HasComments: Yes

Brad Fitzpatrick (Gerrit)

unread,
Oct 21, 2016, 9:08:04 PM10/21/16
to Brad Fitzpatrick, Emmanuel Odeke, golang-co...@googlegroups.com
Reviewers: Emmanuel Odeke

Brad Fitzpatrick uploaded a new patch set:
https://go-review.googlesource.com/31733

net/http: add Request.GetBody func for 307/308 redirects

Updates #10767

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(-)


Brad Fitzpatrick (Gerrit)

unread,
Oct 21, 2016, 9:10:56 PM10/21/16
to Brad Fitzpatrick, Emmanuel Odeke, golang-co...@googlegroups.com
Brad Fitzpatrick has posted comments on this change.

net/http: add Request.GetBody func for 307/308 redirects

Patch Set 3: Run-TryBot+1

--
https://go-review.googlesource.com/31733
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Emmanuel Odeke <emm....@gmail.com>
Gerrit-HasComments: No

Gobot Gobot (Gerrit)

unread,
Oct 21, 2016, 9:11:55 PM10/21/16
to Brad Fitzpatrick, Emmanuel Odeke, golang-co...@googlegroups.com
Gobot Gobot has posted comments on this change.

net/http: add Request.GetBody func for 307/308 redirects

Patch Set 3:

TryBots beginning. Status page: http://farmer.golang.org/try?commit=33507eeb

Gobot Gobot (Gerrit)

unread,
Oct 21, 2016, 9:18:24 PM10/21/16
to Brad Fitzpatrick, Emmanuel Odeke, golang-co...@googlegroups.com
Gobot Gobot has posted comments on this change.

net/http: add Request.GetBody func for 307/308 redirects

Patch Set 3: TryBot-Result+1

TryBots are happy.

--
https://go-review.googlesource.com/31733
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Emmanuel Odeke <emm....@gmail.com>
Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
Gerrit-HasComments: No

Emmanuel Odeke (Gerrit)

unread,
Oct 21, 2016, 9:21:48 PM10/21/16
to Brad Fitzpatrick, Gobot Gobot, golang-co...@googlegroups.com
Emmanuel Odeke has posted comments on this change.

net/http: add Request.GetBody func for 307/308 redirects

Patch Set 3: Code-Review+1

LGTM!

Brad Fitzpatrick (Gerrit)

unread,
Oct 21, 2016, 9:22:53 PM10/21/16
to Brad Fitzpatrick, Emmanuel Odeke, Gobot Gobot, golang-co...@googlegroups.com
Brad Fitzpatrick has posted comments on this change.

net/http: add Request.GetBody func for 307/308 redirects

Patch Set 3: Code-Review+2

Brad Fitzpatrick (Gerrit)

unread,
Oct 21, 2016, 9:22:59 PM10/21/16
to Brad Fitzpatrick, golang-...@googlegroups.com, Emmanuel Odeke, Gobot Gobot, golang-co...@googlegroups.com
Brad Fitzpatrick has submitted this change and it was merged.

net/http: add Request.GetBody func for 307/308 redirects

Updates #10767

Change-Id: I197535f71bc2dc45e783f38d8031aa717d50fd80
Reviewed-on: https://go-review.googlesource.com/31733
Run-TryBot: Brad Fitzpatrick <brad...@golang.org>
TryBot-Result: Gobot Gobot <go...@golang.org>
Reviewed-by: Emmanuel Odeke <emm....@gmail.com>
Reviewed-by: Brad Fitzpatrick <brad...@golang.org>
---
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(-)

Approvals:
Brad Fitzpatrick: Looks good to me, approved; Run TryBots
Emmanuel Odeke: Looks good to me, but someone else must approve
Gobot Gobot: TryBots succeeded
Reply all
Reply to author
Forward
0 new messages