simplify router a lil

This commit is contained in:
uwaa 2024-10-27 23:49:37 +00:00
parent e042d64e5e
commit bc8ce1a19a
2 changed files with 36 additions and 43 deletions

View file

@ -112,6 +112,7 @@ public sealed class HttpServer
while (client.Connected) while (client.Connected)
{ {
HttpRequest req = new HttpRequest(this, client, stream); HttpRequest req = new HttpRequest(this, client, stream);
HttpResponse? response;
try try
{ {
await req.ReadAllHeaders(); await req.ReadAllHeaders();
@ -126,7 +127,7 @@ public sealed class HttpServer
pathSpl = pathSpl.Slice(1); pathSpl = pathSpl.Slice(1);
//Execute //Execute
HttpResponse? response = await Router.Handle(req, pathSpl, null); response = await Router.Handle(req, pathSpl, null);
if (response != null) if (response != null)
await req.Write(response); await req.Write(response);
else else

View file

@ -9,15 +9,17 @@ public class Router : IRouter
{ {
struct Route struct Route
{ {
public string[] Path; public string Path;
public IRouter Router; public IRouter Router;
public Route(string[] paths, IRouter router) public Route(string path, IRouter router)
{ {
Path = paths; Path = path;
Router = router; Router = router;
} }
public override string ToString() => Path;
} }
/// <summary> /// <summary>
@ -47,43 +49,37 @@ public class Router : IRouter
if (Method != HttpMethod.Any && client.Method != Method) if (Method != HttpMethod.Any && client.Method != Method)
return null; return null;
if (Routes.Count > 0)
{
VariableCollection parameters = new VariableCollection(baseParameters); VariableCollection parameters = new VariableCollection(baseParameters);
foreach (var route in Routes) foreach (var route in Routes)
{ {
if (route.Path.Length != 1 || route.Path[0] != "*") if (route.Path != "*")
{ {
if (route.Path.Length > path.Count) string key = route.Path;
continue;
for (int i = 0; i < route.Path.Length; i++) if (key.Length > 0 && key[0] == ':')
{ {
string key = route.Path[i]; parameters[route.Path[1..]] = path[0];
if (key.Length == 0) }
{
if (path[i].Length == 0)
continue;
else else
goto skip;
}
if (key[0] == ':')
{ {
parameters[route.Path[i][1..]] = path[i]; if (!key.Equals(path[0], StringComparison.OrdinalIgnoreCase))
continue;
}
if (!key.Equals(path[i], StringComparison.OrdinalIgnoreCase))
goto skip; goto skip;
} }
} }
HttpResponse? resp = await route.Router.Handle(client, path.Slice(route.Path.Length), parameters); if (path.Count > 0)
{
HttpResponse? resp = await route.Router.Handle(client, path.Slice(1), parameters);
if (resp != null) if (resp != null)
return resp; return resp;
}
skip: skip:
parameters.Clear(); parameters.Clear();
} }
}
if (Endpoint == null) if (Endpoint == null)
return null; return null;
@ -97,30 +93,26 @@ public class Router : IRouter
/// <summary> /// <summary>
/// Adds a new route to the router. /// Adds a new route to the router.
/// </summary> /// </summary>
/// <param name="path">The subpath to the route.</param> /// <param name="pathSegment">The path segment to the route.</param>
/// <param name="route">The router to use.</param> /// <param name="route">The router to use.</param>
public void Use(string path, IRouter route) public void Use(string pathSegment, IRouter route)
{ {
string[] pathParams = path.Split('/', StringSplitOptions.TrimEntries); Routes.Add(new Route(pathSegment, route));
Routes.Add(new Route(pathParams, route));
} }
/// <summary> /// <summary>
/// Adds an endpoint to the router. /// Adds an endpoint to the router.
/// </summary> /// </summary>
/// <param name="method">The HTTP method to respond to.</param> /// <param name="method">The HTTP method to respond to.</param>
/// <param name="path">The subpath to the endpoint.</param> /// <param name="pathSegment">The subpath to the endpoint.</param>
/// <param name="endpoint">The endpoint handler which serves the request.</param> /// <param name="endpoint">The endpoint handler which serves the request.</param>
public void Add(HttpMethod method, string path, EndpointHandlerAsync endpoint) => Use(path, new Router(endpoint, method)); public void Add(HttpMethod method, string pathSegment, EndpointHandlerAsync endpoint) => Use(pathSegment, new Router(endpoint, method));
/// <inheritdoc cref="Add(string, EndpointHandlerAsync)"/> /// <inheritdoc cref="Add(string, EndpointHandlerAsync)"/>
public void Add(HttpMethod method, string path, EndpointHandler endpoint) => Use(path, new Router(endpoint, method)); public void Add(HttpMethod method, string pathSegment, EndpointHandler endpoint) => Use(pathSegment, new Router(endpoint, method));
/// <summary> /// <summary>
/// Adds a static file route to the router. /// Adds a static file route to the router.
/// </summary> /// </summary>
public void Static(string directory) => Routes.Add(new Route([":asset"], new Static(directory, "asset"))); public void Static(string directory) => Routes.Add(new Route(":asset", new Static(directory, "asset")));
/// <inheritdoc cref="Static(string)"/>
public void Static(string directory, string path, string assetParam) => Use(path, new Static(directory, assetParam));
} }