diff --git a/MiniHTTP/Routing/CORS.cs b/MiniHTTP/Routing/CORS.cs index 109ee02..a132fe7 100644 --- a/MiniHTTP/Routing/CORS.cs +++ b/MiniHTTP/Routing/CORS.cs @@ -11,7 +11,7 @@ public class CORS : IRouter { } - public Task Handle(HttpRequest client, ArraySegment path, ParameterCollection? baseParameters) + public Task Handle(HttpRequest client, ArraySegment path, VariableCollection? baseParameters) { if (client.Method != HttpMethod.OPTIONS) return Task.FromResult(null); diff --git a/MiniHTTP/Routing/EndpointHandler.cs b/MiniHTTP/Routing/EndpointHandler.cs index 514c983..5ed05f9 100644 --- a/MiniHTTP/Routing/EndpointHandler.cs +++ b/MiniHTTP/Routing/EndpointHandler.cs @@ -6,14 +6,14 @@ namespace MiniHTTP.Routing; /// An asynchronous endpoint handler. /// /// The request being served. -/// Parameters in the path. +/// Variables provided in the path. /// Returns a HTTP response if the request successfully hit. Returns null if the request misses the handler. -public delegate Task EndpointHandlerAsync(HttpRequest request, ParameterCollection parameters); +public delegate Task EndpointHandlerAsync(HttpRequest request, VariableCollection variables); /// /// An synchronous endpoint handler. /// /// The request being served. -/// Parameters in the path. +/// Variables provided in the path. /// Returns a HTTP response if the request successfully hit. Returns null if the request misses the handler. -public delegate HttpResponse? EndpointHandler(HttpRequest request, ParameterCollection parameters); \ No newline at end of file +public delegate HttpResponse? EndpointHandler(HttpRequest request, VariableCollection variables); \ No newline at end of file diff --git a/MiniHTTP/Routing/IRouter.cs b/MiniHTTP/Routing/IRouter.cs index d72e9f8..a4d8d39 100644 --- a/MiniHTTP/Routing/IRouter.cs +++ b/MiniHTTP/Routing/IRouter.cs @@ -7,5 +7,5 @@ namespace MiniHTTP.Routing; /// public interface IRouter { - Task Handle(HttpRequest client, ArraySegment path, ParameterCollection? baseParameters); + Task Handle(HttpRequest client, ArraySegment path, VariableCollection? baseParameters); } diff --git a/MiniHTTP/Routing/ParameterCollection.cs b/MiniHTTP/Routing/ParameterCollection.cs deleted file mode 100644 index b441419..0000000 --- a/MiniHTTP/Routing/ParameterCollection.cs +++ /dev/null @@ -1,58 +0,0 @@ -namespace MiniHTTP.Routing; - -/// -/// A map of arguments passed as parameters in a HTTP path. -/// -/// -/// -/// Routes may contain parameters, denoted by a colon at the beginning. When a request is made, whatever is provided in that space becomes available in the parameter collection as an argument. -/// If you have a route such as /far/:example/bar, and a client requests the path /foo/quux/bar, then the argument passed to the "example" parameter will be "quux". -/// -/// -/// Arguments can be obtained from the collection like a . For example: -/// -/// string id = parameters["id"]; -/// -/// -/// -public class ParameterCollection -{ - public static readonly ParameterCollection Empty = new ParameterCollection(); - - /// - /// A parameter collection to read from if this instance fails to fulfill a lookup. - /// - public readonly ParameterCollection? Fallback; - - readonly Dictionary Parameters = new Dictionary(StringComparer.OrdinalIgnoreCase); - - public string? this[string key] - { - get - { - if (Parameters.TryGetValue(key, out var value)) - return value; - else if (Fallback == null) - return null; - else - return Fallback[key]; - } - set - { - if (value == null) - Parameters.Remove(key); - else - Parameters[key] = value; - } - } - - public ParameterCollection(ParameterCollection? fallback = null) - { - Fallback = fallback; - } - - public void Clear() - { - Parameters.Clear(); - } -} diff --git a/MiniHTTP/Routing/Router.cs b/MiniHTTP/Routing/Router.cs index 5c69f58..2e503c5 100644 --- a/MiniHTTP/Routing/Router.cs +++ b/MiniHTTP/Routing/Router.cs @@ -9,13 +9,13 @@ public class Router : IRouter { struct Route { - public string[] Paths; + public string[] Path; public IRouter Router; public Route(string[] paths, IRouter router) { - Paths = paths; + Path = paths; Router = router; } } @@ -42,22 +42,22 @@ public class Router : IRouter Endpoint = (client, parameters) => Task.FromResult(endpoint(client, parameters)); } - internal async Task Handle(HttpRequest client, ArraySegment path, ParameterCollection? baseParameters) + internal async Task Handle(HttpRequest client, ArraySegment path, VariableCollection? baseParameters) { if (Method != HttpMethod.Any && client.Method != Method) return null; - ParameterCollection parameters = new ParameterCollection(baseParameters); + VariableCollection parameters = new VariableCollection(baseParameters); foreach (var route in Routes) { - if (route.Paths.Length != 1 || route.Paths[0] != "*") + if (route.Path.Length != 1 || route.Path[0] != "*") { - if (route.Paths.Length > path.Count) + if (route.Path.Length > path.Count) continue; - for (int i = 0; i < route.Paths.Length; i++) + for (int i = 0; i < route.Path.Length; i++) { - string key = route.Paths[i]; + string key = route.Path[i]; if (key.Length == 0) { if (path[i].Length == 0) @@ -68,7 +68,7 @@ public class Router : IRouter if (key[0] == ':') { - parameters[route.Paths[i][1..]] = path[i]; + parameters[route.Path[i][1..]] = path[i]; continue; } @@ -77,7 +77,7 @@ public class Router : IRouter } } - HttpResponse? resp = await route.Router.Handle(client, path.Slice(route.Paths.Length), parameters); + HttpResponse? resp = await route.Router.Handle(client, path.Slice(route.Path.Length), parameters); if (resp != null) return resp; @@ -88,10 +88,10 @@ public class Router : IRouter if (Endpoint == null) return null; else - return await Endpoint(client, baseParameters ?? ParameterCollection.Empty); + return await Endpoint(client, baseParameters ?? VariableCollection.Empty); } - Task IRouter.Handle(HttpRequest client, ArraySegment path, ParameterCollection? baseParameters) + Task IRouter.Handle(HttpRequest client, ArraySegment path, VariableCollection? baseParameters) => Handle(client, path, baseParameters); /// diff --git a/MiniHTTP/Routing/Static.cs b/MiniHTTP/Routing/Static.cs index dfd0d76..a59f470 100644 --- a/MiniHTTP/Routing/Static.cs +++ b/MiniHTTP/Routing/Static.cs @@ -70,7 +70,7 @@ public partial class Static : IRouter AssetParameter = assetParameter; } - async Task IRouter.Handle(HttpRequest client, ArraySegment path, ParameterCollection? baseParameters) + async Task IRouter.Handle(HttpRequest client, ArraySegment path, VariableCollection? baseParameters) { if (baseParameters == null) return null; diff --git a/MiniHTTP/Routing/VariableCollection.cs b/MiniHTTP/Routing/VariableCollection.cs new file mode 100644 index 0000000..7cea356 --- /dev/null +++ b/MiniHTTP/Routing/VariableCollection.cs @@ -0,0 +1,58 @@ +namespace MiniHTTP.Routing; + +/// +/// A map of arguments passed as variables in a HTTP path. +/// +/// +/// +/// Routes may contain variables, denoted by a colon at the beginning. When a request is made, whatever is provided in that space becomes available in the variable collection as an argument. +/// If you have a route such as /far/:example/bar, and a client requests the path /foo/quux/bar, then the argument passed to the "example" variable will be "quux". +/// +/// +/// Arguments can be obtained from the collection like a . For example: +/// +/// string id = variables["id"]; +/// +/// +/// +public class VariableCollection +{ + public static readonly VariableCollection Empty = new VariableCollection(); + + /// + /// A variable collection to read from if this instance fails to fulfill a lookup. + /// + public readonly VariableCollection? Fallback; + + readonly Dictionary Values = new Dictionary(StringComparer.OrdinalIgnoreCase); + + public string? this[string key] + { + get + { + if (Values.TryGetValue(key, out var value)) + return value; + else if (Fallback == null) + return null; + else + return Fallback[key]; + } + set + { + if (value == null) + Values.Remove(key); + else + Values[key] = value; + } + } + + public VariableCollection(VariableCollection? fallback = null) + { + Fallback = fallback; + } + + public void Clear() + { + Values.Clear(); + } +}