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.
This commit is contained in:
Ralf Haferkamp
2022-09-01 16:06:27 +02:00
committed by Ralf Haferkamp
parent 142bf659af
commit fed62d4a1e

View File

@@ -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).