@@ -107,13 +107,23 @@ type CORSConfig struct {
107
107
//
108
108
// See also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Max-Age
109
109
MaxAge int `yaml:"max_age"`
110
+
111
+ // PreflightStatusCode determines the status code to be returned on a
112
+ // successful preflight request.
113
+ //
114
+ // Optional. Default value is http.StatusNoContent(204)
115
+ //
116
+ // See also: https://fetch.spec.whatwg.org/#ref-for-ok-status
117
+ // See also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods/OPTIONS#preflighted_requests_in_cors
118
+ PreflightStatusCode int `yaml:"preflight_status_code"`
110
119
}
111
120
112
121
// DefaultCORSConfig is the default CORS middleware config.
113
122
var DefaultCORSConfig = CORSConfig {
114
- Skipper : DefaultSkipper ,
115
- AllowOrigins : []string {"*" },
116
- AllowMethods : []string {http .MethodGet , http .MethodHead , http .MethodPut , http .MethodPatch , http .MethodPost , http .MethodDelete },
123
+ Skipper : DefaultSkipper ,
124
+ AllowOrigins : []string {"*" },
125
+ AllowMethods : []string {http .MethodGet , http .MethodHead , http .MethodPut , http .MethodPatch , http .MethodPost , http .MethodDelete },
126
+ PreflightStatusCode : http .StatusNoContent ,
117
127
}
118
128
119
129
// CORS returns a Cross-Origin Resource Sharing (CORS) middleware.
@@ -147,6 +157,10 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
147
157
config .AllowMethods = DefaultCORSConfig .AllowMethods
148
158
}
149
159
160
+ if config .PreflightStatusCode == 0 {
161
+ config .PreflightStatusCode = DefaultCORSConfig .PreflightStatusCode
162
+ }
163
+
150
164
allowOriginPatterns := make ([]* regexp.Regexp , 0 , len (config .AllowOrigins ))
151
165
for _ , origin := range config .AllowOrigins {
152
166
if origin == "*" {
@@ -214,7 +228,7 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
214
228
if ! preflight {
215
229
return next (c )
216
230
}
217
- return c .NoContent (http . StatusNoContent )
231
+ return c .NoContent (config . PreflightStatusCode )
218
232
}
219
233
220
234
if config .AllowOriginFunc != nil {
@@ -264,7 +278,7 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
264
278
if ! preflight {
265
279
return echo .ErrUnauthorized
266
280
}
267
- return c .NoContent (http . StatusNoContent )
281
+ return c .NoContent (config . PreflightStatusCode )
268
282
}
269
283
270
284
res .Header ().Set (echo .HeaderAccessControlAllowOrigin , allowOrigin )
@@ -301,7 +315,7 @@ func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
301
315
if config .MaxAge != 0 {
302
316
res .Header ().Set (echo .HeaderAccessControlMaxAge , maxAge )
303
317
}
304
- return c .NoContent (http . StatusNoContent )
318
+ return c .NoContent (config . PreflightStatusCode )
305
319
}
306
320
}
307
321
}
0 commit comments