commit 4b695893676c417c0da1be80dcf64eb1489cf66a
parent dcb1226e0e6e43c2915b6f8898730786da2e2096
Author: anon <anon@example.com>
Date: Wed, 21 Oct 2026 01:54:29 +0000
Return 404 for unknown HTTP paths
Diffstat:
2 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/server/http.go b/server/http.go
@@ -1,8 +1,6 @@
package main
import (
- "fmt"
- "html"
"io/fs"
"log"
"net/http"
@@ -52,14 +50,14 @@ func (server *server) httpHandler() http.Handler {
mux.HandleFunc("GET /{$}", server.browserClient)
mux.HandleFunc("POST /time", server.setGameTime)
mux.HandleFunc("POST /teleport/{character}", server.teleportPlayer)
- // Serve the original launcher's dimension list and informational pages.
+ // Keep the original launcher's dimension list available.
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Printf("HTTP %s %s", r.Method, r.URL.Path)
if strings.HasSuffix(r.URL.Path, "dimensions.txt") {
server.dimensionList(w, r)
return
}
- fmt.Fprintf(w, `<html><body><a href="%swasm/">Play Anarchy Online 14.9.1</a> | <a href="%scontrol">Server controls</a></body></html>`, html.EscapeString(server.baseURL), html.EscapeString(server.baseURL))
+ http.NotFound(w, r)
})
// Keep internal routes independent of the public mounting path.
diff --git a/server/http_test.go b/server/http_test.go
@@ -79,3 +79,19 @@ func TestHTTPHandlerPreservesBaseURLInDirectoryRedirects(t *testing.T) {
}
}
}
+
+func TestHTTPHandlerReturnsNotFoundForUnknownPaths(t *testing.T) {
+ for _, baseURL := range []string{"/", "/share/demo/"} {
+ t.Run(baseURL, func(t *testing.T) {
+ world := &server{baseURL: baseURL}
+ handler := world.httpHandler()
+
+ response := httptest.NewRecorder()
+ handler.ServeHTTP(response, httptest.NewRequest("GET", baseURL+"messages", nil))
+
+ if response.Code != http.StatusNotFound || strings.TrimSpace(response.Body.String()) != "404 page not found" {
+ t.Fatalf("unknown path: HTTP %d, %q", response.Code, response.Body.String())
+ }
+ })
+ }
+}