/
corsrouter.go
229 lines (199 loc) · 5.38 KB
/
corsrouter.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright (C) 2015 Fabrício Godoy <skarllot@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package bll
import (
rqhttp "github.com/skarllot/raiqub/http"
"net/http"
"strconv"
"strings"
"time"
)
const (
DEFAULT_CORS_PREFLIGHT_METHOD = "OPTIONS"
DEFAULT_CORS_MAX_AGE = time.Hour * 24 / time.Second
DEFAULT_CORS_METHODS = "OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT"
DEFAULT_CORS_ORIGIN = "*"
)
type CORSRouter struct {
inner http.Handler
routes map[string]string
AllowOrigin PredicateStringFunc
PreflightMiddleware rqhttp.HttpMiddlewareFunc
}
type CORSHandler struct {
PredicateOrigin PredicateStringFunc
Headers []string
ExposedHeaders []string
}
type PredicateStringFunc func(string) bool
type IntStringTuple struct {
Item1 int
Item2 string
}
func NewCORSHandler() *CORSHandler {
return &CORSHandler{
PredicateOrigin: TrueForAll,
Headers: []string{
"Origin", "X-Requested-With", "Content-Type",
"Accept", "Authorization",
},
ExposedHeaders: make([]string, 0),
}
}
func (s *CORSHandler) CreateOptionsRoutes(routes rqhttp.Routes) rqhttp.Routes {
list := make(rqhttp.Routes, 0, len(routes))
hList := make(map[string]*CORSPreflight, len(routes))
for _, v := range routes {
preflight, ok := hList[v.Path]
if !ok {
preflight = &CORSPreflight{
*s,
make([]string, 0, 1),
v.MustAuth,
}
hList[v.Path] = preflight
}
preflight.Methods = append(preflight.Methods, v.Method)
}
for k, v := range hList {
list = append(list, rqhttp.Route{
Name: "",
Method: DEFAULT_CORS_PREFLIGHT_METHOD,
Path: k,
MustAuth: v.UseCredentials,
ActionFunc: v.ServeHTTP,
})
}
return list
}
func TrueForAll(string) bool {
return true
}
type CORSMethod struct {
CORSHandler
UseCredentials bool
}
func (s *CORSMethod) CORSMiddleware(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
origin := rqhttp.HttpHeader_Origin().GetReader(r.Header)
if r.Method != DEFAULT_CORS_PREFLIGHT_METHOD && origin.Value != "" {
if !s.PredicateOrigin(origin.Value) {
return
}
rqhttp.HttpHeader_AccessControlAllowOrigin().
SetValue(origin.Value).
SetWriter(w.Header())
rqhttp.HttpHeader_AccessControlAllowCredentials().
SetValue(strconv.FormatBool(s.UseCredentials)).
SetWriter(w.Header())
if len(s.Headers) > 0 {
rqhttp.HttpHeader_AccessControlAllowHeaders().
SetValue(strings.Join(s.Headers, ", ")).
SetWriter(w.Header())
} else {
rqhttp.HttpHeader_AccessControlAllowHeaders().
SetWriter(w.Header())
}
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
type CORSPreflight struct {
CORSHandler
Methods []string
UseCredentials bool
}
func (s *CORSPreflight) ServeHTTP(w http.ResponseWriter, r *http.Request) {
origin := rqhttp.HttpHeader_Origin().GetReader(r.Header)
status := http.StatusBadRequest
defer func() {
w.WriteHeader(status)
}()
if origin.Value != "" {
if !s.PredicateOrigin(origin.Value) {
status = http.StatusForbidden
return
}
method := r.Header.Get("Access-Control-Request-Method")
header := strings.Split(
r.Header.Get("Access-Control-Request-Headers"),
", ")
if !StringSlice(s.Methods).Exists(method) {
return
}
if len(s.Headers) == 0 {
rqhttp.HttpHeader_AccessControlAllowHeaders().
SetWriter(w.Header())
} else {
if len(header) > 0 &&
!StringSlice(s.Headers).ExistsAllIgnoreCase(header) {
return
}
rqhttp.HttpHeader_AccessControlAllowHeaders().
SetValue(strings.Join(s.Headers, ", ")).
SetWriter(w.Header())
}
rqhttp.HttpHeader_AccessControlAllowMethods().
SetValue(strings.Join(s.Methods, ", ")).
SetWriter(w.Header())
origin.SetWriter(w.Header())
rqhttp.HttpHeader_AccessControlAllowCredentials().
SetValue(strconv.FormatBool(s.UseCredentials)).
SetWriter(w.Header())
// Optional
rqhttp.HttpHeader_AccessControlMaxAge().
SetValue(strconv.Itoa(int(DEFAULT_CORS_MAX_AGE))).
SetWriter(w.Header())
status = http.StatusOK
} else {
status = http.StatusNotFound
}
}
type StringSlice []string
func (s StringSlice) IndexOf(str string) int {
for i, v := range s {
if str == v {
return i
}
}
return -1
}
func (s StringSlice) IndexOfIgnoreCase(str string) int {
str = strings.ToLower(str)
for i, v := range s {
if str == strings.ToLower(v) {
return i
}
}
return -1
}
func (s StringSlice) Exists(str string) bool {
return s.IndexOf(str) != -1
}
func (s StringSlice) ExistsIgnoreCase(str string) bool {
return s.IndexOfIgnoreCase(str) != -1
}
func (s StringSlice) ExistsAllIgnoreCase(str []string) bool {
for _, v := range str {
if !s.ExistsIgnoreCase(v) {
return false
}
}
return true
}