Added BasicAuth, HostName Server, HTTPS (Maybe)

This commit is contained in:
Michael Nolan
2022-04-03 09:39:54 -05:00
parent 937a2e15c6
commit dc581343a4
6 changed files with 419 additions and 51 deletions
+111
View File
@@ -22,6 +22,11 @@ namespace Tesses.WebServer
public static class DajuricSimpleHttpExtensions
{
static void Deconstruct<T1, T2>(this KeyValuePair<T1, T2> tuple, out T1 key, out T2 value)
{
key = tuple.Key;
value = tuple.Value;
}
const string BYTES_RANGE_HEADER = "Range";
static bool ParseForm(this ServerContext ctx)
@@ -360,5 +365,111 @@ namespace Tesses.WebServer
}
}
/// <summary>
/// Route server, Based on SimpleHTTP (Used most of the Route Source)
/// </summary>
public delegate bool ShouldProcessFunc(ServerContext ctx);
public delegate Task HttpActionAsync(ServerContext ctx);
public delegate void HttpAction(ServerContext ctx);
public class RouteServer : Server
{
public List<(ShouldProcessFunc ShouldProcessFunc, HttpActionAsync Action)> Methods = new List<(ShouldProcessFunc ShouldProcessFunc, HttpActionAsync Action)>();
public override async Task GetAsync(ServerContext ctx)
{
await Process(ctx);
}
public override async Task PostAsync(ServerContext ctx)
{
await Process(ctx);
}
public override async Task OtherAsync(ServerContext ctx)
{
await Process(ctx);
}
public override async Task OptionsAsync(ServerContext ctx)
{
await Process(ctx);
}
private async Task Process(ServerContext ctx)
{
foreach(var (shouldProcessFunc,action) in Methods)
{
if(!shouldProcessFunc(ctx))
{
ctx.ResetQueryParms();
continue;
}
await action(ctx);
return;
}
}
/// <summary>
/// Adds the specified action to the route collection.
/// <para>The order of actions defines the priority.</para>
/// </summary>
/// <param name="shouldProcess">Function defining whether the specified action should be executed or not.</param>
/// <param name="action">Action executed if the specified pattern matches the URL path.</param>
public void Add(ShouldProcessFunc shouldProcess, HttpActionAsync action)
{
Methods.Add((shouldProcess, action));
}
/// <summary>
/// Adds the specified action to the route collection.
/// <para>The order of actions defines the priority.</para>
/// </summary>
/// <param name="url">
/// String url
/// </param>
/// <param name="action">Action executed if the specified pattern matches the URL path.</param>
/// <param name="method">HTTP method (GET, POST, DELETE, HEAD).</param>
public void Add(string url,HttpActionAsync action,string method="GET")
{
Add((e) =>
{
if (!e.Method.Equals( method,StringComparison.Ordinal))
return false;
return e.UrlPath.Equals(url,StringComparison.Ordinal);
},
action);
}
/// <summary>
/// Adds the specified action to the route collection.
/// <para>The order of actions defines the priority.</para>
/// </summary>
/// <param name="url">
/// String url
/// </param>
/// <param name="action">Action executed if the specified pattern matches the URL path.</param>
/// <param name="method">HTTP method (GET, POST, DELETE, HEAD).</param>
public void Add(string url, HttpAction action, string method = "GET")
{
Add((e) =>
{
if (!e.Method.Equals(method, StringComparison.Ordinal))
return false;
return e.UrlPath.Equals(url, StringComparison.Ordinal);
},
action);
}
/// <summary>
/// Adds the specified action to the route collection.
/// <para>The order of actions defines the priority.</para>
/// </summary>
/// <param name="shouldProcess">Function defining whether the specified action should be executed or not.</param>
/// <param name="action">Action executed if the specified pattern matches the URL path.</param>
public void Add(ShouldProcessFunc shouldProcess,HttpAction action)
{
Methods.Add((shouldProcess, (e) =>
{
action(e);
return Task.FromResult(true);
}
));
}
}
}