38 lines
1.3 KiB
C#
38 lines
1.3 KiB
C#
namespace Uwaa.HTTP.Routing;
|
|
|
|
/// <summary>
|
|
/// Parses a path and generates or acquires a response.
|
|
/// </summary>
|
|
public abstract class RouterBase
|
|
{
|
|
/// <summary>
|
|
/// Which HTTP method the router will respond to.
|
|
/// </summary>
|
|
public abstract HttpMethod Method { get; }
|
|
|
|
/// <summary>
|
|
/// The minimum number of path segments required to produce a response.
|
|
/// </summary>
|
|
public abstract int Arguments { get; }
|
|
|
|
public Task<HttpResponse?> GetResponse(HttpRequest req, HttpClientInfo info, ArraySegment<string> path)
|
|
{
|
|
HttpMethod method = Method;
|
|
if (method != HttpMethod.Any && req.Method != method)
|
|
return Task.FromResult<HttpResponse?>(null);
|
|
|
|
int arguments = Arguments;
|
|
if (path.Count < arguments)
|
|
return Task.FromResult<HttpResponse?>(null);
|
|
|
|
return GetResponseInner(req, info, path);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inline method for routing.
|
|
/// </summary>
|
|
/// <param name="req">The request for which the response is being generated.</param>
|
|
/// <param name="info">Information about the client.</param>
|
|
/// <param name="path">The path segments relevant to the router.</param>
|
|
protected abstract Task<HttpResponse?> GetResponseInner(HttpRequest req, HttpClientInfo info, ArraySegment<string> path);
|
|
}
|