aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Gerrand <adg@golang.org>2011-11-17 11:42:25 +1100
committerAndrew Gerrand <adg@golang.org>2011-11-17 11:42:25 +1100
commit0b1bcf8f94620b34396b3549ea959646e830c7c8 (patch)
treee3e892d00ea3410afbfdbe5df9635733497c864d
parent3ec82f6e0960ffd082a6b63b3c784e8901bd3c4d (diff)
downloadgo-0b1bcf8f94620b34396b3549ea959646e830c7c8.tar.gz
go-0b1bcf8f94620b34396b3549ea959646e830c7c8.zip
http: fix serving from CWD with http.ServeFile
http: make Dir("") equivalent to Dir(".") Fixes #2471. R=golang-dev, bradfitz, rsc CC=golang-dev https://golang.org/cl/5370061
-rw-r--r--src/pkg/net/http/fs.go8
-rw-r--r--src/pkg/net/http/fs_test.go28
2 files changed, 35 insertions, 1 deletions
diff --git a/src/pkg/net/http/fs.go b/src/pkg/net/http/fs.go
index 5f91ff5cbf..5aadac17a2 100644
--- a/src/pkg/net/http/fs.go
+++ b/src/pkg/net/http/fs.go
@@ -22,13 +22,19 @@ import (
// A Dir implements http.FileSystem using the native file
// system restricted to a specific directory tree.
+//
+// An empty Dir is treated as ".".
type Dir string
func (d Dir) Open(name string) (File, error) {
if filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0 {
return nil, errors.New("http: invalid character in file path")
}
- f, err := os.Open(filepath.Join(string(d), filepath.FromSlash(path.Clean("/"+name))))
+ dir := string(d)
+ if dir == "" {
+ dir = "."
+ }
+ f, err := os.Open(filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name))))
if err != nil {
return nil, err
}
diff --git a/src/pkg/net/http/fs_test.go b/src/pkg/net/http/fs_test.go
index e1a784c1f6..6697189900 100644
--- a/src/pkg/net/http/fs_test.go
+++ b/src/pkg/net/http/fs_test.go
@@ -208,6 +208,20 @@ func TestDirJoin(t *testing.T) {
test(Dir("/etc/hosts"), "../")
}
+func TestEmptyDirOpenCWD(t *testing.T) {
+ test := func(d Dir) {
+ name := "fs_test.go"
+ f, err := d.Open(name)
+ if err != nil {
+ t.Fatalf("open of %s: %v", name, err)
+ }
+ defer f.Close()
+ }
+ test(Dir(""))
+ test(Dir("."))
+ test(Dir("./"))
+}
+
func TestServeFileContentType(t *testing.T) {
const ctype = "icecream/chocolate"
override := false
@@ -247,6 +261,20 @@ func TestServeFileMimeType(t *testing.T) {
}
}
+func TestServeFileFromCWD(t *testing.T) {
+ ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
+ ServeFile(w, r, "fs_test.go")
+ }))
+ defer ts.Close()
+ r, err := Get(ts.URL)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if r.StatusCode != 200 {
+ t.Fatalf("expected 200 OK, got %s", r.Status)
+ }
+}
+
func TestServeFileWithContentEncoding(t *testing.T) {
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
w.Header().Set("Content-Encoding", "foo")