ext: Fix panic when URL path is empty

This commit is contained in:
Matthew Holt 2015-11-10 16:04:02 -07:00
parent e9b9432da5
commit 76ec785e87
2 changed files with 2 additions and 1 deletions

View file

@ -31,7 +31,7 @@ type Ext struct {
// ServeHTTP implements the middleware.Handler interface.
func (e Ext) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
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 {
if resourceExists(e.Root, urlpath+ext) {
r.URL.Path = urlpath + ext

View file

@ -30,6 +30,7 @@ func TestExtensions(t *testing.T) {
{"/extensions_test/", []string{".html"}, "/extensions_test/"},
{"/extensions_test", []string{".json"}, "/extensions_test"},
{"/another_test", []string{".html"}, "/another_test"},
{"", []string{".html"}, ""},
} {
ex := Ext{
Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {