MCPcopy Index your code
hub / github.com/labstack/echo / ToMiddleware

Method ToMiddleware

middleware/body_limit.go:47–81  ·  view source on GitHub ↗

ToMiddleware converts BodyLimitConfig to middleware or returns an error for invalid configuration

()

Source from the content-addressed store, hash-verified

45
46// ToMiddleware converts BodyLimitConfig to middleware or returns an error for invalid configuration
47func (config BodyLimitConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
48 if config.Skipper == nil {
49 config.Skipper = DefaultSkipper
50 }
51 pool := sync.Pool{
52 New: func() any {
53 return &limitedReader{BodyLimitConfig: config}
54 },
55 }
56
57 return func(next echo.HandlerFunc) echo.HandlerFunc {
58 return func(c *echo.Context) error {
59 if config.Skipper(c) {
60 return next(c)
61 }
62 req := c.Request()
63
64 // Based on content length
65 if req.ContentLength > config.LimitBytes {
66 return echo.ErrStatusRequestEntityTooLarge
67 }
68
69 // Based on content read
70 r, ok := pool.Get().(*limitedReader)
71 if !ok {
72 return echo.NewHTTPError(http.StatusInternalServerError, "invalid pool object")
73 }
74 r.Reset(req.Body)
75 defer pool.Put(r)
76 req.Body = r
77
78 return next(c)
79 }
80 }, nil
81}
82
83func (r *limitedReader) Read(b []byte) (n int, err error) {
84 n, err = r.reader.Read(b)

Callers

nothing calls this directly

Calls 3

RequestMethod · 0.80
GetMethod · 0.45
ResetMethod · 0.45

Tested by

no test coverage detected