Properly dispose of StreamReaders

This commit is contained in:
Slendy 2023-02-11 19:08:44 -06:00
parent 75a5e70635
commit 14fa5e9328
No known key found for this signature in database
GPG key ID: 7288D68361B91428
5 changed files with 17 additions and 7 deletions

View file

@ -44,7 +44,7 @@ public class MatchController : ControllerBase
// Example POST /match: [UpdateMyPlayerData,["Player":"FireGamer9872"]] // 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(); if (bodyString.Length == 0 || bodyString[0] != '[') return this.BadRequest();

View file

@ -81,7 +81,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.";
{ {
GameToken token = this.GetToken(); GameToken token = this.GetToken();
string message = await new StreamReader(this.Request.Body).ReadToEndAsync(); string message = await this.ReadBodyAsync();
if (message.StartsWith("/setemail ")) if (message.StartsWith("/setemail "))
{ {

View file

@ -77,8 +77,7 @@ public class ScoreController : ControllerBase
if (!score.PlayerIds.Contains(username)) if (!score.PlayerIds.Contains(username))
{ {
this.Request.Body.Position = 0; string bodyString = await this.ReadBodyAsync();
string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync();
Logger.Warn("Rejecting score upload, requester username is not present in playerIds" + Logger.Warn("Rejecting score upload, requester username is not present in playerIds" +
$" (user={username}, playerIds={string.Join(",", score.PlayerIds)}, " + $" (user={username}, playerIds={string.Join(",", score.PlayerIds)}, " +
$"gameVersion={token.GameVersion.ToPrettyString()}, type={score.Type}, id={id}, slotType={slotType}, body='{bodyString}')", LogArea.Score); $"gameVersion={token.GameVersion.ToPrettyString()}, type={score.Type}, id={id}, slotType={slotType}, body='{bodyString}')", LogArea.Score);

View file

@ -182,8 +182,9 @@ public class UserController : ControllerBase
User? user = await this.database.UserFromGameToken(this.GetToken()); User? user = await this.database.UserFromGameToken(this.GetToken());
if (user == null) return this.StatusCode(403, ""); if (user == null) return this.StatusCode(403, "");
string pinsString = await new StreamReader(this.Request.Body).ReadToEndAsync(); string bodyString = await this.ReadBodyAsync();
Pins? pinJson = JsonSerializer.Deserialize<Pins>(pinsString);
Pins? pinJson = JsonSerializer.Deserialize<Pins>(bodyString);
if (pinJson == null) return this.BadRequest(); if (pinJson == null) return this.BadRequest();
// Sometimes the update gets called periodically as pin progress updates via playing, // Sometimes the update gets called periodically as pin progress updates via playing,

View file

@ -23,10 +23,20 @@ public static class ControllerExtensions
return token; return token;
} }
public static async Task<string> ReadBodyAsync(this ControllerBase controller)
{
controller.Request.Body.Position = 0;
using StreamReader bodyReader = new(controller.Request.Body);
return await bodyReader.ReadToEndAsync();
}
public static async Task<T?> DeserializeBody<T>(this ControllerBase controller, params string[] rootElements) public static async Task<T?> DeserializeBody<T>(this ControllerBase controller, params string[] rootElements)
{ {
controller.Request.Body.Position = 0; 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 try
{ {