config.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package cors
  2. import (
  3. "net/http"
  4. "gopkg.in/gin-gonic/gin.v1"
  5. )
  6. type cors struct {
  7. allowAllOrigins bool
  8. allowOriginFunc func(string) bool
  9. allowOrigins []string
  10. exposeHeaders []string
  11. normalHeaders http.Header
  12. preflightHeaders http.Header
  13. }
  14. func newCors(config Config) *cors {
  15. if err := config.Validate(); err != nil {
  16. panic(err.Error())
  17. }
  18. return &cors{
  19. allowOriginFunc: config.AllowOriginFunc,
  20. allowAllOrigins: config.AllowAllOrigins,
  21. allowOrigins: normalize(config.AllowOrigins),
  22. normalHeaders: generateNormalHeaders(config),
  23. preflightHeaders: generatePreflightHeaders(config),
  24. }
  25. }
  26. func (cors *cors) applyCors(c *gin.Context) {
  27. origin := c.Request.Header.Get("Origin")
  28. if len(origin) == 0 {
  29. // request is not a CORS request
  30. return
  31. }
  32. if !cors.validateOrigin(origin) {
  33. c.AbortWithStatus(http.StatusForbidden)
  34. return
  35. }
  36. if c.Request.Method == "OPTIONS" {
  37. cors.handlePreflight(c)
  38. defer c.AbortWithStatus(200)
  39. } else {
  40. cors.handleNormal(c)
  41. }
  42. if !cors.allowAllOrigins {
  43. c.Header("Access-Control-Allow-Origin", origin)
  44. }
  45. }
  46. func (cors *cors) validateOrigin(origin string) bool {
  47. if cors.allowAllOrigins {
  48. return true
  49. }
  50. for _, value := range cors.allowOrigins {
  51. if value == origin {
  52. return true
  53. }
  54. }
  55. if cors.allowOriginFunc != nil {
  56. return cors.allowOriginFunc(origin)
  57. }
  58. return false
  59. }
  60. func (cors *cors) handlePreflight(c *gin.Context) {
  61. header := c.Writer.Header()
  62. for key, value := range cors.preflightHeaders {
  63. header[key] = value
  64. }
  65. }
  66. func (cors *cors) handleNormal(c *gin.Context) {
  67. header := c.Writer.Header()
  68. for key, value := range cors.normalHeaders {
  69. header[key] = value
  70. }
  71. }