修复代理与日志链路的可靠性问题
This commit is contained in:
+11
-2
@@ -5,21 +5,30 @@ import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var errRequestBodyTooLarge = errors.New("request body too large")
|
||||
|
||||
// readRequestBody 在转发前完整读取请求体,确保读取失败时不会向上游发送损坏请求。
|
||||
func readRequestBody(r *http.Request, max int64) (captured []byte, truncated bool, err error) {
|
||||
func readRequestBody(r *http.Request, max, requestLimit int64) (captured []byte, truncated bool, err error) {
|
||||
if r.Body == nil || r.Body == http.NoBody {
|
||||
return nil, false, nil
|
||||
}
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if r.ContentLength > requestLimit {
|
||||
return nil, false, errRequestBodyTooLarge
|
||||
}
|
||||
body, err := io.ReadAll(io.LimitReader(r.Body, requestLimit+1))
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if int64(len(body)) > requestLimit {
|
||||
return nil, false, errRequestBodyTooLarge
|
||||
}
|
||||
if err := r.Body.Close(); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user