-
Notifications
You must be signed in to change notification settings - Fork 0
/
cors.go
51 lines (42 loc) · 1.58 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
package handler
import (
"net/http"
"regexp"
)
var allowedCorsOrigins = [...]*regexp.Regexp{
regexp.MustCompile(`^https?://(?:[-\w\.]+\.)?hailoweb.com(:\d+)?$`),
regexp.MustCompile(`^https?://(?:[-\w\.]+\.)?hailoapp.com(:\d+)?$`),
regexp.MustCompile(`^https?://(?:[-\w\.]+\.)?HailoOSS.com(:\d+)?$`),
regexp.MustCompile(`^https?://(?:[-\w\.]+\.)?hailovpn.com(:\d+)?$`),
regexp.MustCompile(`^https?://(?:[-\w\.]+\.)?elasticride.com(:\d+)?$`),
regexp.MustCompile(`^https?://(?:[-\w\.]+\.)?elasticride.local(:\d+)?$`),
regexp.MustCompile(`^https?://(?:[-\w\.]+\.)?elasticride.dev(:\d+)?$`),
regexp.MustCompile(`^https?://(?:[-\w\.]+\.)?hailopay.com(:\d+)?$`),
}
type CORSHandler struct {
Handler http.Handler
}
func (h *CORSHandler) isAllowedCORSOrigin(origin string) bool {
for _, re := range allowedCorsOrigins {
if re.MatchString(origin) {
return true
}
}
return false
}
func (h *CORSHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" && h.isAllowedCORSOrigin(origin) {
rw.Header().Set("Access-Control-Allow-Origin", origin)
rw.Header().Set("Access-Control-Allow-Methods", "DELETE, GET, HEAD, OPTIONS, POST, PUT")
// Allow all headers a client wants to send
if wantedHeaders := r.Header.Get("Access-Control-Request-Headers"); wantedHeaders != "" {
rw.Header().Set("Access-Control-Allow-Headers", wantedHeaders)
}
// Set the CORS security policy cache if this is an OPTIONS request
if r.Method == "OPTIONS" {
rw.Header().Set("Access-Control-Max-Age", "3600")
return
}
}
h.Handler.ServeHTTP(rw, r)
}