-
Notifications
You must be signed in to change notification settings - Fork 5
/
cors_options.go
63 lines (54 loc) · 1.52 KB
/
cors_options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package azugo
import "strings"
var (
defaultAllowedMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}
defaultCORSOptions = CORSOptions{
allowedMethods: strings.Join(defaultAllowedMethods, ", "),
}
)
// CORSOptions is options for CORS middleware.
type CORSOptions struct {
allowedMethods string
allowedHeaders string
allowAllOrigins bool
allowedOrigins map[string]struct{}
}
// SetMethods sets allowed CORS methods.
func (c *CORSOptions) SetMethods(methods ...string) *CORSOptions {
c.allowedMethods = strings.Join(methods, ", ")
return c
}
// Methods returns allowed CORS methods.
func (c *CORSOptions) Methods() string {
return c.allowedMethods
}
// SetHeaders sets allowed CORS methods.
func (c *CORSOptions) SetHeaders(headers ...string) *CORSOptions {
c.allowedHeaders = strings.Join(headers, ", ")
return c
}
// Headers returns allowed CORS headers.
func (c *CORSOptions) Headers() string {
return c.allowedHeaders
}
// SetOrigins sets allowed CORS origins. Set to `*` to allow all origins.
func (c *CORSOptions) SetOrigins(origins ...string) *CORSOptions {
c.allowedOrigins = make(map[string]struct{}, len(origins))
c.allowAllOrigins = false
for _, origin := range origins {
if origin == "*" {
c.allowAllOrigins = true
break
}
c.allowedOrigins[origin] = struct{}{}
}
return c
}
// ValidOrigins returns true if CORS origin is allowed.
func (c *CORSOptions) ValidOrigin(origin string) bool {
if c.allowAllOrigins {
return true
}
_, ok := c.allowedOrigins[origin]
return ok
}