fix(http): handle 0 content length properly in some cases

This commit is contained in:
yusing
2025-12-04 17:33:01 +08:00
parent c1f9c2c957
commit aa2575696d

View File

@@ -23,7 +23,7 @@ type ResponseModifier struct {
statusCode int statusCode int
shared Cache shared Cache
origContentLength int64 // from http.Response in ResponseAsRW origContentLength int64 // from http.Response in ResponseAsRW, -1 if not set
bodyModified bool bodyModified bool
hijacked bool hijacked bool
@@ -98,8 +98,9 @@ func GetSharedData(w http.ResponseWriter) Cache {
// It should only be called once, at the very beginning of the request. // It should only be called once, at the very beginning of the request.
func NewResponseModifier(w http.ResponseWriter) *ResponseModifier { func NewResponseModifier(w http.ResponseWriter) *ResponseModifier {
return &ResponseModifier{ return &ResponseModifier{
bufPool: synk.GetUnsizedBytesPool(), bufPool: synk.GetUnsizedBytesPool(),
w: w, w: w,
origContentLength: -1,
} }
} }
@@ -153,7 +154,7 @@ func (rm *ResponseModifier) SetBody(r io.ReadCloser) error {
func (rm *ResponseModifier) ContentLength() int { func (rm *ResponseModifier) ContentLength() int {
if !rm.bodyModified { if !rm.bodyModified {
if rm.origContentLength > 0 { if rm.origContentLength >= 0 {
return int(rm.origContentLength) return int(rm.origContentLength)
} }
contentLength, _ := strconv.Atoi(rm.ContentLengthStr()) contentLength, _ := strconv.Atoi(rm.ContentLengthStr())
@@ -164,7 +165,7 @@ func (rm *ResponseModifier) ContentLength() int {
func (rm *ResponseModifier) ContentLengthStr() string { func (rm *ResponseModifier) ContentLengthStr() string {
if !rm.bodyModified { if !rm.bodyModified {
if rm.origContentLength > 0 { if rm.origContentLength >= 0 {
return strconv.FormatInt(rm.origContentLength, 10) return strconv.FormatInt(rm.origContentLength, 10)
} }
return rm.w.Header().Get("Content-Length") return rm.w.Header().Get("Content-Length")