mirror of
https://github.com/yusing/godoxy.git
synced 2025-12-21 08:29:57 -06:00
fix(http): handle 0 content length properly in some cases
This commit is contained in:
@@ -23,7 +23,7 @@ type ResponseModifier struct {
|
||||
statusCode int
|
||||
shared Cache
|
||||
|
||||
origContentLength int64 // from http.Response in ResponseAsRW
|
||||
origContentLength int64 // from http.Response in ResponseAsRW, -1 if not set
|
||||
bodyModified 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.
|
||||
func NewResponseModifier(w http.ResponseWriter) *ResponseModifier {
|
||||
return &ResponseModifier{
|
||||
bufPool: synk.GetUnsizedBytesPool(),
|
||||
w: w,
|
||||
bufPool: synk.GetUnsizedBytesPool(),
|
||||
w: w,
|
||||
origContentLength: -1,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,7 +154,7 @@ func (rm *ResponseModifier) SetBody(r io.ReadCloser) error {
|
||||
|
||||
func (rm *ResponseModifier) ContentLength() int {
|
||||
if !rm.bodyModified {
|
||||
if rm.origContentLength > 0 {
|
||||
if rm.origContentLength >= 0 {
|
||||
return int(rm.origContentLength)
|
||||
}
|
||||
contentLength, _ := strconv.Atoi(rm.ContentLengthStr())
|
||||
@@ -164,7 +165,7 @@ func (rm *ResponseModifier) ContentLength() int {
|
||||
|
||||
func (rm *ResponseModifier) ContentLengthStr() string {
|
||||
if !rm.bodyModified {
|
||||
if rm.origContentLength > 0 {
|
||||
if rm.origContentLength >= 0 {
|
||||
return strconv.FormatInt(rm.origContentLength, 10)
|
||||
}
|
||||
return rm.w.Header().Get("Content-Length")
|
||||
|
||||
Reference in New Issue
Block a user