2024-03-09 11:21:24 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
|
|
|
|
|
2017-06-15 08:03:38 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2024-03-09 10:50:47 +02:00
|
|
|
"errors"
|
2018-02-21 20:44:17 +02:00
|
|
|
"io"
|
2017-06-15 08:03:38 +02:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2017-06-15 08:03:38 +02:00
|
|
|
)
|
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// BodyDumpConfig defines the config for BodyDump middleware.
|
|
|
|
type BodyDumpConfig struct {
|
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
2017-06-15 08:03:38 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// Handler receives request and response payload.
|
|
|
|
// Required.
|
|
|
|
Handler BodyDumpHandler
|
|
|
|
}
|
2017-06-15 08:03:38 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// BodyDumpHandler receives the request and response payload.
|
|
|
|
type BodyDumpHandler func(echo.Context, []byte, []byte)
|
2017-06-15 08:03:38 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
type bodyDumpResponseWriter struct {
|
|
|
|
io.Writer
|
|
|
|
http.ResponseWriter
|
|
|
|
}
|
2017-06-15 08:03:38 +02:00
|
|
|
|
2024-03-09 17:53:07 +02:00
|
|
|
// DefaultBodyDumpConfig is the default BodyDump middleware config.
|
|
|
|
var DefaultBodyDumpConfig = BodyDumpConfig{
|
|
|
|
Skipper: DefaultSkipper,
|
|
|
|
}
|
2017-06-15 08:03:38 +02:00
|
|
|
|
|
|
|
// BodyDump returns a BodyDump middleware.
|
|
|
|
//
|
2019-10-31 02:34:27 +02:00
|
|
|
// BodyDump middleware captures the request and response payload and calls the
|
2017-06-15 08:03:38 +02:00
|
|
|
// 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
|
2022-11-21 14:29:43 +02:00
|
|
|
reqBody, _ = io.ReadAll(c.Request().Body)
|
2017-06-15 08:03:38 +02:00
|
|
|
}
|
2022-11-21 14:29:43 +02:00
|
|
|
c.Request().Body = io.NopCloser(bytes.NewBuffer(reqBody)) // Reset
|
2017-06-15 08:03:38 +02:00
|
|
|
|
|
|
|
// 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() {
|
2024-08-15 13:25:35 +02:00
|
|
|
err := http.NewResponseController(w.ResponseWriter).Flush()
|
2024-03-09 10:50:47 +02:00
|
|
|
if err != nil && errors.Is(err, http.ErrNotSupported) {
|
|
|
|
panic(errors.New("response writer flushing is not supported"))
|
|
|
|
}
|
2017-06-15 08:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bodyDumpResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
2024-08-15 13:25:35 +02:00
|
|
|
return http.NewResponseController(w.ResponseWriter).Hijack()
|
2024-03-09 10:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bodyDumpResponseWriter) Unwrap() http.ResponseWriter {
|
|
|
|
return w.ResponseWriter
|
2017-06-15 08:03:38 +02:00
|
|
|
}
|