Skip to content

Commit

Permalink
fix: call write header in write if not written (#1598)
Browse files Browse the repository at this point in the history
If `Write` is called and `WriteHeader` was not previously called, the
`WriteHeader` needs to be set to StatusOK.
  • Loading branch information
hf authored May 30, 2024
1 parent 6c9fbd4 commit 0ef7eb3
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/api/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,19 +288,28 @@ 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)
}

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()
Expand Down

0 comments on commit 0ef7eb3

Please sign in to comment.