From d632db838b4f681456c4c6fc08f5c0cc2c51fefc Mon Sep 17 00:00:00 2001 From: "wangzekun.zekin" Date: Sun, 28 Apr 2024 17:25:47 +0800 Subject: [PATCH 1/2] fix: resp set trailer will panic --- pkg/protocol/header.go | 2 ++ pkg/protocol/header_test.go | 10 ++++++++++ 2 files changed, 12 insertions(+) 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..d88fc06fc 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.Set(consts.HeaderTrailer, "foo,bar,hertz") + assert.DeepEqual(t, "Foo, Bar, Hertz", h.Get(consts.HeaderTrailer)) + + h1 := &ResponseHeader{} + h1.Set(consts.HeaderTrailer, "foo,bar,hertz") + assert.DeepEqual(t, "Foo, Bar, Hertz", h1.Get(consts.HeaderTrailer)) +} From a0dec71d442f6ccf44478b0bd6657f4a5499cd0e Mon Sep 17 00:00:00 2001 From: "yinxuran.lucky" Date: Thu, 9 May 2024 15:19:11 +0800 Subject: [PATCH 2/2] fix: unit test --- pkg/protocol/header_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/protocol/header_test.go b/pkg/protocol/header_test.go index d88fc06fc..ba2bd6998 100644 --- a/pkg/protocol/header_test.go +++ b/pkg/protocol/header_test.go @@ -809,10 +809,10 @@ func TestResponseHeaderDateEmpty(t *testing.T) { func TestSetTrailerWithROString(t *testing.T) { h := &RequestHeader{} - h.Set(consts.HeaderTrailer, "foo,bar,hertz") + h.Add(consts.HeaderTrailer, "foo,bar,hertz") assert.DeepEqual(t, "Foo, Bar, Hertz", h.Get(consts.HeaderTrailer)) h1 := &ResponseHeader{} - h1.Set(consts.HeaderTrailer, "foo,bar,hertz") + h1.Add(consts.HeaderTrailer, "foo,bar,hertz") assert.DeepEqual(t, "Foo, Bar, Hertz", h1.Get(consts.HeaderTrailer)) }