1
0
mirror of https://github.com/golang/go synced 2024-11-23 15:20:03 -07:00

net/http: deep copy Request.TransferEncoding

The existing implementation in Request.Clone() assigns the wrong
pointer to r2.TransferEncoding.

Fixes #41907

Change-Id: I7f220a41b1b46a55d1a1005e47c6dd69478cb025
Reviewed-on: https://go-review.googlesource.com/c/go/+/261258
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Trust: Emmanuel Odeke <emm.odeke@gmail.com>
This commit is contained in:
dqu123 2020-10-10 16:25:07 -04:00 committed by Brad Fitzpatrick
parent e92ce92662
commit 0f53453b32
2 changed files with 22 additions and 1 deletions

View File

@ -382,7 +382,7 @@ func (r *Request) Clone(ctx context.Context) *Request {
if s := r.TransferEncoding; s != nil { if s := r.TransferEncoding; s != nil {
s2 := make([]string, len(s)) s2 := make([]string, len(s))
copy(s2, s) copy(s2, s)
r2.TransferEncoding = s r2.TransferEncoding = s2
} }
r2.Form = cloneURLValues(r.Form) r2.Form = cloneURLValues(r.Form)
r2.PostForm = cloneURLValues(r.PostForm) r2.PostForm = cloneURLValues(r.PostForm)

View File

@ -828,6 +828,27 @@ func TestWithContextDeepCopiesURL(t *testing.T) {
} }
} }
// Ensure that Request.Clone creates a deep copy of TransferEncoding.
// See issue 41907.
func TestRequestCloneTransferEncoding(t *testing.T) {
body := strings.NewReader("body")
req, _ := NewRequest("POST", "https://example.org/", body)
req.TransferEncoding = []string{
"encoding1",
}
clonedReq := req.Clone(context.Background())
// modify original after deep copy
req.TransferEncoding[0] = "encoding2"
if req.TransferEncoding[0] != "encoding2" {
t.Error("expected req.TransferEncoding to be changed")
}
if clonedReq.TransferEncoding[0] != "encoding1" {
t.Error("expected clonedReq.TransferEncoding to be unchanged")
}
}
func TestNoPanicOnRoundTripWithBasicAuth_h1(t *testing.T) { func TestNoPanicOnRoundTripWithBasicAuth_h1(t *testing.T) {
testNoPanicWithBasicAuth(t, h1Mode) testNoPanicWithBasicAuth(t, h1Mode)
} }