diff options
author | Runxi Yu <me@runxiyu.org> | 2025-02-19 00:11:34 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-02-19 00:11:34 +0800 |
commit | beba323119f42177e5298a11676a941ac9b482ad (patch) | |
tree | d5d75f2ddce45997083a977453398c1035906afd /url.go | |
parent | hooks: Check error on conn.Write (diff) | |
download | forge-beba323119f42177e5298a11676a941ac9b482ad.tar.gz forge-beba323119f42177e5298a11676a941ac9b482ad.tar.zst forge-beba323119f42177e5298a11676a941ac9b482ad.zip |
http: Consistently use redirect_with{out,}_slash, never r.URL.Path
Diffstat (limited to 'url.go')
-rw-r--r-- | url.go | 19 |
1 files changed, 19 insertions, 0 deletions
@@ -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 +} |