| 252 | } |
| 253 | |
| 254 | func requestBodyLimitMiddleware(maxBytes int64) gin.HandlerFunc { |
| 255 | return func(c *gin.Context) { |
| 256 | if maxBytes <= 0 || c.Request == nil || c.Request.Body == nil || c.Request.Body == http.NoBody { |
| 257 | c.Next() |
| 258 | return |
| 259 | } |
| 260 | if !strings.HasPrefix(c.Request.URL.Path, "/api/") { |
| 261 | c.Next() |
| 262 | return |
| 263 | } |
| 264 | if c.Request.ContentLength > maxBytes { |
| 265 | core.RespondError(c, http.StatusRequestEntityTooLarge, errRequestBodyTooLarge, false) |
| 266 | c.Abort() |
| 267 | return |
| 268 | } |
| 269 | c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxBytes) |
| 270 | c.Next() |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | func loopbackAPIGuard(boundHost string) gin.HandlerFunc { |
| 275 | return loopbackGuard(boundHost, errLoopbackAPIRequired, true) |