Close files opened in static file handler

This commit is contained in:
Shamus Taylor 2019-10-30 09:53:07 -05:00 committed by GitHub
parent 393a63f3b0
commit f86827dd12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -193,13 +193,15 @@ func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileS
file := c.Param("filepath")
// Check if file exists and/or if we have permission to access it
if _, err := fs.Open(file); err != nil {
f, err := fs.Open(file)
if err != nil {
c.Writer.WriteHeader(http.StatusNotFound)
c.handlers = group.engine.noRoute
// Reset index
c.index = -1
return
}
defer f.Close()
fileServer.ServeHTTP(c.Writer, c.Request)
}