From 98951c44aa637cbee2a17be00931395403bf8001 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Sun, 24 May 2015 17:03:44 +0200 Subject: [PATCH] Return 404 when listing empty directory --- routergroup.go | 8 +++++++- routes_test.go | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/routergroup.go b/routergroup.go index a81f108..8afd29e 100644 --- a/routergroup.go +++ b/routergroup.go @@ -137,7 +137,13 @@ func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) { func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem) HandlerFunc { absolutePath := group.calculateAbsolutePath(relativePath) fileServer := http.StripPrefix(absolutePath, http.FileServer(fs)) - return WrapH(fileServer) + _, nolisting := fs.(*onlyfilesFS) + return func(c *Context) { + if nolisting { + c.Writer.WriteHeader(404) + } + fileServer.ServeHTTP(c.Writer, c.Request) + } } func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain { diff --git a/routes_test.go b/routes_test.go index 1506a86..3d9cf88 100644 --- a/routes_test.go +++ b/routes_test.go @@ -195,6 +195,7 @@ func TestRouteStaticNoListing(t *testing.T) { w := performRequest(router, "GET", "/") + assert.Equal(t, w.Code, 404) assert.NotContains(t, w.Body.String(), "gin.go") }