From c5b0865977668f512cfb7964c21d6141eb3a9969 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 20 Jan 2024 10:33:18 -0500 Subject: [PATCH] fix: need to create a custom version of strip prefix that appends a trailing slash for the router --- api/middleware/s5.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/api/middleware/s5.go b/api/middleware/s5.go index 6b8bc1d..072ec01 100644 --- a/api/middleware/s5.go +++ b/api/middleware/s5.go @@ -134,6 +134,26 @@ func (w *tusJwtResponseWriter) WriteHeader(statusCode int) { w.ResponseWriter.WriteHeader(statusCode) } +func replacePrefix(prefix string, h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + p := strings.TrimPrefix(r.URL.Path, prefix) + rp := strings.TrimPrefix(r.URL.RawPath, prefix) + if len(p) < len(r.URL.Path) && (r.URL.RawPath == "" || len(rp) < len(r.URL.RawPath)) { + p += "/" + rp += "/" + r2 := new(http.Request) + *r2 = *r + r2.URL = new(url.URL) + *r2.URL = *r.URL + r2.URL.Path = p + r2.URL.RawPath = rp + h.ServeHTTP(w, r2) + } else { + http.NotFound(w, r) + } + }) +} + func BuildS5TusApi(portal interfaces.Portal) jape.Handler { // Wrapper function for AuthMiddleware to fit the MiddlewareFunc signature @@ -156,7 +176,7 @@ func BuildS5TusApi(portal interfaces.Portal) jape.Handler { stripPrefix := func(h jape.Handler) jape.Handler { return jape.Adapt(func(h http.Handler) http.Handler { - return http.StripPrefix("/s5/upload/tus", h) + return replacePrefix("/s5/upload/tus", h) })(h) }