From fa02595f8b50834944659df4cfd17461c6c0f103 Mon Sep 17 00:00:00 2001 From: Catherine Date: Sun, 23 Nov 2025 00:14:39 +0000 Subject: [PATCH] Handle `OPTIONS` method. --- src/pages.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/pages.go b/src/pages.go index e0a263b..1f730d4 100644 --- a/src/pages.go +++ b/src/pages.go @@ -13,6 +13,7 @@ import ( "net/url" "os" "path" + "slices" "strconv" "strings" "time" @@ -666,9 +667,15 @@ func ServePages(w http.ResponseWriter, r *http.Request) { } } } + allowedMethods := []string{"OPTIONS", "HEAD", "GET", "PUT", "DELETE", "POST"} + if r.Method == "OPTIONS" || !slices.Contains(allowedMethods, r.Method) { + w.Header().Add("Allow", strings.Join(allowedMethods, ", ")) + } err := error(nil) switch r.Method { // REST API + case http.MethodOptions: + // no preflight options case http.MethodHead, http.MethodGet: err = getPage(w, r) case http.MethodPut: @@ -679,7 +686,6 @@ func ServePages(w http.ResponseWriter, r *http.Request) { case http.MethodPost: err = postPage(w, r) default: - w.Header().Add("Allow", "HEAD, GET, PUT, DELETE, POST") http.Error(w, "method not allowed", http.StatusMethodNotAllowed) err = fmt.Errorf("method %s not allowed", r.Method) }