mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 08:05:12 +00:00
22 lines
606 B
Go
22 lines
606 B
Go
package middleware
|
|
|
|
import (
|
|
pkghttputil "github.com/Wei-Shaw/sub2api/internal/pkg/httputil"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// RequestBodyLimit 使用 MaxBytesReader 限制请求体大小。
|
|
func RequestBodyLimit(maxBytes int64, budgets ...*pkghttputil.BodyMemoryBudget) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
if len(budgets) > 0 {
|
|
ctx, release := pkghttputil.WithRequestBodyMemoryBudget(c.Request.Context(), budgets[0])
|
|
defer release()
|
|
c.Request = c.Request.WithContext(ctx)
|
|
}
|
|
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxBytes)
|
|
c.Next()
|
|
}
|
|
}
|