2024-05-23 21:44:31 +00:00
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
2017-02-18 22:00:46 +00:00
package echo
2020-01-09 20:02:56 +00:00
import (
2022-03-12 18:41:07 +00:00
"bytes"
2023-01-28 21:57:53 +00:00
"fmt"
2020-01-09 20:02:56 +00:00
"net/http"
)
2019-01-31 16:06:36 +00:00
2024-05-23 21:44:31 +00:00
// Router is the registry of all registered routes for an `Echo` instance for
// request matching and URL path parameter parsing.
type Router struct {
tree * node
routes map [ string ] * Route
echo * Echo
}
type node struct {
kind kind
label byte
prefix string
parent * node
staticChildren children
originalPath string
methods * routeMethods
paramChild * node
anyChild * node
paramsCount int
// isLeaf indicates that node does not have child routes
isLeaf bool
// isHandler indicates that node has at least one handler registered to it
isHandler bool
// notFoundHandler is handler registered with RouteNotFound method and is executed for 404 cases
notFoundHandler * routeMethod
}
type kind uint8
type children [ ] * node
type routeMethod struct {
ppath string
pnames [ ] string
handler HandlerFunc
}
type routeMethods struct {
connect * routeMethod
delete * routeMethod
get * routeMethod
head * routeMethod
options * routeMethod
patch * routeMethod
post * routeMethod
propfind * routeMethod
put * routeMethod
trace * routeMethod
report * routeMethod
anyOther map [ string ] * routeMethod
allowHeader string
}
2017-02-18 22:00:46 +00:00
const (
2021-03-20 21:40:23 +00:00
staticKind kind = iota
paramKind
anyKind
paramLabel = byte ( ':' )
anyLabel = byte ( '*' )
2017-02-18 22:00:46 +00:00
)
2022-08-13 14:14:26 +00:00
func ( m * routeMethods ) isHandler ( ) bool {
2021-05-29 22:25:30 +00:00
return m . connect != nil ||
m . delete != nil ||
m . get != nil ||
m . head != nil ||
m . options != nil ||
m . patch != nil ||
m . post != nil ||
m . propfind != nil ||
m . put != nil ||
m . trace != nil ||
2022-08-13 14:14:26 +00:00
m . report != nil ||
len ( m . anyOther ) != 0
// RouteNotFound/404 is not considered as a handler
2021-05-29 22:25:30 +00:00
}
2022-08-13 14:14:26 +00:00
func ( m * routeMethods ) updateAllowHeader ( ) {
2022-03-12 18:41:07 +00:00
buf := new ( bytes . Buffer )
buf . WriteString ( http . MethodOptions )
if m . connect != nil {
buf . WriteString ( ", " )
buf . WriteString ( http . MethodConnect )
}
if m . delete != nil {
buf . WriteString ( ", " )
buf . WriteString ( http . MethodDelete )
}
if m . get != nil {
buf . WriteString ( ", " )
buf . WriteString ( http . MethodGet )
}
if m . head != nil {
buf . WriteString ( ", " )
buf . WriteString ( http . MethodHead )
}
if m . patch != nil {
buf . WriteString ( ", " )
buf . WriteString ( http . MethodPatch )
}
if m . post != nil {
buf . WriteString ( ", " )
buf . WriteString ( http . MethodPost )
}
if m . propfind != nil {
buf . WriteString ( ", PROPFIND" )
}
if m . put != nil {
buf . WriteString ( ", " )
buf . WriteString ( http . MethodPut )
}
if m . trace != nil {
buf . WriteString ( ", " )
buf . WriteString ( http . MethodTrace )
}
if m . report != nil {
buf . WriteString ( ", REPORT" )
}
2022-08-13 14:14:26 +00:00
for method := range m . anyOther { // for simplicity, we use map and therefore order is not deterministic here
buf . WriteString ( ", " )
buf . WriteString ( method )
}
2022-03-12 18:41:07 +00:00
m . allowHeader = buf . String ( )
}
2017-02-18 22:00:46 +00:00
// NewRouter returns a new Router instance.
func NewRouter ( e * Echo ) * Router {
return & Router {
tree : & node {
2022-08-13 14:14:26 +00:00
methods : new ( routeMethods ) ,
2017-02-18 22:00:46 +00:00
} ,
2017-06-05 22:01:05 +00:00
routes : map [ string ] * Route { } ,
2017-02-18 22:00:46 +00:00
echo : e ,
}
}
2023-01-28 21:57:53 +00:00
// Routes returns the registered routes.
func ( r * Router ) Routes ( ) [ ] * Route {
routes := make ( [ ] * Route , 0 , len ( r . routes ) )
for _ , v := range r . routes {
routes = append ( routes , v )
}
return routes
}
2023-08-05 18:43:19 +00:00
// Reverse generates a URL from route name and provided parameters.
2023-01-28 21:57:53 +00:00
func ( r * Router ) Reverse ( name string , params ... interface { } ) string {
uri := new ( bytes . Buffer )
ln := len ( params )
n := 0
for _ , route := range r . routes {
if route . Name == name {
for i , l := 0 , len ( route . Path ) ; i < l ; i ++ {
2023-08-05 18:43:19 +00:00
hasBackslash := route . Path [ i ] == '\\'
if hasBackslash && i + 1 < l && route . Path [ i + 1 ] == ':' {
i ++ // backslash before colon escapes that colon. in that case skip backslash
}
if n < ln && ( route . Path [ i ] == '*' || ( ! hasBackslash && route . Path [ i ] == ':' ) ) {
// in case of `*` wildcard or `:` (unescaped colon) param we replace everything till next slash or end of path
2023-01-28 21:57:53 +00:00
for ; i < l && route . Path [ i ] != '/' ; i ++ {
}
uri . WriteString ( fmt . Sprintf ( "%v" , params [ n ] ) )
n ++
}
if i < l {
uri . WriteByte ( route . Path [ i ] )
}
}
break
}
}
return uri . String ( )
}
2024-05-23 21:44:31 +00:00
func normalizePathSlash ( path string ) string {
if path == "" {
path = "/"
} else if path [ 0 ] != '/' {
path = "/" + path
}
return path
}
2023-01-28 21:57:53 +00:00
func ( r * Router ) add ( method , path , name string , h HandlerFunc ) * Route {
2024-05-23 21:44:31 +00:00
path = normalizePathSlash ( path )
r . insert ( method , path , h )
2023-01-28 21:57:53 +00:00
route := & Route {
Method : method ,
Path : path ,
Name : name ,
}
r . routes [ method + path ] = route
return route
}
2017-02-18 22:00:46 +00:00
// Add registers a new route for method and path with matching handler.
func ( r * Router ) Add ( method , path string , h HandlerFunc ) {
2024-05-23 21:44:31 +00:00
r . insert ( method , normalizePathSlash ( path ) , h )
}
func ( r * Router ) insert ( method , path string , h HandlerFunc ) {
path = normalizePathSlash ( path )
2017-02-18 22:00:46 +00:00
pnames := [ ] string { } // Param names
2018-11-18 17:55:05 +00:00
ppath := path // Pristine path
2017-02-18 22:00:46 +00:00
2021-05-29 22:25:30 +00:00
if h == nil && r . echo . Logger != nil {
// FIXME: in future we should return error
r . echo . Logger . Errorf ( "Adding route without handler function: %v:%v" , method , path )
}
2021-03-20 21:40:23 +00:00
for i , lcpIndex := 0 , len ( path ) ; i < lcpIndex ; i ++ {
2017-02-18 22:00:46 +00:00
if path [ i ] == ':' {
2021-10-16 22:47:22 +00:00
if i > 0 && path [ i - 1 ] == '\\' {
2022-01-18 19:42:25 +00:00
path = path [ : i - 1 ] + path [ i : ]
i --
lcpIndex --
2021-10-16 22:47:22 +00:00
continue
}
2017-02-18 22:00:46 +00:00
j := i + 1
2024-05-23 21:44:31 +00:00
r . insertNode ( method , path [ : i ] , staticKind , routeMethod { } )
2021-03-20 21:40:23 +00:00
for ; i < lcpIndex && path [ i ] != '/' ; i ++ {
2017-02-18 22:00:46 +00:00
}
pnames = append ( pnames , path [ j : i ] )
path = path [ : j ] + path [ i : ]
2021-03-20 21:40:23 +00:00
i , lcpIndex = j , len ( path )
2017-02-18 22:00:46 +00:00
2021-03-20 21:40:23 +00:00
if i == lcpIndex {
2021-05-29 22:25:30 +00:00
// path node is last fragment of route path. ie. `/users/:id`
2024-05-23 21:44:31 +00:00
r . insertNode ( method , path [ : i ] , paramKind , routeMethod { ppath , pnames , h } )
2019-06-16 21:33:25 +00:00
} else {
2024-05-23 21:44:31 +00:00
r . insertNode ( method , path [ : i ] , paramKind , routeMethod { } )
2017-02-18 22:00:46 +00:00
}
} else if path [ i ] == '*' {
2024-05-23 21:44:31 +00:00
r . insertNode ( method , path [ : i ] , staticKind , routeMethod { } )
2017-02-18 22:00:46 +00:00
pnames = append ( pnames , "*" )
2024-05-23 21:44:31 +00:00
r . insertNode ( method , path [ : i + 1 ] , anyKind , routeMethod { ppath , pnames , h } )
2017-02-18 22:00:46 +00:00
}
}
2024-05-23 21:44:31 +00:00
r . insertNode ( method , path , staticKind , routeMethod { ppath , pnames , h } )
2017-02-18 22:00:46 +00:00
}
2024-05-23 21:44:31 +00:00
func ( r * Router ) insertNode ( method , path string , t kind , rm routeMethod ) {
2017-02-18 22:00:46 +00:00
// Adjust max param
2022-08-13 14:14:26 +00:00
paramLen := len ( rm . pnames )
2021-03-20 21:40:23 +00:00
if * r . echo . maxParam < paramLen {
* r . echo . maxParam = paramLen
2017-02-18 22:00:46 +00:00
}
2021-03-20 21:40:23 +00:00
currentNode := r . tree // Current node as root
if currentNode == nil {
2017-06-05 22:01:05 +00:00
panic ( "echo: invalid method" )
2017-02-18 22:00:46 +00:00
}
search := path
for {
2021-03-20 21:40:23 +00:00
searchLen := len ( search )
prefixLen := len ( currentNode . prefix )
lcpLen := 0
// LCP - Longest Common Prefix (https://en.wikipedia.org/wiki/LCP_array)
max := prefixLen
if searchLen < max {
max = searchLen
2017-02-18 22:00:46 +00:00
}
2021-03-20 21:40:23 +00:00
for ; lcpLen < max && search [ lcpLen ] == currentNode . prefix [ lcpLen ] ; lcpLen ++ {
2017-02-18 22:00:46 +00:00
}
2021-03-20 21:40:23 +00:00
if lcpLen == 0 {
2017-02-18 22:00:46 +00:00
// At root node
2021-03-20 21:40:23 +00:00
currentNode . label = search [ 0 ]
currentNode . prefix = search
2022-08-13 14:14:26 +00:00
if rm . handler != nil {
2021-03-20 21:40:23 +00:00
currentNode . kind = t
2022-08-13 14:14:26 +00:00
currentNode . addMethod ( method , & rm )
currentNode . paramsCount = len ( rm . pnames )
currentNode . originalPath = rm . ppath
2017-02-18 22:00:46 +00:00
}
2021-05-29 22:25:30 +00:00
currentNode . isLeaf = currentNode . staticChildren == nil && currentNode . paramChild == nil && currentNode . anyChild == nil
2021-03-20 21:40:23 +00:00
} else if lcpLen < prefixLen {
2022-08-13 14:14:26 +00:00
// Split node into two before we insert new node.
// This happens when we are inserting path that is submatch of any existing inserted paths.
// For example, we have node `/test` and now are about to insert `/te/*`. In that case
// 1. overlapping part is `/te` that is used as parent node
// 2. `st` is part from existing node that is not matching - it gets its own node (child to `/te`)
// 3. `/*` is the new part we are about to insert (child to `/te`)
2021-03-20 21:40:23 +00:00
n := newNode (
currentNode . kind ,
currentNode . prefix [ lcpLen : ] ,
currentNode ,
currentNode . staticChildren ,
2022-08-13 14:14:26 +00:00
currentNode . originalPath ,
currentNode . methods ,
currentNode . paramsCount ,
2021-03-20 21:40:23 +00:00
currentNode . paramChild ,
currentNode . anyChild ,
2022-08-13 14:14:26 +00:00
currentNode . notFoundHandler ,
2021-03-20 21:40:23 +00:00
)
2020-01-09 20:02:56 +00:00
// Update parent path for all children to new node
2021-03-20 21:40:23 +00:00
for _ , child := range currentNode . staticChildren {
2020-01-09 20:02:56 +00:00
child . parent = n
}
2021-03-20 21:40:23 +00:00
if currentNode . paramChild != nil {
currentNode . paramChild . parent = n
}
if currentNode . anyChild != nil {
currentNode . anyChild . parent = n
}
2020-01-09 20:02:56 +00:00
2017-02-18 22:00:46 +00:00
// Reset parent node
2021-03-20 21:40:23 +00:00
currentNode . kind = staticKind
currentNode . label = currentNode . prefix [ 0 ]
currentNode . prefix = currentNode . prefix [ : lcpLen ]
currentNode . staticChildren = nil
2022-08-13 14:14:26 +00:00
currentNode . originalPath = ""
currentNode . methods = new ( routeMethods )
currentNode . paramsCount = 0
2021-03-20 21:40:23 +00:00
currentNode . paramChild = nil
currentNode . anyChild = nil
2021-05-29 22:25:30 +00:00
currentNode . isLeaf = false
currentNode . isHandler = false
2022-08-13 14:14:26 +00:00
currentNode . notFoundHandler = nil
2021-03-20 21:40:23 +00:00
// Only Static children could reach here
currentNode . addStaticChild ( n )
if lcpLen == searchLen {
2017-02-18 22:00:46 +00:00
// At parent node
2021-03-20 21:40:23 +00:00
currentNode . kind = t
2022-08-13 14:14:26 +00:00
if rm . handler != nil {
currentNode . addMethod ( method , & rm )
currentNode . paramsCount = len ( rm . pnames )
currentNode . originalPath = rm . ppath
}
2017-02-18 22:00:46 +00:00
} else {
// Create child node
2022-08-13 14:14:26 +00:00
n = newNode ( t , search [ lcpLen : ] , currentNode , nil , "" , new ( routeMethods ) , 0 , nil , nil , nil )
if rm . handler != nil {
n . addMethod ( method , & rm )
n . paramsCount = len ( rm . pnames )
n . originalPath = rm . ppath
}
2021-03-20 21:40:23 +00:00
// Only Static children could reach here
currentNode . addStaticChild ( n )
2017-02-18 22:00:46 +00:00
}
2021-05-29 22:25:30 +00:00
currentNode . isLeaf = currentNode . staticChildren == nil && currentNode . paramChild == nil && currentNode . anyChild == nil
2021-03-20 21:40:23 +00:00
} else if lcpLen < searchLen {
search = search [ lcpLen : ]
c := currentNode . findChildWithLabel ( search [ 0 ] )
2017-02-18 22:00:46 +00:00
if c != nil {
// Go deeper
2021-03-20 21:40:23 +00:00
currentNode = c
2017-02-18 22:00:46 +00:00
continue
}
// Create child node
2022-08-13 14:14:26 +00:00
n := newNode ( t , search , currentNode , nil , rm . ppath , new ( routeMethods ) , 0 , nil , nil , nil )
if rm . handler != nil {
n . addMethod ( method , & rm )
n . paramsCount = len ( rm . pnames )
}
2021-03-20 21:40:23 +00:00
switch t {
case staticKind :
currentNode . addStaticChild ( n )
case paramKind :
currentNode . paramChild = n
case anyKind :
currentNode . anyChild = n
}
2021-05-29 22:25:30 +00:00
currentNode . isLeaf = currentNode . staticChildren == nil && currentNode . paramChild == nil && currentNode . anyChild == nil
2017-02-18 22:00:46 +00:00
} else {
// Node already exists
2022-08-13 14:14:26 +00:00
if rm . handler != nil {
currentNode . addMethod ( method , & rm )
currentNode . paramsCount = len ( rm . pnames )
currentNode . originalPath = rm . ppath
2017-02-18 22:00:46 +00:00
}
}
return
}
}
2022-08-13 14:14:26 +00:00
func newNode (
t kind ,
pre string ,
p * node ,
sc children ,
originalPath string ,
methods * routeMethods ,
paramsCount int ,
paramChildren ,
anyChildren * node ,
notFoundHandler * routeMethod ,
) * node {
2017-02-18 22:00:46 +00:00
return & node {
2022-08-13 14:14:26 +00:00
kind : t ,
label : pre [ 0 ] ,
prefix : pre ,
parent : p ,
staticChildren : sc ,
originalPath : originalPath ,
methods : methods ,
paramsCount : paramsCount ,
paramChild : paramChildren ,
anyChild : anyChildren ,
isLeaf : sc == nil && paramChildren == nil && anyChildren == nil ,
isHandler : methods . isHandler ( ) ,
notFoundHandler : notFoundHandler ,
2017-02-18 22:00:46 +00:00
}
}
2021-03-20 21:40:23 +00:00
func ( n * node ) addStaticChild ( c * node ) {
n . staticChildren = append ( n . staticChildren , c )
2017-02-18 22:00:46 +00:00
}
2021-03-20 21:40:23 +00:00
func ( n * node ) findStaticChild ( l byte ) * node {
for _ , c := range n . staticChildren {
if c . label == l {
2017-02-18 22:00:46 +00:00
return c
}
}
return nil
}
func ( n * node ) findChildWithLabel ( l byte ) * node {
2022-08-13 14:14:26 +00:00
if c := n . findStaticChild ( l ) ; c != nil {
return c
2017-02-18 22:00:46 +00:00
}
2021-03-20 21:40:23 +00:00
if l == paramLabel {
return n . paramChild
}
if l == anyLabel {
return n . anyChild
2017-02-18 22:00:46 +00:00
}
return nil
}
2022-08-13 14:14:26 +00:00
func ( n * node ) addMethod ( method string , h * routeMethod ) {
2017-02-18 22:00:46 +00:00
switch method {
2019-01-31 16:06:36 +00:00
case http . MethodConnect :
2022-08-13 14:14:26 +00:00
n . methods . connect = h
2019-01-31 16:06:36 +00:00
case http . MethodDelete :
2022-08-13 14:14:26 +00:00
n . methods . delete = h
2019-01-31 16:06:36 +00:00
case http . MethodGet :
2022-08-13 14:14:26 +00:00
n . methods . get = h
2019-01-31 16:06:36 +00:00
case http . MethodHead :
2022-08-13 14:14:26 +00:00
n . methods . head = h
2019-01-31 16:06:36 +00:00
case http . MethodOptions :
2022-08-13 14:14:26 +00:00
n . methods . options = h
2019-01-31 16:06:36 +00:00
case http . MethodPatch :
2022-08-13 14:14:26 +00:00
n . methods . patch = h
2019-01-31 16:06:36 +00:00
case http . MethodPost :
2022-08-13 14:14:26 +00:00
n . methods . post = h
2018-11-18 17:55:05 +00:00
case PROPFIND :
2022-08-13 14:14:26 +00:00
n . methods . propfind = h
2019-01-31 16:06:36 +00:00
case http . MethodPut :
2022-08-13 14:14:26 +00:00
n . methods . put = h
2019-01-31 16:06:36 +00:00
case http . MethodTrace :
2022-08-13 14:14:26 +00:00
n . methods . trace = h
2019-06-16 21:33:25 +00:00
case REPORT :
2022-08-13 14:14:26 +00:00
n . methods . report = h
case RouteNotFound :
n . notFoundHandler = h
return // RouteNotFound/404 is not considered as a handler so no further logic needs to be executed
default :
if n . methods . anyOther == nil {
n . methods . anyOther = make ( map [ string ] * routeMethod )
}
if h . handler == nil {
delete ( n . methods . anyOther , method )
} else {
n . methods . anyOther [ method ] = h
}
2017-02-18 22:00:46 +00:00
}
2021-05-29 22:25:30 +00:00
2022-08-13 14:14:26 +00:00
n . methods . updateAllowHeader ( )
n . isHandler = true
2017-02-18 22:00:46 +00:00
}
2022-08-13 14:14:26 +00:00
func ( n * node ) findMethod ( method string ) * routeMethod {
2017-02-18 22:00:46 +00:00
switch method {
2019-01-31 16:06:36 +00:00
case http . MethodConnect :
2022-08-13 14:14:26 +00:00
return n . methods . connect
2019-01-31 16:06:36 +00:00
case http . MethodDelete :
2022-08-13 14:14:26 +00:00
return n . methods . delete
2019-01-31 16:06:36 +00:00
case http . MethodGet :
2022-08-13 14:14:26 +00:00
return n . methods . get
2019-01-31 16:06:36 +00:00
case http . MethodHead :
2022-08-13 14:14:26 +00:00
return n . methods . head
2019-01-31 16:06:36 +00:00
case http . MethodOptions :
2022-08-13 14:14:26 +00:00
return n . methods . options
2019-01-31 16:06:36 +00:00
case http . MethodPatch :
2022-08-13 14:14:26 +00:00
return n . methods . patch
2019-01-31 16:06:36 +00:00
case http . MethodPost :
2022-08-13 14:14:26 +00:00
return n . methods . post
2018-11-18 17:55:05 +00:00
case PROPFIND :
2022-08-13 14:14:26 +00:00
return n . methods . propfind
2019-01-31 16:06:36 +00:00
case http . MethodPut :
2022-08-13 14:14:26 +00:00
return n . methods . put
2019-01-31 16:06:36 +00:00
case http . MethodTrace :
2022-08-13 14:14:26 +00:00
return n . methods . trace
2019-06-16 21:33:25 +00:00
case REPORT :
2022-08-13 14:14:26 +00:00
return n . methods . report
default : // RouteNotFound/404 is not considered as a handler
return n . methods . anyOther [ method ]
2017-02-18 22:00:46 +00:00
}
}
2022-03-12 18:41:07 +00:00
func optionsMethodHandler ( allowMethods string ) func ( c Context ) error {
return func ( c Context ) error {
// Note: we are not handling most of the CORS headers here. CORS is handled by CORS middleware
// 'OPTIONS' method RFC: https://httpwg.org/specs/rfc7231.html#OPTIONS
// 'Allow' header RFC: https://datatracker.ietf.org/doc/html/rfc7231#section-7.4.1
c . Response ( ) . Header ( ) . Add ( HeaderAllow , allowMethods )
return c . NoContent ( http . StatusNoContent )
2017-02-18 22:00:46 +00:00
}
}
// Find lookup a handler registered for method and path. It also parses URL for path
// parameters and load them into context.
//
// For performance:
//
// - Get context from `Echo#AcquireContext()`
// - Reset it `Context#Reset()`
// - Return it `Echo#ReleaseContext()`.
2017-06-05 22:01:05 +00:00
func ( r * Router ) Find ( method , path string , c Context ) {
ctx := c . ( * context )
2021-03-20 21:40:23 +00:00
currentNode := r . tree // Current node as root
2017-02-18 22:00:46 +00:00
var (
2021-05-29 22:25:30 +00:00
previousBestMatchNode * node
2022-08-13 14:14:26 +00:00
matchedRouteMethod * routeMethod
2021-03-20 21:40:23 +00:00
// search stores the remaining path to check for match. By each iteration we move from start of path to end of the path
// and search value gets shorter and shorter.
search = path
searchIndex = 0
paramIndex int // Param counter
paramValues = ctx . pvalues // Use the internal slice so the interface can keep the illusion of a dynamic slice
2017-02-18 22:00:46 +00:00
)
2021-03-20 21:40:23 +00:00
// Backtracking is needed when a dead end (leaf node) is reached in the router tree.
// To backtrack the current node will be changed to the parent node and the next kind for the
// router logic will be returned based on fromKind or kind of the dead end node (static > param > any).
// For example if there is no static node match we should check parent next sibling by kind (param).
// Backtracking itself does not check if there is a next sibling, this is done by the router logic.
backtrackToNextNodeKind := func ( fromKind kind ) ( nextNodeKind kind , valid bool ) {
previous := currentNode
currentNode = previous . parent
valid = currentNode != nil
// Next node type by priority
2021-05-29 22:25:30 +00:00
if previous . kind == anyKind {
nextNodeKind = staticKind
} else {
nextNodeKind = previous . kind + 1
}
2021-03-20 21:40:23 +00:00
if fromKind == staticKind {
// when backtracking is done from static kind block we did not change search so nothing to restore
return
2017-02-18 22:00:46 +00:00
}
2021-03-20 21:40:23 +00:00
// restore search to value it was before we move to current node we are backtracking from.
if previous . kind == staticKind {
searchIndex -= len ( previous . prefix )
} else {
paramIndex --
// for param/any node.prefix value is always `:` so we can not deduce searchIndex from that and must use pValue
// for that index as it would also contain part of path we cut off before moving into node we are backtracking from
searchIndex -= len ( paramValues [ paramIndex ] )
2021-05-29 22:25:30 +00:00
paramValues [ paramIndex ] = ""
2021-03-20 21:40:23 +00:00
}
search = path [ searchIndex : ]
return
}
2017-02-18 22:00:46 +00:00
2021-03-20 21:40:23 +00:00
// Router tree is implemented by longest common prefix array (LCP array) https://en.wikipedia.org/wiki/LCP_array
// Tree search is implemented as for loop where one loop iteration is divided into 3 separate blocks
// Each of these blocks checks specific kind of node (static/param/any). Order of blocks reflex their priority in routing.
// Search order/priority is: static > param > any.
//
// Note: backtracking in tree is implemented by replacing/switching currentNode to previous node
// and hoping to (goto statement) next block by priority to check if it is the match.
for {
prefixLen := 0 // Prefix length
lcpLen := 0 // LCP (longest common prefix) length
if currentNode . kind == staticKind {
searchLen := len ( search )
prefixLen = len ( currentNode . prefix )
2017-02-18 22:00:46 +00:00
2021-03-20 21:40:23 +00:00
// LCP - Longest Common Prefix (https://en.wikipedia.org/wiki/LCP_array)
max := prefixLen
if searchLen < max {
max = searchLen
2017-02-18 22:00:46 +00:00
}
2021-03-20 21:40:23 +00:00
for ; lcpLen < max && search [ lcpLen ] == currentNode . prefix [ lcpLen ] ; lcpLen ++ {
2017-02-18 22:00:46 +00:00
}
}
2021-03-20 21:40:23 +00:00
if lcpLen != prefixLen {
// No matching prefix, let's backtrack to the first possible alternative node of the decision path
nk , ok := backtrackToNextNodeKind ( staticKind )
if ! ok {
2022-08-13 14:14:26 +00:00
return // No other possibilities on the decision path, handler will be whatever context is reset to.
2021-03-20 21:40:23 +00:00
} else if nk == paramKind {
goto Param
// NOTE: this case (backtracking from static node to previous any node) can not happen by current any matching logic. Any node is end of search currently
//} else if nk == anyKind {
// goto Any
} else {
// Not found (this should never be possible for static node we are looking currently)
2021-05-29 22:25:30 +00:00
break
2020-05-23 22:06:21 +00:00
}
}
2021-03-20 21:40:23 +00:00
// The full prefix has matched, remove the prefix from the remaining search
search = search [ lcpLen : ]
searchIndex = searchIndex + lcpLen
2022-08-13 14:14:26 +00:00
// Finish routing if is no request path remaining to search
if search == "" {
// in case of node that is handler we have exact method type match or something for 405 to use
if currentNode . isHandler {
// check if current node has handler registered for http method we are looking for. we store currentNode as
// best matching in case we do no find no more routes matching this path+method
if previousBestMatchNode == nil {
previousBestMatchNode = currentNode
}
if h := currentNode . findMethod ( method ) ; h != nil {
matchedRouteMethod = h
break
}
} else if currentNode . notFoundHandler != nil {
matchedRouteMethod = currentNode . notFoundHandler
2021-05-29 22:25:30 +00:00
break
}
2017-02-18 22:00:46 +00:00
}
// Static node
2021-03-20 21:40:23 +00:00
if search != "" {
if child := currentNode . findStaticChild ( search [ 0 ] ) ; child != nil {
currentNode = child
continue
2017-02-18 22:00:46 +00:00
}
}
Param :
2020-05-23 22:06:21 +00:00
// Param node
2021-03-20 21:40:23 +00:00
if child := currentNode . paramChild ; search != "" && child != nil {
currentNode = child
2021-05-29 22:25:30 +00:00
i := 0
l := len ( search )
if currentNode . isLeaf {
2022-08-13 14:14:26 +00:00
// when param node does not have any children (path param is last piece of route path) then param node should
// act similarly to any node - consider all remaining search as match
2021-05-29 22:25:30 +00:00
i = l
} else {
for ; i < l && search [ i ] != '/' ; i ++ {
}
2017-02-18 22:00:46 +00:00
}
2021-05-29 22:25:30 +00:00
2021-03-20 21:40:23 +00:00
paramValues [ paramIndex ] = search [ : i ]
paramIndex ++
2017-02-18 22:00:46 +00:00
search = search [ i : ]
2021-03-20 21:40:23 +00:00
searchIndex = searchIndex + i
2017-02-18 22:00:46 +00:00
continue
}
Any :
2020-05-23 22:06:21 +00:00
// Any node
2021-03-20 21:40:23 +00:00
if child := currentNode . anyChild ; child != nil {
// If any node is found, use remaining path for paramValues
currentNode = child
2022-08-13 14:14:26 +00:00
paramValues [ currentNode . paramsCount - 1 ] = search
2021-05-29 22:25:30 +00:00
// update indexes/search in case we need to backtrack when no handler match is found
paramIndex ++
searchIndex += + len ( search )
search = ""
2022-08-13 14:14:26 +00:00
if h := currentNode . findMethod ( method ) ; h != nil {
matchedRouteMethod = h
break
}
// we store currentNode as best matching in case we do not find more routes matching this path+method. Needed for 405
2021-05-29 22:25:30 +00:00
if previousBestMatchNode == nil {
previousBestMatchNode = currentNode
}
2022-08-13 14:14:26 +00:00
if currentNode . notFoundHandler != nil {
matchedRouteMethod = currentNode . notFoundHandler
2021-05-29 22:25:30 +00:00
break
}
2020-05-23 22:06:21 +00:00
}
2021-03-20 21:40:23 +00:00
// Let's backtrack to the first possible alternative node of the decision path
nk , ok := backtrackToNextNodeKind ( anyKind )
if ! ok {
2021-05-29 22:25:30 +00:00
break // No other possibilities on the decision path
2021-03-20 21:40:23 +00:00
} else if nk == paramKind {
goto Param
} else if nk == anyKind {
goto Any
} else {
// Not found
2021-05-29 22:25:30 +00:00
break
2017-02-18 22:00:46 +00:00
}
}
2021-05-29 22:25:30 +00:00
if currentNode == nil && previousBestMatchNode == nil {
return // nothing matched at all
}
2017-02-18 22:00:46 +00:00
2022-08-13 14:14:26 +00:00
// matchedHandler could be method+path handler that we matched or notFoundHandler from node with matching path
// user provided not found (404) handler has priority over generic method not found (405) handler or global 404 handler
var rPath string
var rPNames [ ] string
if matchedRouteMethod != nil {
rPath = matchedRouteMethod . ppath
rPNames = matchedRouteMethod . pnames
ctx . handler = matchedRouteMethod . handler
2021-05-29 22:25:30 +00:00
} else {
// use previous match as basis. although we have no matching handler we have path match.
// so we can send http.StatusMethodNotAllowed (405) instead of http.StatusNotFound (404)
currentNode = previousBestMatchNode
2022-03-12 18:41:07 +00:00
2022-08-13 14:14:26 +00:00
rPath = currentNode . originalPath
rPNames = nil // no params here
2022-03-12 18:41:07 +00:00
ctx . handler = NotFoundHandler
2022-08-13 14:14:26 +00:00
if currentNode . notFoundHandler != nil {
rPath = currentNode . notFoundHandler . ppath
rPNames = currentNode . notFoundHandler . pnames
ctx . handler = currentNode . notFoundHandler . handler
} else if currentNode . isHandler {
ctx . Set ( ContextKeyHeaderAllow , currentNode . methods . allowHeader )
2022-03-12 18:41:07 +00:00
ctx . handler = MethodNotAllowedHandler
if method == http . MethodOptions {
2022-08-13 14:14:26 +00:00
ctx . handler = optionsMethodHandler ( currentNode . methods . allowHeader )
2022-03-12 18:41:07 +00:00
}
}
2017-02-18 22:00:46 +00:00
}
2022-08-13 14:14:26 +00:00
ctx . path = rPath
ctx . pnames = rPNames
2017-02-18 22:00:46 +00:00
}