aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-02-19 00:11:34 +0800
committerRunxi Yu <me@runxiyu.org>2025-02-19 00:11:34 +0800
commitbeba323119f42177e5298a11676a941ac9b482ad (patch)
treed5d75f2ddce45997083a977453398c1035906afd
parenthooks: Check error on conn.Write (diff)
downloadforge-beba323119f42177e5298a11676a941ac9b482ad.tar.gz
forge-beba323119f42177e5298a11676a941ac9b482ad.tar.zst
forge-beba323119f42177e5298a11676a941ac9b482ad.zip
http: Consistently use redirect_with{out,}_slash, never r.URL.Path
-rw-r--r--http_server.go6
-rw-r--r--url.go19
2 files changed, 20 insertions, 5 deletions
diff --git a/http_server.go b/http_server.go
index 0cc6a8b..e5cb6ce 100644
--- a/http_server.go
+++ b/http_server.go
@@ -22,10 +22,8 @@ func (router *http_router_t) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
non_empty_last_segments_len := len(segments)
- trailing_slash := false
if segments[len(segments)-1] == "" {
non_empty_last_segments_len--
- trailing_slash = true
}
if segments[0] == ":" {
@@ -186,9 +184,7 @@ func (router *http_router_t) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
handle_repo_log(w, r, params)
case "commit":
- if trailing_slash {
- http.Redirect(w, r, strings.TrimSuffix(r.URL.Path, "/"), http.StatusSeeOther)
- // TODO
+ if redirect_without_slash(w, r) {
return
}
params["commit_id"] = segments[separator_index+4]
diff --git a/url.go b/url.go
index 37ec699..0cd08d1 100644
--- a/url.go
+++ b/url.go
@@ -82,3 +82,22 @@ func redirect_with_slash(w http.ResponseWriter, r *http.Request) bool {
}
return false
}
+
+func redirect_without_slash(w http.ResponseWriter, r *http.Request) bool {
+ request_uri := r.RequestURI
+
+ path_end := strings.IndexAny(request_uri, "?#")
+ var path, rest string
+ if path_end == -1 {
+ path = request_uri
+ } else {
+ path = request_uri[:path_end]
+ rest = request_uri[path_end:]
+ }
+
+ if strings.HasSuffix(path, "/") {
+ http.Redirect(w, r, strings.TrimSuffix(path, "/") + rest, http.StatusSeeOther)
+ return true
+ }
+ return false
+}