From f714848204c38ecd5e45da15246d0ce3fe66de26 Mon Sep 17 00:00:00 2001 From: jing Date: Sat, 12 Dec 2020 23:31:51 +0800 Subject: [PATCH] fix cache incorrect http response code (#36) --- cache.go | 4 +--- cache_test.go | 1 + response.go | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/cache.go b/cache.go index a820088..d29a61c 100644 --- a/cache.go +++ b/cache.go @@ -328,11 +328,9 @@ func (e *Entry) writePublicResponse(w http.ResponseWriter) error { func (e *Entry) writePrivateResponse(w http.ResponseWriter) error { // wrap the original response writer + w.WriteHeader(e.Response.Code) e.Response.SetBody(backends.WrapResponseWriterToBackend(w)) e.Response.WaitClose() - if !e.Response.IsFirstByteWritten { - w.WriteHeader(e.Response.Code) - } return nil } diff --git a/cache_test.go b/cache_test.go index cd24cb4..8ec5283 100644 --- a/cache_test.go +++ b/cache_test.go @@ -187,6 +187,7 @@ func (suite *EntryTestSuite) TestEntryWritePrivateResponse() { entry := NewEntry("unique_key2", req, res, suite.config) go func() { + entry.Response.Write([]byte(`Bad Gateway`)) entry.Response.Close() }() diff --git a/response.go b/response.go index ce1332d..88da87c 100644 --- a/response.go +++ b/response.go @@ -79,7 +79,7 @@ func (r *Response) writeHeader(b []byte, str string) { h.Set("Content-Type", http.DetectContentType(b)) } - r.WriteHeader(200) + r.WriteHeader(r.Code) } // Write writes the upstream's content in the backend's storage