cors.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package cors
  2. import (
  3. "errors"
  4. "strings"
  5. "time"
  6. "github.com/gin-gonic/gin"
  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. AllowHeaders []string
  25. // AllowCredentials indicates whether the request can include user credentials like
  26. // cookies, HTTP authentication or client side SSL certificates.
  27. AllowCredentials bool
  28. // ExposedHeaders indicates which headers are safe to expose to the API of a CORS
  29. // API specification
  30. ExposeHeaders []string
  31. // MaxAge indicates how long (in seconds) the results of a preflight request
  32. // can be cached
  33. MaxAge time.Duration
  34. }
  35. // AddAllowMethods is allowed to add custom methods
  36. func (c *Config) AddAllowMethods(methods ...string) {
  37. c.AllowMethods = append(c.AllowMethods, methods...)
  38. }
  39. // AddAllowHeaders is allowed to add custom headers
  40. func (c *Config) AddAllowHeaders(headers ...string) {
  41. c.AllowHeaders = append(c.AllowHeaders, headers...)
  42. }
  43. // AddExposeHeaders is allowed to add custom expose headers
  44. func (c *Config) AddExposeHeaders(headers ...string) {
  45. c.ExposeHeaders = append(c.ExposeHeaders, headers...)
  46. }
  47. // Validate is check configuration of user defined.
  48. func (c Config) Validate() error {
  49. if c.AllowAllOrigins && (c.AllowOriginFunc != nil || len(c.AllowOrigins) > 0) {
  50. return errors.New("conflict settings: all origins are allowed. AllowOriginFunc or AllowedOrigins is not needed")
  51. }
  52. if !c.AllowAllOrigins && c.AllowOriginFunc == nil && len(c.AllowOrigins) == 0 {
  53. return errors.New("conflict settings: all origins disabled")
  54. }
  55. for _, origin := range c.AllowOrigins {
  56. if origin != "*" && !strings.HasPrefix(origin, "http://") && !strings.HasPrefix(origin, "https://") {
  57. return errors.New("bad origin: origins must either be '*' or include http:// or https://")
  58. }
  59. }
  60. return nil
  61. }
  62. // DefaultConfig returns a generic default configuration mapped to localhost.
  63. func DefaultConfig() Config {
  64. return Config{
  65. AllowMethods: []string{"GET", "POST", "PUT", "HEAD"},
  66. AllowHeaders: []string{"Origin", "Content-Length", "Content-Type"},
  67. AllowCredentials: false,
  68. MaxAge: 12 * time.Hour,
  69. }
  70. }
  71. // Default returns the location middleware with default configuration.
  72. func Default() gin.HandlerFunc {
  73. config := DefaultConfig()
  74. config.AllowAllOrigins = true
  75. return New(config)
  76. }
  77. // New returns the location middleware with user-defined custom configuration.
  78. func New(config Config) gin.HandlerFunc {
  79. cors := newCors(config)
  80. return func(c *gin.Context) {
  81. cors.applyCors(c)
  82. }
  83. }