Skip to content

Commit fae8e09

Browse files
neilddmitshur
authored andcommitted
[release-branch.go1.14] net/http: make Transport.RoundTrip preserve Requests
Ensure that the exact Request passed to Transport.RoundTrip is returned in the Response. Do not replace the Request with a copy when resetting the request body. Updates #39533. Fixes #40973. Change-Id: Ie6fb080c24b0f6625b0761b7aa542af3d2411817 Reviewed-on: https://go-review.googlesource.com/c/go/+/237560 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Russ Cox <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/249880 Run-TryBot: Dmitri Shuralyov <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent 0d8ee35 commit fae8e09

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/net/http/transport.go

+2
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ func (t *Transport) roundTrip(req *Request) (*Response, error) {
511511
}
512512
}
513513

514+
origReq := req
514515
req = setupRewindBody(req)
515516

516517
if altRT := t.alternateRoundTripper(req); altRT != nil {
@@ -572,6 +573,7 @@ func (t *Transport) roundTrip(req *Request) (*Response, error) {
572573
resp, err = pconn.roundTrip(treq)
573574
}
574575
if err == nil {
576+
resp.Request = origReq
575577
return resp, nil
576578
}
577579

src/net/http/transport_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -3490,7 +3490,8 @@ func TestRetryRequestsOnError(t *testing.T) {
34903490

34913491
for i := 0; i < 3; i++ {
34923492
t0 := time.Now()
3493-
res, err := c.Do(tc.req())
3493+
req := tc.req()
3494+
res, err := c.Do(req)
34943495
if err != nil {
34953496
if time.Since(t0) < MaxWriteWaitBeforeConnReuse/2 {
34963497
mu.Lock()
@@ -3501,6 +3502,9 @@ func TestRetryRequestsOnError(t *testing.T) {
35013502
t.Skipf("connection likely wasn't recycled within %d, interfering with actual test; skipping", MaxWriteWaitBeforeConnReuse)
35023503
}
35033504
res.Body.Close()
3505+
if res.Request != req {
3506+
t.Errorf("Response.Request != original request; want identical Request")
3507+
}
35043508
}
35053509

35063510
mu.Lock()

0 commit comments

Comments
 (0)