API: Add OPTIONS wildcard handler to serve CORS preflight requests #5133

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2025-08-13 15:59:38 +02:00
parent d47b38bc8b
commit a7a41fe000
11 changed files with 239 additions and 28 deletions

View File

@@ -24,11 +24,14 @@ var Api = func(conf *config.Config) gin.HandlerFunc {
if origin := conf.CORSOrigin(); origin != "" {
c.Header(header.AccessControlAllowOrigin, origin)
// Add additional information to preflight OPTION requests.
// Handle OPTIONS preflight requests by adding CORS headers
// and aborting the request with HTTP status code 204.
if c.Request.Method == http.MethodOptions {
c.Header(header.AccessControlAllowHeaders, conf.CORSHeaders())
c.Header(header.AccessControlAllowMethods, conf.CORSMethods())
c.Header(header.AccessControlMaxAge, header.DefaultAccessControlMaxAge)
c.AbortWithStatus(http.StatusNoContent)
return
}
}
}