From 5800ff16e00dfeb3d353fbe4eec3549a340a0c17 Mon Sep 17 00:00:00 2001 From: jing Date: Wed, 24 Jun 2020 14:01:51 +0800 Subject: [PATCH] fix prompting superfluous header (#20) --- cache.go | 5 ++++- response.go | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cache.go b/cache.go index eaea649..d3fcc5a 100644 --- a/cache.go +++ b/cache.go @@ -318,9 +318,12 @@ func (e *Entry) writePublicResponse(w http.ResponseWriter) error { } func (e *Entry) writePrivateResponse(w http.ResponseWriter) error { + // wrap the original response writer e.Response.SetBody(backends.WrapResponseWriterToBackend(w)) e.Response.WaitClose() - w.WriteHeader(e.Response.Code) + if !e.Response.IsFirstByteWritten { + w.WriteHeader(e.Response.Code) + } return nil } diff --git a/response.go b/response.go index 280e2ad..04baa26 100644 --- a/response.go +++ b/response.go @@ -26,8 +26,9 @@ type Response struct { body backends.Backend snapHeader http.Header - wroteHeader bool - bodyComplete bool + wroteHeader bool + bodyComplete bool + IsFirstByteWritten bool bodyChan chan struct{} // indicates whether the backend is set or not. bodyCompleteChan chan struct{} @@ -94,6 +95,9 @@ func (r *Response) Write(buf []byte) (int, error) { } if r.body != nil { + if !r.IsFirstByteWritten { + r.IsFirstByteWritten = true + } return r.body.Write(buf) }