-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewrite.go
88 lines (78 loc) · 1.93 KB
/
rewrite.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
package vweb
import (
"regexp"
"github.com/456vv/verror"
"strings"
"strconv"
)
type Forward struct {
Path []string // 多种路径匹配
ExcludePath []string // 排除多种路径匹配
RePath string // 重写路径
}
func (T *Forward) Rewrite(upath string) (rpath string, rewrited bool, err error) {
//满足路径
var regExp *regexp.Regexp
//排除路径
for _, ep := range T.ExcludePath {
//非正则
if ep == upath {
rpath = upath
return
}
//正则
regExp, err = regexp.Compile(ep)
if err != nil {
err = verror.TrackErrorf("vweb: 是错误正则re2(%s)", ep)
return
}
_, complete := regExp.LiteralPrefix()
if !complete {
regExp.Longest()
if regExp.MatchString(upath) {
rpath = upath
return
}
}
}
//包含路径
regExp = nil
for _, ep := range T.Path {
//非正则
if rewrited = (ep == upath); rewrited {
break
}
//正则
regExp, err = regexp.Compile(ep)
if err != nil {
err = verror.TrackErrorf("vweb: 是错误正则re2(%s)", ep)
return
}
_, complete := regExp.LiteralPrefix()
if !complete {
regExp.Longest()
rewrited = regExp.MatchString(upath)
if rewrited {
break
}
}
regExp = nil
}
//修改路径地址
if rewrited {
if regExp != nil {
var findAllSubmatch [][]string = regExp.FindAllStringSubmatch(upath, 1)
if len(findAllSubmatch) != 0 {
rpath = T.RePath
submatch := findAllSubmatch[0] //使用第一个匹配
for i, match := range submatch {
rpath = strings.Replace(rpath, "$"+strconv.Itoa(i), match, -1)
}
}
}else{
rpath = strings.Replace(T.RePath, "$0", upath, -1)
}
return rpath, true, nil
}
return upath, false, nil
}