-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
154 lines (147 loc) · 3.06 KB
/
tree.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
package mux
import (
"fmt"
"net/http"
"sort"
"strings"
)
// empty represents the empty method set.
const empty = "EMPTY"
// tree is the default router implementation.
// tree also implements the Builder and Walker interfaces.
type tree struct {
names map[string]*Route
methods map[string]*node
}
// Add adds r to the tree.
func (t *tree) Add(r *Route) error {
name := r.Name()
if name != "" {
if t.names == nil {
t.names = make(map[string]*Route)
}
_, ok := t.names[name]
if ok {
return fmt.Errorf("mux: duplicate named route '%s'", name)
}
t.names[name] = r
}
methods := r.Methods()
if len(methods) == 0 {
methods = append(methods, empty)
}
if t.methods == nil {
t.methods = make(map[string]*node)
}
for _, method := range methods {
_, ok := t.methods[method]
if !ok {
t.methods[method] = &node{}
}
err := t.methods[method].add(r)
if err != nil {
return err
}
}
return nil
}
// Build returns the URL for the named route or an error if
// the named route does not exist or a parameter is missing.
//
// Build implements the Builder interface.
func (t *tree) Build(name string, params Params) (string, error) {
r, ok := t.names[name]
if !ok {
return "", ErrBuild
}
var buf strings.Builder
pattern := r.Pattern()
for pattern != "" {
b := pattern[0]
switch b {
case ':':
var i int
for i = 1; i < len(pattern); i++ {
if isBreak(pattern[i]) {
break
}
}
key := pattern[1:i]
v, ok := params[key]
if !ok {
return "", ErrBuild
}
buf.WriteString(v)
pattern = pattern[i:]
case '*':
key := string(b)
v, ok := params[key]
if ok {
buf.WriteString(v)
}
pattern = pattern[1:]
default:
var i int
for i = 1; i < len(pattern); i++ {
if isParam(pattern[i]) {
break
}
}
buf.WriteString(pattern[:i])
pattern = pattern[i:]
}
}
return buf.String(), nil
}
// Match returns the matching route and parameters.
func (t *tree) Match(req *http.Request) (*Route, Params, error) {
path := req.URL.EscapedPath()
root, ok := t.methods[req.Method]
if ok {
route, params, err := root.match(path)
if err == nil {
return route, params, nil
}
}
root, ok = t.methods[empty]
if ok {
route, params, err := root.match(path)
if err == nil {
return route, params, nil
}
}
allowed := make([]string, 0)
for method := range t.methods {
if method == req.Method || method == http.MethodOptions {
continue
}
root = t.methods[method]
_, _, err := root.match(path)
if err == nil {
allowed = append(allowed, method)
}
}
if len(allowed) > 0 {
allowed = append(allowed, http.MethodOptions)
sort.Strings(allowed)
return nil, nil, ErrMethodNotAllowed(allowed)
}
return nil, nil, ErrNotFound
}
// Walk yields named routes to fn sorting alphabetically.
//
// Walk implements the Walker interface.
func (t *tree) Walk(fn WalkFunc) error {
names := make([]string, 0)
for name := range t.names {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
err := fn(t.names[name])
if err != nil {
return err
}
}
return nil
}