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

View file

@ -9,15 +9,17 @@ public class Router : IRouter
{
struct Route
{
public string[] Path;
public string Path;
public IRouter Router;
public Route(string[] paths, IRouter router)
public Route(string path, IRouter router)
{
Path = paths;
Path = path;
Router = router;
}
public override string ToString() => Path;
}
/// <summary>
@ -47,43 +49,37 @@ public class Router : IRouter
if (Method != HttpMethod.Any && client.Method != Method)
return null;
if (Routes.Count > 0)
{
VariableCollection parameters = new VariableCollection(baseParameters);
foreach (var route in Routes)
{
if (route.Path.Length != 1 || route.Path[0] != "*")
if (route.Path != "*")
{
if (route.Path.Length > path.Count)
continue;
string key = route.Path;
for (int i = 0; i < route.Path.Length; i++)
if (key.Length > 0 && key[0] == ':')
{
string key = route.Path[i];
if (key.Length == 0)
{
if (path[i].Length == 0)
continue;
parameters[route.Path[1..]] = path[0];
}
else
goto skip;
}
if (key[0] == ':')
{
parameters[route.Path[i][1..]] = path[i];
continue;
}
if (!key.Equals(path[i], StringComparison.OrdinalIgnoreCase))
if (!key.Equals(path[0], StringComparison.OrdinalIgnoreCase))
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)
return resp;
}
skip:
parameters.Clear();
}
}
if (Endpoint == null)
return null;
@ -97,30 +93,26 @@ public class Router : IRouter
/// <summary>
/// Adds a new route to the router.
/// </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>
public void Use(string path, IRouter route)
public void Use(string pathSegment, IRouter route)
{
string[] pathParams = path.Split('/', StringSplitOptions.TrimEntries);
Routes.Add(new Route(pathParams, route));
Routes.Add(new Route(pathSegment, route));
}
/// <summary>
/// Adds an endpoint to the router.
/// </summary>
/// <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>
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)"/>
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>
/// Adds a static file route to the router.
/// </summary>
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));
public void Static(string directory) => Routes.Add(new Route(":asset", new Static(directory, "asset")));
}