2017-06-15 08:03:38 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2018-02-21 20:44:17 +02:00
|
|
|
"io"
|
2017-06-15 08:03:38 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// BodyDumpConfig defines the config for BodyDump middleware.
|
|
|
|
BodyDumpConfig struct {
|
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
|
|
|
|
|
|
|
// Handler receives request and response payload.
|
|
|
|
// Required.
|
|
|
|
Handler BodyDumpHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// BodyDumpHandler receives the request and response payload.
|
|
|
|
BodyDumpHandler func(echo.Context, []byte, []byte)
|
|
|
|
|
|
|
|
bodyDumpResponseWriter struct {
|
|
|
|
io.Writer
|
|
|
|
http.ResponseWriter
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-12-28 21:24:34 +02:00
|
|
|
// DefaultBodyDumpConfig is the default BodyDump middleware config.
|
2017-06-15 08:03:38 +02:00
|
|
|
DefaultBodyDumpConfig = BodyDumpConfig{
|
|
|
|
Skipper: DefaultSkipper,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// BodyDump returns a BodyDump middleware.
|
|
|
|
//
|
|
|
|
// BodyLimit middleware captures the request and response payload and calls the
|
|
|
|
// registered handler.
|
|
|
|
func BodyDump(handler BodyDumpHandler) echo.MiddlewareFunc {
|
|
|
|
c := DefaultBodyDumpConfig
|
|
|
|
c.Handler = handler
|
|
|
|
return BodyDumpWithConfig(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BodyDumpWithConfig returns a BodyDump middleware with config.
|
2017-06-15 17:19:18 +02:00
|
|
|
// See: `BodyDump()`.
|
2017-06-15 08:03:38 +02:00
|
|
|
func BodyDumpWithConfig(config BodyDumpConfig) echo.MiddlewareFunc {
|
|
|
|
// Defaults
|
|
|
|
if config.Handler == nil {
|
2017-06-23 07:48:23 +02:00
|
|
|
panic("echo: body-dump middleware requires a handler function")
|
2017-06-15 08:03:38 +02:00
|
|
|
}
|
|
|
|
if config.Skipper == nil {
|
|
|
|
config.Skipper = DefaultBodyDumpConfig.Skipper
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) (err error) {
|
|
|
|
if config.Skipper(c) {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request
|
|
|
|
reqBody := []byte{}
|
|
|
|
if c.Request().Body != nil { // Read
|
|
|
|
reqBody, _ = ioutil.ReadAll(c.Request().Body)
|
|
|
|
}
|
|
|
|
c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(reqBody)) // Reset
|
|
|
|
|
|
|
|
// Response
|
|
|
|
resBody := new(bytes.Buffer)
|
|
|
|
mw := io.MultiWriter(c.Response().Writer, resBody)
|
|
|
|
writer := &bodyDumpResponseWriter{Writer: mw, ResponseWriter: c.Response().Writer}
|
|
|
|
c.Response().Writer = writer
|
|
|
|
|
|
|
|
if err = next(c); err != nil {
|
|
|
|
c.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback
|
|
|
|
config.Handler(c, reqBody, resBody.Bytes())
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bodyDumpResponseWriter) WriteHeader(code int) {
|
|
|
|
w.ResponseWriter.WriteHeader(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bodyDumpResponseWriter) Write(b []byte) (int, error) {
|
|
|
|
return w.Writer.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bodyDumpResponseWriter) Flush() {
|
|
|
|
w.ResponseWriter.(http.Flusher).Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bodyDumpResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
return w.ResponseWriter.(http.Hijacker).Hijack()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bodyDumpResponseWriter) CloseNotify() <-chan bool {
|
|
|
|
return w.ResponseWriter.(http.CloseNotifier).CloseNotify()
|
|
|
|
}
|