From b692336c07d9db929d995bba0dfe051953b5ef1d Mon Sep 17 00:00:00 2001 From: honzapatCZ Date: Thu, 11 Sep 2025 13:34:29 +0200 Subject: [PATCH] make all header file responses ascii compliant --- Controllers/ResponseEnricher.cs | 36 ++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/Controllers/ResponseEnricher.cs b/Controllers/ResponseEnricher.cs index 77f3466..0ba829a 100644 --- a/Controllers/ResponseEnricher.cs +++ b/Controllers/ResponseEnricher.cs @@ -1,3 +1,4 @@ +using System.Globalization; using System.Text; using System.Xml; using System.Xml.Serialization; @@ -9,11 +10,44 @@ namespace NejCommon.Controllers; public static class Responses { + public static string ToAscii(string input) + { + if (string.IsNullOrEmpty(input)) + return input; + + // Normalize to FormD (decomposition) + string normalized = input.Normalize(NormalizationForm.FormD); + + var sb = new StringBuilder(); + + foreach (char c in normalized) + { + var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c); + // Skip non-spacing marks (accents, umlauts, etc.) + if (unicodeCategory != UnicodeCategory.NonSpacingMark) + { + sb.Append(c); + } + } + + // Normalize back to FormC and remove any remaining non-ASCII chars + string cleaned = sb.ToString().Normalize(NormalizationForm.FormC); + + // Ensure only ASCII characters remain + var ascii = new StringBuilder(); + foreach (char c in cleaned) + { + ascii.Append(c <= 127 ? c : '?'); // replace non-ASCII with '?' or remove + } + + return ascii.ToString(); + } + public static FileStreamHttpResult RespondFileStream(this ControllerBase controller, Stream stream, string contentType, string fileName) { stream.Seek(0, SeekOrigin.Begin); - controller.Response.Headers.Add("Content-Disposition", "inline; filename=" + fileName); + controller.Response.Headers.Add("Content-Disposition", "inline; filename=" + ToAscii(fileName.Normalize(NormalizationForm.FormD))); return TypedResults.File(stream, contentType, fileName); } public static FileStreamHttpResult RespondXlsx(this ControllerBase controller, XLWorkbook file, string fileName)