gf/net/ghttp/ghttp_response_cors.go

100 lines
3.0 KiB
Go
Raw Normal View History

// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// 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 (
"net/url"
2019-09-03 17:18:16 +08:00
"github.com/gogf/gf/text/gstr"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/util/gconv"
)
// CORSOptions is the options for CORS feature.
// 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-09-23 16:21:19 +08:00
// DefaultCORSOptions returns the default CORS options,
// which allows any cross-domain request.
func (r *Response) DefaultCORSOptions() CORSOptions {
2019-09-03 17:18:16 +08:00
options := CORSOptions{
2019-06-24 19:05:07 +08:00
AllowOrigin: "*",
2019-06-19 09:06:52 +08:00
AllowMethods: HTTP_METHODS,
AllowCredentials: "true",
2019-09-26 15:54:13 +08:00
AllowHeaders: "Origin,Content-Type,Accept,User-Agent,Cookie,Authorization,X-Auth-Token,X-Requested-With",
2019-06-19 09:06:52 +08:00
MaxAge: 3628800,
}
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
} 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-09-23 16:21:19 +08:00
// CORS sets custom CORS options.
// 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)
}
}
// 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
}
origin := r.Request.Header.Get("Origin")
2019-09-23 22:00:04 +08:00
if origin == "" {
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.
func (r *Response) CORSDefault() {
2019-06-19 09:06:52 +08:00
r.CORS(r.DefaultCORSOptions())
}