Skip to content

Commit

Permalink
fix(writer): panic occurred while processing redirect request (#37)
Browse files Browse the repository at this point in the history
* fix: do not panic on status code -1

* test: update test to include verifying that no header was written

---------

Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
larsgroeber and appleboy authored Nov 25, 2023
1 parent f338d36 commit 4c4d0ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ func (w *Writer) Write(data []byte) (int, error) {
// If the response writer has already written headers or if a timeout has occurred,
// this method does nothing.
func (w *Writer) WriteHeader(code int) {
checkWriteHeaderCode(code)
if w.timeout || w.wroteHeaders {
return
}

// gin is using -1 to skip writing the status code
// see https://github.com/gin-gonic/gin/blob/a0acf1df2814fcd828cb2d7128f2f4e2136d3fac/response_writer.go#L61
if code == -1 {
return
}

checkWriteHeaderCode(code)

w.mu.Lock()
defer w.mu.Unlock()

Expand Down
10 changes: 10 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ func TestWriteHeader(t *testing.T) {
})
}

func TestWriteHeader_SkipMinusOne(t *testing.T) {
code := -1

writer := Writer{}
assert.NotPanics(t, func() {
writer.WriteHeader(code)
assert.False(t, writer.wroteHeaders)
})
}

func TestWriter_Status(t *testing.T) {
r := gin.New()

Expand Down

0 comments on commit 4c4d0ab

Please sign in to comment.