From 42f9eca5d9b877a1daa5df0c09d838f1833d9955 Mon Sep 17 00:00:00 2001 From: Paul Volavsek Date: Mon, 16 Oct 2023 14:54:19 +0200 Subject: [PATCH 1/6] fix casing --- src/fiskaltrust.Launcher/Services/HostingService.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/fiskaltrust.Launcher/Services/HostingService.cs b/src/fiskaltrust.Launcher/Services/HostingService.cs index 3bcd8661..683e1225 100644 --- a/src/fiskaltrust.Launcher/Services/HostingService.cs +++ b/src/fiskaltrust.Launcher/Services/HostingService.cs @@ -18,6 +18,7 @@ using System.Security.Cryptography.X509Certificates; using System.Runtime.Versioning; using Microsoft.AspNetCore.Server.HttpSys; +using System.Text.Json; namespace fiskaltrust.Launcher.Services { @@ -133,6 +134,7 @@ private WebApplication CreateRestHost(WebApplicationBuilder builder, Uri uri, { options.SerializerOptions.NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString; options.SerializerOptions.Converters.Add(new NumberToStringConverter()); + options.SerializerOptions.PropertyNamingPolicy = null; }); builder.WebHost.ConfigureBinding(uri, listenOptions => ConfigureTls(listenOptions), isHttps: !string.IsNullOrEmpty(_launcherConfiguration.TlsCertificatePath) || !string.IsNullOrEmpty(_launcherConfiguration.TlsCertificateBase64), allowSynchronousIO: true, useHttpSys: _launcherConfiguration.UseHttpSysBinding!.Value); From 834ecf0efdf8201b6079496112ca70a1463f3fcb Mon Sep 17 00:00:00 2001 From: Paul Volavsek Date: Tue, 17 Oct 2023 17:32:42 +0200 Subject: [PATCH 2/6] add datetime deserialization backwards compatibility --- .../Helpers/DateTimeConverter.cs | 69 +++++++++++++++++++ .../Helpers/NumberToStringConverter.cs | 2 +- .../Services/HostingService.cs | 2 + 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/fiskaltrust.Launcher/Helpers/DateTimeConverter.cs diff --git a/src/fiskaltrust.Launcher/Helpers/DateTimeConverter.cs b/src/fiskaltrust.Launcher/Helpers/DateTimeConverter.cs new file mode 100644 index 00000000..405fe1c8 --- /dev/null +++ b/src/fiskaltrust.Launcher/Helpers/DateTimeConverter.cs @@ -0,0 +1,69 @@ +using System.Globalization; +using System.Text.Json; +using System.Text.Json.Serialization; +using System.Text.RegularExpressions; + +namespace fiskaltrust.Launcher.Helpers +{ + sealed class DateTimeConverter : JsonConverter + { + private readonly static JsonConverter DEFAULT_CONVERTER = (JsonConverter)JsonSerializerOptions.Default.GetConverter(typeof(DateTime)); + static readonly DateTime EPOCH = new DateTime(1970, 1, 1, 0, 0, 0); + static readonly DateTimeOffset EPOCH_OFFSET = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero); + + static readonly Regex REGEX = new Regex("^\\\\?/Date\\(([+-]*\\d+)\\)\\\\?/$", RegexOptions.CultureInvariant); + + static readonly Regex REGEX_OFFSET = new Regex("^\\\\?/Date\\(([+-]*\\d+)([+-])(\\d{2})(\\d{2})\\)\\\\?/$", RegexOptions.CultureInvariant); + + public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + string formatted = reader.GetString()!; + + { + Match match = REGEX_OFFSET.Match(formatted); + + if (match.Success) + { + return ReadDateTimeOffset(match).DateTime; + } + } + + { + Match match = REGEX.Match(formatted); + + if (match.Success) + { + return ReadDateTime(match); + } + } + + return DEFAULT_CONVERTER.Read(ref reader, typeToConvert, options); + } + + private DateTime ReadDateTime(Match date) + { + if (!long.TryParse(date.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out long unixTime)) + { + throw new JsonException(); + } + + return EPOCH.AddMilliseconds(unixTime); + } + + private DateTimeOffset ReadDateTimeOffset(Match date) + { + if (!long.TryParse(date.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out long unixTime) + || !int.TryParse(date.Groups[3].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int hours) + || !int.TryParse(date.Groups[4].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int minutes)) + { + throw new JsonException(); + } + + int sign = date.Groups[2].Value[0] == '+' ? 1 : -1; + TimeSpan utcOffset = new TimeSpan(hours * sign, minutes * sign, 0); + return EPOCH_OFFSET.AddMilliseconds(unixTime).ToOffset(utcOffset); + } + + public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) => DEFAULT_CONVERTER.Write(writer, value, options); + } +} \ No newline at end of file diff --git a/src/fiskaltrust.Launcher/Helpers/NumberToStringConverter.cs b/src/fiskaltrust.Launcher/Helpers/NumberToStringConverter.cs index 9de881f1..c2ae18b2 100644 --- a/src/fiskaltrust.Launcher/Helpers/NumberToStringConverter.cs +++ b/src/fiskaltrust.Launcher/Helpers/NumberToStringConverter.cs @@ -15,7 +15,7 @@ public class NumberToStringConverter : JsonConverter return number.ToString(CultureInfo.InvariantCulture); } - if (reader.TryGetDouble(out var doubleNumber)) + if (reader.TryGetDecimal(out var doubleNumber)) { return doubleNumber.ToString(CultureInfo.InvariantCulture); } diff --git a/src/fiskaltrust.Launcher/Services/HostingService.cs b/src/fiskaltrust.Launcher/Services/HostingService.cs index 683e1225..d3f45a68 100644 --- a/src/fiskaltrust.Launcher/Services/HostingService.cs +++ b/src/fiskaltrust.Launcher/Services/HostingService.cs @@ -134,7 +134,9 @@ private WebApplication CreateRestHost(WebApplicationBuilder builder, Uri uri, { options.SerializerOptions.NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString; options.SerializerOptions.Converters.Add(new NumberToStringConverter()); + options.SerializerOptions.Converters.Add(new DateTimeConverter()); options.SerializerOptions.PropertyNamingPolicy = null; + options.SerializerOptions.DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull; }); builder.WebHost.ConfigureBinding(uri, listenOptions => ConfigureTls(listenOptions), isHttps: !string.IsNullOrEmpty(_launcherConfiguration.TlsCertificatePath) || !string.IsNullOrEmpty(_launcherConfiguration.TlsCertificateBase64), allowSynchronousIO: true, useHttpSys: _launcherConfiguration.UseHttpSysBinding!.Value); From e7a780c2aae9e5a77ac87326bad38b4ae336bd92 Mon Sep 17 00:00:00 2001 From: Paul Volavsek Date: Tue, 17 Oct 2023 17:34:50 +0200 Subject: [PATCH 3/6] use default converter --- .../Helpers/NumberToStringConverter.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/fiskaltrust.Launcher/Helpers/NumberToStringConverter.cs b/src/fiskaltrust.Launcher/Helpers/NumberToStringConverter.cs index c2ae18b2..30b2fccb 100644 --- a/src/fiskaltrust.Launcher/Helpers/NumberToStringConverter.cs +++ b/src/fiskaltrust.Launcher/Helpers/NumberToStringConverter.cs @@ -6,6 +6,8 @@ namespace fiskaltrust.Launcher.Helpers { public class NumberToStringConverter : JsonConverter { + private readonly static JsonConverter DEFAULT_CONVERTER = (JsonConverter)JsonSerializerOptions.Default.GetConverter(typeof(string)); + public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType == JsonTokenType.Number) @@ -29,9 +31,6 @@ public class NumberToStringConverter : JsonConverter return null; } - public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) - { - writer.WriteStringValue(value.ToString()); - } + public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) => DEFAULT_CONVERTER.Write(writer, value, options); } } \ No newline at end of file From 07de92666866a7af91ff382ac9d21aa46cec3d58 Mon Sep 17 00:00:00 2001 From: Paul Volavsek Date: Tue, 17 Oct 2023 17:40:58 +0200 Subject: [PATCH 4/6] review fixes --- .../{DateTimeConverter.cs => CustomDateTimeConverter.cs} | 2 +- src/fiskaltrust.Launcher/Services/HostingService.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/fiskaltrust.Launcher/Helpers/{DateTimeConverter.cs => CustomDateTimeConverter.cs} (97%) diff --git a/src/fiskaltrust.Launcher/Helpers/DateTimeConverter.cs b/src/fiskaltrust.Launcher/Helpers/CustomDateTimeConverter.cs similarity index 97% rename from src/fiskaltrust.Launcher/Helpers/DateTimeConverter.cs rename to src/fiskaltrust.Launcher/Helpers/CustomDateTimeConverter.cs index 405fe1c8..b687d014 100644 --- a/src/fiskaltrust.Launcher/Helpers/DateTimeConverter.cs +++ b/src/fiskaltrust.Launcher/Helpers/CustomDateTimeConverter.cs @@ -5,7 +5,7 @@ namespace fiskaltrust.Launcher.Helpers { - sealed class DateTimeConverter : JsonConverter + sealed class CustomDateTimeConverter : JsonConverter { private readonly static JsonConverter DEFAULT_CONVERTER = (JsonConverter)JsonSerializerOptions.Default.GetConverter(typeof(DateTime)); static readonly DateTime EPOCH = new DateTime(1970, 1, 1, 0, 0, 0); diff --git a/src/fiskaltrust.Launcher/Services/HostingService.cs b/src/fiskaltrust.Launcher/Services/HostingService.cs index d3f45a68..102fce31 100644 --- a/src/fiskaltrust.Launcher/Services/HostingService.cs +++ b/src/fiskaltrust.Launcher/Services/HostingService.cs @@ -134,7 +134,7 @@ private WebApplication CreateRestHost(WebApplicationBuilder builder, Uri uri, { options.SerializerOptions.NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString; options.SerializerOptions.Converters.Add(new NumberToStringConverter()); - options.SerializerOptions.Converters.Add(new DateTimeConverter()); + options.SerializerOptions.Converters.Add(new CustomDateTimeConverter()); options.SerializerOptions.PropertyNamingPolicy = null; options.SerializerOptions.DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull; }); From b1d2680f159411157a956687220b9e80843387a9 Mon Sep 17 00:00:00 2001 From: Paul Volavsek Date: Tue, 17 Oct 2023 17:47:34 +0200 Subject: [PATCH 5/6] add unsafe relaxed encoding --- src/fiskaltrust.Launcher/Services/HostingService.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/fiskaltrust.Launcher/Services/HostingService.cs b/src/fiskaltrust.Launcher/Services/HostingService.cs index 102fce31..7ebdb262 100644 --- a/src/fiskaltrust.Launcher/Services/HostingService.cs +++ b/src/fiskaltrust.Launcher/Services/HostingService.cs @@ -135,6 +135,7 @@ private WebApplication CreateRestHost(WebApplicationBuilder builder, Uri uri, options.SerializerOptions.NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString; options.SerializerOptions.Converters.Add(new NumberToStringConverter()); options.SerializerOptions.Converters.Add(new CustomDateTimeConverter()); + options.SerializerOptions.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping; options.SerializerOptions.PropertyNamingPolicy = null; options.SerializerOptions.DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull; }); From c7233b0ea124095c85f1619ccde531d293895b64 Mon Sep 17 00:00:00 2001 From: Paul Volavsek Date: Tue, 17 Oct 2023 17:54:31 +0200 Subject: [PATCH 6/6] update version --- version.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.json b/version.json index 773fde25..2655135a 100644 --- a/version.json +++ b/version.json @@ -1,5 +1,5 @@ { - "version": "2.0.0-rc.9", + "version": "2.0.0-rc.10", "releaseBranches": [ "^refs/tags/v\\d+(?:\\.\\d+)*(?:-.*)?$" ]