forked from gofiber/fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter_skip.go
More file actions
248 lines (220 loc) · 7.89 KB
/
Copy pathrouter_skip.go
File metadata and controls
248 lines (220 loc) · 7.89 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
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
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 GitHub Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package fiber
// skipRouteIndex holds precomputed unmatched-route indexes, rebuilt by buildSkipIndexes with the route tree.
type skipRouteIndex struct {
// staticMethods maps a static endpoint's path to a method bitmask; nil unless SkipUnmatchedRoutes is on
staticMethods map[string]uint64
// buckets holds the per-tree-bucket lookahead state, one entry per treeStack bucket key (union across methods)
buckets map[int]*skipBucket
// zeroBucket serves tree hashes with no bucket at all, mirroring next()'s treeStack miss
zeroBucket *skipBucket
// routeMethods is a method bitmask with at least one non-use route; only valid when methodMaskValid
routeMethods uint64
// enabled gates the fast path: SkipUnmatchedRoutes is on and middleware is registered
// (without middleware next() already answers 404/405 cheaply)
enabled bool
// hasParamUse is true when any param/wildcard middleware exists; when false next() can reuse the lookahead's params
hasParamUse bool
// methodMaskValid is true when routeMethods is trustworthy (len(RequestMethods) <= 64)
methodMaskValid bool
}
// skipBucket holds one tree bucket's param/root/star candidates per method, with the
// bucket-0 fallback materialized at build time so a lookup never needs a second map access.
type skipBucket struct {
// cands is indexed by method int
cands [][]indexedRoute
// paramMask has a bit per method with at least one candidate in cands
paramMask uint64
}
// indexedRoute pairs a candidate route with its position in its tree bucket.
type indexedRoute struct {
route *Route
idx int
}
// skipDecision is the outcome kind of the SkipUnmatchedRoutes lookahead.
type skipDecision int
const (
skipRunChain skipDecision = iota // run the normal chain
skipNotFound // 404 without running the chain
skipNotAllowed // 405; allowMask holds the methods
)
// skipResult is the outcome of the SkipUnmatchedRoutes lookahead.
type skipResult struct {
decision skipDecision
allowMask uint64 // methods for the Allow header (skipNotAllowed only)
matchIndex int // pre-resolved endpoint index for next()/nextCustom(), or -1
}
// buildSkipIndexes rebuilds app.skip from the route tree; called from buildTree.
func (app *App) buildSkipIndexes() {
idx := skipRouteIndex{}
// Masks are 64-bit; with more methods leave everything disabled, next() answers as usual.
idx.methodMaskValid = len(app.config.RequestMethods) <= 64
if !idx.methodMaskValid {
app.skip = idx
return
}
// 405-fallback prune mask; maintained even when SkipUnmatchedRoutes is off.
for method := range app.config.RequestMethods {
for _, route := range app.stack[method] {
if !route.use {
idx.routeMethods |= uint64(1) << method
break
}
}
}
if app.config.SkipUnmatchedRoutes {
idx.buildLookahead(app)
}
app.skip = idx
}
// buildLookahead fills the SkipUnmatchedRoutes lookahead indexes.
func (idx *skipRouteIndex) buildLookahead(app *App) {
nMethods := len(app.config.RequestMethods)
static := make(map[string]uint64)
buckets := make(map[int]*skipBucket)
hasUse := false
hasParamUse := false
getBucket := func(treeHash int) *skipBucket {
b := buckets[treeHash]
if b == nil {
b = &skipBucket{cands: make([][]indexedRoute, nMethods)}
buckets[treeHash] = b
}
return b
}
for method := range app.config.RequestMethods {
bit := uint64(1) << method
// Static endpoints match by exact compare against route.path, so keying by route.path is exact.
for _, route := range app.stack[method] {
if route.use {
hasUse = true
// param/wildcard middleware clobbers c.values on match
if route.star || len(route.Params) > 0 {
hasParamUse = true
}
continue
}
if route.mount || route.star || route.root || len(route.Params) > 0 {
continue
}
static[route.path] |= bit
}
// Candidates come from the final buckets (post bucket-0 replication) so idx lines up with next()'s scan.
for treeHash, bucket := range app.treeStack[method] {
sb := getBucket(treeHash)
for i, route := range bucket {
if route.use || route.mount {
continue
}
if route.root || route.star || len(route.Params) > 0 {
sb.cands[method] = append(sb.cands[method], indexedRoute{route: route, idx: i})
sb.paramMask |= bit
}
}
}
}
zero := buckets[0]
if zero == nil {
zero = &skipBucket{cands: make([][]indexedRoute, nMethods)}
buckets[0] = zero
}
// Materialize the per-method bucket-0 fallback: a method without a treeStack
// bucket at this hash scans its bucket 0 in next(), so mirror that here.
for treeHash, sb := range buckets {
if treeHash == 0 {
continue
}
for method := range app.config.RequestMethods {
if _, ok := app.treeStack[method][treeHash]; ok {
continue
}
sb.cands[method] = zero.cands[method]
if len(zero.cands[method]) > 0 {
sb.paramMask |= uint64(1) << method
}
}
}
idx.staticMethods = static
idx.buckets = buckets
idx.zeroBucket = zero
idx.enabled = hasUse
idx.hasParamUse = hasParamUse
}
// resolveSkip decides 404/405/run-chain. values is scratch: param/wildcard
// middleware may overwrite it before the endpoint runs, so next() re-matches then.
func (app *App) resolveSkip(methodInt, treeHash, pathSlashes int, detectionPath, path string, values *[maxParams]string) skipResult {
skip := &app.skip
methodBit := uint64(1) << methodInt
staticMask := skip.staticMethods[detectionPath]
// Tier 1: a static endpoint matches this method.
if staticMask&methodBit != 0 {
return skipResult{decision: skipRunChain, matchIndex: -1}
}
// Single bucket lookup; an unknown tree hash falls back to bucket 0 like next() does.
b, ok := skip.buckets[treeHash]
if !ok {
b = skip.zeroBucket
}
// Tier 2: scan this method's parametric candidates.
for _, cand := range b.cands[methodInt] {
if cand.route.match(detectionPath, path, values, pathSlashes) {
return skipResult{decision: skipRunChain, matchIndex: cand.idx}
}
}
// No param route reachable for any method: the static index alone decides 404 vs 405.
if b.paramMask == 0 {
if staticMask != 0 {
return skipResult{decision: skipNotAllowed, allowMask: staticMask, matchIndex: -1}
}
return skipResult{decision: skipNotFound, matchIndex: -1}
}
// Cross-method scan to decide 405 vs 404; paramMask prunes methods without candidates.
allow := staticMask
for m := range app.config.RequestMethods {
bit := uint64(1) << m
if m == methodInt || allow&bit != 0 || b.paramMask&bit == 0 {
continue
}
for _, cand := range b.cands[m] {
if cand.route.match(detectionPath, path, values, pathSlashes) {
allow |= bit
break
}
}
}
if allow != 0 {
return skipResult{decision: skipNotAllowed, allowMask: allow, matchIndex: -1}
}
return skipResult{decision: skipNotFound, matchIndex: -1}
}
// emitSkip answers the skipped 404/405 via the error handler. Takes *DefaultCtx so
// the variadic Append argument stays on the stack (interface dispatch allocates).
func (app *App) emitSkip(c *DefaultCtx, allowMask uint64, err error) {
if allowMask != 0 {
methods := app.config.RequestMethods
for i := range methods {
if allowMask&(uint64(1)<<i) != 0 {
c.Append(HeaderAllow, methods[i])
}
}
}
if catch := app.ErrorHandler(c, err); catch != nil {
_ = c.SendStatus(StatusInternalServerError) //nolint:errcheck // Always return nil
}
}
// emitSkipCustom is the CustomCtx counterpart of emitSkip; Append allocates here.
func (app *App) emitSkipCustom(c CustomCtx, allowMask uint64, err error) {
if allowMask != 0 {
methods := app.config.RequestMethods
for i := range methods {
if allowMask&(uint64(1)<<i) != 0 {
c.Append(HeaderAllow, methods[i])
}
}
}
if catch := app.ErrorHandler(c, err); catch != nil {
_ = c.SendStatus(StatusInternalServerError) //nolint:errcheck // Always return nil
}
}