/
RequestProcess.go
358 lines (338 loc) · 6.84 KB
/
RequestProcess.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package coderg
import (
"regexp"
"strings"
"strconv"
)
/*
* InputProcessor
* 输入处理器
*/
type InputProcessor struct {
reScript *regexp.Regexp;
reMark *regexp.Regexp;
reEmail *regexp.Regexp;
reUrl *regexp.Regexp;
}
func NewInputProcessor() (ip *InputProcessor){
ip = new(InputProcessor);
ip.reScript, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>");
ip.reMark, _ = regexp.Compile("^[A-Za-z0-9_-]+$");
ip.reEmail, _ = regexp.Compile("^[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*.([A-Za-z]){2,5}$");
ip.reUrl, _ = regexp.Compile("^[A-Za-z0-9]+://");
return;
}
/*
* replaceText
* 替换危险字符
*/
func (ip *InputProcessor) replaceText(text string) string{
theReplaceMap := make(map[string]string);
theReplaceMap["<"] = "<";
theReplaceMap[">"] = ">";
theReplaceMap["\""] = "&guot;";
theReplaceMap["'"] = "'";
theReplaceMap["|"] = "¦";
theReplaceMap["`"] = "´";
for index, value := range theReplaceMap {
text = strings.Replace(text, index, value, -1);
}
return text;
}
/*
* minOrMax
* 按要求判断字符长度是否符合要求
*/
func (ip *InputProcessor) minOrMax(text string, min, max int) (err int){
textLen := len(text);
if(textLen < min){
err = 2;
return;
}
if(max != 0 && textLen > max){
err = 3;
return;
}
err = 0;
return;
}
/*
* Text
* 处理简单文本输入,替换可能引起注入的字符、判断长度等
*/
func (ip *InputProcessor) Text(text string, must bool, min, max int) (textc string, err int){
err = 0;
textc = "";
text = strings.TrimSpace(text);
textLen := len(text);
if(must == true && textLen == 0){
err = 1;
return;
}
if(textLen == 0){
return;
}
text = ip.replaceText(text);
err = ip.minOrMax(text,min,max);
if err == 0 {
textc = text;
}
return;
}
/*
* Mark
* 处理作为标记的的字符串,只能由字母数字和连字符组成
*/
func (ip *InputProcessor) Mark(text string, must bool, min, max int) (textc string, err int){
err = 0;
textc = "";
text = strings.TrimSpace(text);
textLen := len(text);
if(must == true && textLen == 0){
err = 1;
return;
}
if(textLen == 0){
return;
}
err = ip.minOrMax(text,min,max);
if err != 0 {
return;
}
ifmatch := ip.reMark.MatchString(text)
if ( ifmatch == false){
err = 4;
return;
}
textc = text;
return;
}
/*
* EditorIn
* 处理编辑器的输入,除了字数检测外,主要是过滤script标签
*/
func (ip *InputProcessor) EditorIn(text string, must bool, min, max int) (textc string, err int){
err = 0;
textc = "";
text = strings.TrimSpace(text);
textLen := len(text);
if(must == true && textLen == 0){
err = 1;
return;
}
if(textLen == 0){
return;
}
text = ip.reScript.ReplaceAllString(text, "");
text = strings.Replace(text, "`", "´", -1);
text = strings.Replace(text, "'", "''", -1);
err = ip.minOrMax(text,min,max);
if err != 0 {
return;
}
textc = text;
return;
}
/*
* EditorRe
* 处理编辑器的重新编辑
*/
func (ip *InputProcessor) EditorRe(text string) (string){
text = strings.Replace(text, "´", "`", -1);
text = strings.Replace(text, "'", "''", -1);
return text;
}
/*
* TextareaOut
* 处理文本域的输出,主要就是对换行符进行处理,将\r\n之类的转换成<p>或<br>
* thetype为true则转成<p>,type为false则转为<br>
*
*/
func (ip *InputProcessor) TextareaOut (text string, thetype bool) string{
if (thetype == true) {
theReplaceMap := make(map[string]string);
theReplaceMap["\r\n"] = "</p><p>";
theReplaceMap["\r"] = "</p><p>";
theReplaceMap["\n"] = "</p><p>";
for index, value := range theReplaceMap {
text = strings.Replace(text, index, value, -1);
}
text = "<p>" + text + "</p>";
}else{
theReplaceMap := make(map[string]string);
theReplaceMap["\r\n"] = "<br>";
theReplaceMap["\r"] = "</br>";
theReplaceMap["\n"] = "<br>";
for index, value := range theReplaceMap {
text = strings.Replace(text, index, value, -1);
}
}
return text;
}
/*
* Int
* 输入是否为整数
*
*/
func (ip *InputProcessor) Int(text string, must bool, min, max int64) (num int64, err int){
text = strings.TrimSpace(text);
err = 0;
textLen := len(text);
if(must == true && textLen == 0){
num = 0;
err = 1;
return;
}
if(textLen == 0){
num = 0;
return;
}
num, e := strconv.ParseInt(text,10,64);
if e != nil {
err = 2;
return;
}
if (num < min || num > max){
err = 3;
return;
}
return;
}
/*
* Float
* 输入是否为浮点
*
*/
func (ip *InputProcessor) Float (text string, must bool, min, max float64) (num float64, err int){
text = strings.TrimSpace(text);
err = 0;
textLen := len(text);
if(must == true && textLen == 0){
num = 0;
err = 1;
return;
}
if(textLen == 0){
num = 0;
return;
}
num, e := strconv.ParseFloat(text,64);
if e != nil {
err = 2;
return;
}
if (num < min || num > max){
err = 3;
return;
}
return;
}
/*
* Enum
* 枚举,判断提供的字符串是否出现在字符串切片里
*/
func (ip *InputProcessor) Enum(text string, must bool, enum []string) (textc string, err int){
err = 0;
textc = "";
text = strings.TrimSpace(text);
textLen := len(text);
if(must == true && textLen == 0){
err = 1;
return;
}
if(textLen == 0){
return;
}
for _, v := range enum {
if text == v {
textc = text;
return;
}
}
err = 2;
return;
}
/*
* Email
* 查看格式是不是邮箱地址
*/
func (ip *InputProcessor) Email(text string, must bool, min, max int)(textc string, err int){
err = 0;
textc = "";
text = strings.TrimSpace(text);
textLen := len(text);
if(must == true && textLen == 0){
err = 1;
return;
}
if(textLen == 0){
return;
}
err = ip.minOrMax(text,min,max);
if err != 0 {
return;
}
ifmatch := ip.reEmail.MatchString(text)
if ( ifmatch == false){
err = 4;
return;
}
textc = text;
return;
}
/*
* Url
* 处理连接,主要是如果没有协议的话,就加上http://
*/
func (ip *InputProcessor) Url (text string, must bool, min, max int) (textc string, err int) {
err = 0;
textc = "";
text = strings.TrimSpace(text);
textLen := len(text);
if(must == true && textLen == 0){
err = 1;
return;
}
if(textLen == 0){
return;
}
err = ip.minOrMax(text,min,max);
if err != 0 {
return;
}
ifmatch := ip.reUrl.MatchString(text)
if ( ifmatch == false){
textc = "http://" + text;
}else{
textc = text;
}
return;
}
/*
* Regular
* 正则判断,提供一个正则表达,如果匹配则返回字符串
*/
func (ip *InputProcessor) Regular (text string, must bool ,rg string) (textc string, err int){
err = 0;
textc = "";
text = strings.TrimSpace(text);
textLen := len(text);
if(must == true && textLen == 0){
err = 1;
return;
}
if(textLen == 0){
return;
}
therp, ther := regexp.MatchString(rg, text);
if (ther != nil){
err = 2;
return;
}
if (therp == false) {
err = 3;
return;
}
textc = text;
return;
}