Skip to content

Commit

Permalink
Return 404 when listing empty directory
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed May 24, 2015
1 parent fcfe656 commit 98951c4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
8 changes: 7 additions & 1 deletion routergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down

0 comments on commit 98951c4

Please sign in to comment.