Enable CORS for "OPTIONS" request

This commit is contained in:
Gucheng Wang 2023-03-16 00:24:18 +08:00
parent fd883a3211
commit 5e47406e09

View File

@ -36,7 +36,7 @@ func CorsFilter(ctx *context.Context) {
if origin != "" && originConf != "" && origin != originConf {
if object.IsOriginAllowed(origin) {
ctx.Output.Header(headerAllowOrigin, origin)
ctx.Output.Header(headerAllowMethods, "POST, GET, OPTIONS")
ctx.Output.Header(headerAllowMethods, "POST, GET, OPTIONS, DELETE")
ctx.Output.Header(headerAllowHeaders, "Content-Type, Authorization")
} else {
ctx.ResponseWriter.WriteHeader(http.StatusForbidden)
@ -48,4 +48,11 @@ func CorsFilter(ctx *context.Context) {
return
}
}
if ctx.Input.Method() == "OPTIONS" {
ctx.Output.Header(headerAllowOrigin, "*")
ctx.Output.Header(headerAllowMethods, "POST, GET, OPTIONS, DELETE")
ctx.ResponseWriter.WriteHeader(http.StatusOK)
return
}
}