diff options
| author | Julian Hurst <julian.hurst@digdash.com> | 2025-04-02 17:52:24 +0200 |
|---|---|---|
| committer | Julian Hurst <julian.hurst@digdash.com> | 2025-04-02 17:52:24 +0200 |
| commit | 6936cf7c5d19737aaa87e21334f0a395f2b3d60d (patch) | |
| tree | 2347db7cf48649be3b2c1cdc8c9920ebffb0c382 | |
| parent | 620281a44db7f0d07e0a92e6511ac7df02ac7318 (diff) | |
| download | box-6936cf7c5d19737aaa87e21334f0a395f2b3d60d.tar.gz | |
Trim proxy prefix in request url path
| -rw-r--r-- | main.go | 15 |
1 files changed, 9 insertions, 6 deletions
@@ -14,6 +14,7 @@ import ( "bufio" "errors" "io/fs" + "strings" "github.com/google/uuid" "golang.org/x/crypto/bcrypt" @@ -25,6 +26,7 @@ var tmplFS embed.FS //go:embed favicon.ico var favicon []byte +const PROXYPREFIXKEY string = "BOX_PROXY_PREFIX" type BoxHandler struct { filesPath string @@ -43,23 +45,24 @@ func serve(w http.ResponseWriter, token []byte, views ...string) { ProxyPrefix template.URL }{ Token: token != nil, - ProxyPrefix: template.URL(os.Getenv("BOX_PROXY_PREFIX")), + ProxyPrefix: template.URL(os.Getenv(PROXYPREFIXKEY)), }); err != nil { log.Fatal(err) } } func (handler BoxHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + reqPath := strings.TrimPrefix(r.URL.Path, os.Getenv(PROXYPREFIXKEY)) switch r.Method { case http.MethodGet: - if r.URL.Path == "/favicon.ico" { + if reqPath == "/favicon.ico" { w.Write(favicon) return } - if r.URL.Path == "/" { + if reqPath == "/" { serve(w, handler.token, "templates/index.html") } else { - resourceId := path.Base(r.URL.Path) + resourceId := path.Base(reqPath) resourcePath := filepath.Join(handler.filesPath, resourceId) fi, err := os.Stat(resourcePath) if errors.Is(err, fs.ErrNotExist) { @@ -91,7 +94,7 @@ func (handler BoxHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusUnauthorized) return } - resourceId := path.Base(r.URL.Path) + resourceId := path.Base(reqPath) filename := filepath.Join(handler.filesPath, resourceId) err := os.Remove(filename) if err != nil { @@ -103,7 +106,7 @@ func (handler BoxHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { log.Printf("Deleted %s", filename) w.WriteHeader(http.StatusNoContent) case http.MethodPost: - if r.URL.Path != "/upload" { + if reqPath != "/upload" { w.WriteHeader(http.StatusBadRequest) return } |
