/
cors.go
150 lines (135 loc) · 3.57 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
package middleware
import (
"net/http"
"strings"
"time"
"github.com/honmaple/forest"
)
type (
CorsConfig struct {
Skipper Skipper
AllowOrigins []string
AllowMethods []string
AllowHeaders []string
AllowCredentials bool
AllowOriginFunc func(*http.Request, string) bool
ExposeHeaders []string
MaxAge time.Duration
}
)
var (
DefaultCorsConfig = CorsConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
AllowHeaders: []string{"*"},
}
)
// *.example.com match a.example.com b.example.com
func matchSubdomain(domain, origin string) bool {
domainScheme := ""
if index := strings.Index(domain, "://"); index > -1 {
domainScheme = domain[:index]
domain = domain[index+3:]
}
originScheme := ""
if index := strings.Index(origin, "://"); index > -1 {
originScheme = origin[:index]
origin = origin[index+3:]
}
if domainScheme != originScheme {
return false
}
domains := strings.Split(domain, ".")
origins := strings.Split(origin, ".")
if len(domains) != len(origins) {
return false
}
for i, s := range domains {
if s != "*" && origins[i] != s {
return false
}
}
return true
}
func Cors() forest.HandlerFunc {
return CorsWithConfig(DefaultCorsConfig)
}
func CorsWithConfig(config CorsConfig) forest.HandlerFunc {
normalHeaders := make(map[string]string)
preflightHeaders := make(map[string]string)
if len(config.ExposeHeaders) > 0 {
normalHeaders["Access-Control-Expose-Headers"] = strings.Join(config.ExposeHeaders, ",")
}
if config.AllowCredentials {
normalHeaders["Access-Control-Allow-Credentials"] = "true"
preflightHeaders["Access-Control-Allow-Credentials"] = "true"
}
if len(config.AllowHeaders) > 0 {
preflightHeaders["Access-Control-Allow-Headers"] = strings.Join(config.AllowHeaders, ",")
}
if len(config.AllowMethods) > 0 {
preflightHeaders["Access-Control-Allow-Methods"] = strings.Join(config.AllowMethods, ",")
}
if config.MaxAge > 0 {
preflightHeaders["Access-Control-Max-Age"] = config.MaxAge.String()
}
return func(c forest.Context) error {
if config.Skipper != nil && config.Skipper(c) {
return c.Next()
}
r := c.Request()
w := c.Response()
origin := r.Header.Get("Origin")
allowedOrigin := ""
if origin != "" {
if matchSubdomain(r.Host, origin) {
return c.Next()
}
if config.AllowOriginFunc != nil && config.AllowOriginFunc(r, origin) {
allowedOrigin = origin
} else {
for _, o := range config.AllowOrigins {
if o == "*" && config.AllowCredentials {
allowedOrigin = origin
break
}
if o == "*" || o == origin {
allowedOrigin = o
break
}
if matchSubdomain(o, origin) {
allowedOrigin = origin
break
}
}
}
}
preflight := r.Method == http.MethodOptions
if origin == "" || allowedOrigin == "" {
if !preflight {
return c.Next()
}
return c.Status(http.StatusNoContent)
}
header := w.Header()
header.Add("Vary", "Origin")
header.Set("Access-Control-Allow-Origin", allowedOrigin)
if !preflight {
for k, v := range normalHeaders {
header.Set(k, v)
}
return c.Next()
}
header.Add("Vary", "Access-Control-Request-Method")
header.Add("Vary", "Access-Control-Request-Headers")
for k, v := range preflightHeaders {
header.Set(k, v)
}
if len(config.AllowHeaders) == 0 {
if h := header.Get("Access-Control-Request-Headers"); h != "" {
header.Set("Access-Control-Allow-Headers", h)
}
}
return c.Status(http.StatusNoContent)
}
}