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

fix: no copy some fields at ResponseHeader and RequestHeader #796

Merged
merged 1 commit into from
May 29, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/protocol/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ func (h *ResponseHeader) CopyTo(dst *ResponseHeader) {
dst.server = append(dst.server[:0], h.server...)
dst.h = copyArgs(dst.h, h.h)
dst.cookies = copyArgs(dst.cookies, h.cookies)
dst.protocol = h.protocol
wzekin marked this conversation as resolved.
Show resolved Hide resolved
dst.headerLength = h.headerLength
h.Trailer().CopyTo(dst.Trailer())
}

Expand Down Expand Up @@ -1107,6 +1109,7 @@ func (h *RequestHeader) CopyTo(dst *RequestHeader) {
dst.cookies = copyArgs(dst.cookies, h.cookies)
dst.cookiesCollected = h.cookiesCollected
dst.rawHeaders = append(dst.rawHeaders[:0], h.rawHeaders...)
dst.protocol = h.protocol
}

// Peek returns header value for the given key.
Expand Down
58 changes: 58 additions & 0 deletions pkg/protocol/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,3 +656,61 @@ func expectResponseHeaderAll(t *testing.T, h *ResponseHeader, key string, expect
}
assert.DeepEqual(t, h.PeekAll(key), expectedValue)
}

func TestRequestHeaderCopyTo(t *testing.T) {
t.Parallel()

h, hCopy := &RequestHeader{}, &RequestHeader{}
h.SetProtocol(consts.HTTP10)
h.SetMethod(consts.MethodPatch)
h.SetNoDefaultContentType(true)
h.Add(consts.HeaderConnection, "keep-alive")
h.Add("Content-Type", "aaa")
h.Add(consts.HeaderHost, "aaabbb")
h.Add("User-Agent", "asdfas")
h.Add("Content-Length", "1123")
h.Add("Cookie", "foobar=baz")
h.Add("aaa", "aaa")
h.Add("aaa", "bbb")

h.CopyTo(hCopy)
expectRequestHeaderAll(t, hCopy, consts.HeaderConnection, [][]byte{[]byte("keep-alive")})
expectRequestHeaderAll(t, hCopy, "Content-Type", [][]byte{[]byte("aaa")})
expectRequestHeaderAll(t, hCopy, consts.HeaderHost, [][]byte{[]byte("aaabbb")})
expectRequestHeaderAll(t, hCopy, "User-Agent", [][]byte{[]byte("asdfas")})
expectRequestHeaderAll(t, hCopy, "Content-Length", [][]byte{[]byte("1123")})
expectRequestHeaderAll(t, hCopy, "Cookie", [][]byte{[]byte("foobar=baz")})
expectRequestHeaderAll(t, hCopy, "aaa", [][]byte{[]byte("aaa"), []byte("bbb")})
assert.DeepEqual(t, hCopy.GetProtocol(), consts.HTTP10)
assert.DeepEqual(t, hCopy.noDefaultContentType, true)
assert.DeepEqual(t, string(hCopy.Method()), consts.MethodPatch)
}

func TestResponseHeaderCopyTo(t *testing.T) {
t.Parallel()

h, hCopy := &ResponseHeader{}, &ResponseHeader{}
h.SetProtocol(consts.HTTP10)
h.SetHeaderLength(100)
h.SetNoDefaultContentType(true)
h.Add(consts.HeaderContentType, "aaa/bbb")
h.Add(consts.HeaderContentEncoding, "gzip")
h.Add(consts.HeaderConnection, "close")
h.Add(consts.HeaderContentLength, "1234")
h.Add(consts.HeaderServer, "aaaa")
h.Add(consts.HeaderSetCookie, "cccc")
h.Add("aaa", "aaa")
h.Add("aaa", "bbb")

h.CopyTo(hCopy)
expectResponseHeaderAll(t, hCopy, consts.HeaderContentType, [][]byte{[]byte("aaa/bbb")})
expectResponseHeaderAll(t, hCopy, consts.HeaderContentEncoding, [][]byte{[]byte("gzip")})
expectResponseHeaderAll(t, hCopy, consts.HeaderConnection, [][]byte{[]byte("close")})
expectResponseHeaderAll(t, hCopy, consts.HeaderContentLength, [][]byte{[]byte("1234")})
expectResponseHeaderAll(t, hCopy, consts.HeaderServer, [][]byte{[]byte("aaaa")})
expectResponseHeaderAll(t, hCopy, consts.HeaderSetCookie, [][]byte{[]byte("cccc")})
expectResponseHeaderAll(t, hCopy, "aaa", [][]byte{[]byte("aaa"), []byte("bbb")})
assert.DeepEqual(t, hCopy.GetProtocol(), consts.HTTP10)
assert.DeepEqual(t, hCopy.noDefaultContentType, true)
assert.DeepEqual(t, hCopy.GetHeaderLength(), 100)
}