15
15
Skipper Skipper
16
16
17
17
// AllowOrigin defines a list of origins that may access the resource.
18
- // Optional. If request header `Origin` is set, value is []string{"<Origin>"}
19
- // else []string{"*"}.
18
+ // Optional. Default value []string{"*"}.
20
19
AllowOrigins []string `json:"allow_origins"`
21
20
22
21
// AllowMethods defines a list methods allowed when accessing the resource.
52
51
// DefaultCORSConfig is the default CORS middleware config.
53
52
DefaultCORSConfig = CORSConfig {
54
53
Skipper : defaultSkipper ,
54
+ AllowOrigins : []string {"*" },
55
55
AllowMethods : []string {echo .GET , echo .HEAD , echo .PUT , echo .PATCH , echo .POST , echo .DELETE },
56
56
}
57
57
)
@@ -69,11 +69,13 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
69
69
if config .Skipper == nil {
70
70
config .Skipper = DefaultCORSConfig .Skipper
71
71
}
72
+ if len (config .AllowOrigins ) == 0 {
73
+ config .AllowOrigins = DefaultCORSConfig .AllowOrigins
74
+ }
72
75
if len (config .AllowMethods ) == 0 {
73
76
config .AllowMethods = DefaultCORSConfig .AllowMethods
74
77
}
75
78
76
- allowedOrigins := strings .Join (config .AllowOrigins , "," )
77
79
allowMethods := strings .Join (config .AllowMethods , "," )
78
80
allowHeaders := strings .Join (config .AllowHeaders , "," )
79
81
exposeHeaders := strings .Join (config .ExposeHeaders , "," )
@@ -88,21 +90,20 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
88
90
req := c .Request ()
89
91
res := c .Response ()
90
92
origin := req .Header .Get (echo .HeaderOrigin )
93
+ allowOrigin := ""
91
94
92
- if allowedOrigins == "" {
93
- if origin != "" {
94
- allowedOrigins = origin
95
- } else {
96
- if ! config .AllowCredentials {
97
- allowedOrigins = "*"
98
- }
95
+ // Check allowed origins
96
+ for _ , o := range config .AllowOrigins {
97
+ if o == "*" || o == origin {
98
+ allowOrigin = o
99
+ break
99
100
}
100
101
}
101
102
102
103
// Simple request
103
104
if req .Method != echo .OPTIONS {
104
105
res .Header ().Add (echo .HeaderVary , echo .HeaderOrigin )
105
- res .Header ().Set (echo .HeaderAccessControlAllowOrigin , allowedOrigins )
106
+ res .Header ().Set (echo .HeaderAccessControlAllowOrigin , allowOrigin )
106
107
if config .AllowCredentials {
107
108
res .Header ().Set (echo .HeaderAccessControlAllowCredentials , "true" )
108
109
}
@@ -116,7 +117,7 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
116
117
res .Header ().Add (echo .HeaderVary , echo .HeaderOrigin )
117
118
res .Header ().Add (echo .HeaderVary , echo .HeaderAccessControlRequestMethod )
118
119
res .Header ().Add (echo .HeaderVary , echo .HeaderAccessControlRequestHeaders )
119
- res .Header ().Set (echo .HeaderAccessControlAllowOrigin , allowedOrigins )
120
+ res .Header ().Set (echo .HeaderAccessControlAllowOrigin , allowOrigin )
120
121
res .Header ().Set (echo .HeaderAccessControlAllowMethods , allowMethods )
121
122
if config .AllowCredentials {
122
123
res .Header ().Set (echo .HeaderAccessControlAllowCredentials , "true" )
0 commit comments