-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtree.go
More file actions
219 lines (202 loc) · 5.16 KB
/
Copy pathtree.go
File metadata and controls
219 lines (202 loc) · 5.16 KB
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
// Copyright 2013 Julien Schmidt. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// at https://github.com/julienschmidt/httprouter/blob/master/LICENSE
package fwncs
import (
"net/http"
"regexp"
"strings"
)
// Param is a single URL parameter, consisting of a key and a value.
type Param struct {
Key string
Value string
}
// Params is a Param-slice, as returned by the router.
// The slice is ordered, the first URL parameter is also the first slice value.
// It is therefore safe to read values by the index.
type Params []Param
// Get returns the value of the first Param which key matches the given name.
// If no matching Param is found, an empty string is returned.
func (ps Params) Get(name string) (string, bool) {
for _, entry := range ps {
if entry.Key == name {
return entry.Value, true
}
}
return "", false
}
// ByName returns the value of the first Param which key matches the given name.
// If no matching Param is found, an empty string is returned.
func (ps Params) ByName(name string) (va string) {
va, _ = ps.Get(name)
return
}
func (ps Params) Values() []string {
values := make([]string, len(ps))
for i := range ps {
values[i] = ps[i].Value
}
return values
}
type locationNode struct {
reg *regexp.Regexp
originalPath string
replacePath string
index int
}
type locationNodes []locationNode
type paramNode struct {
index int
params *Params
matchPath string
}
// Match
func (nodes locationNodes) Match(rawURI string) (param *paramNode) {
var index = -1
var max int = 0
var groups [][]string
for idx := range nodes {
node := nodes[idx]
v := node.reg.FindAllStringSubmatch(rawURI, -1)
if v != nil {
// 始めは無条件で設定
if index == -1 {
index = idx
groups = v
} else {
// マッチした数が多い = パスの数が多いのでこちらを優先
value := v[0]
if len(value[1:]) > max && value[len(value)-1] != "" {
max = len(v[0][1:])
index = idx
groups = v
} else {
if value[len(value)-1] != "" {
oldPath := nodes[index].replacePath
nowPath := node.replacePath
if len(nowPath) > len(oldPath) {
index = idx
groups = v
}
}
}
}
}
}
if index > -1 {
node := nodes[index]
originalUrl := node.replacePath
matchValue := groups[0][1:]
idxs := namedParam.FindAllStringIndex(originalUrl, -1)
params := make(Params, len(idxs))
for i, idx := range idxs {
start := idx[0]
end := idx[1]
value := matchValue[i]
if value != "" && lastChar(value) == '/' {
value = value[0 : len(value)-1]
}
params[i] = Param{
Key: originalUrl[start+1 : end],
Value: value,
}
}
param = ¶mNode{
index: node.index,
params: ¶ms,
matchPath: node.originalPath,
}
return
}
return
}
type nodelocation struct {
nodes locationNodes
full locationNodes
prefix locationNodes
}
var namedParam, _ = regexp.Compile(":[a-zA-Z0-9]+")
func locationRegex(paths []string) nodelocation {
const (
full = "= "
prefix = "~ "
)
var locations locationNodes
var fullLocations locationNodes
var prefixLocations locationNodes
for idx, path := range paths {
location := locationNode{
index: idx,
}
if strings.HasPrefix(path, "= ") {
path = strings.Replace(path, full, "", 1)
location.originalPath = path
fullLocations = append(fullLocations, location)
continue
}
prefixFlg := strings.HasPrefix(path, "~ ")
if prefixFlg {
path = strings.Replace(path, prefix, "", 1)
}
location.originalPath = path
path = regexp.QuoteMeta(path)
path = strings.Replace(path, `\*`, ":", -1)
path = strings.Replace(path, `\.`, ".", -1)
if strings.HasPrefix(path, `\^`) {
path = strings.Replace(path, `\^`, "^", -1)
}
location.replacePath = path
path = namedParam.ReplaceAllString(path, "(.*?)")
path = path + "$"
location.reg = regexp.MustCompile(path)
if prefixFlg {
prefixLocations = append(prefixLocations, location)
continue
}
locations = append(locations, location)
}
return nodelocation{
nodes: locations,
full: fullLocations,
prefix: prefixLocations,
}
}
/*
1.完全一致のURLを選択し、終了
2.優先したいlocationから正規表現で最も長い文字列でマッチしたもの
3.それ以外のlocationから正規表現で最も長い文字列でマッチしたもの
*/
func matchRequestURL(patterns nodelocation, req *http.Request) (node *paramNode) {
rawURI := req.URL.Path
if rawURI != "" && rawURI[0] != '/' {
prefix := ""
if req.URL.Scheme != "" {
prefix = req.URL.Scheme + "://"
}
if req.URL.Host != "" {
prefix += req.URL.Host // host or host:port
}
if prefix != "" {
rawURI = strings.TrimPrefix(rawURI, prefix)
}
}
return matchURL(patterns, rawURI)
}
func matchURL(patterns nodelocation, rawURI string) (node *paramNode) {
for _, location := range patterns.full {
if strings.EqualFold(rawURI, location.originalPath) {
node = ¶mNode{
index: location.index,
params: &Params{},
matchPath: location.originalPath,
}
return
}
}
node = patterns.prefix.Match(rawURI)
if node == nil {
node = patterns.nodes.Match(rawURI)
}
return
}