aboutsummaryrefslogtreecommitdiff
path: root/plugins/VNLib.Plugins.Essentials.Content.Routing/src/RouteComparer.cs
blob: 189da622b7bcb7c490236ab57faafc4ca3efcde7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Copyright (c) 2022 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Essentials.Content.Routing
* File: RouteComparer.cs 
*
* RouteComparer.cs is part of VNLib.Plugins.Essentials.Content.Routing which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Essentials.Content.Routing is free software: you can redistribute it and/or modify 
* it under the terms of the GNU Affero General Public License as 
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* VNLib.Plugins.Essentials.Content.Routing is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see https://www.gnu.org/licenses/.
*/

using System.Collections.Generic;

using VNLib.Plugins.Essentials.Content.Routing.Model;

using static VNLib.Plugins.Essentials.Accounts.AccountManager;

namespace VNLib.Plugins.Essentials.Content.Routing
{
    /// <summary>
    /// Sorts routing rules based on closest match path/hostname routing along with privilage priority
    /// </summary>
    internal class RouteComparer : IComparer<Route>
    {
        //The idea is that hostnames without wildcards are exact, and hostnames with wildcards are "catch all"
        public int Compare(Route x, Route y)
        {
            int val = 0;
            //If x contains a wildcard in the hostname, then it is less than y
            if (x.Hostname.Contains('*'))
            {
                val--;
            }
            //If y containts a wildcard, then y is less than x
            if (y.Hostname.Contains('*'))
            {
                val++;
            }
            //If there was no wildcard, check paths
            if (val == 0)
            {
                //If x containts a wildcard in the path, then x is less than y
                if (x.MatchPath.Contains('*'))
                {
                    val--;
                }
                //If y containts a wildcard in the path, then y is less than x
                if (y.MatchPath.Contains('*'))
                {
                    val++;

                }
            }
            //If hostnames and paths are stil equal, check privilage level
            if (val == 0)
            {
                //Higher privilage routine is greater than lower privilage
                val = (x.Privilage & LEVEL_MSK) > (y.Privilage & LEVEL_MSK) ? 1 : -1;
            }
            //If both contain (or are) wildcards, then they are equal
            return val;
        }
    }
}