mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-15 06:02:28 +00:00
Properly dispose of StreamReaders
This commit is contained in:
parent
75a5e70635
commit
14fa5e9328
5 changed files with 17 additions and 7 deletions
|
@ -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();
|
||||
|
||||
|
|
|
@ -81,7 +81,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.";
|
|||
{
|
||||
GameToken token = this.GetToken();
|
||||
|
||||
string message = await new StreamReader(this.Request.Body).ReadToEndAsync();
|
||||
string message = await this.ReadBodyAsync();
|
||||
|
||||
if (message.StartsWith("/setemail "))
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<Pins>(pinsString);
|
||||
string bodyString = await this.ReadBodyAsync();
|
||||
|
||||
Pins? pinJson = JsonSerializer.Deserialize<Pins>(bodyString);
|
||||
if (pinJson == null) return this.BadRequest();
|
||||
|
||||
// Sometimes the update gets called periodically as pin progress updates via playing,
|
||||
|
|
|
@ -23,10 +23,20 @@ public static class ControllerExtensions
|
|||
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)
|
||||
{
|
||||
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
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue