From 14fa5e9328a23a99af8b6810afb674a5bdd28203 Mon Sep 17 00:00:00 2001 From: Slendy Date: Sat, 11 Feb 2023 19:08:44 -0600 Subject: [PATCH] Properly dispose of StreamReaders --- .../Controllers/Matching/MatchController.cs | 2 +- .../Controllers/MessageController.cs | 2 +- .../Controllers/Slots/ScoreController.cs | 3 +-- .../Controllers/UserController.cs | 5 +++-- ProjectLighthouse/Extensions/ControllerExtensions.cs | 12 +++++++++++- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/Matching/MatchController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/Matching/MatchController.cs index 87e77543..e3aba48d 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/Matching/MatchController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/Matching/MatchController.cs @@ -44,7 +44,7 @@ public class MatchController : ControllerBase // Example POST /match: [UpdateMyPlayerData,["Player":"FireGamer9872"]] - string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync(); + string bodyString = await this.ReadBodyAsync(); if (bodyString.Length == 0 || bodyString[0] != '[') return this.BadRequest(); diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/MessageController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/MessageController.cs index afff8391..c6f8f1c8 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/MessageController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/MessageController.cs @@ -81,7 +81,7 @@ along with this program. If not, see ."; { GameToken token = this.GetToken(); - string message = await new StreamReader(this.Request.Body).ReadToEndAsync(); + string message = await this.ReadBodyAsync(); if (message.StartsWith("/setemail ")) { diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs index fda3c09a..277f7a2c 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/Slots/ScoreController.cs @@ -77,8 +77,7 @@ public class ScoreController : ControllerBase if (!score.PlayerIds.Contains(username)) { - this.Request.Body.Position = 0; - string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync(); + string bodyString = await this.ReadBodyAsync(); Logger.Warn("Rejecting score upload, requester username is not present in playerIds" + $" (user={username}, playerIds={string.Join(",", score.PlayerIds)}, " + $"gameVersion={token.GameVersion.ToPrettyString()}, type={score.Type}, id={id}, slotType={slotType}, body='{bodyString}')", LogArea.Score); diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/UserController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/UserController.cs index 54546354..143cd16d 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/UserController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/UserController.cs @@ -182,8 +182,9 @@ public class UserController : ControllerBase User? user = await this.database.UserFromGameToken(this.GetToken()); if (user == null) return this.StatusCode(403, ""); - string pinsString = await new StreamReader(this.Request.Body).ReadToEndAsync(); - Pins? pinJson = JsonSerializer.Deserialize(pinsString); + string bodyString = await this.ReadBodyAsync(); + + Pins? pinJson = JsonSerializer.Deserialize(bodyString); if (pinJson == null) return this.BadRequest(); // Sometimes the update gets called periodically as pin progress updates via playing, diff --git a/ProjectLighthouse/Extensions/ControllerExtensions.cs b/ProjectLighthouse/Extensions/ControllerExtensions.cs index bee96926..179587cd 100644 --- a/ProjectLighthouse/Extensions/ControllerExtensions.cs +++ b/ProjectLighthouse/Extensions/ControllerExtensions.cs @@ -23,10 +23,20 @@ public static class ControllerExtensions return token; } + public static async Task ReadBodyAsync(this ControllerBase controller) + { + controller.Request.Body.Position = 0; + + using StreamReader bodyReader = new(controller.Request.Body); + return await bodyReader.ReadToEndAsync(); + } + public static async Task DeserializeBody(this ControllerBase controller, params string[] rootElements) { controller.Request.Body.Position = 0; - string bodyString = await new StreamReader(controller.Request.Body).ReadToEndAsync(); + + using StreamReader bodyReader = new(controller.Request.Body); + string bodyString = await bodyReader.ReadToEndAsync(); try {