Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize: adjust the behavior of PeekAll based on VisitAll #1403

Merged
merged 1 commit into from Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 18 additions & 7 deletions header.go
Expand Up @@ -1811,11 +1811,17 @@ func (h *RequestHeader) peekAll(key []byte) [][]byte {
h.mulHeader = h.mulHeader[:0]
switch string(key) {
case HeaderHost:
h.mulHeader = append(h.mulHeader, h.Host())
if host := h.Host(); len(host) > 0 {
h.mulHeader = append(h.mulHeader, host)
}
case HeaderContentType:
h.mulHeader = append(h.mulHeader, h.ContentType())
if contentType := h.ContentType(); len(contentType) > 0 {
h.mulHeader = append(h.mulHeader, contentType)
}
case HeaderUserAgent:
h.mulHeader = append(h.mulHeader, h.UserAgent())
if ua := h.UserAgent(); len(ua) > 0 {
h.mulHeader = append(h.mulHeader, ua)
}
case HeaderConnection:
if h.ConnectionClose() {
h.mulHeader = append(h.mulHeader, strClose)
Expand All @@ -1827,7 +1833,6 @@ func (h *RequestHeader) peekAll(key []byte) [][]byte {
case HeaderCookie:
if h.cookiesCollected {
h.mulHeader = append(h.mulHeader, appendRequestCookieBytes(nil, h.cookies))
return [][]byte{appendRequestCookieBytes(nil, h.cookies)}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize for a problem here that I didn't find before.

} else {
h.mulHeader = peekAllArgBytesToDst(h.mulHeader, h.h, key)
}
Expand All @@ -1853,11 +1858,17 @@ func (h *ResponseHeader) peekAll(key []byte) [][]byte {
h.mulHeader = h.mulHeader[:0]
switch string(key) {
case HeaderContentType:
h.mulHeader = append(h.mulHeader, h.ContentType())
if contentType := h.ContentType(); len(contentType) > 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Referring to VisitAll, I found that when using PeekAll, if the field is empty, he returns [][]byte{ '' } instead of [][]byte{} to the point that I modified it.

h.mulHeader = append(h.mulHeader, contentType)
}
case HeaderContentEncoding:
h.mulHeader = append(h.mulHeader, h.ContentEncoding())
if contentEncoding := h.ContentEncoding(); len(contentEncoding) > 0 {
h.mulHeader = append(h.mulHeader, contentEncoding)
}
case HeaderServer:
h.mulHeader = append(h.mulHeader, h.Server())
if server := h.Server(); len(server) > 0 {
h.mulHeader = append(h.mulHeader, server)
}
case HeaderConnection:
if h.ConnectionClose() {
h.mulHeader = append(h.mulHeader, strClose)
Expand Down
12 changes: 12 additions & 0 deletions header_test.go
Expand Up @@ -2883,6 +2883,13 @@ func TestRequestHeader_PeekAll(t *testing.T) {
expectRequestHeaderAll(t, h, "Cookie", [][]byte{s2b("foobar=baz")})
expectRequestHeaderAll(t, h, HeaderTrailer, [][]byte{s2b("Foo, Bar")})
expectRequestHeaderAll(t, h, "aaa", [][]byte{s2b("aaa"), s2b("bbb")})

h.Del("Content-Type")
h.Del(HeaderHost)
h.Del("aaa")
expectRequestHeaderAll(t, h, "Content-Type", [][]byte{})
expectRequestHeaderAll(t, h, HeaderHost, [][]byte{})
expectRequestHeaderAll(t, h, "aaa", [][]byte{})
}
func expectRequestHeaderAll(t *testing.T, h *RequestHeader, key string, expectedValue [][]byte) {
if len(h.PeekAll(key)) != len(expectedValue) {
Expand Down Expand Up @@ -2913,6 +2920,11 @@ func TestResponseHeader_PeekAll(t *testing.T) {
expectResponseHeaderAll(t, h, HeaderServer, [][]byte{s2b("aaaa")})
expectResponseHeaderAll(t, h, HeaderSetCookie, [][]byte{s2b("cccc")})
expectResponseHeaderAll(t, h, "aaa", [][]byte{s2b("aaa"), s2b("bbb")})

h.Del(HeaderContentType)
h.Del(HeaderContentEncoding)
expectResponseHeaderAll(t, h, HeaderContentType, [][]byte{defaultContentType})
expectResponseHeaderAll(t, h, HeaderContentEncoding, [][]byte{})
}

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