1
0
mirror of https://github.com/golang/go synced 2024-09-28 20:14:28 -06:00

net/http: always set Content-Length in serveContent

This commit is contained in:
Mitar 2022-01-29 06:51:46 -08:00 committed by Mitar
parent 4cd201b14b
commit 3aff9126f5
2 changed files with 3 additions and 5 deletions

View File

@ -343,9 +343,7 @@ func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time,
}
w.Header().Set("Accept-Ranges", "bytes")
if w.Header().Get("Content-Encoding") == "" {
w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
}
w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
w.WriteHeader(code)

View File

@ -571,7 +571,7 @@ func testServeDirWithoutTrailingSlash(t *testing.T, mode testMode) {
}
}
// Tests that ServeFile doesn't add a Content-Length if a Content-Encoding is
// Tests that ServeFile adds a Content-Length even if a Content-Encoding is
// specified.
func TestServeFileWithContentEncoding(t *testing.T) { run(t, testServeFileWithContentEncoding) }
func testServeFileWithContentEncoding(t *testing.T, mode testMode) {
@ -593,7 +593,7 @@ func testServeFileWithContentEncoding(t *testing.T, mode testMode) {
t.Fatal(err)
}
resp.Body.Close()
if g, e := resp.ContentLength, int64(-1); g != e {
if g, e := resp.ContentLength, int64(11); g != e {
t.Errorf("Content-Length mismatch: got %d, want %d", g, e)
}
}