You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
httpdebug/httpdebug.go

137 lines
2.6 KiB

8 years ago
package httpdebug
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
8 years ago
"strings"
8 years ago
"unicode"
)
8 years ago
type Mode int
const (
ModeOneline Mode = iota
ModeTrimmed
ModeFull
)
var ModeDefault = ModeOneline
// Wrap returns an http handler that serves h while logging requests and responses to stderr. It uses ModeDefault.
8 years ago
func Wrap(h http.Handler) http.Handler {
8 years ago
return WrapMode(h, ModeDefault)
}
// WrapMode returns an http handler that serves h while logging requests and responses to stderr.
func WrapMode(h http.Handler, m Mode) http.Handler {
8 years ago
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
8 years ago
switch m {
case ModeTrimmed:
serveTrimmed(h, w, r, 1024, 128)
case ModeFull:
serveTrimmed(h, w, r, 0, 0)
case ModeOneline:
buf := &bytes.Buffer{}
rw := &rw{
ResponseWriter: w,
w: buf,
}
h.ServeHTTP(rw, r)
log.Println(r.Method, r.URL.Path, strings.Repeat("\t", 5-((len(r.Method)+len(r.URL.Path))/12)+1), rw.status, http.StatusText(rw.status), buf.Len(), "bytes", "\t", "("+w.Header().Get("Content-Type")+")")
}
})
}
8 years ago
8 years ago
func serveTrimmed(h http.Handler, w http.ResponseWriter, r *http.Request, trimAt, trimTo int64) {
log.Println(r.Method, r.URL.Path, r.Proto)
8 years ago
8 years ago
hBuf := &bytes.Buffer{}
r.Header.Write(hBuf)
log.Println(hBuf.String())
8 years ago
8 years ago
requestBuf := &bytes.Buffer{}
responseBuf := &bytes.Buffer{}
8 years ago
8 years ago
defer r.Body.Close()
8 years ago
8 years ago
r.Body = ioutil.NopCloser(io.TeeReader(r.Body, requestBuf))
rw := &rw{
ResponseWriter: w,
w: responseBuf,
}
8 years ago
8 years ago
h.ServeHTTP(rw, r)
if trimAt > 0 && trimTo > 0 {
if int64(requestBuf.Len()) > trimAt {
8 years ago
trimmed := &bytes.Buffer{}
8 years ago
io.CopyN(trimmed, requestBuf, trimTo)
8 years ago
requestBuf = trimmed
}
8 years ago
}
8 years ago
8 years ago
log.Println()
log.Println(logsafe(requestBuf.String()))
8 years ago
8 years ago
log.Println(rw.status)
8 years ago
8 years ago
hBuf.Reset()
w.Header().Write(hBuf)
log.Println(hBuf.String())
8 years ago
8 years ago
if trimTo > 0 {
responseBuf = trim(responseBuf, int(trimTo))
}
log.Println()
log.Println(logsafe(responseBuf.String()))
8 years ago
}
type rw struct {
http.ResponseWriter
8 years ago
w io.Writer
status int
8 years ago
}
8 years ago
func (rw *rw) WriteHeader(s int) {
rw.status = s
rw.ResponseWriter.WriteHeader(s)
}
func (rw *rw) Write(b []byte) (int, error) {
if rw.status == 0 {
rw.WriteHeader(http.StatusOK)
}
if rw.w != nil {
rw.w.Write(b)
}
8 years ago
return rw.ResponseWriter.Write(b)
}
func logsafe(str string) string {
buf := &bytes.Buffer{}
for _, r := range str {
if unicode.IsPrint(r) || unicode.IsSpace(r) {
fmt.Fprintf(buf, "%c", r)
} else {
fmt.Fprintf(buf, "%x", r)
}
}
return buf.String()
}
func trim(b *bytes.Buffer, n int) *bytes.Buffer {
trimmed := &bytes.Buffer{}
io.CopyN(trimmed, b, int64(n))
return trimmed
}