Skip to content

Commit

Permalink
fix: no copy some fields at ResponseHeader and RequestHeader (cloudwe…
Browse files Browse the repository at this point in the history
  • Loading branch information
wzekin authored and Skyenought committed Jun 10, 2023
1 parent aec5711 commit 037aa21
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
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
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)
}

0 comments on commit 037aa21

Please sign in to comment.