1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 // One of the copies, say from b to r2, could be avoided by using a more
15 // elaborate trick where the other copy is made during Request/Response.Write.
16 // This would complicate things too much, given that these functions are for
18 func drainBody(b io
.ReadCloser
) (r1
, r2 io
.ReadCloser
, err os
.Error
) {
20 if _
, err
= buf
.ReadFrom(b
); err
!= nil {
23 if err
= b
.Close(); err
!= nil {
26 return nopCloser
{&buf
}, nopCloser
{bytes
.NewBuffer(buf
.Bytes())}, nil
29 // DumpRequest returns the wire representation of req,
30 // optionally including the request body, for debugging.
31 // DumpRequest is semantically a no-op, but in order to
32 // dump the body, it reads the body data into memory and
33 // changes req.Body to refer to the in-memory copy.
34 func DumpRequest(req
*Request
, body
bool) (dump
[]byte, err os
.Error
) {
37 if !body || req
.Body
== nil {
40 save
, req
.Body
, err
= drainBody(req
.Body
)
54 // DumpResponse is like DumpRequest but dumps a response.
55 func DumpResponse(resp
*Response
, body
bool) (dump
[]byte, err os
.Error
) {
58 savecl
:= resp
.ContentLength
59 if !body || resp
.Body
== nil {
61 resp
.ContentLength
= 0
63 save
, resp
.Body
, err
= drainBody(resp
.Body
)
70 resp
.ContentLength
= savecl