Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: make Request.Clone create fresh copies for matches and otherValues #64913

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/net/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,20 @@ func (r *Request) Clone(ctx context.Context) *Request {
r2.Form = cloneURLValues(r.Form)
r2.PostForm = cloneURLValues(r.PostForm)
r2.MultipartForm = cloneMultipartForm(r.MultipartForm)

// Copy matches and otherValues. See issue 61410.
if s := r.matches; s != nil {
s2 := make([]string, len(s))
copy(s2, s)
r2.matches = s2
}
if s := r.otherValues; s != nil {
s2 := make(map[string]string, len(s))
for k, v := range s {
s2[k] = v
}
r2.otherValues = s2
}
return r2
}

Expand Down Expand Up @@ -1427,6 +1441,8 @@ func (r *Request) PathValue(name string) string {
return r.otherValues[name]
}

// SetPathValue sets name to value, so that subsequent calls to r.PathValue(name)
// return value.
func (r *Request) SetPathValue(name, value string) {
if i := r.patIndex(name); i >= 0 {
r.matches[i] = value
Expand Down
27 changes: 27 additions & 0 deletions src/net/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,33 @@ func TestRequestCloneTransferEncoding(t *testing.T) {
}
}

// Ensure that Request.Clone works correctly with PathValue.
// See issue 64911.
func TestRequestClonePathValue(t *testing.T) {
req, _ := http.NewRequest("GET", "https://example.org/", nil)
req.SetPathValue("p1", "orig")

clonedReq := req.Clone(context.Background())
clonedReq.SetPathValue("p2", "copy")

// Ensure that any modifications to the cloned
// request do not pollute the original request.
if g, w := req.PathValue("p2"), ""; g != w {
t.Fatalf("p2 mismatch got %q, want %q", g, w)
}
if g, w := req.PathValue("p1"), "orig"; g != w {
t.Fatalf("p1 mismatch got %q, want %q", g, w)
}

// Assert on the changes to the cloned request.
if g, w := clonedReq.PathValue("p1"), "orig"; g != w {
t.Fatalf("p1 mismatch got %q, want %q", g, w)
}
if g, w := clonedReq.PathValue("p2"), "copy"; g != w {
t.Fatalf("p2 mismatch got %q, want %q", g, w)
}
}

// Issue 34878: verify we don't panic when including basic auth (Go 1.13 regression)
func TestNoPanicOnRoundTripWithBasicAuth(t *testing.T) { run(t, testNoPanicWithBasicAuth) }
func testNoPanicWithBasicAuth(t *testing.T, mode testMode) {
Expand Down