improve CORS feature for ghttp.Server

This commit is contained in:
John
2020-02-16 23:18:37 +08:00
parent 89f869dd44
commit f18312419b

View File

@ -8,6 +8,7 @@
package ghttp
import (
"net/http"
"net/url"
"github.com/gogf/gf/text/gstr"
@ -69,6 +70,13 @@ func (r *Response) CORS(options CORSOptions) {
if options.AllowHeaders != "" {
r.Header().Set("Access-Control-Allow-Headers", options.AllowHeaders)
}
// No continue service handling if it's OPTIONS request.
if gstr.Equal(r.Request.Method, "OPTIONS") {
if r.Status == 0 {
r.Status = http.StatusOK
}
r.Request.ExitAll()
}
}
// CORSAllowed checks whether the current request origin is allowed cross-domain.