1
0
mirror of https://github.com/golang/go synced 2024-11-22 08:34:40 -07:00

net/http: simplify http.Request.Clone

By using maps.Clone and omitting nil checks when calling
Header.Clone().

I'm not using slices.Clone because the result of slices.Clone
may have additional unused capacity.

Change-Id: I6054b6091d2526975b0087abd86afa42412e90f2
This commit is contained in:
Jes Cok 2024-08-26 21:14:57 +08:00
parent 96d8ff00c2
commit 9fd5dd5907

View File

@ -15,6 +15,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"maps"
"mime" "mime"
"mime/multipart" "mime/multipart"
"net/http/httptrace" "net/http/httptrace"
@ -390,12 +391,8 @@ func (r *Request) Clone(ctx context.Context) *Request {
*r2 = *r *r2 = *r
r2.ctx = ctx r2.ctx = ctx
r2.URL = cloneURL(r.URL) r2.URL = cloneURL(r.URL)
if r.Header != nil { r2.Header = r.Header.Clone()
r2.Header = r.Header.Clone() r2.Trailer = r.Trailer.Clone()
}
if r.Trailer != nil {
r2.Trailer = r.Trailer.Clone()
}
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)
@ -411,13 +408,7 @@ func (r *Request) Clone(ctx context.Context) *Request {
copy(s2, s) copy(s2, s)
r2.matches = s2 r2.matches = s2
} }
if s := r.otherValues; s != nil { r2.otherValues = maps.Clone(r.otherValues)
s2 := make(map[string]string, len(s))
for k, v := range s {
s2[k] = v
}
r2.otherValues = s2
}
return r2 return r2
} }