Commit a610957f authored by Nathan Caza's avatar Nathan Caza Committed by Brad Fitzpatrick

net/http: handle absolute paths in mapDirOpenError

The current implementation does not account for Dir being
initialized with an absolute path on systems that start
paths with filepath.Separator. In this scenario, the
original error is returned, and not checked for file
segments.

This change adds a test for this case, and corrects the
behavior by ignoring blank path segments in the loop.

Refs #18984

Change-Id: I9b79fd0a73a46976c8e2feda0283ef0bb2b62ea1
Reviewed-on: https://go-review.googlesource.com/36804Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent ef30a1c8
...@@ -43,6 +43,9 @@ func mapDirOpenError(originalErr error, name string) error { ...@@ -43,6 +43,9 @@ func mapDirOpenError(originalErr error, name string) error {
parts := strings.Split(name, string(filepath.Separator)) parts := strings.Split(name, string(filepath.Separator))
for i := range parts { for i := range parts {
if parts[i] == "" {
continue
}
fi, err := os.Stat(strings.Join(parts[:i+1], string(filepath.Separator))) fi, err := os.Stat(strings.Join(parts[:i+1], string(filepath.Separator)))
if err != nil { if err != nil {
return originalErr return originalErr
......
...@@ -1176,22 +1176,33 @@ func TestFileServerNotDirError(t *testing.T) { ...@@ -1176,22 +1176,33 @@ func TestFileServerNotDirError(t *testing.T) {
t.Errorf("StatusCode = %v; want 404", res.StatusCode) t.Errorf("StatusCode = %v; want 404", res.StatusCode)
} }
dir := Dir("testdata") test := func(name string, dir Dir) {
_, err = dir.Open("/index.html/not-a-file") t.Run(name, func(t *testing.T) {
if err == nil { _, err = dir.Open("/index.html/not-a-file")
t.Fatal("err == nil; want != nil") if err == nil {
} t.Fatal("err == nil; want != nil")
if !os.IsNotExist(err) { }
t.Errorf("err = %v; os.IsNotExist(err) = %v; want true", err, os.IsNotExist(err)) if !os.IsNotExist(err) {
} t.Errorf("err = %v; os.IsNotExist(err) = %v; want true", err, os.IsNotExist(err))
}
_, err = dir.Open("/index.html/not-a-dir/not-a-file") _, err = dir.Open("/index.html/not-a-dir/not-a-file")
if err == nil { if err == nil {
t.Fatal("err == nil; want != nil") t.Fatal("err == nil; want != nil")
}
if !os.IsNotExist(err) {
t.Errorf("err = %v; os.IsNotExist(err) = %v; want true", err, os.IsNotExist(err))
}
})
} }
if !os.IsNotExist(err) {
t.Errorf("err = %v; os.IsNotExist(err) = %v; want true", err, os.IsNotExist(err)) absPath, err := filepath.Abs("testdata")
if err != nil {
t.Fatal("get abs path:", err)
} }
test("RelativePath", Dir("testdata"))
test("AbsolutePath", Dir(absPath))
} }
func TestFileServerCleanPath(t *testing.T) { func TestFileServerCleanPath(t *testing.T) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment