Dont handle RequestDelegate in SptHttpListener anymore

This commit is contained in:
Archangel
2025-09-20 16:57:04 +02:00
parent ef6e3b8c3a
commit 72559be340
5 changed files with 30 additions and 20 deletions
@@ -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);
}
@@ -27,18 +27,18 @@ public class SptHttpListener(
{
private static readonly ImmutableHashSet<string> 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<string?> GetResponse(MongoId sessionId, RequestDelegate next, HttpContext context, string? body)
public async ValueTask<string> 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<object?>(
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;
}
@@ -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
{