Skip to content

Commit 2349357

Browse files
committed
net/http: 308 redirects should use the previous hop's body
On a 301 redirect, the HTTP client changes the request to be a GET with no body. On a 308 redirect, the client leaves the request method and body unchanged. A 308 following a 301 should preserve the rewritten request from the first redirect: GET with no body. We were preserving the method, but sending the original body. Fix this. Fixes #70180 Change-Id: Ie20027a6058a82bfdffc7197d07ac6c7f98099e2 Reviewed-on: https://go-review.googlesource.com/c/go/+/626055 Reviewed-by: Jonathan Amsterdam <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 2c7b5ba commit 2349357

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/net/http/client.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ func (c *Client) do(req *Request) (retres *Response, reterr error) {
611611

612612
// Redirect behavior:
613613
redirectMethod string
614-
includeBody bool
614+
includeBody = true
615615
)
616616
uerr := func(err error) error {
617617
// the body may have been closed already by c.send()
@@ -728,11 +728,16 @@ func (c *Client) do(req *Request) (retres *Response, reterr error) {
728728
return nil, uerr(err)
729729
}
730730

731-
var shouldRedirect bool
732-
redirectMethod, shouldRedirect, includeBody = redirectBehavior(req.Method, resp, reqs[0])
731+
var shouldRedirect, includeBodyOnHop bool
732+
redirectMethod, shouldRedirect, includeBodyOnHop = redirectBehavior(req.Method, resp, reqs[0])
733733
if !shouldRedirect {
734734
return resp, nil
735735
}
736+
if !includeBodyOnHop {
737+
// Once a hop drops the body, we never send it again
738+
// (because we're now handling a redirect for a request with no body).
739+
includeBody = false
740+
}
736741

737742
req.closeBody()
738743
}

src/net/http/client_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func TestPostRedirects(t *testing.T) {
346346
`POST /?code=307&next=303,308,302 "c307"`,
347347
`POST /?code=303&next=308,302 "c307"`,
348348
`GET /?code=308&next=302 ""`,
349-
`GET /?code=302 "c307"`,
349+
`GET /?code=302 ""`,
350350
`GET / ""`,
351351
`POST /?code=308&next=302,301 "c308"`,
352352
`POST /?code=302&next=301 "c308"`,
@@ -376,7 +376,7 @@ func TestDeleteRedirects(t *testing.T) {
376376
`DELETE /?code=301&next=302,308 "c301"`,
377377
`GET /?code=302&next=308 ""`,
378378
`GET /?code=308 ""`,
379-
`GET / "c301"`,
379+
`GET / ""`,
380380
`DELETE /?code=302&next=302 "c302"`,
381381
`GET /?code=302 ""`,
382382
`GET / ""`,
@@ -385,7 +385,7 @@ func TestDeleteRedirects(t *testing.T) {
385385
`DELETE /?code=307&next=301,308,303,302,304 "c307"`,
386386
`DELETE /?code=301&next=308,303,302,304 "c307"`,
387387
`GET /?code=308&next=303,302,304 ""`,
388-
`GET /?code=303&next=302,304 "c307"`,
388+
`GET /?code=303&next=302,304 ""`,
389389
`GET /?code=302&next=304 ""`,
390390
`GET /?code=304 ""`,
391391
`DELETE /?code=308&next=307 "c308"`,

0 commit comments

Comments
 (0)