Remove usages of Newtonsoft.Json from StatusHost (#2405)

This commit is contained in:
Pieter-Jan Briers
2022-01-10 01:36:00 +01:00
committed by GitHub
parent 4d6183d6af
commit 43059b3985
7 changed files with 39 additions and 71 deletions

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Http;
using Microsoft.Extensions.Primitives;
@@ -15,8 +14,7 @@ namespace Robust.Server.ServerStatus
bool IsGetLike { get; }
IReadOnlyDictionary<string, StringValues> RequestHeaders { get; }
[return: MaybeNull]
public T RequestBodyJson<T>();
public T? RequestBodyJson<T>();
void Respond(
string text,

View File

@@ -1,5 +1,5 @@
using System;
using Newtonsoft.Json.Linq;
using System.Text.Json.Nodes;
namespace Robust.Server.ServerStatus
{
@@ -16,7 +16,7 @@ namespace Robust.Server.ServerStatus
/// I REPEAT, THIS DOES NOT RUN ON THE MAIN THREAD.
/// MAKE TRIPLE SURE EVERYTHING IN HERE IS THREAD SAFE DEAR GOD.
/// </summary>
event Action<JObject> OnStatusRequest;
event Action<JsonNode> OnStatusRequest;
/// <summary>
/// Invoked when a client queries an info request from the server.
@@ -24,7 +24,7 @@ namespace Robust.Server.ServerStatus
/// I REPEAT, THIS DOES NOT RUN ON THE MAIN THREAD.
/// MAKE TRIPLE SURE EVERYTHING IN HERE IS THREAD SAFE DEAR GOD.
/// </summary>
event Action<JObject> OnInfoRequest;
event Action<JsonNode> OnInfoRequest;
/// <summary>
/// Set information used by automatic-client-zipping to determine the layout of your dev setup,

View File

@@ -6,10 +6,8 @@ using System.IO;
using System.IO.Compression;
using System.Net;
using System.Security.Cryptography;
using Newtonsoft.Json.Linq;
using Robust.Shared;
using Robust.Shared.ContentPack;
using Robust.Shared.Utility;
namespace Robust.Server.ServerStatus
{

View File

@@ -1,7 +1,7 @@
using System;
using System.Threading.Tasks;
using System.Net;
using Newtonsoft.Json.Linq;
using System.Text.Json.Nodes;
using Robust.Shared;
namespace Robust.Server.ServerStatus
@@ -36,7 +36,7 @@ namespace Robust.Server.ServerStatus
return false;
}
var jObject = new JObject
var jObject = new JsonObject
{
// We need to send at LEAST name and player count to have the launcher work with us.
// Content can override these if it wants (e.g. stealthmins).
@@ -60,7 +60,7 @@ namespace Robust.Server.ServerStatus
var downloadUrl = _configurationManager.GetCVar(CVars.BuildDownloadUrl);
JObject? buildInfo;
JsonObject? buildInfo;
if (string.IsNullOrEmpty(downloadUrl))
{
@@ -74,7 +74,7 @@ namespace Robust.Server.ServerStatus
hash = null;
}
buildInfo = new JObject
buildInfo = new JsonObject
{
["engine_version"] = _configurationManager.GetCVar(CVars.BuildEngineVersion),
["fork_id"] = _configurationManager.GetCVar(CVars.BuildForkId),
@@ -84,7 +84,7 @@ namespace Robust.Server.ServerStatus
};
}
var authInfo = new JObject
var authInfo = new JsonObject
{
["mode"] = _netManager.Auth.ToString(),
["public_key"] = _netManager.RsaPublicKey != null
@@ -92,7 +92,7 @@ namespace Robust.Server.ServerStatus
: null
};
var jObject = new JObject
var jObject = new JsonObject
{
["connect_address"] = _configurationManager.GetCVar(CVars.StatusConnectAddress),
["auth"] = authInfo,
@@ -106,7 +106,7 @@ namespace Robust.Server.ServerStatus
return true;
}
private async Task<JObject?> PrepareACZBuildInfo()
private async Task<JsonObject?> PrepareACZBuildInfo()
{
var acz = await PrepareACZ();
if (acz == null) return null;
@@ -121,7 +121,7 @@ namespace Robust.Server.ServerStatus
{
fork = "custom";
}
return new JObject
return new JsonObject
{
["engine_version"] = engineVersion,
["fork_id"] = fork,

View File

@@ -1,15 +1,15 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Mime;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Robust.Server.Player;
using Robust.Shared;
using Robust.Shared.Configuration;
@@ -18,7 +18,6 @@ using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Network;
using Robust.Shared.Utility;
using Robust.Shared.Exceptions;
using HttpListener = ManagedHttpListener.HttpListener;
using HttpListenerContext = ManagedHttpListener.HttpListenerContext;
@@ -36,7 +35,6 @@ namespace Robust.Server.ServerStatus
[Dependency] private readonly IServerNetManager _netManager = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
private static readonly JsonSerializer JsonSerializer = new();
private readonly List<StatusHostHandlerAsync> _handlers = new();
private HttpListener? _listener;
private TaskCompletionSource? _stopSource;
@@ -77,9 +75,9 @@ namespace Robust.Server.ServerStatus
*/
}
public event Action<JObject>? OnStatusRequest;
public event Action<JsonNode>? OnStatusRequest;
public event Action<JObject>? OnInfoRequest;
public event Action<JsonNode>? OnInfoRequest;
public void AddHandler(StatusHostHandler handler)
{
@@ -164,7 +162,7 @@ namespace Robust.Server.ServerStatus
if (File.Exists(path))
{
var buildInfo = File.ReadAllText(path);
var info = JsonConvert.DeserializeObject<BuildInfo>(buildInfo)!;
var info = JsonSerializer.Deserialize<BuildInfo>(buildInfo)!;
// Don't replace cvars with contents of build.json if overriden by --cvar or such.
SetCVarIfUnmodified(CVars.BuildEngineVersion, info.EngineVersion);
@@ -200,17 +198,17 @@ namespace Robust.Server.ServerStatus
_listener!.Stop();
}
#pragma warning disable CS0649
[JsonObject(ItemRequired = Required.DisallowNull)]
private sealed class BuildInfo
{
[JsonProperty("engine_version")] public string EngineVersion = default!;
[JsonProperty("hash")] public string? Hash;
[JsonProperty("download")] public string? Download = default;
[JsonProperty("fork_id")] public string ForkId = default!;
[JsonProperty("version")] public string Version = default!;
}
#pragma warning restore CS0649
private sealed record BuildInfo(
[property: JsonPropertyName("engine_version")]
string EngineVersion,
[property: JsonPropertyName("hash")]
string? Hash,
[property: JsonPropertyName("download")]
string? Download,
[property: JsonPropertyName("fork_id")]
string ForkId,
[property: JsonPropertyName("version")]
string Version);
private sealed class ContextImpl : IStatusHandlerContext
{
@@ -240,11 +238,7 @@ namespace Robust.Server.ServerStatus
public T? RequestBodyJson<T>()
{
using var streamReader = new StreamReader(_context.Request.InputStream, EncodingHelpers.UTF8);
using var jsonReader = new JsonTextReader(streamReader);
var serializer = new JsonSerializer();
return serializer.Deserialize<T>(jsonReader);
return JsonSerializer.Deserialize<T>(_context.Request.InputStream);
}
public void Respond(string text, HttpStatusCode code = HttpStatusCode.OK, string contentType = MediaTypeNames.Text.Plain)
@@ -300,15 +294,11 @@ namespace Robust.Server.ServerStatus
public void RespondJson(object jsonData, HttpStatusCode code = HttpStatusCode.OK)
{
using var streamWriter = new StreamWriter(_context.Response.OutputStream, EncodingHelpers.UTF8);
_context.Response.ContentType = MediaTypeNames.Application.Json;
using var jsonWriter = new JsonTextWriter(streamWriter);
JsonSerializer.Serialize(_context.Response.OutputStream, jsonData);
JsonSerializer.Serialize(jsonWriter, jsonData);
jsonWriter.Flush();
_context.Response.Close();
}
}
}

View File

@@ -1,21 +0,0 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using Robust.Shared.Utility;
namespace Robust.Server.ServerStatus
{
public static class StatusHostHelpers
{
[return: MaybeNull]
public static T GetFromJson<T>(this HttpListenerRequest request)
{
using var streamReader = new StreamReader(request.InputStream, EncodingHelpers.UTF8);
using var jsonReader = new JsonTextReader(streamReader);
var serializer = new JsonSerializer();
return serializer.Deserialize<T>(jsonReader);
}
}
}

View File

@@ -3,8 +3,8 @@ using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Robust.Shared;
using Robust.Shared.Asynchronous;
using Robust.Shared.Configuration;
@@ -85,7 +85,11 @@ namespace Robust.Server.ServerStatus
return false;
}
var auth = context.RequestHeaders["WatchdogToken"];
if (!context.RequestHeaders.TryGetValue("WatchdogToken", out var auth))
{
context.Respond("Expected WatchdogToken header", HttpStatusCode.BadRequest);
return true;
}
if (auth != _watchdogToken)
{
@@ -101,7 +105,7 @@ namespace Robust.Server.ServerStatus
{
parameters = context.RequestBodyJson<ShutdownParameters>();
}
catch (JsonSerializationException)
catch (JsonException)
{
// parameters null so it'll catch the block down below.
}
@@ -183,7 +187,6 @@ namespace Robust.Server.ServerStatus
private sealed class ShutdownParameters
{
// ReSharper disable once RedundantDefaultMemberInitializer
[JsonProperty(Required = Required.Always)]
public string Reason { get; set; } = default!;
}
}