]> Cypherpunks repositories - gostls13.git/commitdiff
http: fix Content-Type of file extension.
authorYasuhiro Matsumoto <mattn.jp@gmail.com>
Wed, 13 Jul 2011 21:39:33 +0000 (14:39 -0700)
committerRuss Cox <rsc@golang.org>
Wed, 13 Jul 2011 21:39:33 +0000 (14:39 -0700)
ServeFile() pass empty string to serveFile(). serveFile() should get
file extension via joining root and filename.

R=bradfitz, rsc
CC=golang-dev
https://golang.org/cl/4654089

src/pkg/http/fs.go
src/pkg/http/fs_test.go
src/pkg/http/testdata/style.css [new file with mode: 0644]

index 0b830053a9b2c2235a39b03e3092faf0cb1d6786..34fe77d6bd677c1ea021f7d283f44bfcf7e2fac4 100644 (file)
@@ -222,7 +222,8 @@ func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirec
 
 // ServeFile replies to the request with the contents of the named file or directory.
 func ServeFile(w ResponseWriter, r *Request, name string) {
-       serveFile(w, r, Dir(name), "", false)
+       dir, file := filepath.Split(name)
+       serveFile(w, r, Dir(dir), file, false)
 }
 
 type fileHandler struct {
index dbbdf05bdc4431874f2dbec67ab4a7fb00857a11..0c6edba9bcf5df6b256cedb86a69367432e7bfa2 100644 (file)
@@ -175,6 +175,21 @@ func TestServeFileContentType(t *testing.T) {
        get(ctype)
 }
 
+func TestServeFileMimeType(t *testing.T) {
+       ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
+               ServeFile(w, r, "testdata/style.css")
+       }))
+       defer ts.Close()
+       resp, err := Get(ts.URL)
+       if err != nil {
+               t.Fatal(err)
+       }
+       want := "text/css"
+       if h := resp.Header.Get("Content-Type"); h != want {
+               t.Errorf("Content-Type mismatch: got %q, want %q", h, want)
+       }
+}
+
 func TestServeFileWithContentEncoding(t *testing.T) {
        ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
                w.Header().Set("Content-Encoding", "foo")
diff --git a/src/pkg/http/testdata/style.css b/src/pkg/http/testdata/style.css
new file mode 100644 (file)
index 0000000..208d16d
--- /dev/null
@@ -0,0 +1 @@
+body {}