Skip to content

Commit 08abf4d

Browse files
committed
internal/http3: infer headers when Content-Encoding is set but is empty
TestContentEncodingNoSniffing in std verifies that when a Content-Encoding header is set to an empty value, it is considered as if the header is not set at all, and that Content-Type header should be inferred. Make HTTP/3 behave similarly to HTTP/1 and HTTP/2 implementations in this regard. For golang/go#70914 Change-Id: Icbb60fd00f182c1ddc4e2372e4d2e4686a6a6964 Reviewed-on: https://go-review.googlesource.com/c/net/+/812760 Reviewed-by: Nicholas Husin <[email protected]> LUCI-TryBot-Result: [email protected] <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent 8d10596 commit 08abf4d

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎internal/http3/server.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ func (bb *bodyBuffer) inferHeader(h http.Header, status int) {
915915
}
916916
// If the Content-Encoding is non-blank, we shouldn't
917917
// sniff the body. See Issue golang.org/issue/31753.
918-
_, hasCE := h["Content-Encoding"]
918+
hasCE := len(h.Get("Content-Encoding")) > 0
919919
_, hasCT := h["Content-Type"]
920920
if !hasCE && !hasCT && responseCanHaveBody(status) && len(*bb) > 0 {
921921
h.Set("Content-Type", http.DetectContentType(*bb))

‎internal/http3/server_test.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,20 @@ func TestServerInfersHeaders(t *testing.T) {
10381038
"Some-Other-Header": {"some value"},
10391039
},
10401040
},
1041+
{
1042+
name: "infers content type for response with empty content encoding",
1043+
responseStatus: 200,
1044+
declaredHeader: http.Header{
1045+
"Content-Encoding": {""},
1046+
"Some-Other-Header": {"some value"},
1047+
},
1048+
want: http.Header{
1049+
"Date": {"Sat, 01 Jan 2000 00:00:00 GMT"}, // Synctest starting time.
1050+
"Content-Encoding": {""},
1051+
"Content-Type": {"text/html; charset=utf-8"},
1052+
"Some-Other-Header": {"some value"},
1053+
},
1054+
},
10411055
{
10421056
name: "does not infer content type when header is flushed before body is written",
10431057
responseStatus: 200,

0 commit comments

Comments
 (0)