diff --git a/src/Http/HttpServer__Core.cs b/src/Http/HttpServer__Core.cs index 6e24273..10944cd 100644 --- a/src/Http/HttpServer__Core.cs +++ b/src/Http/HttpServer__Core.cs @@ -93,15 +93,8 @@ private async void ListenerCallback(IAsyncResult result) var listener = (HttpListener)result.AsyncState!; listener.BeginGetContext(_listenerCallback, listener); - try - { - HttpListenerContext context = listener.EndGetContext(result); - await ProcessRequest(context); - } - catch (Exception) - { - return; - } + HttpListenerContext context = listener.EndGetContext(result); + await ProcessRequest(context); } private async Task ProcessRequest(HttpListenerContext context) @@ -170,6 +163,9 @@ private async Task ProcessRequest(HttpListenerContext context) } request = new HttpRequest(this, matchedListeningHost, context); + srContext = new HttpContext(this, request, null, matchedListeningHost); + + request.Context = srContext; executionResult.Request = request; if (ServerConfiguration.ResolveForwardedOriginAddress) @@ -229,7 +225,7 @@ private async Task ProcessRequest(HttpListenerContext context) var timeout = flag.RouteActionTimeout; if (timeout.Ticks > 0) { - var routerTask = matchedListeningHost.Router.Execute(request, baseRequest, matchedListeningHost, srContext); + var routerTask = matchedListeningHost.Router.Execute(srContext); if (await Task.WhenAny(routerTask, Task.Delay(timeout)) == routerTask) { routerResult = routerTask.Result; @@ -241,7 +237,7 @@ private async Task ProcessRequest(HttpListenerContext context) } else { - routerResult = await matchedListeningHost.Router.Execute(request, baseRequest, matchedListeningHost, srContext); + routerResult = await matchedListeningHost.Router.Execute(srContext); } response = routerResult.Response; diff --git a/src/Routing/RequestHandledAttribute.cs b/src/Routing/RequestHandledAttribute.cs index 52cf576..d119c21 100644 --- a/src/Routing/RequestHandledAttribute.cs +++ b/src/Routing/RequestHandledAttribute.cs @@ -11,6 +11,34 @@ namespace Sisk.Core.Routing { +#if NET8_0_OR_GREATER + /// + /// Specifies that the method, when used on this attribute, will instantiate the type and call the with given parameters. + /// + /// + /// [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] + /// public class RequestHandlerAttribute{{T}} : Attribute where T : IRequestHandler + /// + /// + /// Class + /// + public class RequestHandlerAttribute<[DynamicallyAccessedMembers( + DynamicallyAccessedMemberTypes.PublicProperties + | DynamicallyAccessedMemberTypes.PublicFields + | DynamicallyAccessedMemberTypes.PublicConstructors + | DynamicallyAccessedMemberTypes.PublicMethods + | DynamicallyAccessedMemberTypes.NonPublicMethods + | DynamicallyAccessedMemberTypes.NonPublicFields + | DynamicallyAccessedMemberTypes.NonPublicConstructors + )] T> : RequestHandlerAttribute where T : IRequestHandler + { + /// + /// Creates an new instance of this class. + /// + public RequestHandlerAttribute() : base(typeof(T)) { } + } +#endif + /// /// Specifies that the method, when used on this attribute, will instantiate the type and call the with given parameters. /// diff --git a/src/Routing/Router.cs b/src/Routing/Router.cs index 3b9a4a8..3b9f5c8 100644 --- a/src/Routing/Router.cs +++ b/src/Routing/Router.cs @@ -25,6 +25,7 @@ namespace Sisk.Core.Routing public partial class Router { internal record RouterExecutionResult(HttpResponse? Response, Route? Route, RouteMatchResult Result, Exception? Exception); + internal HttpServer? ParentServer { get; private set; } internal HashSet _routes = new HashSet(); private bool throwException = false; diff --git a/src/Routing/Router__CoreInvoker.cs b/src/Routing/Router__CoreInvoker.cs index f0ab4d2..20d8f3a 100644 --- a/src/Routing/Router__CoreInvoker.cs +++ b/src/Routing/Router__CoreInvoker.cs @@ -96,18 +96,17 @@ private Internal.HttpStringInternals.PathMatchResult TestRouteMatchUsingRegex(Ro [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "Task<> is already included with dynamic dependency.")] [MethodImpl(MethodImplOptions.AggressiveOptimization)] - internal async Task Execute(HttpRequest request, HttpListenerRequest baseRequest, ListeningHost matchedHost, HttpContext? context) + internal async Task Execute(HttpContext context) { if (this.ParentServer == null) throw new InvalidOperationException(SR.Router_NotBinded); + HttpRequest request = context.Request; Route? matchedRoute = null; RouteMatchResult matchResult = RouteMatchResult.NotMatched; - context = new HttpContext(this.ParentServer, request, matchedRoute, matchedHost); HttpServerFlags flag = ParentServer!.ServerConfiguration.Flags; - request.Context = context; bool hasGlobalHandlers = this.GlobalRequestHandlers?.Length > 0; - + foreach (Route route in _routes) { // test path