2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-03-01 23:45:55 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
//
|
|
|
|
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
import (
|
2020-03-04 22:52:56 +08:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-11-13 23:23:55 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2019-03-01 23:45:55 +08:00
|
|
|
)
|
|
|
|
|
2019-11-20 12:09:26 +08:00
|
|
|
// CORSOptions is the options for CORS feature.
|
2019-03-01 23:45:55 +08:00
|
|
|
// See https://www.w3.org/TR/cors/ .
|
|
|
|
type CORSOptions struct {
|
2019-09-23 16:21:19 +08:00
|
|
|
AllowDomain []string // Used for allowing requests from custom domains
|
|
|
|
AllowOrigin string // Access-Control-Allow-Origin
|
|
|
|
AllowCredentials string // Access-Control-Allow-Credentials
|
|
|
|
ExposeHeaders string // Access-Control-Expose-Headers
|
|
|
|
MaxAge int // Access-Control-Max-Age
|
|
|
|
AllowMethods string // Access-Control-Allow-Methods
|
|
|
|
AllowHeaders string // Access-Control-Allow-Headers
|
2019-03-01 23:45:55 +08:00
|
|
|
}
|
|
|
|
|
2020-03-04 17:29:23 +08:00
|
|
|
var (
|
|
|
|
// defaultAllowHeaders is the default allowed headers for CORS.
|
2022-03-19 17:58:21 +08:00
|
|
|
// It defined another map for better header key searching performance.
|
2020-03-04 22:52:56 +08:00
|
|
|
defaultAllowHeaders = "Origin,Content-Type,Accept,User-Agent,Cookie,Authorization,X-Auth-Token,X-Requested-With"
|
|
|
|
defaultAllowHeadersMap = make(map[string]struct{})
|
2020-03-04 17:29:23 +08:00
|
|
|
)
|
|
|
|
|
2020-03-04 22:52:56 +08:00
|
|
|
func init() {
|
|
|
|
array := gstr.SplitAndTrim(defaultAllowHeaders, ",")
|
|
|
|
for _, header := range array {
|
|
|
|
defaultAllowHeadersMap[header] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 16:21:19 +08:00
|
|
|
// DefaultCORSOptions returns the default CORS options,
|
|
|
|
// which allows any cross-domain request.
|
2019-03-01 23:45:55 +08:00
|
|
|
func (r *Response) DefaultCORSOptions() CORSOptions {
|
2019-09-03 17:18:16 +08:00
|
|
|
options := CORSOptions{
|
2019-06-24 19:05:07 +08:00
|
|
|
AllowOrigin: "*",
|
2021-01-19 19:33:21 +08:00
|
|
|
AllowMethods: supportedHttpMethods,
|
2019-06-19 09:06:52 +08:00
|
|
|
AllowCredentials: "true",
|
2020-03-04 22:52:56 +08:00
|
|
|
AllowHeaders: defaultAllowHeaders,
|
2019-06-19 09:06:52 +08:00
|
|
|
MaxAge: 3628800,
|
|
|
|
}
|
2020-03-04 17:29:23 +08:00
|
|
|
// Allow all client's custom headers in default.
|
|
|
|
if headers := r.Request.Header.Get("Access-Control-Request-Headers"); headers != "" {
|
|
|
|
array := gstr.SplitAndTrim(headers, ",")
|
|
|
|
for _, header := range array {
|
2020-03-04 22:52:56 +08:00
|
|
|
if _, ok := defaultAllowHeadersMap[header]; !ok {
|
2020-08-04 11:57:42 +08:00
|
|
|
options.AllowHeaders += "," + header
|
2020-03-04 17:29:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Allow all anywhere origin in default.
|
2019-09-26 15:54:13 +08:00
|
|
|
if origin := r.Request.Header.Get("Origin"); origin != "" {
|
2019-09-23 22:00:04 +08:00
|
|
|
options.AllowOrigin = origin
|
2019-09-24 20:19:18 +08:00
|
|
|
} else if referer := r.Request.Referer(); referer != "" {
|
2019-09-03 17:18:16 +08:00
|
|
|
if p := gstr.PosR(referer, "/", 6); p != -1 {
|
|
|
|
options.AllowOrigin = referer[:p]
|
|
|
|
} else {
|
|
|
|
options.AllowOrigin = referer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return options
|
2019-03-01 23:45:55 +08:00
|
|
|
}
|
|
|
|
|
2019-09-23 16:21:19 +08:00
|
|
|
// CORS sets custom CORS options.
|
2019-03-01 23:45:55 +08:00
|
|
|
// See https://www.w3.org/TR/cors/ .
|
|
|
|
func (r *Response) CORS(options CORSOptions) {
|
2019-09-23 22:00:04 +08:00
|
|
|
if r.CORSAllowedOrigin(options) {
|
2019-06-19 09:06:52 +08:00
|
|
|
r.Header().Set("Access-Control-Allow-Origin", options.AllowOrigin)
|
|
|
|
}
|
|
|
|
if options.AllowCredentials != "" {
|
|
|
|
r.Header().Set("Access-Control-Allow-Credentials", options.AllowCredentials)
|
|
|
|
}
|
|
|
|
if options.ExposeHeaders != "" {
|
|
|
|
r.Header().Set("Access-Control-Expose-Headers", options.ExposeHeaders)
|
|
|
|
}
|
|
|
|
if options.MaxAge != 0 {
|
|
|
|
r.Header().Set("Access-Control-Max-Age", gconv.String(options.MaxAge))
|
|
|
|
}
|
|
|
|
if options.AllowMethods != "" {
|
|
|
|
r.Header().Set("Access-Control-Allow-Methods", options.AllowMethods)
|
|
|
|
}
|
|
|
|
if options.AllowHeaders != "" {
|
|
|
|
r.Header().Set("Access-Control-Allow-Headers", options.AllowHeaders)
|
|
|
|
}
|
2020-02-16 23:18:37 +08:00
|
|
|
// No continue service handling if it's OPTIONS request.
|
2020-03-17 17:46:43 +08:00
|
|
|
// Note that there's special checks in previous router searching,
|
|
|
|
// so if it goes to here it means there's already serving handler exist.
|
2020-02-16 23:18:37 +08:00
|
|
|
if gstr.Equal(r.Request.Method, "OPTIONS") {
|
|
|
|
if r.Status == 0 {
|
2020-03-17 17:46:43 +08:00
|
|
|
r.Status = http.StatusOK
|
2020-02-16 23:18:37 +08:00
|
|
|
}
|
2020-03-17 17:46:43 +08:00
|
|
|
// No continue serving.
|
2020-02-16 23:18:37 +08:00
|
|
|
r.Request.ExitAll()
|
|
|
|
}
|
2019-03-01 23:45:55 +08:00
|
|
|
}
|
|
|
|
|
2021-11-13 23:23:55 +08:00
|
|
|
// CORSAllowedOrigin CORSAllowed checks whether the current request origin is allowed cross-domain.
|
2019-09-23 22:00:04 +08:00
|
|
|
func (r *Response) CORSAllowedOrigin(options CORSOptions) bool {
|
|
|
|
if options.AllowDomain == nil {
|
|
|
|
return true
|
|
|
|
}
|
2019-09-24 20:19:18 +08:00
|
|
|
origin := r.Request.Header.Get("Origin")
|
2019-09-23 22:00:04 +08:00
|
|
|
if origin == "" {
|
2019-09-24 20:19:18 +08:00
|
|
|
return true
|
2019-09-23 22:00:04 +08:00
|
|
|
}
|
|
|
|
parsed, err := url.Parse(origin)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, v := range options.AllowDomain {
|
|
|
|
if gstr.IsSubDomain(parsed.Host, v) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-09-23 16:21:19 +08:00
|
|
|
// CORSDefault sets CORS with default CORS options,
|
|
|
|
// which allows any cross-domain request.
|
2019-03-01 23:45:55 +08:00
|
|
|
func (r *Response) CORSDefault() {
|
2019-06-19 09:06:52 +08:00
|
|
|
r.CORS(r.DefaultCORSOptions())
|
2019-03-01 23:45:55 +08:00
|
|
|
}
|