aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan <me@jordan.im>2022-09-08 14:46:47 -0700
committerJordan <me@jordan.im>2022-09-08 14:46:47 -0700
commita4f620df90fc5022c3b217d1e75cc7be65ef6a28 (patch)
tree72d3bc0e6537d2fea07ee8ea649bcefb1c310bfa
parent6fba4c80ed803fb806d03ea740c1035725b7f282 (diff)
downloadtent-a4f620df90fc5022c3b217d1e75cc7be65ef6a28.tar.gz
tent-a4f620df90fc5022c3b217d1e75cc7be65ef6a28.zip
cleanup: wrap http.HandlerFunc, rm superfluous traversal checkHEADmaster
-rw-r--r--tent.go22
1 files 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))
}