cors.go 2.8 KB

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