Skip to content
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
11 changes: 8 additions & 3 deletions pkg/protocols/http/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,14 @@ func newPipelinedRequest(req *retryablehttp.Request, body []byte) (*clientpipeli
}

piped := clientpipeline.ToRequest(req.Method, path, nil, req.Header, reader)
// The client sends a Content-Length for a body when the caller set none.
if len(body) > 0 && !hasHeaderFold(req.Header, contentLengthHeader) {
piped.Headers = append(piped.Headers, clientpipeline.Header{Key: contentLengthHeader, Value: strconv.Itoa(len(body))})
// The client frames a body the caller did not frame with Content-Length,
// trusting req.ContentLength when it is set.
if len(body) > 0 && !hasHeaderFold(req.Header, contentLengthHeader) && !hasHeaderFold(req.Header, "Transfer-Encoding") {
length := req.ContentLength
if length <= 0 {
length = int64(len(body))
}
piped.Headers = append(piped.Headers, clientpipeline.Header{Key: contentLengthHeader, Value: strconv.FormatInt(length, 10)})
}
// The client writes headers in map order. A fixed order keeps the dump
// the same between runs.
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocols/http/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestDumpPipelinedRequestWritesWhatTheClientSends(t *testing.T) {
require.Equal(t,
"POST /a?x=1&y=2 HTTP/1.1\r\nHost: example.com:8443\r\nA-First: 1\r\nA-First: 2\r\nContent-Length: 32\r\nZ-Last: 1\r\n\r\n"+body,
string(full.data),
"no Connection or Accept-Encoding line: the client writes none")
"no Connection or Accept-Encoding line: the client writes none, and the client's own Content-Length")

capped, err := dumpPipelinedRequest(req, 10)
require.NoError(t, err)
Expand Down
Loading