Added CopyTo() to Request and Response

This commit is contained in:
Aliaksandr Valialkin
2015-11-09 16:46:41 +02:00
parent 3aa2987131
commit 0f67cb2ee4
2 changed files with 27 additions and 2 deletions
+2
View File
@@ -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
+25 -2
View File
@@ -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 {