2024-05-23 23:44:31 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
|
|
|
|
|
2017-12-07 23:00:56 +01:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2024-05-23 23:44:31 +02:00
|
|
|
"errors"
|
2018-11-18 18:55:05 +01:00
|
|
|
"io"
|
2017-12-07 23:00:56 +01:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
|
2019-01-31 17:06:36 +01:00
|
|
|
"github.com/labstack/echo/v4"
|
2017-12-07 23:00:56 +01:00
|
|
|
)
|
|
|
|
|
2024-05-23 23:44:31 +02:00
|
|
|
// BodyDumpConfig defines the config for BodyDump middleware.
|
|
|
|
type BodyDumpConfig struct {
|
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
2017-12-07 23:00:56 +01:00
|
|
|
|
2024-05-23 23:44:31 +02:00
|
|
|
// Handler receives request and response payload.
|
|
|
|
// Required.
|
|
|
|
Handler BodyDumpHandler
|
|
|
|
}
|
2017-12-07 23:00:56 +01:00
|
|
|
|
2024-05-23 23:44:31 +02:00
|
|
|
// BodyDumpHandler receives the request and response payload.
|
|
|
|
type BodyDumpHandler func(echo.Context, []byte, []byte)
|
2017-12-07 23:00:56 +01:00
|
|
|
|
2024-05-23 23:44:31 +02:00
|
|
|
type bodyDumpResponseWriter struct {
|
|
|
|
io.Writer
|
|
|
|
http.ResponseWriter
|
|
|
|
}
|
2017-12-07 23:00:56 +01:00
|
|
|
|
2024-05-23 23:44:31 +02:00
|
|
|
// DefaultBodyDumpConfig is the default BodyDump middleware config.
|
|
|
|
var DefaultBodyDumpConfig = BodyDumpConfig{
|
|
|
|
Skipper: DefaultSkipper,
|
|
|
|
}
|
2017-12-07 23:00:56 +01:00
|
|
|
|
|
|
|
// BodyDump returns a BodyDump middleware.
|
|
|
|
//
|
2020-01-09 21:02:56 +01:00
|
|
|
// BodyDump middleware captures the request and response payload and calls the
|
2017-12-07 23:00:56 +01:00
|
|
|
// registered handler.
|
|
|
|
func BodyDump(handler BodyDumpHandler) echo.MiddlewareFunc {
|
|
|
|
c := DefaultBodyDumpConfig
|
|
|
|
c.Handler = handler
|
|
|
|
return BodyDumpWithConfig(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BodyDumpWithConfig returns a BodyDump middleware with config.
|
|
|
|
// See: `BodyDump()`.
|
|
|
|
func BodyDumpWithConfig(config BodyDumpConfig) echo.MiddlewareFunc {
|
|
|
|
// Defaults
|
|
|
|
if config.Handler == nil {
|
|
|
|
panic("echo: body-dump middleware requires a handler function")
|
|
|
|
}
|
|
|
|
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
|
2023-01-28 22:57:53 +01:00
|
|
|
reqBody, _ = io.ReadAll(c.Request().Body)
|
2017-12-07 23:00:56 +01:00
|
|
|
}
|
2023-01-28 22:57:53 +01:00
|
|
|
c.Request().Body = io.NopCloser(bytes.NewBuffer(reqBody)) // Reset
|
2017-12-07 23:00:56 +01: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-05-23 23:44:31 +02:00
|
|
|
err := responseControllerFlush(w.ResponseWriter)
|
|
|
|
if err != nil && errors.Is(err, http.ErrNotSupported) {
|
|
|
|
panic(errors.New("response writer flushing is not supported"))
|
|
|
|
}
|
2017-12-07 23:00:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bodyDumpResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
2024-05-23 23:44:31 +02:00
|
|
|
return responseControllerHijack(w.ResponseWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bodyDumpResponseWriter) Unwrap() http.ResponseWriter {
|
|
|
|
return w.ResponseWriter
|
2017-12-07 23:00:56 +01:00
|
|
|
}
|