-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcors.go
57 lines (53 loc) · 1.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
package gw
import (
"net/http"
"net/url"
"strings"
)
type CORSOptions struct {
Origins []string `json:"origins"`
}
func AllowedOriginMethod(opts *CORSOptions, origin, method string) bool {
o, err := url.Parse(origin)
if err != nil {
return false
}
for _, opt := range opts.Origins {
if o.Host == opt || strings.HasSuffix(o.Host, "."+opt) {
if method == "GET" || method == "POST" {
return true
}
}
}
return false
}
func CORS(fn AppHandler) AppHandler {
return func(w http.ResponseWriter, r *http.Request) *AppError {
env := r.Context().Value("env").(Settings)
co := env.GetCORSOptions()
origin := r.Header.Get("Origin")
method := r.Header.Get("Access-Control-Request-Method")
if r.Method == "OPTIONS" {
if origin != "" {
if AllowedOriginMethod(co, origin, method) {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Methods", "GET, POST")
w.Header().Set("Access-Control-Allow-Headers", "origin, content-type, content-length, accept, accept-encoding, accept-language, referer, user-agent")
w.Header().Set("Allow", "GET, POST")
return nil
}
// Not in allowed origins, ok but no additional headers
return nil
}
// Empty preflight, ok but no additional headers
return nil
} else {
if AllowedOriginMethod(co, origin, r.Method) {
w.Header().Set("Access-Control-Allow-Origin", origin)
return fn(w, r)
} else {
return fn(w, r)
}
}
}
}