diff --git a/Libraries/SPTarkov.Server.Core/Routers/HttpRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/HttpRouter.cs index a1c29ea3..a5f8a329 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/HttpRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/HttpRouter.cs @@ -8,6 +8,12 @@ namespace SPTarkov.Server.Core.Routers; [Injectable] public class HttpRouter(IEnumerable staticRouters, IEnumerable dynamicRoutes) { + public bool CanHandle(HttpContext context) + { + return staticRouters.Any(sr => sr.CanHandle(context.Request.Path.Value, false)) + || dynamicRoutes.Any(dr => dr.CanHandle(context.Request.Path.Value, true)); + } + public async ValueTask GetResponse(HttpRequest req, MongoId sessionID, string? body) { var wrapper = new ResponseWrapper(""); diff --git a/Libraries/SPTarkov.Server.Core/Servers/Http/IHttpListener.cs b/Libraries/SPTarkov.Server.Core/Servers/Http/IHttpListener.cs index 77b48d50..7728923d 100644 --- a/Libraries/SPTarkov.Server.Core/Servers/Http/IHttpListener.cs +++ b/Libraries/SPTarkov.Server.Core/Servers/Http/IHttpListener.cs @@ -5,6 +5,6 @@ namespace SPTarkov.Server.Core.Servers.Http; public interface IHttpListener { - bool CanHandle(MongoId sessionId, HttpRequest req); - Task Handle(MongoId sessionId, RequestDelegate next, HttpContext context); + bool CanHandle(MongoId sessionId, HttpContext context); + Task Handle(MongoId sessionId, HttpContext context); } diff --git a/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs b/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs index 6ebac185..804869c8 100644 --- a/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs +++ b/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs @@ -27,18 +27,18 @@ public class SptHttpListener( { private static readonly ImmutableHashSet SupportedMethods = ["GET", "PUT", "POST"]; - public bool CanHandle(MongoId _, HttpRequest req) + public bool CanHandle(MongoId _, HttpContext context) { - return SupportedMethods.Contains(req.Method); + return SupportedMethods.Contains(context.Request.Method) && httpRouter.CanHandle(context); } - public async Task Handle(MongoId sessionId, RequestDelegate next, HttpContext context) + public async Task Handle(MongoId sessionId, HttpContext context) { switch (context.Request.Method) { case "GET": { - var response = await GetResponse(sessionId, next, context, null); + var response = await GetResponse(sessionId, context, null); // Another handler is already handling this, or no handler was found. if (response is null) @@ -83,7 +83,7 @@ public class SptHttpListener( } } - var response = await GetResponse(sessionId, next, context, body); + var response = await GetResponse(sessionId, context, body); // Another handler is already handling this, or no handler was found. if (response is null) @@ -163,10 +163,21 @@ public class SptHttpListener( } } - public async ValueTask GetResponse(MongoId sessionId, RequestDelegate next, HttpContext context, string? body) + public async ValueTask GetResponse(MongoId sessionId, HttpContext context, string? body) { var output = await httpRouter.GetResponse(context.Request, sessionId, body); + // Route doesn't exist or response is not properly set up + if (string.IsNullOrEmpty(output)) + { + logger.Error(serverLocalisationService.GetText("unhandled_response", context.Request.Path.ToString())); + output = httpResponseUtil.GetBody( + null, + BackendErrorCodes.HTTPNotFound, + $"UNHANDLED RESPONSE: {context.Request.Path.ToString()}" + ); + } + if (ProgramStatics.ENTRY_TYPE() != EntryType.RELEASE) { // Parse quest info into object @@ -174,13 +185,6 @@ public class SptHttpListener( requestsLogger.Info($"REQUEST={jsonUtil.Serialize(log)}"); } - // Route doesn't exist or response is not properly set up, continue to next handlers. - if (string.IsNullOrEmpty(output)) - { - await next(context); - return null; - } - return output; } diff --git a/Libraries/SPTarkov.Server.Core/Servers/HttpServer.cs b/Libraries/SPTarkov.Server.Core/Servers/HttpServer.cs index 3af697aa..4e8fbc0a 100644 --- a/Libraries/SPTarkov.Server.Core/Servers/HttpServer.cs +++ b/Libraries/SPTarkov.Server.Core/Servers/HttpServer.cs @@ -35,11 +35,11 @@ public class HttpServer( profileActivityService.SetActivityTimestamp(sessionId); } - var listener = httpListeners.FirstOrDefault(listener => listener.CanHandle(sessionId, context.Request)); + var listener = httpListeners.FirstOrDefault(listener => listener.CanHandle(sessionId, context)); if (listener != null) { - await listener.Handle(sessionId, next, context); + await listener.Handle(sessionId, context); } else { diff --git a/Libraries/SPTarkov.Server.Core/Status/StatusPage.cs b/Libraries/SPTarkov.Server.Core/Status/StatusPage.cs index e1c3e973..626f802e 100644 --- a/Libraries/SPTarkov.Server.Core/Status/StatusPage.cs +++ b/Libraries/SPTarkov.Server.Core/Status/StatusPage.cs @@ -15,12 +15,12 @@ public class StatusPage(TimeUtil timeUtil, ProfileActivityService profileActivit { protected readonly CoreConfig CoreConfig = configServer.GetConfig(); - public bool CanHandle(MongoId sessionId, HttpRequest req) + public bool CanHandle(MongoId sessionId, HttpContext context) { - return req.Method == "GET" && req.Path.Value.Contains("/status"); + return context.Request.Method == "GET" && context.Request.Path.Value.Contains("/status"); } - public async Task Handle(MongoId sessionId, RequestDelegate next, HttpContext context) + public async Task Handle(MongoId sessionId, HttpContext context) { var resp = context.Response;