mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-02-03 08:51:11 +08:00
refactor: Improve engine's tree processing and context handling.
This commit is contained in:
parent
ee0ebc986c
commit
904aea5df8
3 changed files with 28 additions and 25 deletions
|
|
@ -432,9 +432,8 @@ func MethodNotAllowed() HandlerFunc {
|
|||
// 如果是 OPTIONS 请求,尝试查找所有允许的方法
|
||||
allowedMethods := []string{}
|
||||
for _, treeIter := range engine.methodTrees {
|
||||
var tempSkippedNodes []skippedNode
|
||||
// 注意这里 treeIter.root 才是正确的,因为 treeIter 是 methodTree 类型
|
||||
value := treeIter.root.getValue(requestPath, nil, &tempSkippedNodes, false)
|
||||
value := treeIter.root.getValue(requestPath, nil, &c.SkippedNodes, false)
|
||||
if value.handlers != nil {
|
||||
allowedMethods = append(allowedMethods, treeIter.method)
|
||||
}
|
||||
|
|
@ -451,9 +450,8 @@ func MethodNotAllowed() HandlerFunc {
|
|||
if treeIter.method == httpMethod { // 已经处理过当前方法,跳过
|
||||
continue
|
||||
}
|
||||
var tempSkippedNodes []skippedNode // 用于临时查找,不影响主 Context
|
||||
// 注意这里 treeIter.root 才是正确的,因为 treeIter 是 methodTree 类型
|
||||
value := treeIter.root.getValue(requestPath, nil, &tempSkippedNodes, false) // 只查找是否存在,不需要参数
|
||||
value := treeIter.root.getValue(requestPath, nil, &c.SkippedNodes, false) // 只查找是否存在,不需要参数
|
||||
if value.handlers != nil {
|
||||
// 使用定义的ErrorHandle处理
|
||||
engine.errorHandle.handler(c, http.StatusMethodNotAllowed, errors.New("method not allowed"))
|
||||
|
|
@ -661,9 +659,8 @@ func (engine *Engine) handleRequest(c *Context) {
|
|||
// 查找匹配的节点和处理函数
|
||||
// 这里传递 &c.Params 而不是重新创建,以利用 Context 中预分配的容量
|
||||
// skippedNodes 内部使用,因此无需从外部传入已分配的 slice
|
||||
var skippedNodes []skippedNode // 用于回溯的跳过节点
|
||||
// 直接在 rootNode 上调用 getValue 方法
|
||||
value := rootNode.getValue(requestPath, &c.Params, &skippedNodes, true) // unescape=true 对路径参数进行 URL 解码
|
||||
value := rootNode.getValue(requestPath, &c.Params, &c.SkippedNodes, true) // unescape=true 对路径参数进行 URL 解码
|
||||
|
||||
if value.handlers != nil {
|
||||
//c.handlers = engine.combineHandlers(engine.globalHandlers, value.handlers) // 组合全局中间件和路由处理函数
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue