commit fac2451318dbfbd07447825201bfe5eb400bf125
parent a7d0db93843b25b804c5c85cea7fad2d0f4c63bd
Author: anon <anon@example.com>
Date: Tue, 20 Oct 2026 23:32:26 +0000
Keep directory redirects inside the public URL prefix
Diffstat:
2 files changed, 27 insertions(+), 0 deletions(-)
diff --git a/server/http.go b/server/http.go
@@ -11,6 +11,17 @@ import (
func (server *server) httpHandler() http.Handler {
mux := http.NewServeMux()
+ // Directory redirects must keep the public prefix and query string.
+ for _, directory := range []string{"wasm", "map-assets"} {
+ mux.HandleFunc("GET /"+directory, func(w http.ResponseWriter, r *http.Request) {
+ location := server.baseURL + directory + "/"
+ if r.URL.RawQuery != "" {
+ location += "?" + r.URL.RawQuery
+ }
+ http.Redirect(w, r, location, http.StatusMovedPermanently)
+ })
+ }
+
mux.HandleFunc("GET /control", server.controlPage)
mux.HandleFunc("GET /users/{character}", server.userPage)
mux.HandleFunc("POST /users", server.createUser)
diff --git a/server/http_test.go b/server/http_test.go
@@ -63,3 +63,19 @@ func TestHTTPHandlerScopesVisitorCookieAndRedirects(t *testing.T) {
t.Fatalf("unmounted control page: HTTP %d", outside.Code)
}
}
+
+func TestHTTPHandlerPreservesBaseURLInDirectoryRedirects(t *testing.T) {
+ world := &server{baseURL: "/share/demo/"}
+ handler := world.httpHandler()
+ for _, redirect := range []struct{ from, to string }{
+ {"/share/demo/wasm?profile=1", "/share/demo/wasm/?profile=1"},
+ {"/share/demo/map-assets", "/share/demo/map-assets/"},
+ } {
+ response := httptest.NewRecorder()
+ handler.ServeHTTP(response, httptest.NewRequest("GET", redirect.from, nil))
+
+ if response.Code != http.StatusMovedPermanently || response.Header().Get("Location") != redirect.to {
+ t.Fatalf("directory redirect: HTTP %d, %v", response.Code, response.Header())
+ }
+ }
+}