From dd0a4a8ba9a0f58e7a016171b7561cb8487ca216 Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Sun, 6 Aug 2023 04:03:04 +0200 Subject: [PATCH] Instead of keeping a requestPath in app, use request.RequestURI I was keeping this variable to redirect to the requested URL when the user has no permission, is not logged in, and is shown the login form to redirect them back to the original URL. Since each handler removes a part of the path each time, i need to keep the original Path for that. However, i just found out that request.RequestURI already is that original URI requested by the client. No need for an extra variable. --- pkg/app/admin.go | 4 ++-- pkg/app/app.go | 7 +++---- pkg/app/user.go | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/pkg/app/admin.go b/pkg/app/admin.go index 3dd2a92..ca1eaf8 100644 --- a/pkg/app/admin.go +++ b/pkg/app/admin.go @@ -25,11 +25,11 @@ func newAdminHandler() *adminHandler { } } -func (h *adminHandler) Handle(user *auth.User, company *auth.Company, conn *database.Conn, requestPath string) http.Handler { +func (h *adminHandler) Handle(user *auth.User, company *auth.Company, conn *database.Conn) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if !user.LoggedIn { w.WriteHeader(http.StatusUnauthorized) - serveLoginForm(w, r, user, company, requestPath) + serveLoginForm(w, r, user, company, r.RequestURI) return } diff --git a/pkg/app/app.go b/pkg/app/app.go index 8930d8c..a26bb60 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -52,14 +52,13 @@ func New(db *database.DB, avatarsDir string) (http.Handler, error) { } func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { - requestPath := r.URL.Path var head string head, r.URL.Path = httplib.ShiftPath(r.URL.Path) switch head { case "static": h.fileHandler.ServeHTTP(w, r) case "favicon.ico": - r.URL.Path = requestPath + r.URL.Path = head h.fileHandler.ServeHTTP(w, r) default: conn, err := h.db.Acquire(r.Context()) @@ -85,7 +84,7 @@ func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { case "": http.Redirect(w, r, "/"+user.Locale.Language.String()+"/", http.StatusFound) case "admin": - h.admin.Handle(user, company, conn, requestPath).ServeHTTP(w, r) + h.admin.Handle(user, company, conn).ServeHTTP(w, r) case "login": switch r.Method { case http.MethodGet: @@ -100,7 +99,7 @@ func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { httplib.MethodNotAllowed(w, r, http.MethodPost, http.MethodGet) } case "me": - h.profile.Handler(user, company, conn, requestPath).ServeHTTP(w, r) + h.profile.Handler(user, company, conn).ServeHTTP(w, r) default: langTag, err := language.Parse(head) if err != nil { diff --git a/pkg/app/user.go b/pkg/app/user.go index dce876a..c903706 100644 --- a/pkg/app/user.go +++ b/pkg/app/user.go @@ -80,11 +80,11 @@ func newProfileHandler(static http.Handler, avatarsDir string) (*profileHandler, return handler, nil } -func (h *profileHandler) Handler(user *auth.User, company *auth.Company, conn *database.Conn, requestPath string) http.HandlerFunc { +func (h *profileHandler) Handler(user *auth.User, company *auth.Company, conn *database.Conn) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if !user.LoggedIn { w.WriteHeader(http.StatusUnauthorized) - serveLoginForm(w, r, user, company, requestPath) + serveLoginForm(w, r, user, company, r.RequestURI) return }