From 0e1edf4c06cbe64db42937408b8f8c6f4c4f0803 Mon Sep 17 00:00:00 2001 From: Joe Shaw Date: Thu, 7 Mar 2024 15:41:07 -0500 Subject: [PATCH] add Request.CloneWithBody Fixes #104 --- fsthttp/request.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/fsthttp/request.go b/fsthttp/request.go index 329721a..6bb6ce5 100644 --- a/fsthttp/request.go +++ b/fsthttp/request.go @@ -210,7 +210,7 @@ func newClientRequest() (*Request, error) { } // Clone returns a copy of the request. The returned copy will have a nil Body -// field, and it's URL will have a nil User field. +// field, and its URL will have a nil User field. func (req *Request) Clone() *Request { return &Request{ Method: req.Method, @@ -230,6 +230,14 @@ func (req *Request) Clone() *Request { } } +// CloneWithBody returns a copy of the request, with the Body field set +// to the provided io.Reader. Its URL will have a nil User field. +func (req *Request) CloneWithBody(body io.Reader) *Request { + r := req.Clone() + r.Body = makeBodyFor(body) + return r +} + func cloneURL(u *url.URL) *url.URL { return &url.URL{ Scheme: u.Scheme, @@ -560,8 +568,8 @@ func makeBodyFor(r io.Reader) io.ReadCloser { return nil } - if b, ok := r.(*fastly.HTTPBody); ok { - return b + if rc, ok := r.(io.ReadCloser); ok { + return rc } return nopCloser{r}