From afeb535502e82cceab6e9e56855a45111c2842b9 Mon Sep 17 00:00:00 2001 From: Skyenought Date: Tue, 14 Nov 2023 14:15:33 +0800 Subject: [PATCH] update header.go --- pkg/protocol/http1/resp/writer.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/protocol/http1/resp/writer.go b/pkg/protocol/http1/resp/writer.go index 745ab4c5c..c7c3d8fa2 100644 --- a/pkg/protocol/http1/resp/writer.go +++ b/pkg/protocol/http1/resp/writer.go @@ -59,6 +59,14 @@ func (c *chunkedBodyWriter) Flush() error { // Warning: do not call this method by yourself, unless you know what you are doing. func (c *chunkedBodyWriter) Finalize() error { c.Do(func() { + // in case no actual data from user + if !c.wroteHeader { + c.r.Header.SetContentLength(-1) + if c.finalizeErr = WriteHeader(&c.r.Header, c.w); c.finalizeErr != nil { + return + } + c.wroteHeader = true + } c.finalizeErr = ext.WriteChunk(c.w, nil, true) if c.finalizeErr != nil { return @@ -70,7 +78,8 @@ func (c *chunkedBodyWriter) Finalize() error { func NewChunkedBodyWriter(r *protocol.Response, w network.Writer) network.ExtWriter { return &chunkedBodyWriter{ - r: r, - w: w, + r: r, + w: w, + Once: sync.Once{}, } }