aboutsummaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2019-06-04 22:26:09 +0000
committerBrad Fitzpatrick <bradfitz@golang.org>2019-08-28 18:38:52 +0000
commit7ed973b4d9dab38347f34e87febf3c8659160ce6 (patch)
tree9a398d0a24aee47847b791b1458f2c1811d07184 /src/net
parentb3a1205a1133328068f002dd44141cb9efb2127b (diff)
downloadgo-7ed973b4d9dab38347f34e87febf3c8659160ce6.tar.xz
net/http: don't panic serving dir in ServeFile with empty Request.URL.Path
Updates #30165 Updates #31622 Change-Id: I7a4b91aa7c5c3af8c0b1273cbb42046feddf7d78 Reviewed-on: https://go-review.googlesource.com/c/go/+/180499 Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com> Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/net')
-rw-r--r--src/net/http/fs.go2
-rw-r--r--src/net/http/fs_test.go12
2 files changed, 13 insertions, 1 deletions
diff --git a/src/net/http/fs.go b/src/net/http/fs.go
index 41d46dced2..4c4f0e429e 100644
--- a/src/net/http/fs.go
+++ b/src/net/http/fs.go
@@ -585,7 +585,7 @@ func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirec
// redirect if the directory name doesn't end in a slash
if d.IsDir() {
url := r.URL.Path
- if url[len(url)-1] != '/' {
+ if url == "" || url[len(url)-1] != '/' {
localRedirect(w, r, path.Base(url)+"/")
return
}
diff --git a/src/net/http/fs_test.go b/src/net/http/fs_test.go
index 762e88b05f..047bb04ad8 100644
--- a/src/net/http/fs_test.go
+++ b/src/net/http/fs_test.go
@@ -207,6 +207,18 @@ func TestServeFile_DotDot(t *testing.T) {
}
}
+// Tests that this doesn't panic. (Issue 30165)
+func TestServeFileDirPanicEmptyPath(t *testing.T) {
+ rec := httptest.NewRecorder()
+ req := httptest.NewRequest("GET", "/", nil)
+ req.URL.Path = ""
+ ServeFile(rec, req, "testdata")
+ res := rec.Result()
+ if res.StatusCode != 301 {
+ t.Errorf("code = %v; want 301", res.Status)
+ }
+}
+
var fsRedirectTestData = []struct {
original, redirect string
}{