cors/cors.go

101 lines
3.2 KiB
Go
Raw Normal View History

2015-11-11 22:16:49 +01:00
package cors
import (
"errors"
"strings"
"time"
"github.com/gin-gonic/gin"
2015-11-11 22:16:49 +01:00
)
// Config represents all available options for the middleware.
2015-11-11 22:16:49 +01:00
type Config struct {
AllowAllOrigins bool
// AllowedOrigins is a list of origins a cross-domain request can be executed from.
// If the special "*" value is present in the list, all origins will be allowed.
// Default value is []
2015-11-12 15:14:38 +01:00
AllowOrigins []string
2015-11-11 22:16:49 +01:00
// AllowOriginFunc is a custom function to validate the origin. It take the origin
// as argument and returns true if allowed or false otherwise. If this option is
// set, the content of AllowedOrigins is ignored.
AllowOriginFunc func(origin string) bool
// AllowedMethods is a list of methods the client is allowed to use with
// cross-domain requests. Default value is simple methods (GET and POST)
2015-11-12 15:14:38 +01:00
AllowMethods []string
2015-11-11 22:16:49 +01:00
// AllowedHeaders is list of non simple headers the client is allowed to use with
// cross-domain requests.
2015-11-12 15:14:38 +01:00
AllowHeaders []string
2015-11-11 22:16:49 +01:00
// AllowCredentials indicates whether the request can include user credentials like
// cookies, HTTP authentication or client side SSL certificates.
AllowCredentials bool
2015-11-12 15:14:38 +01:00
// ExposedHeaders indicates which headers are safe to expose to the API of a CORS
// API specification
ExposeHeaders []string
2015-11-11 22:16:49 +01:00
// MaxAge indicates how long (in seconds) the results of a preflight request
// can be cached
MaxAge time.Duration
}
// AddAllowMethods is allowed to add custom methods
2015-11-12 15:14:38 +01:00
func (c *Config) AddAllowMethods(methods ...string) {
c.AllowMethods = append(c.AllowMethods, methods...)
2015-11-11 22:16:49 +01:00
}
// AddAllowHeaders is allowed to add custom headers
2015-11-12 15:14:38 +01:00
func (c *Config) AddAllowHeaders(headers ...string) {
c.AllowHeaders = append(c.AllowHeaders, headers...)
2015-11-11 22:16:49 +01:00
}
// AddExposeHeaders is allowed to add custom expose headers
2015-11-12 15:14:38 +01:00
func (c *Config) AddExposeHeaders(headers ...string) {
c.ExposeHeaders = append(c.ExposeHeaders, headers...)
2015-11-11 22:16:49 +01:00
}
// Validate is check configuration of user defined.
2015-11-11 22:16:49 +01:00
func (c Config) Validate() error {
2015-11-12 15:14:38 +01:00
if c.AllowAllOrigins && (c.AllowOriginFunc != nil || len(c.AllowOrigins) > 0) {
2015-11-11 22:16:49 +01:00
return errors.New("conflict settings: all origins are allowed. AllowOriginFunc or AllowedOrigins is not needed")
}
2015-11-12 15:14:38 +01:00
if !c.AllowAllOrigins && c.AllowOriginFunc == nil && len(c.AllowOrigins) == 0 {
2015-11-11 22:16:49 +01:00
return errors.New("conflict settings: all origins disabled")
}
2015-11-12 15:14:38 +01:00
for _, origin := range c.AllowOrigins {
if origin != "*" && !strings.HasPrefix(origin, "http://") && !strings.HasPrefix(origin, "https://") {
return errors.New("bad origin: origins must either be '*' or include http:// or https://")
2015-11-11 22:16:49 +01:00
}
}
return nil
}
// DefaultConfig returns a generic default configuration mapped to localhost.
2015-11-11 22:16:49 +01:00
func DefaultConfig() Config {
return Config{
2015-11-12 15:14:38 +01:00
AllowMethods: []string{"GET", "POST", "PUT", "HEAD"},
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type"},
2015-11-11 22:16:49 +01:00
AllowCredentials: false,
MaxAge: 12 * time.Hour,
}
}
// Default returns the location middleware with default configuration.
2015-11-11 22:16:49 +01:00
func Default() gin.HandlerFunc {
2015-11-12 15:14:38 +01:00
config := DefaultConfig()
config.AllowAllOrigins = true
return New(config)
2015-11-11 22:16:49 +01:00
}
// New returns the location middleware with user-defined custom configuration.
2015-11-11 22:16:49 +01:00
func New(config Config) gin.HandlerFunc {
2015-11-12 01:17:15 +01:00
cors := newCors(config)
2015-11-11 22:16:49 +01:00
return func(c *gin.Context) {
2015-11-12 01:17:15 +01:00
cors.applyCors(c)
2015-11-11 22:16:49 +01:00
}
}