]> Cypherpunks repositories - gostls13.git/commitdiff
http: allow override of Content-Type for ServeFile
authorAndrew Gerrand <adg@golang.org>
Wed, 6 Apr 2011 04:52:42 +0000 (14:52 +1000)
committerAndrew Gerrand <adg@golang.org>
Wed, 6 Apr 2011 04:52:42 +0000 (14:52 +1000)
R=bradfitz, bradfitzwork
CC=golang-dev
https://golang.org/cl/4368041

src/pkg/http/fs.go
src/pkg/http/fs_test.go

index 7b2cc7f93fd594d49a39af24b3fbb437ae0b46be..c5efffca9cdb0ae0989b6172a18d190e38fdd800 100644 (file)
@@ -134,21 +134,23 @@ func serveFile(w ResponseWriter, r *Request, name string, redirect bool) {
        size := d.Size
        code := StatusOK
 
-       // use extension to find content type.
-       ext := filepath.Ext(name)
-       if ctype := mime.TypeByExtension(ext); ctype != "" {
-               w.Header().Set("Content-Type", ctype)
-       } else {
-               // read first chunk to decide between utf-8 text and binary
-               var buf [1024]byte
-               n, _ := io.ReadFull(f, buf[:])
-               b := buf[:n]
-               if isText(b) {
-                       w.Header().Set("Content-Type", "text-plain; charset=utf-8")
-               } else {
-                       w.Header().Set("Content-Type", "application/octet-stream") // generic binary
+       // If Content-Type isn't set, use the file's extension to find it.
+       if w.Header().Get("Content-Type") == "" {
+               ctype := mime.TypeByExtension(filepath.Ext(name))
+               if ctype == "" {
+                       // read a chunk to decide between utf-8 text and binary
+                       var buf [1024]byte
+                       n, _ := io.ReadFull(f, buf[:])
+                       b := buf[:n]
+                       if isText(b) {
+                               ctype = "text-plain; charset=utf-8"
+                       } else {
+                               // generic binary
+                               ctype = "application/octet-stream"
+                       }
+                       f.Seek(0, os.SEEK_SET) // rewind to output whole file
                }
-               f.Seek(0, os.SEEK_SET) // rewind to output whole file
+               w.Header().Set("Content-Type", ctype)
        }
 
        // handle Content-Range header.
index a89c76d0bfb5c90b267f7c4e25bdb459b4e47d0d..692b9863e82fdc9ba4ec85a4d11d764c47c64869 100644 (file)
@@ -85,6 +85,30 @@ func TestServeFile(t *testing.T) {
        }
 }
 
+func TestServeFileContentType(t *testing.T) {
+       const ctype = "icecream/chocolate"
+       override := false
+       ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
+               if override {
+                       w.Header().Set("Content-Type", ctype)
+               }
+               ServeFile(w, r, "testdata/file")
+       }))
+       defer ts.Close()
+       get := func(want string) {
+               resp, _, err := Get(ts.URL)
+               if err != nil {
+                       t.Fatal(err)
+               }
+               if h := resp.Header.Get("Content-Type"); h != want {
+                       t.Errorf("Content-Type mismatch: got %q, want %q", h, want)
+               }
+       }
+       get("text-plain; charset=utf-8")
+       override = true
+       get(ctype)
+}
+
 func getBody(t *testing.T, req Request) (*Response, []byte) {
        r, err := DefaultClient.Do(&req)
        if err != nil {