88 "net/url"
99 "os"
1010 "path"
11- "path/filepath"
1211 "strings"
1312
1413 "github.com/labstack/echo/v4"
@@ -157,9 +156,9 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
157156 }
158157
159158 // Index template
160- t , err := template .New ("index" ).Parse (html )
161- if err != nil {
162- panic (fmt .Sprintf ("echo: %v " , err ))
159+ t , tErr := template .New ("index" ).Parse (html )
160+ if tErr != nil {
161+ panic (fmt .Errorf ("echo: %w " , tErr ))
163162 }
164163
165164 return func (next echo.HandlerFunc ) echo.HandlerFunc {
@@ -176,7 +175,7 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
176175 if err != nil {
177176 return
178177 }
179- name := filepath .Join (config .Root , filepath .Clean ("/" + p )) // "/"+ for security
178+ name := path .Join (config .Root , path .Clean ("/" + p )) // "/"+ for security
180179
181180 if config .IgnoreBase {
182181 routePath := path .Base (strings .TrimRight (c .Path (), "/*" ))
@@ -187,12 +186,14 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
187186 }
188187 }
189188
190- file , err := openFile ( config .Filesystem , name )
189+ file , err := config .Filesystem . Open ( name )
191190 if err != nil {
192- if ! os . IsNotExist (err ) {
191+ if ! isIgnorableOpenFileError (err ) {
193192 return err
194193 }
195194
195+ // file with that path did not exist, so we continue down in middleware/handler chain, hoping that we end up in
196+ // handler that is meant to handle this request
196197 if err = next (c ); err == nil {
197198 return err
198199 }
@@ -202,7 +203,7 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
202203 return err
203204 }
204205
205- file , err = openFile ( config .Filesystem , filepath .Join (config .Root , config .Index ))
206+ file , err = config .Filesystem . Open ( path .Join (config .Root , config .Index ))
206207 if err != nil {
207208 return err
208209 }
@@ -216,15 +217,13 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
216217 }
217218
218219 if info .IsDir () {
219- index , err := openFile ( config .Filesystem , filepath .Join (name , config .Index ))
220+ index , err := config .Filesystem . Open ( path .Join (name , config .Index ))
220221 if err != nil {
221222 if config .Browse {
222223 return listDir (t , name , file , c .Response ())
223224 }
224225
225- if os .IsNotExist (err ) {
226- return next (c )
227- }
226+ return next (c )
228227 }
229228
230229 defer index .Close ()
@@ -242,11 +241,6 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
242241 }
243242}
244243
245- func openFile (fs http.FileSystem , name string ) (http.File , error ) {
246- pathWithSlashes := filepath .ToSlash (name )
247- return fs .Open (pathWithSlashes )
248- }
249-
250244func serveFile (c echo.Context , file http.File , info os.FileInfo ) error {
251245 http .ServeContent (c .Response (), c .Request (), info .Name (), info .ModTime (), file )
252246 return nil
0 commit comments