From 301838e7b1e31dabf2fdd6f358e565a465a4a3fa Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sun, 24 May 2020 23:55:28 +0300 Subject: [PATCH] lib/httpserver: properly set status code for empty response --- lib/httpserver/httpserver.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/httpserver/httpserver.go b/lib/httpserver/httpserver.go index ded29542da..3ee057d284 100644 --- a/lib/httpserver/httpserver.go +++ b/lib/httpserver/httpserver.go @@ -314,10 +314,7 @@ func (zrw *gzipResponseWriter) Write(p []byte) (int, error) { h.Set("Content-Type", "text/html") } } - if zrw.statusCode == 0 { - zrw.statusCode = http.StatusOK - } - zrw.ResponseWriter.WriteHeader(zrw.statusCode) + zrw.writeHeader() zrw.firstWriteDone = true } if zrw.disableCompression { @@ -327,12 +324,16 @@ func (zrw *gzipResponseWriter) Write(p []byte) (int, error) { } func (zrw *gzipResponseWriter) WriteHeader(statusCode int) { - if zrw.statusCode != 0 { - return - } zrw.statusCode = statusCode } +func (zrw *gzipResponseWriter) writeHeader() { + if zrw.statusCode == 0 { + zrw.statusCode = http.StatusOK + } + zrw.ResponseWriter.WriteHeader(zrw.statusCode) +} + // Implements http.Flusher func (zrw *gzipResponseWriter) Flush() { if err := zrw.bw.Flush(); err != nil && !isTrivialNetworkError(err) { @@ -348,6 +349,7 @@ func (zrw *gzipResponseWriter) Flush() { func (zrw *gzipResponseWriter) Close() error { if !zrw.firstWriteDone { + zrw.writeHeader() return nil } zrw.Flush()