From 39afd6e24a857c90b699a8fcfc79a1380f72e419 Mon Sep 17 00:00:00 2001 From: Archangel Date: Thu, 29 May 2025 19:00:27 +0200 Subject: [PATCH 1/8] Update exception message to be more clear --- Libraries/SPTarkov.DI/DependencyInjectionHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Libraries/SPTarkov.DI/DependencyInjectionHandler.cs b/Libraries/SPTarkov.DI/DependencyInjectionHandler.cs index dcd95361..900ef644 100644 --- a/Libraries/SPTarkov.DI/DependencyInjectionHandler.cs +++ b/Libraries/SPTarkov.DI/DependencyInjectionHandler.cs @@ -174,7 +174,7 @@ public class DependencyInjectionHandler _serviceCollection.AddScoped(registrableInterface, implementationType); break; default: - throw new ArgumentOutOfRangeException(nameof(injectionType), "unknown injection type"); + throw new ArgumentOutOfRangeException(nameof(injectionType), $"Unknown injection type on {implementationType.Namespace}.{implementationType.Name}"); } } From 6f3244362c0e02f5cf00697fc9184c3f4c22a9fe Mon Sep 17 00:00:00 2001 From: Archangel Date: Thu, 29 May 2025 19:01:18 +0200 Subject: [PATCH 2/8] Make sure value is not null --- Libraries/SPTarkov.Server.Core/Utils/Json/LazyLoad.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Libraries/SPTarkov.Server.Core/Utils/Json/LazyLoad.cs b/Libraries/SPTarkov.Server.Core/Utils/Json/LazyLoad.cs index effd9d53..92d14be0 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/Json/LazyLoad.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/Json/LazyLoad.cs @@ -26,7 +26,10 @@ public class LazyLoad(Func deserialize) OnLazyLoadEventArgs args = new(_result); OnLazyLoad?.Invoke(this, args); - _result = args.Value; + if (args.Value != null) + { + _result = args.Value; + } autoCleanerTimeout = new Timer( _ => From c930197942ada09b93b1d9a9fe45c38ae70593e1 Mon Sep 17 00:00:00 2001 From: Archangel Date: Thu, 29 May 2025 19:03:18 +0200 Subject: [PATCH 3/8] Cleanup AppContext --- .../SPTarkov.Server.Core/Callbacks/HttpCallbacks.cs | 6 ++---- .../Context/ContextVariableType.cs | 3 --- .../Controllers/GameController.cs | 8 ++------ .../Controllers/LauncherController.cs | 9 ++------- .../Controllers/LauncherV2Controller.cs | 4 ++-- .../SPTarkov.Server.Core/Services/BackupService.cs | 11 ++++------- SPTarkov.Server/Program.cs | 8 ++------ 7 files changed, 14 insertions(+), 35 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Callbacks/HttpCallbacks.cs b/Libraries/SPTarkov.Server.Core/Callbacks/HttpCallbacks.cs index ec0d5361..f03df458 100644 --- a/Libraries/SPTarkov.Server.Core/Callbacks/HttpCallbacks.cs +++ b/Libraries/SPTarkov.Server.Core/Callbacks/HttpCallbacks.cs @@ -4,14 +4,12 @@ using SPTarkov.Server.Core.DI; using SPTarkov.Server.Core.Servers; namespace SPTarkov.Server.Core.Callbacks; - [Injectable(InjectionType.Singleton, TypePriority = OnLoadOrder.HttpCallbacks)] -public class HttpCallbacks(HttpServer _httpServer, ApplicationContext _applicationContext) : IOnLoad +public class HttpCallbacks(HttpServer _httpServer) : IOnLoad { public Task OnLoad() { - _httpServer.Load(_applicationContext.GetLatestValue(ContextVariableType.APP_BUILDER)?.GetValue()); - _applicationContext.ClearValues(ContextVariableType.APP_BUILDER); + _httpServer.Load(); return Task.CompletedTask; } diff --git a/Libraries/SPTarkov.Server.Core/Context/ContextVariableType.cs b/Libraries/SPTarkov.Server.Core/Context/ContextVariableType.cs index 69dcda07..919fdccc 100644 --- a/Libraries/SPTarkov.Server.Core/Context/ContextVariableType.cs +++ b/Libraries/SPTarkov.Server.Core/Context/ContextVariableType.cs @@ -17,8 +17,5 @@ public enum ContextVariableType // Data returned from client request object from endLocalRaid() TRANSIT_INFO = 5, - APP_BUILDER = 6, - LOADED_MOD_ASSEMBLIES = 7, - WEB_APPLICATION = 8, SERVICE_PROVIDER = 9 } diff --git a/Libraries/SPTarkov.Server.Core/Controllers/GameController.cs b/Libraries/SPTarkov.Server.Core/Controllers/GameController.cs index 59cbe466..353928e1 100644 --- a/Libraries/SPTarkov.Server.Core/Controllers/GameController.cs +++ b/Libraries/SPTarkov.Server.Core/Controllers/GameController.cs @@ -22,6 +22,7 @@ namespace SPTarkov.Server.Core.Controllers; [Injectable] public class GameController( ISptLogger _logger, + IReadOnlyList _loadedMods, ConfigServer _configServer, DatabaseService _databaseService, TimeUtil _timeUtil, @@ -472,13 +473,8 @@ public class GameController( protected void SaveActiveModsToProfile(SptProfile fullProfile) { fullProfile.SptData!.Mods ??= []; - var mods = _applicationContext?.GetLatestValue(ContextVariableType.LOADED_MOD_ASSEMBLIES)?.GetValue>(); - if (mods == null) - { - return; - } - foreach (var mod in mods) + foreach (var mod in _loadedMods) { if ( fullProfile.SptData.Mods.Any(m => diff --git a/Libraries/SPTarkov.Server.Core/Controllers/LauncherController.cs b/Libraries/SPTarkov.Server.Core/Controllers/LauncherController.cs index d4ddef63..ab0624fb 100644 --- a/Libraries/SPTarkov.Server.Core/Controllers/LauncherController.cs +++ b/Libraries/SPTarkov.Server.Core/Controllers/LauncherController.cs @@ -18,6 +18,7 @@ namespace SPTarkov.Server.Core.Controllers; [Injectable] public class LauncherController( ISptLogger _logger, + IReadOnlyList _loadedMods, HashUtil _hashUtil, TimeUtil _timeUtil, RandomUtil _randomUtil, @@ -242,13 +243,7 @@ public class LauncherController( /// Dictionary of mod name and mod details public Dictionary GetLoadedServerMods() { - var mods = _applicationContext?.GetLatestValue(ContextVariableType.LOADED_MOD_ASSEMBLIES)?.GetValue>(); - if (mods == null) - { - return []; - } - - return mods.ToDictionary(sptMod => sptMod.ModMetadata?.Name ?? "UNKNOWN MOD", sptMod => sptMod.ModMetadata); + return _loadedMods.ToDictionary(sptMod => sptMod.ModMetadata?.Name ?? "UNKNOWN MOD", sptMod => sptMod.ModMetadata); } /// diff --git a/Libraries/SPTarkov.Server.Core/Controllers/LauncherV2Controller.cs b/Libraries/SPTarkov.Server.Core/Controllers/LauncherV2Controller.cs index ac941818..4e056f00 100644 --- a/Libraries/SPTarkov.Server.Core/Controllers/LauncherV2Controller.cs +++ b/Libraries/SPTarkov.Server.Core/Controllers/LauncherV2Controller.cs @@ -17,6 +17,7 @@ namespace SPTarkov.Server.Core.Controllers; [Injectable] public class LauncherV2Controller( ISptLogger _logger, + IReadOnlyList _loadedMods, HashUtil _hashUtil, TimeUtil _timeUtil, RandomUtil _randomUtil, @@ -158,10 +159,9 @@ public class LauncherV2Controller( /// public Dictionary LoadedMods() { - var mods = _applicationContext?.GetLatestValue(ContextVariableType.LOADED_MOD_ASSEMBLIES).GetValue>(); var result = new Dictionary(); - foreach (var sptMod in mods) + foreach (var sptMod in _loadedMods) { result.Add(sptMod.ModMetadata.Name, sptMod.ModMetadata); } diff --git a/Libraries/SPTarkov.Server.Core/Services/BackupService.cs b/Libraries/SPTarkov.Server.Core/Services/BackupService.cs index 0dc2d649..a60e440d 100644 --- a/Libraries/SPTarkov.Server.Core/Services/BackupService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/BackupService.cs @@ -24,9 +24,11 @@ public class BackupService protected JsonUtil _jsonUtil; protected ISptLogger _logger; protected TimeUtil _timeUtil; + protected IReadOnlyList _loadedMods; public BackupService( ISptLogger logger, + IReadOnlyList loadedMods, JsonUtil jsonUtil, TimeUtil timeUtil, ConfigServer configServer, @@ -39,6 +41,7 @@ public class BackupService _timeUtil = timeUtil; _fileUtil = fileUtil; _applicationContext = applicationContext; + _loadedMods = loadedMods; _activeServerMods = GetActiveServerMods(); _backupConfig = configServer.GetConfig(); @@ -306,15 +309,9 @@ public class BackupService /// A List of mod names. protected List GetActiveServerMods() { - var mods = _applicationContext?.GetLatestValue(ContextVariableType.LOADED_MOD_ASSEMBLIES)?.GetValue>(); - if (mods == null) - { - return []; - } - List result = []; - foreach (var mod in mods) + foreach (var mod in _loadedMods) { result.Add($"{mod.ModMetadata.Author} - {mod.ModMetadata.Version ?? ""}"); } diff --git a/SPTarkov.Server/Program.cs b/SPTarkov.Server/Program.cs index 923c7b30..5c7455db 100644 --- a/SPTarkov.Server/Program.cs +++ b/SPTarkov.Server/Program.cs @@ -42,6 +42,8 @@ public static class Program } diHandler.InjectAll(); + builder.Services.AddSingleton(builder); + builder.Services.AddSingleton>(loadedMods); var serviceProvider = builder.Services.BuildServiceProvider(); var logger = serviceProvider.GetService().CreateLogger("Server"); @@ -66,12 +68,6 @@ public static class Program } } - // Add the Loaded Mod Assemblies for later - appContext?.AddValue(ContextVariableType.LOADED_MOD_ASSEMBLIES, loadedMods); - - // This is the builder that will get use by the HttpServer to start up the web application - appContext?.AddValue(ContextVariableType.APP_BUILDER, builder); - // Get the Built app and run it var app = serviceProvider.GetService(); app?.Run().Wait(); From fa1368fb47da837872abd50021dfb84769d6d4b1 Mon Sep 17 00:00:00 2001 From: Archangel Date: Thu, 29 May 2025 19:05:55 +0200 Subject: [PATCH 4/8] Move more initialization, mod loading and http requests over to async --- .../SPTarkov.Server.Core/DI/ISerializer.cs | 2 +- .../Loaders/PostDBModLoader.cs | 4 +- .../Loaders/PostSptModLoader.cs | 4 +- .../Models/External/IPostDBLoadMod.cs | 6 - .../Models/External/IPostDBLoadModAsync.cs | 6 + .../Models/External/IPostSptLoadMod.cs | 6 - .../Models/External/IPostSptLoadModAsync.cs | 6 + .../Models/External/IPreSptLoadMod.cs | 6 - .../Models/External/IPreSptLoadModAsync.cs | 6 + .../Routers/ImageRouter.cs | 4 +- .../Routers/Serializers/BundleSerializer.cs | 4 +- .../Routers/Serializers/ImageSerializer.cs | 4 +- .../Routers/Serializers/NotifySerializer.cs | 4 +- .../Servers/Http/IHttpListener.cs | 2 +- .../Servers/Http/SptHttpListener.cs | 125 ++++++++---------- .../Servers/HttpServer.cs | 36 +++-- Libraries/SPTarkov.Server.Core/Utils/App.cs | 9 +- .../Utils/HttpFileUtil.cs | 13 +- SPTarkov.Server/Program.cs | 28 ++-- 19 files changed, 136 insertions(+), 139 deletions(-) delete mode 100644 Libraries/SPTarkov.Server.Core/Models/External/IPostDBLoadMod.cs create mode 100644 Libraries/SPTarkov.Server.Core/Models/External/IPostDBLoadModAsync.cs delete mode 100644 Libraries/SPTarkov.Server.Core/Models/External/IPostSptLoadMod.cs create mode 100644 Libraries/SPTarkov.Server.Core/Models/External/IPostSptLoadModAsync.cs delete mode 100644 Libraries/SPTarkov.Server.Core/Models/External/IPreSptLoadMod.cs create mode 100644 Libraries/SPTarkov.Server.Core/Models/External/IPreSptLoadModAsync.cs diff --git a/Libraries/SPTarkov.Server.Core/DI/ISerializer.cs b/Libraries/SPTarkov.Server.Core/DI/ISerializer.cs index b566c16e..b4593842 100644 --- a/Libraries/SPTarkov.Server.Core/DI/ISerializer.cs +++ b/Libraries/SPTarkov.Server.Core/DI/ISerializer.cs @@ -2,6 +2,6 @@ namespace SPTarkov.Server.Core.DI; public interface ISerializer { - public void Serialize(string sessionID, HttpRequest req, HttpResponse resp, object? body); + public Task Serialize(string sessionID, HttpRequest req, HttpResponse resp, object? body); public bool CanHandle(string route); } diff --git a/Libraries/SPTarkov.Server.Core/Loaders/PostDBModLoader.cs b/Libraries/SPTarkov.Server.Core/Loaders/PostDBModLoader.cs index 017d450a..7898e0e0 100644 --- a/Libraries/SPTarkov.Server.Core/Loaders/PostDBModLoader.cs +++ b/Libraries/SPTarkov.Server.Core/Loaders/PostDBModLoader.cs @@ -9,7 +9,7 @@ namespace SPTarkov.Server.Core.Loaders; [Injectable(TypePriority = OnLoadOrder.PostDBModLoader)] public class PostDBModLoader( ISptLogger _logger, - IEnumerable _postDbLoadMods + IEnumerable _postDbLoadMods ) : IOnLoad { public async Task OnLoad() @@ -19,7 +19,7 @@ public class PostDBModLoader( _logger.Info("Loading PostDBMods..."); foreach (var postDbLoadMod in _postDbLoadMods) { - postDbLoadMod.PostDBLoad(); + await postDbLoadMod.PostSptLoadAsync(); } _logger.Info("Finished loading PostDBMods..."); diff --git a/Libraries/SPTarkov.Server.Core/Loaders/PostSptModLoader.cs b/Libraries/SPTarkov.Server.Core/Loaders/PostSptModLoader.cs index 35684c93..bbc7fc6e 100644 --- a/Libraries/SPTarkov.Server.Core/Loaders/PostSptModLoader.cs +++ b/Libraries/SPTarkov.Server.Core/Loaders/PostSptModLoader.cs @@ -9,7 +9,7 @@ namespace SPTarkov.Server.Core.Loaders; [Injectable(TypePriority = OnLoadOrder.PostSptModLoader)] public class PostSptModLoader( ISptLogger _logger, - IEnumerable _postSptLoadMods + IEnumerable _postSptLoadMods ) : IOnLoad { public async Task OnLoad() @@ -19,7 +19,7 @@ public class PostSptModLoader( _logger.Info("Loading PostSptMods..."); foreach (var postSptLoadMod in _postSptLoadMods) { - postSptLoadMod.PostSptLoad(); + await postSptLoadMod.PostSptLoadAsync(); } _logger.Info("Finished loading PostSptMods..."); diff --git a/Libraries/SPTarkov.Server.Core/Models/External/IPostDBLoadMod.cs b/Libraries/SPTarkov.Server.Core/Models/External/IPostDBLoadMod.cs deleted file mode 100644 index fed88749..00000000 --- a/Libraries/SPTarkov.Server.Core/Models/External/IPostDBLoadMod.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace SPTarkov.Server.Core.Models.External; - -public interface IPostDBLoadMod -{ - void PostDBLoad(); -} diff --git a/Libraries/SPTarkov.Server.Core/Models/External/IPostDBLoadModAsync.cs b/Libraries/SPTarkov.Server.Core/Models/External/IPostDBLoadModAsync.cs new file mode 100644 index 00000000..64db0b60 --- /dev/null +++ b/Libraries/SPTarkov.Server.Core/Models/External/IPostDBLoadModAsync.cs @@ -0,0 +1,6 @@ +namespace SPTarkov.Server.Core.Models.External; + +public interface IPostDBLoadModAsync +{ + Task PostDBLoadAsync(); +} diff --git a/Libraries/SPTarkov.Server.Core/Models/External/IPostSptLoadMod.cs b/Libraries/SPTarkov.Server.Core/Models/External/IPostSptLoadMod.cs deleted file mode 100644 index 69371f41..00000000 --- a/Libraries/SPTarkov.Server.Core/Models/External/IPostSptLoadMod.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace SPTarkov.Server.Core.Models.External; - -public interface IPostSptLoadMod -{ - void PostSptLoad(); -} diff --git a/Libraries/SPTarkov.Server.Core/Models/External/IPostSptLoadModAsync.cs b/Libraries/SPTarkov.Server.Core/Models/External/IPostSptLoadModAsync.cs new file mode 100644 index 00000000..e82cdda5 --- /dev/null +++ b/Libraries/SPTarkov.Server.Core/Models/External/IPostSptLoadModAsync.cs @@ -0,0 +1,6 @@ +namespace SPTarkov.Server.Core.Models.External; + +public interface IPostSptLoadModAsync +{ + Task PostSptLoadAsync(); +} diff --git a/Libraries/SPTarkov.Server.Core/Models/External/IPreSptLoadMod.cs b/Libraries/SPTarkov.Server.Core/Models/External/IPreSptLoadMod.cs deleted file mode 100644 index 75781ffa..00000000 --- a/Libraries/SPTarkov.Server.Core/Models/External/IPreSptLoadMod.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace SPTarkov.Server.Core.Models.External; - -public interface IPreSptLoadMod -{ - void PreSptLoad(); -} diff --git a/Libraries/SPTarkov.Server.Core/Models/External/IPreSptLoadModAsync.cs b/Libraries/SPTarkov.Server.Core/Models/External/IPreSptLoadModAsync.cs new file mode 100644 index 00000000..241bca30 --- /dev/null +++ b/Libraries/SPTarkov.Server.Core/Models/External/IPreSptLoadModAsync.cs @@ -0,0 +1,6 @@ +namespace SPTarkov.Server.Core.Models.External; + +public interface IPreSptLoadModAsync +{ + Task PreSptLoadAsync(); +} diff --git a/Libraries/SPTarkov.Server.Core/Routers/ImageRouter.cs b/Libraries/SPTarkov.Server.Core/Routers/ImageRouter.cs index 6863b725..d52f5e56 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/ImageRouter.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/ImageRouter.cs @@ -31,7 +31,7 @@ public class ImageRouter _imageRouterService.AddRoute(key.ToLower(), valueToAdd); } - public void SendImage(string sessionId, HttpRequest req, HttpResponse resp, object body) + public async Task SendImage(string sessionId, HttpRequest req, HttpResponse resp, object body) { // remove file extension var url = _fileUtil.StripExtension(req.Path, true); @@ -40,7 +40,7 @@ public class ImageRouter var urlKeyLower = url.ToLower(); if (_imageRouterService.ExistsByKey(urlKeyLower)) { - _httpFileUtil.SendFile(resp, _imageRouterService.GetByKey(urlKeyLower)); + await _httpFileUtil.SendFile(resp, _imageRouterService.GetByKey(urlKeyLower)); return; } diff --git a/Libraries/SPTarkov.Server.Core/Routers/Serializers/BundleSerializer.cs b/Libraries/SPTarkov.Server.Core/Routers/Serializers/BundleSerializer.cs index 2d020d41..c4034517 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/Serializers/BundleSerializer.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/Serializers/BundleSerializer.cs @@ -13,7 +13,7 @@ public class BundleSerializer( HttpFileUtil httpFileUtil ) : ISerializer { - public void Serialize(string sessionID, HttpRequest req, HttpResponse resp, object? body) + public async Task Serialize(string sessionID, HttpRequest req, HttpResponse resp, object? body) { var key = req.Path.Value.Split("/bundle/")[1]; var bundle = bundleLoader.GetBundle(key); @@ -30,7 +30,7 @@ public class BundleSerializer( } var bundlePath = Path.Join(bundle.ModPath, "/bundles/", bundle.FileName); - httpFileUtil.SendFile(resp, bundlePath); + await httpFileUtil.SendFile(resp, bundlePath); } public bool CanHandle(string route) diff --git a/Libraries/SPTarkov.Server.Core/Routers/Serializers/ImageSerializer.cs b/Libraries/SPTarkov.Server.Core/Routers/Serializers/ImageSerializer.cs index 8dfcd085..a8fac549 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/Serializers/ImageSerializer.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/Serializers/ImageSerializer.cs @@ -13,9 +13,9 @@ public class ImageSerializer : ISerializer _imageRouter = imageRouter; } - public void Serialize(string sessionID, HttpRequest req, HttpResponse resp, object? body) + public async Task Serialize(string sessionID, HttpRequest req, HttpResponse resp, object? body) { - _imageRouter.SendImage(sessionID, req, resp, body); + await _imageRouter.SendImage(sessionID, req, resp, body); } public bool CanHandle(string route) diff --git a/Libraries/SPTarkov.Server.Core/Routers/Serializers/NotifySerializer.cs b/Libraries/SPTarkov.Server.Core/Routers/Serializers/NotifySerializer.cs index 870bec25..5cd58b27 100644 --- a/Libraries/SPTarkov.Server.Core/Routers/Serializers/NotifySerializer.cs +++ b/Libraries/SPTarkov.Server.Core/Routers/Serializers/NotifySerializer.cs @@ -13,7 +13,7 @@ public class NotifySerializer( HttpServerHelper httpServerHelper ) : ISerializer { - public void Serialize(string sessionID, HttpRequest req, HttpResponse resp, object? body) + public async Task Serialize(string sessionID, HttpRequest req, HttpResponse resp, object? body) { var splittedUrl = req.Path.Value.Split("/"); var tmpSessionID = splittedUrl[^1].Split("?last_id")[0]; @@ -22,7 +22,7 @@ public class NotifySerializer( * Take our array of JSON message objects and cast them to JSON strings, so that they can then * be sent to client as NEWLINE separated strings... yup. */ - notifierController.NotifyAsync(tmpSessionID) + await notifierController.NotifyAsync(tmpSessionID) .ContinueWith(messages => messages.Result.Select(message => string.Join("\n", jsonUtil.Serialize(message)))) .ContinueWith(text => httpServerHelper.SendTextJson(resp, text)); } diff --git a/Libraries/SPTarkov.Server.Core/Servers/Http/IHttpListener.cs b/Libraries/SPTarkov.Server.Core/Servers/Http/IHttpListener.cs index 6e1586d2..af794c01 100644 --- a/Libraries/SPTarkov.Server.Core/Servers/Http/IHttpListener.cs +++ b/Libraries/SPTarkov.Server.Core/Servers/Http/IHttpListener.cs @@ -3,5 +3,5 @@ public interface IHttpListener { bool CanHandle(string sessionId, HttpRequest req); - void Handle(string sessionId, HttpRequest req, HttpResponse resp); + Task Handle(string sessionId, HttpRequest req, HttpResponse resp); } diff --git a/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs b/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs index 268d0712..c7a79696 100644 --- a/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs +++ b/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs @@ -13,57 +13,43 @@ using LogLevel = SPTarkov.Server.Core.Models.Spt.Logging.LogLevel; namespace SPTarkov.Server.Core.Servers.Http; [Injectable] -public class SptHttpListener : IHttpListener +public class SptHttpListener( + HttpRouter httpRouter, + IEnumerable serializers, + ISptLogger logger, + ISptLogger requestsLogger, + JsonUtil jsonUtil, + HttpResponseUtil httpHttpResponseUtil, + LocalisationService localisationService + ) : IHttpListener { - // we want to reserve on the list 512KB capacity before it needs to expand, should be enough for most requests - private const int InitialCapacityForListBuffer = 1024 * 512; - // We want to read 1KB at a time, for most request this is already big enough private const int BodyReadBufferSize = 1024 * 1; private static readonly ImmutableHashSet SupportedMethods = ["GET", "PUT", "POST"]; - protected readonly HttpResponseUtil _httpResponseUtil; - protected readonly JsonUtil _jsonUtil; - protected readonly LocalisationService _localisationService; - protected readonly ISptLogger _logger; - protected readonly ISptLogger _requestLogger; + protected readonly HttpResponseUtil _httpResponseUtil = httpHttpResponseUtil; + protected readonly JsonUtil _jsonUtil = jsonUtil; + protected readonly LocalisationService _localisationService = localisationService; + protected readonly ISptLogger _logger = logger; + protected readonly ISptLogger _requestLogger = requestsLogger; - protected readonly HttpRouter _router; - protected readonly IEnumerable _serializers; - - public SptHttpListener( - HttpRouter httpRouter, - IEnumerable serializers, - ISptLogger logger, - ISptLogger requestsLogger, - JsonUtil jsonUtil, - HttpResponseUtil httpHttpResponseUtil, - LocalisationService localisationService - ) - { - _router = httpRouter; - _serializers = serializers; - _logger = logger; - _requestLogger = requestsLogger; - _httpResponseUtil = httpHttpResponseUtil; - _localisationService = localisationService; - _jsonUtil = jsonUtil; - } + protected readonly HttpRouter _router = httpRouter; + protected readonly IEnumerable _serializers = serializers; public bool CanHandle(string _, HttpRequest req) { return SupportedMethods.Contains(req.Method); } - public void Handle(string sessionId, HttpRequest req, HttpResponse resp) + public async Task Handle(string sessionId, HttpRequest req, HttpResponse resp) { switch (req.Method) { case "GET": { var response = GetResponse(sessionId, req, null); - SendResponse(sessionId, req, resp, null, response); + await SendResponse(sessionId, req, resp, null, response); break; } // these are handled almost identically. @@ -75,49 +61,50 @@ public class SptHttpListener : IHttpListener // debug = 1 are as well. This should be fixed. // let compressed = req.headers["content-encoding"] === "deflate"; var requestIsCompressed = !req.Headers.TryGetValue("requestcompressed", out var compressHeader) || - compressHeader != "0"; + compressHeader != "0"; var requestCompressed = req.Method == "PUT" || requestIsCompressed; - // reserve some capacity to avoid having the list to resize - var totalRead = new List(InitialCapacityForListBuffer); - // read 1KB at a time - var memory = new Memory(new byte[BodyReadBufferSize]); - var readTask = req.Body.ReadAsync(memory).AsTask(); - readTask.Wait(); - var readBytes = 0; - while (readTask.Result != 0) + var body = string.Empty; + using MemoryStream bufferStream = new(); + + var buffer = new byte[BodyReadBufferSize]; + int bytesRead; + + while ((bytesRead = await req.Body.ReadAsync(buffer)) > 0) { - readBytes += readTask.Result; - totalRead.AddRange(memory[..readTask.Result].ToArray()); - memory = new Memory(new byte[BodyReadBufferSize]); - readTask = req.Body.ReadAsync(memory).AsTask(); - readTask.Wait(); + await bufferStream.WriteAsync(buffer.AsMemory(0, bytesRead)); } - string value; + bufferStream.Position = 0; + if (requestCompressed) { - using var uncompressedDataStream = new MemoryStream(); - using var compressedDataStream = new MemoryStream(totalRead[..readBytes].ToArray()); - using var deflateStream = new ZLibStream(compressedDataStream, CompressionMode.Decompress, true); - deflateStream.CopyTo(uncompressedDataStream); - value = Encoding.UTF8.GetString(uncompressedDataStream.ToArray()); + using var deflateStream = new ZLibStream(bufferStream, CompressionMode.Decompress); + using var decompressedStream = new MemoryStream(); + await deflateStream.CopyToAsync(decompressedStream); + decompressedStream.Position = 0; + + using var reader = new StreamReader(decompressedStream, Encoding.UTF8); + body = await reader.ReadToEndAsync(); } else { - value = Encoding.UTF8.GetString(totalRead[..readBytes].ToArray()); + // No decompression needed, decode directly from the bufferStream's buffer + bufferStream.Position = 0; + using var reader = new StreamReader(bufferStream, Encoding.UTF8); + body = await reader.ReadToEndAsync(); } if (!requestIsCompressed) { if (_logger.IsLogEnabled(LogLevel.Debug)) { - _logger.Debug(value); + _logger.Debug(body); } } - var response = GetResponse(sessionId, req, value); - SendResponse(sessionId, req, resp, value, response); + var response = GetResponse(sessionId, req, body); + await SendResponse(sessionId, req, resp, body, response); break; } @@ -137,7 +124,7 @@ public class SptHttpListener : IHttpListener /// Outgoing response /// Buffer /// Server generated response data - public void SendResponse( + public async Task SendResponse( string sessionID, HttpRequest req, HttpResponse resp, @@ -155,7 +142,7 @@ public class SptHttpListener : IHttpListener if (IsDebugRequest(req)) { // Send only raw response without transformation - SendJson(resp, output, sessionID); + await SendJson(resp, output, sessionID); if (_logger.IsLogEnabled(LogLevel.Debug)) { _logger.Debug($"Response: {output}"); @@ -169,12 +156,12 @@ public class SptHttpListener : IHttpListener var serialiser = _serializers.FirstOrDefault(x => x.CanHandle(output)); if (serialiser != null) { - serialiser.Serialize(sessionID, req, resp, bodyInfo); + await serialiser.Serialize(sessionID, req, resp, bodyInfo); } else // No serializer can handle the request (majority of requests dont), zlib the output and send response back { - SendZlibJson(resp, output, sessionID); + await SendZlibJson(resp, output, sessionID); } LogRequest(req, output); @@ -225,35 +212,35 @@ public class SptHttpListener : IHttpListener return output; } - public void SendJson(HttpResponse resp, string? output, string sessionID) + public async Task SendJson(HttpResponse resp, string? output, string sessionID) { resp.StatusCode = 200; resp.ContentType = "application/json"; resp.Headers.Append("Set-Cookie", $"PHPSESSID={sessionID}"); if (!string.IsNullOrEmpty(output)) { - resp.Body.WriteAsync(Encoding.UTF8.GetBytes(output)).AsTask().Wait(); + await resp.Body.WriteAsync(Encoding.UTF8.GetBytes(output)); } - resp.StartAsync().Wait(); - resp.CompleteAsync().Wait(); + await resp.StartAsync(); + await resp.CompleteAsync(); } - public void SendZlibJson(HttpResponse resp, string? output, string sessionID) + public async Task SendZlibJson(HttpResponse resp, string? output, string sessionID) { using (var ms = new MemoryStream()) { using (var deflateStream = new ZLibStream(ms, CompressionLevel.SmallestSize)) { - deflateStream.WriteAsync(Encoding.UTF8.GetBytes(output)).AsTask().Wait(); + await deflateStream.WriteAsync(Encoding.UTF8.GetBytes(output)); } var bytes = ms.ToArray(); - resp.Body.WriteAsync(bytes, 0, bytes.Length).Wait(); + await resp.Body.WriteAsync(bytes); } - resp.StartAsync().Wait(); - resp.CompleteAsync().Wait(); + await resp.StartAsync(); + await resp.CompleteAsync(); } private record Response(string Method, string jsonData); diff --git a/Libraries/SPTarkov.Server.Core/Servers/HttpServer.cs b/Libraries/SPTarkov.Server.Core/Servers/HttpServer.cs index 1faaa838..88c1d316 100644 --- a/Libraries/SPTarkov.Server.Core/Servers/HttpServer.cs +++ b/Libraries/SPTarkov.Server.Core/Servers/HttpServer.cs @@ -15,11 +15,11 @@ namespace SPTarkov.Server.Core.Servers; [Injectable(InjectionType.Singleton)] public class HttpServer( + WebApplicationBuilder _builder, ISptLogger _logger, LocalisationService _localisationService, ConfigServer _configServer, CertificateHelper _certificateHelper, - ApplicationContext _applicationContext, WebSocketServer _webSocketServer, ProfileActivityService _profileActivityService, IEnumerable _httpListeners @@ -27,20 +27,21 @@ public class HttpServer( { private readonly HttpConfig _httpConfig = _configServer.GetConfig(); private bool _started; + private WebApplication? _webApplication; /// /// Handle server loading event /// /// Server builder /// Throws Exception when WebApplicationBuiler or WebApplication are null - public void Load(WebApplicationBuilder? builder) + public void Load() { - if (builder is null) + if (_builder is null) { throw new Exception("WebApplicationBuilder is null in HttpServer.Load()"); } - builder.WebHost.ConfigureKestrel(options => + _builder.WebHost.ConfigureKestrel(options => { options.Listen(IPAddress.Parse(_httpConfig.Ip), _httpConfig.Port, listenOptions => { @@ -53,29 +54,35 @@ public class HttpServer( }); }); - var app = builder.Build(); + _webApplication = _builder.Build(); - if (app is null) + if (_webApplication is null) { throw new Exception("WebApplication is null in HttpServer.Load()"); } // Enable web socket - app.UseWebSockets(new WebSocketOptions + _webApplication.UseWebSockets(new WebSocketOptions { // Every minute a heartbeat is sent to keep the connection alive. KeepAliveInterval = TimeSpan.FromSeconds(60) }); - app?.Use((HttpContext req, RequestDelegate _) => + _webApplication.Use(async (HttpContext req, RequestDelegate _) => { - return Task.Factory.StartNew(async () => await HandleFallback(req)); + await HandleFallback(req); } ); + } - _started = true; + public async Task StartAsync() + { + if (_webApplication != null && !_started) + { + _started = true; + await _webApplication.RunAsync(); + } - _applicationContext.AddValue(ContextVariableType.WEB_APPLICATION, app); } private async Task HandleFallback(HttpContext context) @@ -103,7 +110,12 @@ public class HttpServer( try { - _httpListeners.SingleOrDefault(l => l.CanHandle(sessionId, context.Request))?.Handle(sessionId, context.Request, context.Response); + var listener = _httpListeners.FirstOrDefault(l => l.CanHandle(sessionId, context.Request)); + + if (listener != null) + { + await listener.Handle(sessionId, context.Request, context.Response); + } } catch (Exception ex) { diff --git a/Libraries/SPTarkov.Server.Core/Utils/App.cs b/Libraries/SPTarkov.Server.Core/Utils/App.cs index c0686642..d610524e 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/App.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/App.cs @@ -57,7 +57,7 @@ public class App _coreConfig = configServer.GetConfig(); } - public async Task Run() + public async Task InitializeAsync() { // execute onLoad callbacks _logger.Info(_localisationService.GetText("executing_startup_callbacks")); @@ -96,14 +96,19 @@ public class App } _timer = new Timer(_ => Update(_onUpdate), null, TimeSpan.Zero, TimeSpan.FromMilliseconds(5000)); + } - if (_httpServer.IsStarted()) + public async Task StartAsync() + { + if(!_httpServer.IsStarted()) { _logger.Success(_localisationService.GetText("started_webserver_success", _httpServer.ListeningUrl())); _logger.Success(_localisationService.GetText("websocket-started", _httpServer.ListeningUrl().Replace("https://", "wss://"))); } _logger.Success(GetRandomisedStartMessage()); + + await _httpServer.StartAsync(); } protected string GetRandomisedStartMessage() diff --git a/Libraries/SPTarkov.Server.Core/Utils/HttpFileUtil.cs b/Libraries/SPTarkov.Server.Core/Utils/HttpFileUtil.cs index a4128d9f..ee777d4f 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/HttpFileUtil.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/HttpFileUtil.cs @@ -4,21 +4,16 @@ using SPTarkov.Server.Core.Helpers; namespace SPTarkov.Server.Core.Utils; [Injectable] -public class HttpFileUtil +public class HttpFileUtil(HttpServerHelper httpServerHelper) { - protected HttpServerHelper _httpServerHelper; + protected HttpServerHelper _httpServerHelper = httpServerHelper; - public HttpFileUtil(HttpServerHelper httpServerHelper) - { - _httpServerHelper = httpServerHelper; - } - - public void SendFile(HttpResponse resp, string filePath) + public async Task SendFile(HttpResponse resp, string filePath) { var pathSlice = filePath.Split("/"); var mimePath = _httpServerHelper.GetMimeText(pathSlice[^1].Split(".")[^1]); var type = string.IsNullOrWhiteSpace(mimePath) ? _httpServerHelper.GetMimeText("txt") : mimePath; resp.Headers.Append("Content-Type", type); - resp.SendFileAsync(filePath, CancellationToken.None).Wait(); + await resp.SendFileAsync(filePath, CancellationToken.None); } } diff --git a/SPTarkov.Server/Program.cs b/SPTarkov.Server/Program.cs index 5c7455db..98fbe1a4 100644 --- a/SPTarkov.Server/Program.cs +++ b/SPTarkov.Server/Program.cs @@ -8,6 +8,7 @@ using SPTarkov.Server.Core.Helpers; using SPTarkov.Server.Core.Models.External; using SPTarkov.Server.Core.Models.Spt.Mod; using SPTarkov.Server.Core.Models.Utils; +using SPTarkov.Server.Core.Servers; using SPTarkov.Server.Core.Utils; using SPTarkov.Server.Core.Utils.Logger; using SPTarkov.Server.Logger; @@ -17,7 +18,7 @@ namespace SPTarkov.Server; public static class Program { - public static void Main(string[] args) + public static async Task Main(string[] args) { // Initialize the program variables ProgramStatics.Initialize(); @@ -30,7 +31,7 @@ public static class Program diHandler.AddInjectableTypesFromTypeAssembly(typeof(Program)); diHandler.AddInjectableTypesFromTypeAssembly(typeof(App)); - List loadedMods = null; + List loadedMods = []; if (ProgramStatics.MODS()) { // Search for mod dlls @@ -61,28 +62,25 @@ public static class Program if (ProgramStatics.MODS()) { // Initialize PreSptMods - var preSptLoadMods = serviceProvider.GetServices(); + var preSptLoadMods = serviceProvider.GetServices(); foreach (var preSptLoadMod in preSptLoadMods) { - preSptLoadMod.PreSptLoad(); + await preSptLoadMod.PreSptLoadAsync(); } } // Get the Built app and run it var app = serviceProvider.GetService(); - app?.Run().Wait(); - // Run garbage collection now the server is ready to start - GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce; - GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true, true); - - - var httpServerHelper = serviceProvider.GetService(); - // When the application is started by the HttpServer it will be added into the AppContext of the WebApplication - // object, which we can use here to start the webapp. - if (httpServerHelper != null) + if (app != null) { - appContext?.GetLatestValue(ContextVariableType.WEB_APPLICATION)?.GetValue().Run(); + await app.InitializeAsync(); + + // Run garbage collection now the server is ready to start + GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce; + GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true, true); + + await app.StartAsync(); } } catch (Exception ex) From 5ddeee30bbd020a4c0ccd12ebfb9649cb0a805e0 Mon Sep 17 00:00:00 2001 From: Archangel Date: Thu, 29 May 2025 19:38:51 +0200 Subject: [PATCH 5/8] Fix issue with renaming --- Libraries/SPTarkov.Server.Core/Loaders/PostDBModLoader.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Loaders/PostDBModLoader.cs b/Libraries/SPTarkov.Server.Core/Loaders/PostDBModLoader.cs index 7898e0e0..04e1210c 100644 --- a/Libraries/SPTarkov.Server.Core/Loaders/PostDBModLoader.cs +++ b/Libraries/SPTarkov.Server.Core/Loaders/PostDBModLoader.cs @@ -9,7 +9,7 @@ namespace SPTarkov.Server.Core.Loaders; [Injectable(TypePriority = OnLoadOrder.PostDBModLoader)] public class PostDBModLoader( ISptLogger _logger, - IEnumerable _postDbLoadMods + IEnumerable _postDbLoadMods ) : IOnLoad { public async Task OnLoad() @@ -19,7 +19,7 @@ public class PostDBModLoader( _logger.Info("Loading PostDBMods..."); foreach (var postDbLoadMod in _postDbLoadMods) { - await postDbLoadMod.PostSptLoadAsync(); + await postDbLoadMod.PostDBLoadAsync(); } _logger.Info("Finished loading PostDBMods..."); From 8e69bd4aecff7c526855c35ad8bbf76e503fe85e Mon Sep 17 00:00:00 2001 From: Archangel Date: Thu, 29 May 2025 20:01:23 +0200 Subject: [PATCH 6/8] Await disposal --- .../SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs b/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs index c7a79696..c9c815d3 100644 --- a/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs +++ b/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs @@ -79,8 +79,8 @@ public class SptHttpListener( if (requestCompressed) { - using var deflateStream = new ZLibStream(bufferStream, CompressionMode.Decompress); - using var decompressedStream = new MemoryStream(); + await using var deflateStream = new ZLibStream(bufferStream, CompressionMode.Decompress); + await using var decompressedStream = new MemoryStream(); await deflateStream.CopyToAsync(decompressedStream); decompressedStream.Position = 0; From cc4f1f296422f007254f7b966c34eabbb173614c Mon Sep 17 00:00:00 2001 From: Archangel Date: Thu, 29 May 2025 20:44:22 +0200 Subject: [PATCH 7/8] Remove assignments as requested --- .../Servers/Http/SptHttpListener.cs | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs b/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs index c9c815d3..c2cb4ec5 100644 --- a/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs +++ b/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs @@ -19,7 +19,7 @@ public class SptHttpListener( ISptLogger logger, ISptLogger requestsLogger, JsonUtil jsonUtil, - HttpResponseUtil httpHttpResponseUtil, + HttpResponseUtil httpResponseUtil, LocalisationService localisationService ) : IHttpListener { @@ -27,11 +27,6 @@ public class SptHttpListener( private const int BodyReadBufferSize = 1024 * 1; private static readonly ImmutableHashSet SupportedMethods = ["GET", "PUT", "POST"]; - protected readonly HttpResponseUtil _httpResponseUtil = httpHttpResponseUtil; - protected readonly JsonUtil _jsonUtil = jsonUtil; - protected readonly LocalisationService _localisationService = localisationService; - protected readonly ISptLogger _logger = logger; - protected readonly ISptLogger _requestLogger = requestsLogger; protected readonly HttpRouter _router = httpRouter; @@ -97,9 +92,9 @@ public class SptHttpListener( if (!requestIsCompressed) { - if (_logger.IsLogEnabled(LogLevel.Debug)) + if (logger.IsLogEnabled(LogLevel.Debug)) { - _logger.Debug(body); + logger.Debug(body); } } @@ -110,7 +105,7 @@ public class SptHttpListener( default: { - _logger.Warning($"{_localisationService.GetText("unknown_request")}: {req.Method}"); + logger.Warning($"{localisationService.GetText("unknown_request")}: {req.Method}"); break; } } @@ -137,15 +132,15 @@ public class SptHttpListener( body = new object(); } - var bodyInfo = _jsonUtil.Serialize(body); + var bodyInfo = jsonUtil.Serialize(body); if (IsDebugRequest(req)) { // Send only raw response without transformation await SendJson(resp, output, sessionID); - if (_logger.IsLogEnabled(LogLevel.Debug)) + if (logger.IsLogEnabled(LogLevel.Debug)) { - _logger.Debug($"Response: {output}"); + logger.Debug($"Response: {output}"); } LogRequest(req, output); @@ -187,7 +182,7 @@ public class SptHttpListener( if (ProgramStatics.ENTRY_TYPE() != EntryType.RELEASE) { var log = new Response(req.Method, output); - _requestLogger.Info($"RESPONSE={_jsonUtil.Serialize(log)}"); + requestsLogger.Info($"RESPONSE={jsonUtil.Serialize(log)}"); } } @@ -197,16 +192,16 @@ public class SptHttpListener( /* route doesn't exist or response is not properly set up */ if (string.IsNullOrEmpty(output)) { - _logger.Error(_localisationService.GetText("unhandled_response", req.Path.ToString())); - _logger.Info(_jsonUtil.Serialize(deserializedObject)); - output = _httpResponseUtil.GetBody(null, BackendErrorCodes.HTTPNotFound, $"UNHANDLED RESPONSE: {req.Path.ToString()}"); + logger.Error(localisationService.GetText("unhandled_response", req.Path.ToString())); + logger.Info(jsonUtil.Serialize(deserializedObject)); + output = httpResponseUtil.GetBody(null, BackendErrorCodes.HTTPNotFound, $"UNHANDLED RESPONSE: {req.Path.ToString()}"); } if (ProgramStatics.ENTRY_TYPE() != EntryType.RELEASE) { // Parse quest info into object var log = new Request(req.Method, new RequestData(req.Path, req.Headers, deserializedObject)); - _requestLogger.Info($"REQUEST={_jsonUtil.Serialize(log)}"); + requestsLogger.Info($"REQUEST={jsonUtil.Serialize(log)}"); } return output; From 7d0c8dfc387b432102af39e945626534aa1ded6b Mon Sep 17 00:00:00 2001 From: Archangel Date: Thu, 29 May 2025 20:45:19 +0200 Subject: [PATCH 8/8] Rename parameters with underscore --- .../Servers/Http/SptHttpListener.cs | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs b/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs index c2cb4ec5..c9286bb8 100644 --- a/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs +++ b/Libraries/SPTarkov.Server.Core/Servers/Http/SptHttpListener.cs @@ -14,13 +14,13 @@ namespace SPTarkov.Server.Core.Servers.Http; [Injectable] public class SptHttpListener( - HttpRouter httpRouter, - IEnumerable serializers, - ISptLogger logger, - ISptLogger requestsLogger, - JsonUtil jsonUtil, - HttpResponseUtil httpResponseUtil, - LocalisationService localisationService + HttpRouter _httpRouter, + IEnumerable _serializers, + ISptLogger _logger, + ISptLogger _requestsLogger, + JsonUtil _jsonUtil, + HttpResponseUtil _httpResponseUtil, + LocalisationService _localisationService ) : IHttpListener { // We want to read 1KB at a time, for most request this is already big enough @@ -29,8 +29,8 @@ public class SptHttpListener( private static readonly ImmutableHashSet SupportedMethods = ["GET", "PUT", "POST"]; - protected readonly HttpRouter _router = httpRouter; - protected readonly IEnumerable _serializers = serializers; + protected readonly HttpRouter _router = _httpRouter; + protected readonly IEnumerable _serializers = _serializers; public bool CanHandle(string _, HttpRequest req) { @@ -92,9 +92,9 @@ public class SptHttpListener( if (!requestIsCompressed) { - if (logger.IsLogEnabled(LogLevel.Debug)) + if (_logger.IsLogEnabled(LogLevel.Debug)) { - logger.Debug(body); + _logger.Debug(body); } } @@ -105,7 +105,7 @@ public class SptHttpListener( default: { - logger.Warning($"{localisationService.GetText("unknown_request")}: {req.Method}"); + _logger.Warning($"{_localisationService.GetText("unknown_request")}: {req.Method}"); break; } } @@ -132,15 +132,15 @@ public class SptHttpListener( body = new object(); } - var bodyInfo = jsonUtil.Serialize(body); + var bodyInfo = _jsonUtil.Serialize(body); if (IsDebugRequest(req)) { // Send only raw response without transformation await SendJson(resp, output, sessionID); - if (logger.IsLogEnabled(LogLevel.Debug)) + if (_logger.IsLogEnabled(LogLevel.Debug)) { - logger.Debug($"Response: {output}"); + _logger.Debug($"Response: {output}"); } LogRequest(req, output); @@ -182,7 +182,7 @@ public class SptHttpListener( if (ProgramStatics.ENTRY_TYPE() != EntryType.RELEASE) { var log = new Response(req.Method, output); - requestsLogger.Info($"RESPONSE={jsonUtil.Serialize(log)}"); + _requestsLogger.Info($"RESPONSE={_jsonUtil.Serialize(log)}"); } } @@ -192,16 +192,16 @@ public class SptHttpListener( /* route doesn't exist or response is not properly set up */ if (string.IsNullOrEmpty(output)) { - logger.Error(localisationService.GetText("unhandled_response", req.Path.ToString())); - logger.Info(jsonUtil.Serialize(deserializedObject)); - output = httpResponseUtil.GetBody(null, BackendErrorCodes.HTTPNotFound, $"UNHANDLED RESPONSE: {req.Path.ToString()}"); + _logger.Error(_localisationService.GetText("unhandled_response", req.Path.ToString())); + _logger.Info(_jsonUtil.Serialize(deserializedObject)); + output = _httpResponseUtil.GetBody(null, BackendErrorCodes.HTTPNotFound, $"UNHANDLED RESPONSE: {req.Path.ToString()}"); } if (ProgramStatics.ENTRY_TYPE() != EntryType.RELEASE) { // Parse quest info into object var log = new Request(req.Method, new RequestData(req.Path, req.Headers, deserializedObject)); - requestsLogger.Info($"REQUEST={jsonUtil.Serialize(log)}"); + _requestsLogger.Info($"REQUEST={_jsonUtil.Serialize(log)}"); } return output;