Commit 76ec785e authored by Matthew Holt's avatar Matthew Holt

ext: Fix panic when URL path is empty

parent e9b9432d
...@@ -31,7 +31,7 @@ type Ext struct { ...@@ -31,7 +31,7 @@ type Ext struct {
// ServeHTTP implements the middleware.Handler interface. // ServeHTTP implements the middleware.Handler interface.
func (e Ext) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { func (e Ext) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
urlpath := strings.TrimSuffix(r.URL.Path, "/") urlpath := strings.TrimSuffix(r.URL.Path, "/")
if path.Ext(urlpath) == "" && r.URL.Path[len(r.URL.Path)-1] != '/' { if path.Ext(urlpath) == "" && len(r.URL.Path) > 0 && r.URL.Path[len(r.URL.Path)-1] != '/' {
for _, ext := range e.Extensions { for _, ext := range e.Extensions {
if resourceExists(e.Root, urlpath+ext) { if resourceExists(e.Root, urlpath+ext) {
r.URL.Path = urlpath + ext r.URL.Path = urlpath + ext
......
...@@ -30,6 +30,7 @@ func TestExtensions(t *testing.T) { ...@@ -30,6 +30,7 @@ func TestExtensions(t *testing.T) {
{"/extensions_test/", []string{".html"}, "/extensions_test/"}, {"/extensions_test/", []string{".html"}, "/extensions_test/"},
{"/extensions_test", []string{".json"}, "/extensions_test"}, {"/extensions_test", []string{".json"}, "/extensions_test"},
{"/another_test", []string{".html"}, "/another_test"}, {"/another_test", []string{".html"}, "/another_test"},
{"", []string{".html"}, ""},
} { } {
ex := Ext{ ex := Ext{
Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
......
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