Skip to content

Commit

Permalink
Match ns prefix by longest match, not first match
Browse files Browse the repository at this point in the history
Previously, when two namespaces like /chtc and /chtc/PUBLIC existed in the cache,
the matchPrefix function wound up matching whichever happened to be read first.
Now it matches against the longest prefix, so a request for path
/chtc/PUBLIC/foo/bar will always match /chtc/PUBLIC.

Closes issue PelicanPlatform#140
  • Loading branch information
jhiemstrawisc committed Sep 14, 2023
1 parent ae00643 commit 1b9c2c2
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions director/cache_ads.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,39 @@ func UpdateLatLong(ad *ServerAd) error {
}

func matchesPrefix(reqPath string, namespaceAds []NamespaceAd) *NamespaceAd {
var best *NamespaceAd
for _, namespace := range namespaceAds {
serverPath := namespace.Path
if strings.Compare(serverPath, reqPath) == 0 {
return &namespace
}

// Some namespaces in Topology already have the trailing /, some don't
// Perhaps this should be standardized, but in case it isn't we need to
// handle it
if serverPath[len(serverPath)-1:] != "/" {
serverPath += "/"
}
if strings.HasPrefix(reqPath, serverPath) {
return &namespace

// The assignment of best doesn't account for the trailing / that we need to consider
// Account for that by setting up a tmpBest string that adds the / if needed
var tmpBest string
if best != nil {
tmpBest = best.Path
if tmpBest[len(tmpBest)-1:] != "/" {
tmpBest += "/"
}
}

// Make the len comparison with tmpBest, becasue serverPath is one char longer now
if strings.HasPrefix(reqPath, serverPath) && len(serverPath) > len(tmpBest) {
if best == nil {
best = new(NamespaceAd)
}
*best = namespace
}
}
return nil
return best
}

func GetAdsForPath(reqPath string) (originNamespace NamespaceAd, originAds []ServerAd, cacheAds []ServerAd) {
Expand Down

0 comments on commit 1b9c2c2

Please sign in to comment.