8
8
"net/url"
9
9
"os"
10
10
"path"
11
- "path/filepath"
12
11
"strings"
13
12
14
13
"github.com/labstack/echo/v4"
@@ -157,9 +156,9 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
157
156
}
158
157
159
158
// 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 ))
163
162
}
164
163
165
164
return func (next echo.HandlerFunc ) echo.HandlerFunc {
@@ -176,7 +175,7 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
176
175
if err != nil {
177
176
return
178
177
}
179
- name := filepath .Join (config .Root , filepath .Clean ("/" + p )) // "/"+ for security
178
+ name := path .Join (config .Root , path .Clean ("/" + p )) // "/"+ for security
180
179
181
180
if config .IgnoreBase {
182
181
routePath := path .Base (strings .TrimRight (c .Path (), "/*" ))
@@ -187,12 +186,14 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
187
186
}
188
187
}
189
188
190
- file , err := openFile ( config .Filesystem , name )
189
+ file , err := config .Filesystem . Open ( name )
191
190
if err != nil {
192
- if ! os . IsNotExist (err ) {
191
+ if ! isIgnorableOpenFileError (err ) {
193
192
return err
194
193
}
195
194
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
196
197
if err = next (c ); err == nil {
197
198
return err
198
199
}
@@ -202,7 +203,7 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
202
203
return err
203
204
}
204
205
205
- file , err = openFile ( config .Filesystem , filepath .Join (config .Root , config .Index ))
206
+ file , err = config .Filesystem . Open ( path .Join (config .Root , config .Index ))
206
207
if err != nil {
207
208
return err
208
209
}
@@ -216,15 +217,13 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
216
217
}
217
218
218
219
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 ))
220
221
if err != nil {
221
222
if config .Browse {
222
223
return listDir (t , name , file , c .Response ())
223
224
}
224
225
225
- if os .IsNotExist (err ) {
226
- return next (c )
227
- }
226
+ return next (c )
228
227
}
229
228
230
229
defer index .Close ()
@@ -242,11 +241,6 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
242
241
}
243
242
}
244
243
245
- func openFile (fs http.FileSystem , name string ) (http.File , error ) {
246
- pathWithSlashes := filepath .ToSlash (name )
247
- return fs .Open (pathWithSlashes )
248
- }
249
-
250
244
func serveFile (c echo.Context , file http.File , info os.FileInfo ) error {
251
245
http .ServeContent (c .Response (), c .Request (), info .Name (), info .ModTime (), file )
252
246
return nil
0 commit comments