Skip to content

Commit 128e9b3

Browse files
authored
optimize: adjust the behavior of PeekAll based on VisitAll (#1403)
1 parent 2c8ce3b commit 128e9b3

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

header.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,11 +1811,17 @@ func (h *RequestHeader) peekAll(key []byte) [][]byte {
18111811
h.mulHeader = h.mulHeader[:0]
18121812
switch string(key) {
18131813
case HeaderHost:
1814-
h.mulHeader = append(h.mulHeader, h.Host())
1814+
if host := h.Host(); len(host) > 0 {
1815+
h.mulHeader = append(h.mulHeader, host)
1816+
}
18151817
case HeaderContentType:
1816-
h.mulHeader = append(h.mulHeader, h.ContentType())
1818+
if contentType := h.ContentType(); len(contentType) > 0 {
1819+
h.mulHeader = append(h.mulHeader, contentType)
1820+
}
18171821
case HeaderUserAgent:
1818-
h.mulHeader = append(h.mulHeader, h.UserAgent())
1822+
if ua := h.UserAgent(); len(ua) > 0 {
1823+
h.mulHeader = append(h.mulHeader, ua)
1824+
}
18191825
case HeaderConnection:
18201826
if h.ConnectionClose() {
18211827
h.mulHeader = append(h.mulHeader, strClose)
@@ -1827,7 +1833,6 @@ func (h *RequestHeader) peekAll(key []byte) [][]byte {
18271833
case HeaderCookie:
18281834
if h.cookiesCollected {
18291835
h.mulHeader = append(h.mulHeader, appendRequestCookieBytes(nil, h.cookies))
1830-
return [][]byte{appendRequestCookieBytes(nil, h.cookies)}
18311836
} else {
18321837
h.mulHeader = peekAllArgBytesToDst(h.mulHeader, h.h, key)
18331838
}
@@ -1853,11 +1858,17 @@ func (h *ResponseHeader) peekAll(key []byte) [][]byte {
18531858
h.mulHeader = h.mulHeader[:0]
18541859
switch string(key) {
18551860
case HeaderContentType:
1856-
h.mulHeader = append(h.mulHeader, h.ContentType())
1861+
if contentType := h.ContentType(); len(contentType) > 0 {
1862+
h.mulHeader = append(h.mulHeader, contentType)
1863+
}
18571864
case HeaderContentEncoding:
1858-
h.mulHeader = append(h.mulHeader, h.ContentEncoding())
1865+
if contentEncoding := h.ContentEncoding(); len(contentEncoding) > 0 {
1866+
h.mulHeader = append(h.mulHeader, contentEncoding)
1867+
}
18591868
case HeaderServer:
1860-
h.mulHeader = append(h.mulHeader, h.Server())
1869+
if server := h.Server(); len(server) > 0 {
1870+
h.mulHeader = append(h.mulHeader, server)
1871+
}
18611872
case HeaderConnection:
18621873
if h.ConnectionClose() {
18631874
h.mulHeader = append(h.mulHeader, strClose)

header_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,6 +2883,13 @@ func TestRequestHeader_PeekAll(t *testing.T) {
28832883
expectRequestHeaderAll(t, h, "Cookie", [][]byte{s2b("foobar=baz")})
28842884
expectRequestHeaderAll(t, h, HeaderTrailer, [][]byte{s2b("Foo, Bar")})
28852885
expectRequestHeaderAll(t, h, "aaa", [][]byte{s2b("aaa"), s2b("bbb")})
2886+
2887+
h.Del("Content-Type")
2888+
h.Del(HeaderHost)
2889+
h.Del("aaa")
2890+
expectRequestHeaderAll(t, h, "Content-Type", [][]byte{})
2891+
expectRequestHeaderAll(t, h, HeaderHost, [][]byte{})
2892+
expectRequestHeaderAll(t, h, "aaa", [][]byte{})
28862893
}
28872894
func expectRequestHeaderAll(t *testing.T, h *RequestHeader, key string, expectedValue [][]byte) {
28882895
if len(h.PeekAll(key)) != len(expectedValue) {
@@ -2913,6 +2920,11 @@ func TestResponseHeader_PeekAll(t *testing.T) {
29132920
expectResponseHeaderAll(t, h, HeaderServer, [][]byte{s2b("aaaa")})
29142921
expectResponseHeaderAll(t, h, HeaderSetCookie, [][]byte{s2b("cccc")})
29152922
expectResponseHeaderAll(t, h, "aaa", [][]byte{s2b("aaa"), s2b("bbb")})
2923+
2924+
h.Del(HeaderContentType)
2925+
h.Del(HeaderContentEncoding)
2926+
expectResponseHeaderAll(t, h, HeaderContentType, [][]byte{defaultContentType})
2927+
expectResponseHeaderAll(t, h, HeaderContentEncoding, [][]byte{})
29162928
}
29172929

29182930
func expectResponseHeaderAll(t *testing.T, h *ResponseHeader, key string, expectedValue [][]byte) {

0 commit comments

Comments
 (0)