/
cors.go
139 lines (122 loc) · 3.63 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
package proxy
import (
"fmt"
"net/http"
"strings"
"github.com/codeready-toolchain/registration-service/pkg/log"
)
const toLower = 'a' - 'A'
// corsPreflightHandler handles the CORS preflight requests
func corsPreflightHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions && r.Header.Get("Access-Control-Request-Method") != "" {
log.Info(nil, "Handling preflight request")
handlePreflight(w, r)
// Preflight requests are standalone and should stop the chain
w.WriteHeader(http.StatusNoContent)
} else {
// Actual request
h.ServeHTTP(w, r)
}
})
}
func handlePreflight(w http.ResponseWriter, r *http.Request) {
headers := w.Header()
origin := r.Header.Get("Origin")
// Allow all origins but empty
if origin == "" {
log.Info(nil, "Preflight aborted: empty origin")
return
}
log.Info(nil, "Preflight request from "+origin)
// Allow all known methods
reqMethod := r.Header.Get("Access-Control-Request-Method")
if !isMethodAllowed(reqMethod) {
log.Info(nil, fmt.Sprintf("Preflight aborted: method '%s' not allowed", reqMethod))
return
}
headers.Add("Vary", "Origin")
headers.Add("Vary", "Access-Control-Request-Method")
headers.Add("Vary", "Access-Control-Request-Headers")
// Since we allow all headers we don't check the "Access-Control-Request-Method" header
reqHeaders := parseHeaderList(r.Header.Get("Access-Control-Request-Headers"))
// Set the response headers
headers.Set("Access-Control-Allow-Origin", origin)
headers.Set("Access-Control-Allow-Methods", strings.Join(allowedMethods, ", "))
if len(reqHeaders) > 0 {
// Simply returning requested headers from Access-Control-Request-Headers should be enough
headers.Set("Access-Control-Allow-Headers", strings.Join(reqHeaders, ", "))
}
// Allow credentials
headers.Set("Access-Control-Allow-Credentials", "true")
}
var allowedMethods = []string{"PUT", "PATCH", "POST", "GET", "DELETE", "OPTIONS"}
func isMethodAllowed(method string) bool {
method = strings.ToUpper(method)
for _, m := range allowedMethods {
if m == method {
return true
}
}
return false
}
type responseModifier struct {
requestOrigin string
}
// addCorsToResponse adds CORS headers to the response
func (r *responseModifier) addCorsToResponse(response *http.Response) error {
origin := r.requestOrigin
if origin == "" {
origin = "*"
}
// CORS Headers
response.Header.Add("Vary", "Origin")
response.Header.Set("Access-Control-Allow-Origin", origin)
response.Header.Set("Access-Control-Allow-Credentials", "true")
response.Header.Set("Access-Control-Expose-Headers", "Content-Length, Content-Encoding, Authorization")
return nil
}
// parseHeaderList tokenize + normalize a string containing a list of headers
func parseHeaderList(headerList string) []string {
l := len(headerList)
h := make([]byte, 0, l)
upper := true
// Estimate the number headers in order to allocate the right splice size
t := 0
for i := 0; i < l; i++ {
if headerList[i] == ',' {
t++
}
}
headers := make([]string, 0, t)
for i := 0; i < l; i++ {
b := headerList[i]
switch {
case b >= 'a' && b <= 'z':
if upper {
h = append(h, b-toLower)
} else {
h = append(h, b)
}
case b >= 'A' && b <= 'Z':
if !upper {
h = append(h, b+toLower)
} else {
h = append(h, b)
}
case b == '-' || b == '_' || b == '.' || (b >= '0' && b <= '9'):
h = append(h, b)
}
if b == ' ' || b == ',' || i == l-1 {
if len(h) > 0 {
// Flush the found header
headers = append(headers, string(h))
h = h[:0]
upper = true
}
} else {
upper = b == '-' || b == '_'
}
}
return headers
}