@@ -20,7 +20,7 @@ import (
2020
2121const (
2222 // Version information
23- Version = "SimpleHttpServer v1.3-beta.2 "
23+ Version = "SimpleHttpServer v1.3-beta.3 "
2424 // HTTPProxy returns HTTP_PROXY
2525 HTTPProxy = "HTTP_PROXY"
2626 // HTTPSProxy returns HTTPS_PROXY
@@ -289,26 +289,26 @@ func main() {
289289 if len (config .Fallback ) > 0 {
290290 fs .PathNotFound = func (ctx * fasthttp.RequestCtx ) {
291291 fallbackpath := filepath .Join (v , config .Fallback )
292- if _ , err := os . Stat (fallbackpath ); err == nil {
292+ if fileIsExist (fallbackpath ) {
293293 mimeType := staticFileGetMimeType (filepath .Ext (fallbackpath ))
294294 if len (mimeType ) > 0 {
295295 ctx .SetContentType (mimeType )
296296 }
297297 ctx .SendFile (fallbackpath )
298298 } else {
299- ctx .Error (fasthttp .StatusMessage (fasthttp .StatusNotFound ), fasthttp .StatusNotFound )
299+ statusCode := fasthttp .StatusNotFound
300+ ctx .Error (fasthttp .StatusMessage (statusCode ), statusCode )
300301 if config .Verbose {
301- statusCode := ctx .Response .StatusCode ()
302302 go logInfo (statusCode , "%d | %s | %s | %s\n " , statusCode , ctx .RemoteIP (), ctx .Method (), ctx .Path ())
303303 }
304304 }
305305 }
306306 }
307+
307308 if k != "/" {
308309 fs .PathRewrite = fasthttp .NewPathPrefixStripper (len (k ))
309310 }
310311 fsMap [k ] = fs .NewRequestHandler ()
311- // print map paths
312312 log .Printf ("%s -> %s\n " , k , v )
313313 }
314314 if len (fsMap ) > 1 {
@@ -333,11 +333,11 @@ func requestHandler(ctx *fasthttp.RequestCtx) {
333333 if enableBasicAuth {
334334 user , pwd , ok := basicAuth (ctx )
335335 if ! ok || user != config .Username || pwd != config .Password {
336- ctx .Error (fasthttp .StatusMessage (fasthttp .StatusUnauthorized ), fasthttp .StatusUnauthorized )
336+ statusCode := fasthttp .StatusUnauthorized
337+ ctx .Error (fasthttp .StatusMessage (statusCode ), statusCode )
337338 ctx .Response .Header .Set ("WWW-Authenticate" , "Basic realm=Restricted" )
338339 // log print
339340 if config .Verbose {
340- statusCode := ctx .Response .StatusCode ()
341341 go logInfo (statusCode , "%d | %s | %s | %s | %s | %s \n " ,
342342 statusCode , ctx .RemoteIP (), ctx .Method (), ctx .Path (), user , pwd )
343343 }
@@ -346,11 +346,6 @@ func requestHandler(ctx *fasthttp.RequestCtx) {
346346 }
347347
348348 // router
349- // log print
350- if config .Verbose {
351- statusCode := ctx .Response .StatusCode ()
352- go logInfo (statusCode , "%d | %s | %s | %s\n " , statusCode , ctx .RemoteIP (), ctx .Method (), ctx .Path ())
353- }
354349 switch string (ctx .Method ()) {
355350 case "POST" :
356351 switch string (ctx .Path ()) {
@@ -362,12 +357,20 @@ func requestHandler(ctx *fasthttp.RequestCtx) {
362357 case "/upload" :
363358 uploadHandle (ctx )
364359 default :
365- ctx .Error (fasthttp .StatusMessage (fasthttp .StatusBadRequest ), fasthttp .StatusBadRequest )
360+ statusCode := fasthttp .StatusBadRequest
361+ ctx .Error (fasthttp .StatusMessage (statusCode ), statusCode )
366362 }
367363 case "GET" :
368364 fsHandler (ctx )
369365 default :
370- ctx .Error (fasthttp .StatusMessage (fasthttp .StatusNotFound ), fasthttp .StatusNotFound )
366+ statusCode := fasthttp .StatusNotFound
367+ ctx .Error (fasthttp .StatusMessage (statusCode ), statusCode )
368+ }
369+
370+ // log print
371+ if config .Verbose {
372+ statusCode := ctx .Response .StatusCode ()
373+ go logInfo (statusCode , "%d | %s | %s | %s\n " , statusCode , ctx .RemoteIP (), ctx .Method (), ctx .Path ())
371374 }
372375}
373376
@@ -388,13 +391,16 @@ func fsHandler(ctx *fasthttp.RequestCtx) {
388391
389392 for k , handler := range fsMap {
390393 if strings .HasPrefix (path , k ) {
391- if dirHandler (path , ctx ) {
394+ isDir , ok := dirHandler (path , ctx )
395+ if ok {
392396 return
393397 }
394- handler (ctx )
395- mimeType := staticFileGetMimeType (filepath .Ext (path ))
396- if len (mimeType ) > 0 {
397- ctx .SetContentType (mimeType )
398+ if ! isDir {
399+ handler (ctx )
400+ mimeType := staticFileGetMimeType (filepath .Ext (path ))
401+ if len (mimeType ) > 0 {
402+ ctx .SetContentType (mimeType )
403+ }
398404 }
399405 return
400406 }
@@ -403,31 +409,31 @@ func fsHandler(ctx *fasthttp.RequestCtx) {
403409 ctx .Error (fasthttp .StatusMessage (fasthttp .StatusNotFound ), fasthttp .StatusNotFound )
404410}
405411
406- func dirHandler (path string , ctx * fasthttp.RequestCtx ) bool {
407- localpath := ""
412+ func dirHandler (path string , ctx * fasthttp.RequestCtx ) ( isDir bool , ok bool ) {
413+ var localpath string
408414 for k , v := range config .Paths {
409415 if strings .HasPrefix (path , k ) {
410416 localpath = filepath .Join (v , path [len (k ):])
411417 break
412418 }
413419 }
414420 if len (localpath ) == 0 {
415- return false
421+ return
416422 }
417- // if localpath is a directory
418- dir , err := os .Stat (localpath )
419- if err == nil && dir .IsDir () {
423+
424+ var err error
425+ if dirIsExist (localpath ) {
426+ isDir = true
420427 for _ , v := range config .IndexNames {
421428 indexfile := filepath .Join (localpath , v )
422- if fi , err := os .Stat (indexfile ); err == nil {
423- if ! fi .IsDir () {
424- mimeType := staticFileGetMimeType (filepath .Ext (indexfile ))
425- if len (mimeType ) > 0 {
426- ctx .SetContentType (mimeType )
427- }
428- ctx .SendFile (indexfile )
429- return true
429+ if fileIsExist (indexfile ) {
430+ mimeType := staticFileGetMimeType (filepath .Ext (indexfile ))
431+ if len (mimeType ) > 0 {
432+ ctx .SetContentType (mimeType )
430433 }
434+ ctx .SendFile (indexfile )
435+ ok = true
436+ return
431437 }
432438 }
433439
@@ -479,14 +485,15 @@ func dirHandler(path string, ctx *fasthttp.RequestCtx) bool {
479485 }
480486 fmt .Fprintf (ctx , "</table></body></html>" )
481487 ctx .SetContentType ("text/html; charset=utf8" )
482- return true
488+ ok = true
489+ return
483490 }
484491 }
485492
486493 if err != nil {
487- ctx .Error (err .Error (), fasthttp .StatusOK )
494+ ctx .Error (err .Error (), fasthttp .StatusInternalServerError )
488495 }
489- return false
496+ return
490497}
491498
492499func uploadHandle (ctx * fasthttp.RequestCtx ) {
@@ -554,6 +561,13 @@ func dirIsExist(path string) bool {
554561 return false
555562}
556563
564+ func fileIsExist (path string ) bool {
565+ if fi , err := os .Stat (path ); err == nil {
566+ return ! fi .IsDir ()
567+ }
568+ return false
569+ }
570+
557571func fileOrDirIsExist (path string ) bool {
558572 if _ , err := os .Stat (path ); err == nil {
559573 return true
0 commit comments