Skip to content

Commit

Permalink
Do not set the content-type when response has no body (#1013)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpollet authored Oct 3, 2024
1 parent f2a4f25 commit f73ab1e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
23 changes: 13 additions & 10 deletions gzhttp/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ func (w *GzipResponseWriter) Write(b []byte) (int, error) {

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

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

// If the Content-Type is acceptable to GZIP, initialize the GZIP writer.
Expand Down Expand Up @@ -349,12 +349,14 @@ func (w *GzipResponseWriter) Close() error {
ce = w.Header().Get(contentEncoding)
cr = w.Header().Get(contentRange)
)
if ct == "" {

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

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

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

// Handles the intended case of setting a nil Content-Type (as for http/server or http/fs)
Expand Down
19 changes: 19 additions & 0 deletions gzhttp/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,25 @@ func TestNoContentTypeWhenNoContent(t *testing.T) {

}

func TestNoContentTypeWhenNoBody(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})

wrapper, err := NewWrapper()
assertNil(t, err)

req, _ := http.NewRequest("GET", "/", nil)
req.Header.Set("Accept-Encoding", "gzip")
resp := httptest.NewRecorder()
wrapper(handler).ServeHTTP(resp, req)
res := resp.Result()

assertEqual(t, http.StatusOK, res.StatusCode)
assertEqual(t, "", res.Header.Get("Content-Type"))

}

func TestContentTypeDetect(t *testing.T) {
for _, tt := range sniffTests {
t.Run(tt.desc, func(t *testing.T) {
Expand Down

0 comments on commit f73ab1e

Please sign in to comment.