-
Notifications
You must be signed in to change notification settings - Fork 7
/
cors.go
171 lines (141 loc) · 3.45 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// SPDX-License-Identifier: MIT
package options
import (
"errors"
"net/http"
"strconv"
"strings"
"github.com/issue9/mux/v5/internal/tree"
)
type CORS struct {
Origins []string
anyOrigins bool
deny bool
AllowHeaders []string
allowHeadersString string
anyHeaders bool
ExposedHeaders []string
exposedHeadersString string
MaxAge int
maxAgeString string
AllowCredentials bool
}
func AllowedCORS() *CORS {
return &CORS{
Origins: []string{"*"},
AllowHeaders: []string{"*"},
MaxAge: 3600,
}
}
func (o *Options) HandleCORS(node *tree.Node, w http.ResponseWriter, r *http.Request) {
o.CORS.handle(node, w, r)
}
func (c *CORS) sanitize() error {
for _, o := range c.Origins {
if o == "*" {
c.anyOrigins = true
break
}
}
c.deny = len(c.Origins) == 0
for _, h := range c.AllowHeaders {
if h == "*" {
c.allowHeadersString = "*"
c.anyHeaders = true
break
}
}
if c.allowHeadersString == "" && len(c.AllowHeaders) > 0 {
c.allowHeadersString = strings.Join(c.AllowHeaders, ",")
}
if len(c.ExposedHeaders) > 0 {
c.exposedHeadersString = strings.Join(c.ExposedHeaders, ",")
}
switch {
case c.MaxAge == 0:
case c.MaxAge >= -1:
c.maxAgeString = strconv.Itoa(c.MaxAge)
default:
return errors.New("maxAge 的值只能是 >= -1")
}
if c.anyOrigins && c.AllowCredentials {
return errors.New("origin=* 和 allowCredentials=true 不能同时成立")
}
return nil
}
func (c *CORS) handle(node *tree.Node, w http.ResponseWriter, r *http.Request) {
if c.deny {
return
}
// Origin 是可以为空的,所以采用 Access-Control-Request-Method 判断是否为预检。
reqMethod := r.Header.Get("Access-Control-Request-Method")
preflight := r.Method == http.MethodOptions &&
reqMethod != "" &&
r.URL.Path != "*" // OPTIONS * 不算预检,也不存在其它的请求方法处理方式。
wh := w.Header()
if preflight {
// Access-Control-Allow-Methods
methods := node.Methods()
if !inStrings(methods, reqMethod) {
return
}
wh.Set("Access-Control-Allow-Methods", node.Options())
wh.Add("Vary", "Access-Control-Request-Method")
// Access-Control-Allow-Headers
if !c.headerIsAllowed(r) {
return
}
if c.allowHeadersString != "" {
wh.Set("Access-Control-Allow-Headers", c.allowHeadersString)
wh.Add("Vary", "Access-Control-Request-Headers")
}
// Access-Control-Max-Age
if c.maxAgeString != "" {
wh.Set("Access-Control-Max-Age", c.maxAgeString)
}
}
// Access-Control-Allow-Origin
allowOrigin := "*"
if !c.anyOrigins {
origin := r.Header.Get("Origin")
if !inStrings(c.Origins, origin) {
return
}
allowOrigin = origin
}
wh.Set("Access-Control-Allow-Origin", allowOrigin)
wh.Add("Vary", "Origin")
// Access-Control-Allow-Credentials
if c.AllowCredentials {
wh.Set("Access-Control-Allow-Credentials", "true")
}
// Access-Control-Expose-Headers
if c.exposedHeadersString != "" {
wh.Set("Access-Control-Expose-Headers", c.exposedHeadersString)
}
}
func (c *CORS) headerIsAllowed(r *http.Request) bool {
if c.anyHeaders {
return true
}
h := strings.TrimSpace(r.Header.Get("Access-Control-Request-Headers"))
if h == "" {
return true
}
headers := strings.Split(h, ",")
for _, v := range headers {
v = strings.TrimSpace(v)
if !inStrings(c.AllowHeaders, v) {
return false
}
}
return true
}
func inStrings(strs []string, s string) bool {
for _, str := range strs {
if str == s {
return true
}
}
return false
}