Switch over to a check-based system with multi attribute support
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
using System.Net;
|
||||
|
||||
namespace SimpleHttpServer.Types;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
|
||||
public abstract class BaseEndpointCheckAttribute : Attribute {
|
||||
|
||||
public BaseEndpointCheckAttribute() { }
|
||||
|
||||
/// <summary>
|
||||
/// Executed when the endpoint is invoked. The endpoint invocation is skipped if any of the checks fail.
|
||||
/// </summary>
|
||||
/// <returns>True to allow invocation, false to prevent.</returns>
|
||||
public abstract bool Check(HttpListenerRequest req);
|
||||
}
|
||||
@@ -1,12 +1,19 @@
|
||||
using System.Reflection;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
|
||||
namespace SimpleHttpServer.Types;
|
||||
internal struct EndpointInvocationInfo {
|
||||
internal readonly MethodInfo methodInfo;
|
||||
internal readonly List<(string, (Type type, bool isOptional))> queryParameters;
|
||||
internal readonly struct EndpointInvocationInfo {
|
||||
internal record struct QueryParameterInfo(string Name, Type Type, bool IsOptional);
|
||||
|
||||
public EndpointInvocationInfo(MethodInfo methodInfo, List<(string, (Type type, bool isOptional))> queryParameters) {
|
||||
internal readonly MethodInfo methodInfo;
|
||||
internal readonly List<QueryParameterInfo> queryParameters;
|
||||
internal readonly BaseEndpointCheckAttribute[] requiredChecks;
|
||||
|
||||
public EndpointInvocationInfo(MethodInfo methodInfo, List<QueryParameterInfo> queryParameters, BaseEndpointCheckAttribute[] requiredChecks) {
|
||||
this.methodInfo = methodInfo ?? throw new ArgumentNullException(nameof(methodInfo));
|
||||
this.queryParameters = queryParameters ?? throw new ArgumentNullException(nameof(queryParameters));
|
||||
this.requiredChecks = requiredChecks;
|
||||
}
|
||||
|
||||
public readonly bool CheckAll(HttpListenerRequest req) => requiredChecks.All(x => x.Check(req));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user