Relax API CORS preflight handling

This commit is contained in:
LPF
2026-03-17 17:48:50 +08:00
parent 8da396c1ce
commit 4edfe8e2f0
2 changed files with 47 additions and 3 deletions

View File

@@ -238,10 +238,25 @@ func (s *Server) withCORS(next http.Handler) http.Handler {
next = http.NotFoundHandler()
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Requested-With")
origin := strings.TrimSpace(r.Header.Get("Origin"))
if origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Add("Vary", "Origin")
} else {
w.Header().Set("Access-Control-Allow-Origin", "*")
}
allowMethods := strings.TrimSpace(r.Header.Get("Access-Control-Request-Method"))
if allowMethods == "" {
allowMethods = "GET, POST, PUT, PATCH, DELETE, OPTIONS"
}
w.Header().Set("Access-Control-Allow-Methods", allowMethods)
allowHeaders := strings.TrimSpace(r.Header.Get("Access-Control-Request-Headers"))
if allowHeaders == "" {
allowHeaders = "Authorization, Content-Type, X-Requested-With, Accept, Origin, Cache-Control, Pragma"
}
w.Header().Set("Access-Control-Allow-Headers", allowHeaders)
w.Header().Set("Access-Control-Expose-Headers", "*")
w.Header().Set("Access-Control-Max-Age", "86400")
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return