wip - formatting
This commit is contained in:
+81
-27
@@ -7,57 +7,111 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Wrap returns an http handler that serves h while logging requests and responses to stderr.
|
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.
|
||||||
func Wrap(h http.Handler) http.Handler {
|
func Wrap(h http.Handler) http.Handler {
|
||||||
|
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 {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println(r.Method, r.URL.Path, r.Proto)
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
hBuf := &bytes.Buffer{}
|
h.ServeHTTP(rw, r)
|
||||||
r.Header.Write(hBuf)
|
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")+")")
|
||||||
log.Println(hBuf.String())
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
requestBuf := &bytes.Buffer{}
|
func serveTrimmed(h http.Handler, w http.ResponseWriter, r *http.Request, trimAt, trimTo int64) {
|
||||||
responseBuf := &bytes.Buffer{}
|
log.Println(r.Method, r.URL.Path, r.Proto)
|
||||||
|
|
||||||
defer r.Body.Close()
|
hBuf := &bytes.Buffer{}
|
||||||
|
r.Header.Write(hBuf)
|
||||||
|
log.Println(hBuf.String())
|
||||||
|
|
||||||
r.Body = ioutil.NopCloser(io.TeeReader(r.Body, requestBuf))
|
requestBuf := &bytes.Buffer{}
|
||||||
|
responseBuf := &bytes.Buffer{}
|
||||||
|
|
||||||
h.ServeHTTP(rw{
|
defer r.Body.Close()
|
||||||
ResponseWriter: w,
|
|
||||||
w: responseBuf,
|
|
||||||
}, r)
|
|
||||||
|
|
||||||
if requestBuf.Len() > 1024 {
|
r.Body = ioutil.NopCloser(io.TeeReader(r.Body, requestBuf))
|
||||||
|
|
||||||
|
rw := &rw{
|
||||||
|
ResponseWriter: w,
|
||||||
|
w: responseBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
h.ServeHTTP(rw, r)
|
||||||
|
|
||||||
|
if trimAt > 0 && trimTo > 0 {
|
||||||
|
if int64(requestBuf.Len()) > trimAt {
|
||||||
trimmed := &bytes.Buffer{}
|
trimmed := &bytes.Buffer{}
|
||||||
io.CopyN(trimmed, requestBuf, 128)
|
io.CopyN(trimmed, requestBuf, trimTo)
|
||||||
requestBuf = trimmed
|
requestBuf = trimmed
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.Println()
|
log.Println()
|
||||||
log.Println(logsafe(requestBuf.String()))
|
log.Println(logsafe(requestBuf.String()))
|
||||||
|
|
||||||
hBuf.Reset()
|
log.Println(rw.status)
|
||||||
w.Header().Write(hBuf)
|
|
||||||
log.Println(hBuf.String())
|
|
||||||
|
|
||||||
responseBuf = trim(responseBuf, 128)
|
hBuf.Reset()
|
||||||
|
w.Header().Write(hBuf)
|
||||||
|
log.Println(hBuf.String())
|
||||||
|
|
||||||
log.Println()
|
if trimTo > 0 {
|
||||||
log.Println(logsafe(responseBuf.String()))
|
responseBuf = trim(responseBuf, int(trimTo))
|
||||||
})
|
}
|
||||||
|
|
||||||
|
log.Println()
|
||||||
|
log.Println(logsafe(responseBuf.String()))
|
||||||
}
|
}
|
||||||
|
|
||||||
type rw struct {
|
type rw struct {
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
w io.Writer
|
w io.Writer
|
||||||
|
status int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rw rw) Write(b []byte) (int, error) {
|
func (rw *rw) WriteHeader(s int) {
|
||||||
rw.w.Write(b)
|
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)
|
||||||
|
}
|
||||||
return rw.ResponseWriter.Write(b)
|
return rw.ResponseWriter.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user