BlueGit

timfa bluegit

Routes: afsluitende slash op profiel-/groepspagina's doorsturen (/timfa/ gaf 404)

timfa Tim Falken · pushed by timfa

Authored 2026-11-04 · 13 days ago

Hash
8c9688648e75bd9da399b78c639ce9682e83f1c9
Parent
999fb362
Pipeline
Passed
3 files changed +73 −0
internal/server/people.go +13 −0
@@ -217,3 +217,16 @@ func initialOf(s string) string {
217 217 r, _ := utf8.DecodeRuneInString(s)
218 218 return strings.ToUpper(string(r))
219 219 }
220
221 // trimSlash stuurt een pagina-URL met afsluitende slash permanent door naar
222 // dezelfde URL zonder slash (querystring blijft behouden).
223 func (s *Server) trimSlash(w http.ResponseWriter, r *http.Request) {
224 target := strings.TrimRight(r.URL.Path, "/")
225 if target == "" {
226 target = "/"
227 }
228 if r.URL.RawQuery != "" {
229 target += "?" + r.URL.RawQuery
230 }
231 http.Redirect(w, r, target, http.StatusMovedPermanently)
232 }
@@ -217,3 +217,16 @@ func initialOf(s string) string {
217 r, _ := utf8.DecodeRuneInString(s)
217 r, _ := utf8.DecodeRuneInString(s)
218 return strings.ToUpper(string(r))
218 return strings.ToUpper(string(r))
219}
219}
220
221// trimSlash stuurt een pagina-URL met afsluitende slash permanent door naar
222// dezelfde URL zonder slash (querystring blijft behouden).
223func (s *Server) trimSlash(w http.ResponseWriter, r *http.Request) {
224 target := strings.TrimRight(r.URL.Path, "/")
225 if target == "" {
226 target = "/"
227 }
228 if r.URL.RawQuery != "" {
229 target += "?" + r.URL.RawQuery
230 }
231 http.Redirect(w, r, target, http.StatusMovedPermanently)
232}
internal/server/server.go +3 −0
@@ -124,7 +124,10 @@ func (s *Server) Handler() http.Handler {
124 124 mux.HandleFunc("POST /settings/ssh/delete", s.deleteSSH)
125 125 mux.HandleFunc("GET /settings/export", s.export)
126 126 mux.HandleFunc("GET /users/{username}", s.profile)
127 mux.HandleFunc("GET /users/{username}/{$}", s.trimSlash)
127 128 mux.HandleFunc("GET /{name}", s.userShort)
129 mux.HandleFunc("GET /{name}/{$}", s.userShort)
130 mux.HandleFunc("GET /groups/{path}/{$}", s.trimSlash)
128 131 mux.HandleFunc("GET /groups/{path}", s.groupPage)
129 132 mux.HandleFunc("GET /groups/{path}/logo", s.groupLogo)
130 133 mux.HandleFunc("POST /groups/{path}/settings", s.saveGroup)
@@ -124,7 +124,10 @@ func (s *Server) Handler() http.Handler {
124 mux.HandleFunc("POST /settings/ssh/delete", s.deleteSSH)
124 mux.HandleFunc("POST /settings/ssh/delete", s.deleteSSH)
125 mux.HandleFunc("GET /settings/export", s.export)
125 mux.HandleFunc("GET /settings/export", s.export)
126 mux.HandleFunc("GET /users/{username}", s.profile)
126 mux.HandleFunc("GET /users/{username}", s.profile)
127 mux.HandleFunc("GET /users/{username}/{$}", s.trimSlash)
127 mux.HandleFunc("GET /{name}", s.userShort)
128 mux.HandleFunc("GET /{name}", s.userShort)
129 mux.HandleFunc("GET /{name}/{$}", s.userShort)
130 mux.HandleFunc("GET /groups/{path}/{$}", s.trimSlash)
128 mux.HandleFunc("GET /groups/{path}", s.groupPage)
131 mux.HandleFunc("GET /groups/{path}", s.groupPage)
129 mux.HandleFunc("GET /groups/{path}/logo", s.groupLogo)
132 mux.HandleFunc("GET /groups/{path}/logo", s.groupLogo)
130 mux.HandleFunc("POST /groups/{path}/settings", s.saveGroup)
133 mux.HandleFunc("POST /groups/{path}/settings", s.saveGroup)
internal/server/trailing_slash_test.go added +57 −0
@@ -0,0 +1,57 @@
1 package server
2
3 import (
4 "net/http"
5 "net/http/httptest"
6 "path/filepath"
7 "testing"
8
9 "gitlab.com/Timfa/bluegit/internal/auth"
10 "gitlab.com/Timfa/bluegit/internal/config"
11 "gitlab.com/Timfa/bluegit/internal/store"
12 )
13
14 func TestTrailingSlashPages(t *testing.T) {
15 storage := t.TempDir()
16 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
17 if err != nil {
18 t.Fatal(err)
19 }
20 t.Cleanup(func() { _ = st.Close() })
21 cfg := config.Config{
22 BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage,
23 Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test"},
24 Git: config.Git{Binary: "git", DefaultBranch: "master"},
25 }
26 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"}
27 ts := httptest.NewServer(srv.Handler())
28 t.Cleanup(ts.Close)
29 if _, err := st.UpsertUser(t.Context(), 2, "ada", "", false); err != nil {
30 t.Fatal(err)
31 }
32 client := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
33 cases := []struct{ path, loc string }{
34 {"/ada", "/users/ada"},
35 {"/ada/", "/users/ada"},
36 {"/users/ada/", "/users/ada"},
37 {"/groups/plein/", "/groups/plein"},
38 }
39 for _, c := range cases {
40 res, err := client.Get(ts.URL + c.path)
41 if err != nil {
42 t.Fatal(err)
43 }
44 res.Body.Close()
45 if res.StatusCode/100 != 3 || res.Header.Get("Location") != c.loc {
46 t.Errorf("%s: status %d location %q, wil redirect naar %q", c.path, res.StatusCode, res.Header.Get("Location"), c.loc)
47 }
48 }
49 res, err := client.Get(ts.URL + "/niemand/")
50 if err != nil {
51 t.Fatal(err)
52 }
53 res.Body.Close()
54 if res.StatusCode != http.StatusNotFound {
55 t.Errorf("/niemand/: status %d, wil 404", res.StatusCode)
56 }
57 }
@@ -0,0 +1,57 @@
1package server
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "path/filepath"
7 "testing"
8
9 "gitlab.com/Timfa/bluegit/internal/auth"
10 "gitlab.com/Timfa/bluegit/internal/config"
11 "gitlab.com/Timfa/bluegit/internal/store"
12)
13
14func TestTrailingSlashPages(t *testing.T) {
15 storage := t.TempDir()
16 st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32())
17 if err != nil {
18 t.Fatal(err)
19 }
20 t.Cleanup(func() { _ = st.Close() })
21 cfg := config.Config{
22 BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage,
23 Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test"},
24 Git: config.Git{Binary: "git", DefaultBranch: "master"},
25 }
26 srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", HookBin: "/bin/true"}
27 ts := httptest.NewServer(srv.Handler())
28 t.Cleanup(ts.Close)
29 if _, err := st.UpsertUser(t.Context(), 2, "ada", "", false); err != nil {
30 t.Fatal(err)
31 }
32 client := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
33 cases := []struct{ path, loc string }{
34 {"/ada", "/users/ada"},
35 {"/ada/", "/users/ada"},
36 {"/users/ada/", "/users/ada"},
37 {"/groups/plein/", "/groups/plein"},
38 }
39 for _, c := range cases {
40 res, err := client.Get(ts.URL + c.path)
41 if err != nil {
42 t.Fatal(err)
43 }
44 res.Body.Close()
45 if res.StatusCode/100 != 3 || res.Header.Get("Location") != c.loc {
46 t.Errorf("%s: status %d location %q, wil redirect naar %q", c.path, res.StatusCode, res.Header.Get("Location"), c.loc)
47 }
48 }
49 res, err := client.Get(ts.URL + "/niemand/")
50 if err != nil {
51 t.Fatal(err)
52 }
53 res.Body.Close()
54 if res.StatusCode != http.StatusNotFound {
55 t.Errorf("/niemand/: status %d, wil 404", res.StatusCode)
56 }
57}