mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 16:04:24 +00:00
Upstream added WithBodyReadTimeout (gateway.request_body_read_timeout_seconds) and client-gone handling for body reads. Drop this branch's own server-level idle-timeout wrapper and server.request_body_idle_timeout setting in favor of it, and keep the classification and diagnostics on top: - readRequestBodyChunksWithBudget reports the read error before ctx.Err(): on HTTP/1 a read deadline also cancels the request context, which turned every timeout into context.Canceled. Read errors carry bytes read and elapsed time in RequestBodyReadError. - handleRequestBodyReadError and RejectRequestBodyReadError skip the client-gone (empty 499) path for read timeouts, so they are reported as 408 instead of a client cancellation (HTTP/1) or a generic 400 (HTTP/2). - Every gateway body read site and both body-reading middlewares log payload-free diagnostics, including the client-gone path.
51 lines
1.6 KiB
Go
51 lines
1.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
pkghttputil "github.com/Wei-Shaw/sub2api/internal/pkg/httputil"
|
|
middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// handleRequestBodyReadError handles cancellation and temporary capacity errors
|
|
// before callers classify malformed or oversized request bodies.
|
|
//
|
|
// A read deadline on HTTP/1 also cancels the request context, so a body read
|
|
// timeout is left to requestBodyReadFailure (408) instead of being reported as
|
|
// a client that went away (499).
|
|
func handleRequestBodyReadError(c *gin.Context, reqLog *zap.Logger, err error) bool {
|
|
if err != nil && !pkghttputil.IsRequestBodyReadTimeout(err) && failoverClientGone(c) {
|
|
logRequestBodyReadFailure(reqLog, pkghttputil.DescribeRequestBodyReadFailure(c.Request, err))
|
|
c.Abort()
|
|
return true
|
|
}
|
|
return middleware2.RejectRequestBodyMemoryOverload(c, err)
|
|
}
|
|
|
|
func formatBodyLimit(limit int64) string {
|
|
const mb = 1024 * 1024
|
|
if limit >= mb {
|
|
return fmt.Sprintf("%dMB", limit/mb)
|
|
}
|
|
return fmt.Sprintf("%dB", limit)
|
|
}
|
|
|
|
func buildBodyTooLargeMessage(limit int64) string {
|
|
return fmt.Sprintf("Request body too large, limit is %s", formatBodyLimit(limit))
|
|
}
|
|
|
|
func readLenientJSONRequestBodyWithPrealloc(req *http.Request, cfg *config.Config) ([]byte, error) {
|
|
return pkghttputil.ReadLenientJSONRequestBodyWithPrealloc(req, gatewayMaxBodySize(cfg))
|
|
}
|
|
|
|
func gatewayMaxBodySize(cfg *config.Config) int64 {
|
|
if cfg == nil {
|
|
return 0
|
|
}
|
|
return cfg.Gateway.MaxBodySize
|
|
}
|