cors.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package cors
  2. import (
  3. "errors"
  4. "strings"
  5. "time"
  6. "gopkg.in/gin-gonic/gin.v1"
  7. )
  8. // Config represents all available options for the middleware.
  9. type Config struct {
  10. AllowAllOrigins bool
  11. // AllowedOrigins is a list of origins a cross-domain request can be executed from.
  12. // If the special "*" value is present in the list, all origins will be allowed.
  13. // Default value is ["*"]
  14. AllowOrigins []string
  15. // AllowOriginFunc is a custom function to validate the origin. It take the origin
  16. // as argument and returns true if allowed or false otherwise. If this option is
  17. // set, the content of AllowedOrigins is ignored.
  18. AllowOriginFunc func(origin string) bool
  19. // AllowedMethods is a list of methods the client is allowed to use with
  20. // cross-domain requests. Default value is simple methods (GET and POST)
  21. AllowMethods []string
  22. // AllowedHeaders is list of non simple headers the client is allowed to use with
  23. // cross-domain requests.
  24. // If the special "*" value is present in the list, all headers will be allowed.
  25. // Default value is [] but "Origin" is always appended to the list.
  26. AllowHeaders []string
  27. // AllowCredentials indicates whether the request can include user credentials like
  28. // cookies, HTTP authentication or client side SSL certificates.
  29. AllowCredentials bool
  30. // ExposedHeaders indicates which headers are safe to expose to the API of a CORS
  31. // API specification
  32. ExposeHeaders []string
  33. // MaxAge indicates how long (in seconds) the results of a preflight request
  34. // can be cached
  35. MaxAge time.Duration
  36. }
  37. // AddAllowMethods is allowed to add custom methods
  38. func (c *Config) AddAllowMethods(methods ...string) {
  39. c.AllowMethods = append(c.AllowMethods, methods...)
  40. }
  41. // AddAllowHeaders is allowed to add custom headers
  42. func (c *Config) AddAllowHeaders(headers ...string) {
  43. c.AllowHeaders = append(c.AllowHeaders, headers...)
  44. }
  45. // AddExposeHeaders is allowed to add custom expose headers
  46. func (c *Config) AddExposeHeaders(headers ...string) {
  47. c.ExposeHeaders = append(c.ExposeHeaders, headers...)
  48. }
  49. // Validate is check configuration of user defined.
  50. func (c Config) Validate() error {
  51. if c.AllowAllOrigins && (c.AllowOriginFunc != nil || len(c.AllowOrigins) > 0) {
  52. return errors.New("conflict settings: all origins are allowed. AllowOriginFunc or AllowedOrigins is not needed")
  53. }
  54. if !c.AllowAllOrigins && c.AllowOriginFunc == nil && len(c.AllowOrigins) == 0 {
  55. return errors.New("conflict settings: all origins disabled")
  56. }
  57. for _, origin := range c.AllowOrigins {
  58. if !strings.HasPrefix(origin, "http://") && !strings.HasPrefix(origin, "https://") {
  59. return errors.New("bad origin: origins must include http:// or https://")
  60. }
  61. }
  62. return nil
  63. }
  64. // DefaultConfig returns a generic default configuration mapped to localhost.
  65. func DefaultConfig() Config {
  66. return Config{
  67. AllowMethods: []string{"GET", "POST", "PUT", "HEAD"},
  68. AllowHeaders: []string{"Origin", "Content-Length", "Content-Type"},
  69. AllowCredentials: false,
  70. MaxAge: 12 * time.Hour,
  71. }
  72. }
  73. // Default returns the location middleware with default configuration.
  74. func Default() gin.HandlerFunc {
  75. config := DefaultConfig()
  76. config.AllowAllOrigins = true
  77. return New(config)
  78. }
  79. // New returns the location middleware with user-defined custom configuration.
  80. func New(config Config) gin.HandlerFunc {
  81. cors := newCors(config)
  82. return func(c *gin.Context) {
  83. cors.applyCors(c)
  84. }
  85. }