/
adventurer.go
309 lines (292 loc) · 7.3 KB
/
adventurer.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Package adventurer @author K·J Create at 2019-01-09 11:00
package adventurer
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"reflect"
"regexp"
"strings"
"time"
"github.com/sirupsen/logrus"
)
// Adventurer 路由对象
type Adventurer struct {
owner interface{}
stories []Story
profile *Profile
cros bool
hook *StoryHook
resp *Resp
enableTrialsErr bool
logger *logrus.Logger
}
// Equipment 请求的信息
type Equipment struct {
Query url.Values
Body []byte
Header http.Header
Method string
}
// Hook 校验
type Hook interface {
Fire(prerequisite []string, equipment Equipment) (bool, error)
ErrResp(w *http.ResponseWriter)
}
type StoryHook map[string]Hook // 基本信息,key:校验的名字,value:校验实现
type Resp struct {
Code int
Msg []byte
}
// NewAdventurer 生成路由对象
func NewAdventurer(owner interface{}, stories *[]Story,
profile *Profile, hook *StoryHook, resp *Resp,
enableTrialsErr bool, logger *logrus.Logger) (*Adventurer, error) {
if nil == owner || nil == stories {
return nil, errors.New("param is nil")
}
adventurer := &Adventurer{
owner: owner,
profile: profile,
hook: hook,
resp: resp,
enableTrialsErr: enableTrialsErr,
logger: logger,
}
if nil == adventurer.logger {
adventurer.logger = logrus.StandardLogger()
adventurer.logger.SetFormatter(&logrus.JSONFormatter{})
}
err := adventurer.InitStory(*stories)
if nil != err {
logger.Error(err)
return nil, err
}
if nil != profile || "" != profile.URL {
story := Story{
URL: profile.URL,
Handler: "Handler",
Method: []string{http.MethodGet},
}
err = adventurer.AddStory(story)
if nil != err {
logger.Error(err)
return nil, err
}
}
return adventurer, nil
}
// AddStory add story
func (a *Adventurer) AddStory(s Story) error {
if "" == s.URL || nil == s.Method || "" == s.Handler {
a.logger.Error("param is invalid")
return errors.New("param is invalid")
}
methods := strings.Join(s.Method, ",")
if nil == a.stories {
a.stories = make([]Story, 0)
}
for _, v := range a.stories {
if ok, _ := regexp.MatchString("[A-Z].*", v.Handler); !ok {
a.logger.Error("handler should be exported")
return errors.New("handler should be exported")
}
if v.URL == s.URL {
for _, m := range v.Method {
if strings.Contains(methods, m) {
a.logger.Error("url handler already exist")
return errors.New("url handler already exist")
}
}
}
}
a.stories = append(a.stories, s)
return nil
}
// InitStory 初始化story
func (a *Adventurer) InitStory(stories []Story) error {
if nil == stories {
a.logger.Error("stories is nil")
return errors.New("stories is nil")
}
for _, s := range stories {
err := a.AddStory(s)
if nil != err {
a.logger.Error(err)
return err
}
}
return nil
}
// SetCros enable or disable cros
func (a *Adventurer) SetCros(cros bool) {
a.cros = cros
}
// Explore url router
func (a *Adventurer) Explore(w http.ResponseWriter, r *http.Request) {
startTime := unixMillisecond()
if "" == r.Header.Get("X-Real-IP") {
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
r.Header.Set("X-Real-IP", ip)
}
if a.cros {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
}
found := false
match := false
trialFailed := false
for _, v := range a.stories {
methods := strings.Join(v.Method, ",")
if ok, _ := regexp.MatchString("^"+v.URL+"$", r.URL.Path); ok {
found = true
if strings.Contains(methods, r.Method) {
match = true
if nil != a.profile && r.URL.Path == a.profile.URL {
a.profile.Handler(w, r)
goto End
}
// 校验
if nil != a.hook && len(*a.hook) > 0 {
if nil != v.Trials {
equipment, err := parseEquipment(r)
if nil != err {
log.Println(err)
trialFailed = true
if nil != a.resp {
w.WriteHeader((*a.resp).Code)
w.Write((*a.resp).Msg)
} else {
w.WriteHeader(http.StatusBadRequest)
}
goto End
}
for k, t := range v.Trials {
if trial, ok := (*a.hook)[k]; ok {
if nil != trial {
b, err := trial.Fire(t, *equipment)
if nil != err {
a.logger.Error(err)
trialFailed = true
if a.enableTrialsErr {
trial.ErrResp(&w)
} else {
if nil != a.resp {
w.WriteHeader((*a.resp).Code)
w.Write((*a.resp).Msg)
} else {
w.WriteHeader(http.StatusBadRequest)
}
}
goto End
}
if !b {
trialFailed = true
if nil != a.resp {
w.WriteHeader((*a.resp).Code)
w.Write((*a.resp).Msg)
} else {
w.WriteHeader(http.StatusBadRequest)
}
goto End
}
}
}
}
}
}
param := make([]reflect.Value, 2)
param[0] = reflect.ValueOf(w)
param[1] = reflect.ValueOf(r)
reflect.ValueOf(a.owner).MethodByName(v.Handler).Call(param)
break
}
}
}
End:
endTime := unixMillisecond()
a.logger.WithFields(logrus.Fields{
"url": r.URL.Path,
"method": r.Method,
"cost": fmt.Sprintf("%d ms", endTime-startTime),
}).Info("OK")
if trialFailed {
return
}
if found && !match {
w.WriteHeader(http.StatusBadRequest)
return
}
if !found {
w.WriteHeader(http.StatusNotFound)
return
}
}
// parseEquipment 获取request
func parseEquipment(r *http.Request) (*Equipment, error) {
switch r.Method {
case http.MethodGet:
return &Equipment{Header: r.Header, Query: r.URL.Query(), Method: r.Method}, nil
case http.MethodDelete:
fallthrough
case http.MethodPost:
fallthrough
case http.MethodPut:
exp, err := regexp.Compile("([a-zA-z/\\-_\\.]+)(;*(.+))*")
if nil != err {
return nil, err
}
param := exp.FindStringSubmatch(r.Header.Get("Content-Type"))
if nil == param || len(param) < 2 {
return nil, errors.New("not found content-type")
}
switch param[1] {
case "multipart/form-data":
body, err := httputil.DumpRequest(r, true)
if nil != err {
return nil, err
}
copy, err := http.NewRequest("POST", "", bytes.NewReader(body))
if nil != err {
return nil, err
}
copy.Header = r.Header
err = copy.ParseMultipartForm(32 << 20)
if nil != err {
copy.Body.Close()
return nil, err
}
copy.Body.Close()
return &Equipment{Header: r.Header, Query: r.PostForm, Method: r.Method}, nil
//case "application/json":
// body, err := ioutil.ReadAll(r.Body)
// if nil != err {
// logger.Error(err.Error())
// return nil, err
// }
// r.Body.Close()
// r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
// return &Equipment{Header: r.Header, Query: r.URL.Query(), Method: r.Method, Body: body}, nil
default:
body, err := ioutil.ReadAll(r.Body)
if nil != err {
return nil, err
}
r.Body.Close()
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
return &Equipment{Header: r.Header, Query: r.URL.Query(), Method: r.Method, Body: body}, nil
}
}
return nil, errors.New("method not supported")
}
// unixMillisecond unix time millisecond
func unixMillisecond() int64 {
return time.Now().UnixNano() / 1e6
}