github.com/crowdsecurity/crowdsec@v1.6.1/pkg/apiclient/clone.go (about) 1 package apiclient 2 3 import ( 4 "bytes" 5 "io" 6 "net/http" 7 ) 8 9 // cloneRequest returns a clone of the provided *http.Request. The clone is a 10 // shallow copy of the struct and its Header map. 11 func cloneRequest(r *http.Request) *http.Request { 12 // shallow copy of the struct 13 r2 := new(http.Request) 14 *r2 = *r 15 // deep copy of the Header 16 r2.Header = make(http.Header, len(r.Header)) 17 18 for k, s := range r.Header { 19 r2.Header[k] = append([]string(nil), s...) 20 } 21 22 if r.Body != nil { 23 var b bytes.Buffer 24 25 b.ReadFrom(r.Body) 26 27 r.Body = io.NopCloser(&b) 28 r2.Body = io.NopCloser(bytes.NewReader(b.Bytes())) 29 } 30 31 return r2 32 }