-
Notifications
You must be signed in to change notification settings - Fork 19
/
cors.go
117 lines (101 loc) · 2.83 KB
/
cors.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package cors
import (
"fmt"
"net/http"
"regexp"
)
const (
allowOriginKey string = "Access-Control-Allow-Origin"
allowCredentialsKey = "Access-Control-Allow-Credentials"
allowHeadersKey = "Access-Control-Allow-Headers"
allowMethodsKey = "Access-Control-Allow-Methods"
maxAgeKey = "Access-Control-Max-Age"
originKey = "Origin"
varyKey = "Vary"
requestMethodKey = "Access-Control-Request-Method"
requestHeadersKey = "Access-Control-Request-Headers"
exposeHeadersKey = "Access-Control-Expose-Headers"
options = "OPTIONS"
)
type Config struct {
AllowedOrigins []string
OriginRegexps []*regexp.Regexp
AllowedMethods string
AllowedHeaders string
ExposedHeaders string
AllowCredentials *bool
MaxAge int
}
func Default() *Config {
return &Config{
AllowedOrigins: []string{"*"},
OriginRegexps: []*regexp.Regexp{},
AllowedMethods: "POST, GET, OPTIONS, PUT, DELETE",
AllowedHeaders: "",
ExposedHeaders: "",
MaxAge: 0,
AllowCredentials: nil,
}
}
// Read the request, setting response headers as appropriate.
// Will NOT write anything to response in any circumstances.
func (c *Config) HandleRequest(w http.ResponseWriter, r *http.Request) {
requestOrigin := r.Header.Get(originKey)
if requestOrigin == "" {
return
}
//check origin against allowed origins
for _, ao := range c.AllowedOrigins {
if ao == "*" || ao == requestOrigin {
responseOrigin := "*"
if ao != "*" {
responseOrigin = requestOrigin
}
addAllowOriginHeader(w, responseOrigin)
break
}
}
if w.Header().Get(allowOriginKey) == "" {
if c.anyOriginRegexpMatch(requestOrigin) {
addAllowOriginHeader(w, requestOrigin)
} else {
return //if we didn't set a valid allow-origin, none of the other headers matter
}
}
if IsPreflight(r) {
w.Header().Set(allowMethodsKey, c.AllowedMethods)
if c.AllowedHeaders != "" {
if c.AllowedHeaders != "*" {
w.Header().Set(allowHeadersKey, c.AllowedHeaders)
} else {
w.Header().Set(allowHeadersKey, r.Header.Get(requestHeadersKey))
}
}
if c.MaxAge > 0 {
w.Header().Set(maxAgeKey, fmt.Sprint(c.MaxAge))
}
} else {
//regular request
if c.ExposedHeaders != "" {
w.Header().Set(exposeHeadersKey, c.ExposedHeaders)
}
}
if c.AllowCredentials != nil {
w.Header().Set(allowCredentialsKey, fmt.Sprint(*c.AllowCredentials))
}
}
func IsPreflight(r *http.Request) bool {
return r.Method == options && r.Header.Get(requestMethodKey) != ""
}
func addAllowOriginHeader(w http.ResponseWriter, allowedOrigin string) {
w.Header().Set(allowOriginKey, allowedOrigin)
w.Header().Add(varyKey, originKey)
}
func (c *Config) anyOriginRegexpMatch(origin string) bool {
for _, r := range c.OriginRegexps {
if r.MatchString(origin) {
return true
}
}
return false
}