From 0ef7eb30619d4c365e06a94a79b9cb0333d792da Mon Sep 17 00:00:00 2001 From: Stojan Dimitrovski Date: Thu, 30 May 2024 12:42:30 +0200 Subject: [PATCH] fix: call write header in write if not written (#1598) If `Write` is called and `WriteHeader` was not previously called, the `WriteHeader` needs to be set to StatusOK. --- internal/api/middleware.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/internal/api/middleware.go b/internal/api/middleware.go index 4338ced3e..291eba5e8 100644 --- a/internal/api/middleware.go +++ b/internal/api/middleware.go @@ -288,6 +288,10 @@ func (t *timeoutResponseWriter) Write(bytes []byte) (int, error) { t.Lock() defer t.Unlock() + if !t.wroteHeader { + t.writeHeaderLocked(http.StatusOK) + } + return t.buf.Write(bytes) } @@ -295,12 +299,17 @@ func (t *timeoutResponseWriter) WriteHeader(statusCode int) { t.Lock() defer t.Unlock() + t.writeHeaderLocked(statusCode) +} + +func (t *timeoutResponseWriter) writeHeaderLocked(statusCode int) { if t.wroteHeader { // ignore multiple calls to WriteHeader // once WriteHeader has been called once, a snapshot of the header map is taken // and saved in snapHeader to be used in finallyWrite return } + t.statusCode = statusCode t.wroteHeader = true t.snapHeader = t.header.Clone()