diff --git a/pkg/protocol/header.go b/pkg/protocol/header.go index 278b137b5..88885483c 100644 --- a/pkg/protocol/header.go +++ b/pkg/protocol/header.go @@ -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 } diff --git a/pkg/protocol/header_test.go b/pkg/protocol/header_test.go index a14704053..ba2bd6998 100644 --- a/pkg/protocol/header_test.go +++ b/pkg/protocol/header_test.go @@ -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)) +}