aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2011-06-02 13:36:52 -0700
committerBrad Fitzpatrick <bradfitz@golang.org>2011-06-02 13:36:52 -0700
commit31c79c4effb40ea938d2c740ad7149e4ac4a45a6 (patch)
treec72222b5773e8dd9e4c57a8ae73faf351cc470a9
parent5bf57c1b416ec5031f48cbc3300beebdffaacc70 (diff)
downloadgo-31c79c4effb40ea938d2c740ad7149e4ac4a45a6.tar.gz
go-31c79c4effb40ea938d2c740ad7149e4ac4a45a6.zip
http: ServeFile shouldn't send Content-Length when Content-Encoding is set
Fixes #1905 R=rsc CC=golang-dev https://golang.org/cl/4538111
-rw-r--r--src/pkg/http/fs.go4
-rw-r--r--src/pkg/http/fs_test.go17
2 files changed, 19 insertions, 2 deletions
diff --git a/src/pkg/http/fs.go b/src/pkg/http/fs.go
index 17d5297b82..28a0c51ef5 100644
--- a/src/pkg/http/fs.go
+++ b/src/pkg/http/fs.go
@@ -175,7 +175,9 @@ func serveFile(w ResponseWriter, r *Request, name string, redirect bool) {
}
w.Header().Set("Accept-Ranges", "bytes")
- w.Header().Set("Content-Length", strconv.Itoa64(size))
+ if w.Header().Get("Content-Encoding") == "" {
+ w.Header().Set("Content-Length", strconv.Itoa64(size))
+ }
w.WriteHeader(code)
diff --git a/src/pkg/http/fs_test.go b/src/pkg/http/fs_test.go
index b94196258e..554053449e 100644
--- a/src/pkg/http/fs_test.go
+++ b/src/pkg/http/fs_test.go
@@ -101,7 +101,7 @@ func TestServeFileContentType(t *testing.T) {
t.Fatal(err)
}
if h := resp.Header.Get("Content-Type"); h != want {
- t.Errorf("Content-Type mismatch: got %q, want %q", h, want)
+ t.Errorf("Content-Type mismatch: got %d, want %d", h, want)
}
}
get("text/plain; charset=utf-8")
@@ -109,6 +109,21 @@ func TestServeFileContentType(t *testing.T) {
get(ctype)
}
+func TestServeFileWithContentEncoding(t *testing.T) {
+ ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
+ w.Header().Set("Content-Encoding", "foo")
+ ServeFile(w, r, "testdata/file")
+ }))
+ defer ts.Close()
+ resp, err := Get(ts.URL)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if g, e := resp.ContentLength, int64(-1); g != e {
+ t.Errorf("Content-Length mismatch: got %q, want %q", g, e)
+ }
+}
+
func getBody(t *testing.T, req Request) (*Response, []byte) {
r, err := DefaultClient.Do(&req)
if err != nil {