From fed62d4a1e425e839378cc608d4ff470c8011379 Mon Sep 17 00:00:00 2001 From: Ralf Haferkamp Date: Thu, 1 Sep 2022 16:06:27 +0200 Subject: [PATCH] Evaluate routing rules ordered by path-length This is a quickfix for #4497. Before evaluating, we now sort the rules of a specific type by the length of the endpoints and start evaluation with the most specific endpoint first. There's obviously quite a bit room for optimization here and this will only fix the issue for routes of type `PrefixRoute`. But it should solve the immediate issue. --- services/proxy/pkg/router/router.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/services/proxy/pkg/router/router.go b/services/proxy/pkg/router/router.go index 80e1a720db..7becfff7fe 100644 --- a/services/proxy/pkg/router/router.go +++ b/services/proxy/pkg/router/router.go @@ -5,6 +5,7 @@ import ( "net/http" "net/url" "regexp" + "sort" "strings" "github.com/owncloud/ocis/v2/ocis-pkg/log" @@ -205,9 +206,16 @@ func (rt Router) Route(r *http.Request) (RoutingInfo, bool) { // use specific method method = r.Method } - for endpoint := range rt.directors[pol][rtype][method] { - if handler(endpoint, *r.URL) { + endpoints := make([]string, 0, len(rt.directors[pol][rtype][method])) + for endpoint := range rt.directors[pol][rtype][method] { + endpoints = append(endpoints, endpoint) + } + sort.Slice(endpoints, func(i, j int) bool { + return len(endpoints[j]) < len(endpoints[i]) + }) + for _, endpoint := range endpoints { + if handler(endpoint, *r.URL) { rt.logger.Debug(). Str("policy", pol). Str("method", r.Method).