From a4f620df90fc5022c3b217d1e75cc7be65ef6a28 Mon Sep 17 00:00:00 2001 From: Jordan Date: Thu, 8 Sep 2022 14:46:47 -0700 Subject: cleanup: wrap http.HandlerFunc, rm superfluous traversal check --- tent.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/tent.go b/tent.go index 65d65dd..deebcf5 100644 --- a/tent.go +++ b/tent.go @@ -10,21 +10,21 @@ import ( "os" "path/filepath" "strconv" - "strings" "time" ) -var path string - -func RequestHandler(w http.ResponseWriter, r *http.Request) { - p := filepath.Join(path, strings.Replace(r.URL.Path, "..", "", -1)) - http.ServeFile(w, r, p) +func RequestHandler(path string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, filepath.Join(path, r.URL.Path)) + } } func main() { - var host string - var port uint64 - + var ( + host string + port uint64 + path string + ) flag.StringVar(&host, "host", "127.0.0.1", "IP address to listen on") flag.Uint64Var(&port, "port", 0, "Port to listen on (default random)") flag.StringVar(&path, "path", "./", "Absolute or relative path to serve") @@ -33,7 +33,6 @@ func main() { if _, err := os.Stat(path); err != nil { panic(err) } - if port == 0 { rand.Seed(time.Now().UnixNano()) for { @@ -47,8 +46,7 @@ func main() { } } } - - http.HandleFunc("/", RequestHandler) + http.HandleFunc("/", RequestHandler(path)) fmt.Printf("Listening on %v port %v (http://%v:%v/)\n", host, port, host, port) log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), nil)) } -- cgit v1.2.3-54-g00ecf