diff --git a/header.go b/header.go index b5f28ac..7dea91d 100644 --- a/header.go +++ b/header.go @@ -105,6 +105,7 @@ func (h *RequestHeader) Clear() { // CopyTo copies all the headers to dst. func (h *ResponseHeader) CopyTo(dst *ResponseHeader) { + dst.Clear() dst.StatusCode = h.StatusCode dst.ContentLength = h.ContentLength dst.ConnectionClose = h.ConnectionClose @@ -115,6 +116,7 @@ func (h *ResponseHeader) CopyTo(dst *ResponseHeader) { // CopyTo copies all the headers to dst. func (h *RequestHeader) CopyTo(dst *RequestHeader) { + dst.Clear() dst.Method = append(dst.Method[:0], h.Method...) dst.RequestURI = append(dst.RequestURI[:0], h.RequestURI...) dst.ContentLength = h.ContentLength diff --git a/http.go b/http.go index 4133a49..beccbbe 100644 --- a/http.go +++ b/http.go @@ -11,7 +11,8 @@ import ( // Request represents HTTP request. // -// It is forbidden copying Request instances. Create new instances instead. +// It is forbidden copying Request instances. Create new instances +// and use CopyTo() instead. type Request struct { // Request header Header RequestHeader @@ -35,7 +36,8 @@ type Request struct { // Response represents HTTP response. // -// It is forbidden copying Response instances. Create new instances instead. +// It is forbidden copying Response instances. Create new instances +// and use CopyTo() instead. type Response struct { // Response header Header ResponseHeader @@ -51,6 +53,27 @@ type Response struct { timeoutTimer *time.Timer } +// CopyTo copies req contents to dst. +func (req *Request) CopyTo(dst *Request) { + dst.Clear() + req.Header.CopyTo(&dst.Header) + dst.Body = append(dst.Body[:0], req.Body...) + if req.parsedURI { + dst.ParseURI() + } + if req.parsedPostArgs { + dst.ParsePostArgs() + } +} + +// CopyTo copies resp contents to dst. +func (resp *Response) CopyTo(dst *Response) { + dst.Clear() + resp.Header.CopyTo(&resp.Header) + dst.Body = append(dst.Body[:0], resp.Body...) + dst.SkipBody = resp.SkipBody +} + // ParseURI parses request uri and fills Request.URI. func (req *Request) ParseURI() { if req.parsedURI {