CSharpHttpServer/SimpleHttpServer/Internal/HttpEndpointHandler.cs
2024-07-25 03:41:53 +02:00

74 lines
2.6 KiB
C#

//using Newtonsoft.Json;
//using System.Collections;
//using System.Net;
//using System.Reflection;
//namespace SimpleHttpServer.Internal;
//internal class HttpEndpointHandler {
// private static readonly DefaultAuthorizer defaultAuth = new();
// private readonly IAuthorizer auth;
// private readonly MethodInfo handler;
// private readonly Dictionary<string, (int pindex, Type type, int pparamIdx)> @params;
// private readonly Func<Exception, HttpResponseBuilder> errorPageBuilder;
// public HttpEndpointHandler() {
// auth = defaultAuth;
// }
// public HttpEndpointHandler(IAuthorizer auth) {
// }
// public virtual void Handle(HttpListenerContext ctx) {
// try {
// var (isAuth, authData) = auth.IsAuthenticated(ctx);
// if (!isAuth) {
// throw new HttpHandlingException(401, "Authorization required!");
// }
// // collect parameters
// var invokeParams = new object?[@params.Count + 1];
// var set = new BitArray(@params.Count);
// invokeParams[0] = ctx;
// // read pparams
// // read qparams
// var qst = ctx.Request.QueryString;
// foreach (var qelem in ctx.Request.QueryString.AllKeys) {
// if (@params.ContainsKey(qelem!)) {
// var (pindex, type, isPParam) = @params[qelem!];
// if (type == typeof(string)) {
// invokeParams[pindex] = ctx.Request.QueryString[qelem!];
// set.Set(pindex - 1, true);
// } else {
// var elem = JsonConvert.DeserializeObject(ctx.Request.QueryString[qelem!]!, type);
// if (elem != null) {
// invokeParams[pindex] = elem;
// set.Set(pindex - 1, true);
// }
// }
// }
// }
// // fill with defaults
// foreach (var p in @params) {
// if (!set.Get(p.Value.pindex)) {
// invokeParams[p.Value.pindex] = p.Value.type.IsValueType ? Activator.CreateInstance(p.Value.type) : null;
// }
// }
// var builder = handler.Invoke(null, invokeParams) as HttpResponseBuilder;
// builder!.SendResponse(ctx.Response);
// } catch (Exception e) {
// if (e is TargetInvocationException tex) {
// e = tex.InnerException!;
// }
// errorPageBuilder(e).SendResponse(ctx.Response);
// }
// }
//}