Skip to content

Commit

Permalink
fix: resp set trailer will panic (#1102)
Browse files Browse the repository at this point in the history
Co-authored-by: yinxuran.lucky <yinxuran.lucky@bytedance.com>
  • Loading branch information
wzekin and Duslia committed May 10, 2024
1 parent a80eadb commit cbd63c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/protocol/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,8 @@ func (h *ResponseHeader) setSpecialHeader(key, value []byte) bool {
// Transfer-Encoding is managed automatically.
return true
} else if utils.CaseInsensitiveCompare(bytestr.StrTrailer, key) {
// copy value to avoid panic
value = append(h.bufKV.value[:0], value...)
h.Trailer().SetTrailers(value)
return true
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/protocol/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,3 +806,13 @@ func TestResponseHeaderDateEmpty(t *testing.T) {
t.Fatalf("ResponseDateNoDefaultNotEmpty fail, response: \n%+v\noutcome: \n%q\n", h, headers) //nolint:govet
}
}

func TestSetTrailerWithROString(t *testing.T) {
h := &RequestHeader{}
h.Add(consts.HeaderTrailer, "foo,bar,hertz")
assert.DeepEqual(t, "Foo, Bar, Hertz", h.Get(consts.HeaderTrailer))

h1 := &ResponseHeader{}
h1.Add(consts.HeaderTrailer, "foo,bar,hertz")
assert.DeepEqual(t, "Foo, Bar, Hertz", h1.Get(consts.HeaderTrailer))
}

0 comments on commit cbd63c6

Please sign in to comment.