Dont handle RequestDelegate in SptHttpListener anymore
This commit is contained in:
@@ -8,6 +8,12 @@ namespace SPTarkov.Server.Core.Routers;
|
||||
[Injectable]
|
||||
public class HttpRouter(IEnumerable<StaticRouter> staticRouters, IEnumerable<DynamicRouter> 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<string?> GetResponse(HttpRequest req, MongoId sessionID, string? body)
|
||||
{
|
||||
var wrapper = new ResponseWrapper("");
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -15,12 +15,12 @@ public class StatusPage(TimeUtil timeUtil, ProfileActivityService profileActivit
|
||||
{
|
||||
protected readonly CoreConfig CoreConfig = configServer.GetConfig<CoreConfig>();
|
||||
|
||||
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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user