Skip to content

Commit

Permalink
add Request.CloneWithBody
Browse files Browse the repository at this point in the history
Fixes #104
  • Loading branch information
joeshaw committed Mar 7, 2024
1 parent bd600e4 commit 0e1edf4
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions fsthttp/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit 0e1edf4

Please sign in to comment.