Skip to content

Commit f73ab1e

Browse files
authored
Do not set the content-type when response has no body (#1013)
1 parent f2a4f25 commit f73ab1e

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

Diff for: gzhttp/compress.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ func (w *GzipResponseWriter) Write(b []byte) (int, error) {
131131

132132
// If the Content-Length is larger than minSize or the current buffer is larger than minSize, then continue.
133133
if cl >= w.minSize || len(w.buf) >= w.minSize {
134-
// If a Content-Type wasn't specified, infer it from the current buffer.
135-
if ct == "" {
134+
// If a Content-Type wasn't specified, infer it from the current buffer when the response has a body.
135+
if ct == "" && bodyAllowedForStatus(w.code) && len(w.buf) > 0 {
136136
ct = http.DetectContentType(w.buf)
137-
}
138137

139-
// Handles the intended case of setting a nil Content-Type (as for http/server or http/fs)
140-
// Set the header only if the key does not exist
141-
if _, ok := hdr[contentType]; w.setContentType && !ok {
142-
hdr.Set(contentType, ct)
138+
// Handles the intended case of setting a nil Content-Type (as for http/server or http/fs)
139+
// Set the header only if the key does not exist
140+
if _, ok := hdr[contentType]; w.setContentType && !ok {
141+
hdr.Set(contentType, ct)
142+
}
143143
}
144144

145145
// If the Content-Type is acceptable to GZIP, initialize the GZIP writer.
@@ -349,12 +349,14 @@ func (w *GzipResponseWriter) Close() error {
349349
ce = w.Header().Get(contentEncoding)
350350
cr = w.Header().Get(contentRange)
351351
)
352-
if ct == "" {
352+
353+
// Detects the response content-type when it does not exist and the response has a body.
354+
if ct == "" && bodyAllowedForStatus(w.code) && len(w.buf) > 0 {
353355
ct = http.DetectContentType(w.buf)
354356

355357
// Handles the intended case of setting a nil Content-Type (as for http/server or http/fs)
356358
// Set the header only if the key does not exist
357-
if _, ok := w.Header()[contentType]; bodyAllowedForStatus(w.code) && w.setContentType && !ok {
359+
if _, ok := w.Header()[contentType]; w.setContentType && !ok {
358360
w.Header().Set(contentType, ct)
359361
}
360362
}
@@ -393,7 +395,8 @@ func (w *GzipResponseWriter) Flush() {
393395
cr = w.Header().Get(contentRange)
394396
)
395397

396-
if ct == "" {
398+
// Detects the response content-type when it does not exist and the response has a body.
399+
if ct == "" && bodyAllowedForStatus(w.code) && len(w.buf) > 0 {
397400
ct = http.DetectContentType(w.buf)
398401

399402
// Handles the intended case of setting a nil Content-Type (as for http/server or http/fs)

Diff for: gzhttp/compress_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,25 @@ func TestNoContentTypeWhenNoContent(t *testing.T) {
16171617

16181618
}
16191619

1620+
func TestNoContentTypeWhenNoBody(t *testing.T) {
1621+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1622+
w.WriteHeader(http.StatusOK)
1623+
})
1624+
1625+
wrapper, err := NewWrapper()
1626+
assertNil(t, err)
1627+
1628+
req, _ := http.NewRequest("GET", "/", nil)
1629+
req.Header.Set("Accept-Encoding", "gzip")
1630+
resp := httptest.NewRecorder()
1631+
wrapper(handler).ServeHTTP(resp, req)
1632+
res := resp.Result()
1633+
1634+
assertEqual(t, http.StatusOK, res.StatusCode)
1635+
assertEqual(t, "", res.Header.Get("Content-Type"))
1636+
1637+
}
1638+
16201639
func TestContentTypeDetect(t *testing.T) {
16211640
for _, tt := range sniffTests {
16221641
t.Run(tt.desc, func(t *testing.T) {

0 commit comments

Comments
 (0)