-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
53 lines (43 loc) · 1.27 KB
/
utils.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
package corset
import (
"net/http"
"unicode"
)
// isPreflightRequest determines whether the given request is a Preflight
// A Preflight must:
// 1) use the OPTIONS method
// 2) include an Origin request header
// 3) Include an Access-Control-Request-Method header
func isPreflightRequest(r *http.Request) bool {
isOptionsReq := r.Method == http.MethodOptions
hasOriginHeader := r.Header.Get(originHeader) != ""
hasRequestMethod := r.Header.Get(requestMethodHeader) != ""
return isOptionsReq && hasOriginHeader && hasRequestMethod
}
// deriveHeaders extracts the headers in the value of the
// `Access-Control-Request-Headers` header
// @todo optimize
func deriveHeaders(r *http.Request) []string {
headersStr := r.Header.Get(requestHeadersHeader)
headers := []string{}
if headersStr == "" {
return headers
}
length := len(headersStr)
var tmp []rune
for i, char := range headersStr {
if (char >= 'a' && char <= 'z') || char == '_' || char == '-' || char == '.' || (char >= '0' && char <= '9') {
tmp = append(tmp, char)
}
if char >= 'A' && char <= 'Z' {
tmp = append(tmp, unicode.ToLower(char))
}
if char == ' ' || char == ',' || i == length-1 {
if len(tmp) > 0 {
headers = append(headers, string(tmp))
tmp = []rune{}
}
}
}
return headers
}