Browse Source

Return 404 when listing empty directory

Manu Mtz-Almeida 10 năm trước cách đây
mục cha
commit
98951c44aa
2 tập tin đã thay đổi với 8 bổ sung1 xóa
  1. 7 1
      routergroup.go
  2. 1 0
      routes_test.go

+ 7 - 1
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 {
 func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem) HandlerFunc {
 	absolutePath := group.calculateAbsolutePath(relativePath)
 	absolutePath := group.calculateAbsolutePath(relativePath)
 	fileServer := http.StripPrefix(absolutePath, http.FileServer(fs))
 	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 {
 func (group *RouterGroup) combineHandlers(handlers HandlersChain) HandlersChain {

+ 1 - 0
routes_test.go

@@ -195,6 +195,7 @@ func TestRouteStaticNoListing(t *testing.T) {
 
 
 	w := performRequest(router, "GET", "/")
 	w := performRequest(router, "GET", "/")
 
 
+	assert.Equal(t, w.Code, 404)
 	assert.NotContains(t, w.Body.String(), "gin.go")
 	assert.NotContains(t, w.Body.String(), "gin.go")
 }
 }