util/http: trie support method not allowed

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2022-12-27 23:47:11 +03:00
parent 1687b98b11
commit 5e47cc7e8c
2 changed files with 51 additions and 25 deletions

View File

@@ -7,6 +7,7 @@ package http
// Modified by Unistack LLC to support interface{} type handler and parameters in map[string]string
import (
"errors"
"fmt"
"net/http"
"regexp"
@@ -15,6 +16,11 @@ import (
"strings"
)
var (
ErrNotFound = errors.New("route not found")
ErrMethodNotAllowed = errors.New("method not allowed")
)
type methodTyp uint
const (
@@ -399,16 +405,19 @@ func (n *Trie) setEndpoint(method methodTyp, handler interface{}, pattern string
}
// Search try to find element in tree with path and method
func (n *Trie) Search(method string, path string) (interface{}, map[string]string, bool) {
func (n *Trie) Search(method string, path string) (interface{}, map[string]string, error) {
params := &routeParams{}
// Find the routing handlers for the path
rn := n.findRoute(params, methodMap[method], path)
if rn == nil {
return nil, nil, false
if rn == nil && !params.methodNotAllowed {
return nil, nil, ErrNotFound
}
if params.methodNotAllowed {
return nil, nil, ErrMethodNotAllowed
}
ep, ok := rn.endpoints[methodMap[method]]
if !ok {
return nil, nil, false
return nil, nil, ErrMethodNotAllowed
}
eparams := make(map[string]string, len(params.keys))
@@ -416,12 +425,13 @@ func (n *Trie) Search(method string, path string) (interface{}, map[string]strin
eparams[key] = params.vals[idx]
}
return ep.handler, eparams, true
return ep.handler, eparams, nil
}
type routeParams struct {
keys []string
vals []string
keys []string
vals []string
methodNotAllowed bool
}
// Recursive edge traversal by checking all nodeTyp groups along the way.
@@ -495,6 +505,7 @@ func (n *Trie) findRoute(params *routeParams, method methodTyp, path string) *Tr
params.keys = append(params.keys, h.paramKeys...)
return xn
}
params.methodNotAllowed = true
}
}
@@ -530,6 +541,7 @@ func (n *Trie) findRoute(params *routeParams, method methodTyp, path string) *Tr
params.keys = append(params.keys, h.paramKeys...)
return xn
}
params.methodNotAllowed = true
}
}