cleanup some old auth stuff
This commit is contained in:
@@ -1,7 +0,0 @@
|
||||
using System.Net;
|
||||
|
||||
namespace SimpleHttpServer.Internal;
|
||||
|
||||
public sealed class DefaultAuthorizer : IAuthorizer {
|
||||
public (bool auth, object? data) IsAuthenticated(HttpListenerContext contect) => (true, null);
|
||||
}
|
||||
@@ -1,73 +1,73 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
//using Newtonsoft.Json;
|
||||
//using System.Collections;
|
||||
//using System.Net;
|
||||
//using System.Reflection;
|
||||
|
||||
namespace SimpleHttpServer.Internal;
|
||||
//namespace SimpleHttpServer.Internal;
|
||||
|
||||
internal class HttpEndpointHandler {
|
||||
private static readonly DefaultAuthorizer defaultAuth = new();
|
||||
//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;
|
||||
// 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() {
|
||||
// auth = defaultAuth;
|
||||
// }
|
||||
|
||||
public HttpEndpointHandler(IAuthorizer auth) {
|
||||
// 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!");
|
||||
}
|
||||
// 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;
|
||||
// // collect parameters
|
||||
// var invokeParams = new object?[@params.Count + 1];
|
||||
// var set = new BitArray(@params.Count);
|
||||
// invokeParams[0] = ctx;
|
||||
|
||||
// read pparams
|
||||
// // 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// // 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;
|
||||
}
|
||||
}
|
||||
// // 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user