Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not set the content-type when response has no body #1013

Merged
merged 1 commit into from
Oct 3, 2024
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
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)
}
klauspost marked this conversation as resolved.
Show resolved Hide resolved
}

// 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
Loading