Skip to content

Commit 3a53db5

Browse files
committed
add option to allow all methods
1 parent 64821dd commit 3a53db5

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

cors.go

+13
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type Options struct {
4747
AllowOriginRequestFunc func(r *http.Request, origin string) bool
4848
// AllowedMethods is a list of methods the client is allowed to use with
4949
// cross-domain requests. Default value is simple methods (HEAD, GET and POST).
50+
// If the special "*" value is present in the list, all methods will be allowed.
5051
AllowedMethods []string
5152
// AllowedHeaders is list of non simple headers the client is allowed to use with
5253
// cross-domain requests.
@@ -97,6 +98,8 @@ type Cors struct {
9798
allowedOriginsAll bool
9899
// Set to true when allowed headers contains a "*"
99100
allowedHeadersAll bool
101+
// Set to true when allowed methods contains a "*"
102+
allowedMethodsAll bool
100103
allowCredentials bool
101104
optionPassthrough bool
102105
}
@@ -169,6 +172,13 @@ func New(options Options) *Cors {
169172
c.allowedMethods = []string{http.MethodGet, http.MethodPost, http.MethodHead}
170173
} else {
171174
c.allowedMethods = convert(options.AllowedMethods, strings.ToUpper)
175+
for _, h := range options.AllowedMethods {
176+
if h == "*" {
177+
c.allowedMethodsAll = true
178+
c.allowedMethods = nil
179+
break
180+
}
181+
}
172182
}
173183

174184
return c
@@ -392,6 +402,9 @@ func (c *Cors) isOriginAllowed(r *http.Request, origin string) bool {
392402
// isMethodAllowed checks if a given method can be used as part of a cross-domain request
393403
// on the endpoint
394404
func (c *Cors) isMethodAllowed(method string) bool {
405+
if c.allowedMethodsAll {
406+
return true
407+
}
395408
if len(c.allowedMethods) == 0 {
396409
// If no method allowed, always return false, even for preflight request
397410
return false

0 commit comments

Comments
 (0)