lib/httpserver: handle preflight HTTP requests properly

Previously OPTIONS HTTP requests for CORS preflight checks would trigger
the original request handler. This pull request fixes that behavior to
align with https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS

Fixes https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5563
This commit is contained in:
andriibeee
2026-03-05 16:57:13 +02:00
committed by GitHub
parent 8f215137e7
commit 686c9a21ff
3 changed files with 62 additions and 0 deletions

View File

@@ -357,6 +357,12 @@ func handlerWrapper(w http.ResponseWriter, r *http.Request, rh RequestHandler) {
r.URL.Path = path
}
if r.Method == http.MethodOptions {
EnableCORS(w, r)
w.WriteHeader(http.StatusNoContent)
return
}
w = &responseWriterWithAbort{
ResponseWriter: w,
}
@@ -511,6 +517,8 @@ func EnableCORS(w http.ResponseWriter, _ *http.Request) {
return
}
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
}
func pprofHandler(profileName string, w http.ResponseWriter, r *http.Request) {