diff options
author | Petar Maymounkov <petarm@gmail.com> | 2010-02-19 07:51:51 -0800 |
---|---|---|
committer | Petar Maymounkov <petarm@gmail.com> | 2010-02-19 07:51:51 -0800 |
commit | dfef91c08aa8ab5be55da221fa9a8f8f436c94e1 (patch) | |
tree | a07c70e80562b5c64f81b08685aaf3c9195d103f | |
parent | 846a4441a1de8f0dc05727453bd49aaea566e78a (diff) | |
download | golang-dfef91c08aa8ab5be55da221fa9a8f8f436c94e1.tar.gz |
http: add DumpRequest, DumpResponse, for debugging
R=rsc
CC=golang-dev
http://codereview.appspot.com/206050
Committer: Russ Cox <rsc@golang.org>
-rw-r--r-- | src/pkg/http/Makefile | 1 | ||||
-rw-r--r-- | src/pkg/http/dump.go | 76 |
2 files changed, 77 insertions, 0 deletions
diff --git a/src/pkg/http/Makefile b/src/pkg/http/Makefile index 3966372a7..d7149d70b 100644 --- a/src/pkg/http/Makefile +++ b/src/pkg/http/Makefile @@ -8,6 +8,7 @@ TARG=http GOFILES=\ chunked.go\ client.go\ + dump.go\ fs.go\ lex.go\ persist.go\ diff --git a/src/pkg/http/dump.go b/src/pkg/http/dump.go new file mode 100644 index 000000000..73ac97973 --- /dev/null +++ b/src/pkg/http/dump.go @@ -0,0 +1,76 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package http + +import ( + "bytes" + "io" + "os" +) + + +// One of the copies, say from b to r2, could be avoided by using a more +// elaborate trick where the other copy is made during Request/Response.Write. +// This would complicate things too much, given that these functions are for +// debugging only. +func drainBody(b io.ReadCloser) (r1, r2 io.ReadCloser, err os.Error) { + var buf bytes.Buffer + if _, err = buf.ReadFrom(b); err != nil { + return nil, nil, err + } + if err = b.Close(); err != nil { + return nil, nil, err + } + return nopCloser{&buf}, nopCloser{bytes.NewBuffer(buf.Bytes())}, nil +} + +// DumpRequest returns the wire representation of req, +// optionally including the request body, for debugging. +// DumpRequest is semantically a no-op, but in order to +// dump the body, it reads the body data into memory and +// changes req.Body to refer to the in-memory copy. +func DumpRequest(req *Request, body bool) (dump []byte, err os.Error) { + var b bytes.Buffer + save := req.Body + if !body || req.Body == nil { + req.Body = nil + } else { + save, req.Body, err = drainBody(req.Body) + if err != nil { + return + } + } + err = req.Write(&b) + req.Body = save + if err != nil { + return + } + dump = b.Bytes() + return +} + +// DumpResponse is like DumpRequest but dumps a response. +func DumpResponse(resp *Response, body bool) (dump []byte, err os.Error) { + var b bytes.Buffer + save := resp.Body + savecl := resp.ContentLength + if !body || resp.Body == nil { + resp.Body = nil + resp.ContentLength = 0 + } else { + save, resp.Body, err = drainBody(resp.Body) + if err != nil { + return + } + } + err = resp.Write(&b) + resp.Body = save + resp.ContentLength = savecl + if err != nil { + return + } + dump = b.Bytes() + return +} |