mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 09:18:51 +02: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
|
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")
|
||||||
|
|||||||
Reference in New Issue
Block a user