diff --git a/contrib/jmux/mux.go b/contrib/jmux/mux.go index 8fed2bc12f4df8686129254ae37f41681946c10b..5c3be7dc5708af585c419c7edbcaa219f97004f5 100644 --- a/contrib/jmux/mux.go +++ b/contrib/jmux/mux.go @@ -13,6 +13,9 @@ import ( var _ Router = &Mux{} +const sepRune = '_' +const sepString = string(sepRune) + // Mux is a simple JRPC route multiplexer that parses a request path, // records any URL params, and executes an end handler. It implements // the Handler interface and is friendly with the standard library. @@ -277,10 +280,10 @@ func (mx *Mux) Mount(pattern string, handler codec.Handler) { handler.ServeRPC(w, r) }) - if pattern == "" || pattern[len(pattern)-1] != '_' { + if pattern == "" || pattern[len(pattern)-1] != sepRune { mx.handle(pattern, mountHandler) - mx.handle(pattern+"_", mountHandler) - pattern += "_" + mx.handle(pattern+sepString, mountHandler) + pattern += sepString } n := mx.handle(pattern+"*", mountHandler) diff --git a/contrib/jmux/router_context.go b/contrib/jmux/router_context.go index 7b851fdecd9d401ccfdab9b83c2fdcf3441657e1..cd1eb2dcccb095f78d4a346ea9047a4c443869f0 100644 --- a/contrib/jmux/router_context.go +++ b/contrib/jmux/router_context.go @@ -122,16 +122,16 @@ func (x *Context) MethodParam(key string) string { func (x *Context) RoutePattern() string { routePattern := strings.Join(x.RoutePatterns, "") routePattern = replaceWildcards(routePattern) - routePattern = strings.TrimSuffix(routePattern, "__") - routePattern = strings.TrimSuffix(routePattern, "_") + routePattern = strings.TrimSuffix(routePattern, sepString + sepString) + routePattern = strings.TrimSuffix(routePattern, sepString) return routePattern } // replaceWildcards takes a route pattern and recursively replaces all // occurrences of "_*_" to "_". func replaceWildcards(p string) string { - if strings.Contains(p, "_*_") { - return replaceWildcards(strings.Replace(p, "_*_", "_", -1)) + if strings.Contains(p, sepString+"*"+sepString) { + return replaceWildcards(strings.Replace(p, sepString+"*"+sepString, sepString, -1)) } return p } @@ -155,5 +155,5 @@ type contextKey struct { } func (k *contextKey) String() string { - return "jsonrpc2 context value " + k.name + return "jrpc context value " + k.name } diff --git a/contrib/jmux/router_tree.go b/contrib/jmux/router_tree.go index 2b7a2f1ac3badc6b9aa6351fbf429343c5497e44..b9e9e4207aa11520574f78f5fbfd988a20f73898 100644 --- a/contrib/jmux/router_tree.go +++ b/contrib/jmux/router_tree.go @@ -549,7 +549,7 @@ func patNextSegment(pattern string) (nodeTyp, string, string, byte, int, int) { panic("rpc: wildcard '*' must be the last pattern in a route, otherwise use a '{param}'") } - var tail byte = '_' // Default endpoint tail to _ byte + var tail byte = sepRune // Default endpoint tail to _ byte if ps >= 0 { // Param/Regexp pattern is next @@ -648,11 +648,11 @@ func (ns nodes) Len() int { return len(ns) } func (ns nodes) Swap(i, j int) { ns[i], ns[j] = ns[j], ns[i] } func (ns nodes) Less(i, j int) bool { return ns[i].label < ns[j].label } -// tailSort pushes nodes with '/' as the tail to the end of the list for param nodes. +// tailSort pushes nodes with sepRune as the tail to the end of the list for param nodes. // The list order determines the traversal order. func (ns nodes) tailSort() { for i := len(ns) - 1; i >= 0; i-- { - if ns[i].typ > ntStatic && ns[i].tail == '_' { + if ns[i].typ > ntStatic && ns[i].tail == sepRune { ns.Swap(i, len(ns)-1) return } @@ -708,8 +708,8 @@ func walk(r Routes, walkFn WalkFunc, parentRoute string, parentMw ...func(codec. } handler := route.Handler - fullRoute := parentRoute + "_" + route.Pattern - fullRoute = strings.Replace(fullRoute, "_*_", "_", -1) + fullRoute := parentRoute + sepString + route.Pattern + fullRoute = strings.Replace(fullRoute, sepString+"*"+sepString, sepString, -1) if chain, ok := handler.(*ChainHandler); ok { if err := walkFn(fullRoute, chain.Endpoint, append(mws, chain.Middlewares...)...); err != nil {