Skip to content

Commit

Permalink
context fix
Browse files Browse the repository at this point in the history
  • Loading branch information
CypherPotato committed Nov 28, 2023
1 parent 88f19cd commit 5951693
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
18 changes: 7 additions & 11 deletions src/Http/HttpServer__Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions src/Routing/RequestHandledAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,34 @@

namespace Sisk.Core.Routing
{
#if NET8_0_OR_GREATER
/// <summary>
/// Specifies that the method, when used on this attribute, will instantiate the type and call the <see cref="IRequestHandler"/> with given parameters.
/// </summary>
/// <definition>
/// [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
/// public class RequestHandlerAttribute{{T}} : Attribute where T : IRequestHandler
/// </definition>
/// <type>
/// Class
/// </type>
public class RequestHandlerAttribute<[DynamicallyAccessedMembers(
DynamicallyAccessedMemberTypes.PublicProperties
| DynamicallyAccessedMemberTypes.PublicFields
| DynamicallyAccessedMemberTypes.PublicConstructors
| DynamicallyAccessedMemberTypes.PublicMethods
| DynamicallyAccessedMemberTypes.NonPublicMethods
| DynamicallyAccessedMemberTypes.NonPublicFields
| DynamicallyAccessedMemberTypes.NonPublicConstructors
)] T> : RequestHandlerAttribute where T : IRequestHandler
{
/// <summary>
/// Creates an new instance of this <see cref="RequestHandlerAttribute{T}"/> class.
/// </summary>
public RequestHandlerAttribute() : base(typeof(T)) { }
}
#endif

/// <summary>
/// Specifies that the method, when used on this attribute, will instantiate the type and call the <see cref="IRequestHandler"/> with given parameters.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/Routing/Router.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Route> _routes = new HashSet<Route>();
private bool throwException = false;
Expand Down
7 changes: 3 additions & 4 deletions src/Routing/Router__CoreInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RouterExecutionResult> Execute(HttpRequest request, HttpListenerRequest baseRequest, ListeningHost matchedHost, HttpContext? context)
internal async Task<RouterExecutionResult> 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
Expand Down

0 comments on commit 5951693

Please sign in to comment.