http urls monitor.

cors.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // Allows usage of popular browser extensions schemas
  35. AllowBrowserExtensions bool
  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. func (c Config) getAllowedSchemas() []string {
  50. allowedSchemas := DefaultSchemas
  51. if c.AllowBrowserExtensions {
  52. allowedSchemas = append(allowedSchemas, ExtensionSchemas...)
  53. }
  54. return allowedSchemas
  55. }
  56. func (c Config) validateAllowedSchemas(origin string) bool {
  57. allowedSchemas := c.getAllowedSchemas()
  58. for _, schema := range allowedSchemas {
  59. if strings.HasPrefix(origin, schema) {
  60. return true
  61. }
  62. }
  63. return false
  64. }
  65. // Validate is check configuration of user defined.
  66. func (c Config) Validate() error {
  67. if c.AllowAllOrigins && (c.AllowOriginFunc != nil || len(c.AllowOrigins) > 0) {
  68. return errors.New("conflict settings: all origins are allowed. AllowOriginFunc or AllowedOrigins is not needed")
  69. }
  70. if !c.AllowAllOrigins && c.AllowOriginFunc == nil && len(c.AllowOrigins) == 0 {
  71. return errors.New("conflict settings: all origins disabled")
  72. }
  73. for _, origin := range c.AllowOrigins {
  74. if origin != "*" && !c.validateAllowedSchemas(origin) {
  75. return errors.New("bad origin: origins must either be '*' or include " + strings.Join(c.getAllowedSchemas(), ","))
  76. }
  77. }
  78. return nil
  79. }
  80. // DefaultConfig returns a generic default configuration mapped to localhost.
  81. func DefaultConfig() Config {
  82. return Config{
  83. AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"},
  84. AllowHeaders: []string{"Origin", "Content-Length", "Content-Type"},
  85. AllowCredentials: false,
  86. MaxAge: 12 * time.Hour,
  87. }
  88. }
  89. // Default returns the location middleware with default configuration.
  90. func Default() gin.HandlerFunc {
  91. config := DefaultConfig()
  92. config.AllowAllOrigins = true
  93. return New(config)
  94. }
  95. // New returns the location middleware with user-defined custom configuration.
  96. func New(config Config) gin.HandlerFunc {
  97. cors := newCors(config)
  98. return func(c *gin.Context) {
  99. cors.applyCors(c)
  100. }
  101. }