From 0086427477102618ff5c14c5ec370965490466b4 Mon Sep 17 00:00:00 2001 From: Vitalii Mikhailov Date: Tue, 26 Mar 2024 22:59:29 +0200 Subject: [PATCH] Fix --- .../BUTR.CrashReportServer.csproj | 1 - .../Controllers/ReportController.cs | 25 ++++++------------- .../Services/GZipCompressor.cs | 2 ++ src/BUTR.CrashReportServer/Startup.cs | 19 ++++++++++++++ 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/BUTR.CrashReportServer/BUTR.CrashReportServer.csproj b/src/BUTR.CrashReportServer/BUTR.CrashReportServer.csproj index a88b9c8..33a4a35 100644 --- a/src/BUTR.CrashReportServer/BUTR.CrashReportServer.csproj +++ b/src/BUTR.CrashReportServer/BUTR.CrashReportServer.csproj @@ -17,7 +17,6 @@ - diff --git a/src/BUTR.CrashReportServer/Controllers/ReportController.cs b/src/BUTR.CrashReportServer/Controllers/ReportController.cs index bc083bc..84dc8aa 100644 --- a/src/BUTR.CrashReportServer/Controllers/ReportController.cs +++ b/src/BUTR.CrashReportServer/Controllers/ReportController.cs @@ -1,7 +1,6 @@ using BUTR.CrashReportServer.Contexts; using BUTR.CrashReportServer.Models; using BUTR.CrashReportServer.Models.API; -using BUTR.CrashReportServer.Models.Database; using BUTR.CrashReportServer.Models.Sitemaps; using BUTR.CrashReportServer.Options; using BUTR.CrashReportServer.Services; @@ -21,7 +20,6 @@ using System.Net; using System.Runtime.CompilerServices; using System.Security.Authentication; -using System.Text; using System.Threading; using System.Threading.Tasks; @@ -31,10 +29,7 @@ namespace BUTR.CrashReportServer.Controllers; [Route("/report")] public class ReportController : ControllerBase { - public sealed record GetNewCrashReportsBody - { - public required DateTime DateTime { get; init; } - } + public sealed record GetNewCrashReportsBody(DateTime DateTime); private readonly ILogger _logger; private readonly ReportOptions _options; @@ -58,7 +53,7 @@ public ReportController(ILogger logger, IOptionsSnapshot fileName?.Length is 6 or 8 or 10 && fileName.All(IsHex); - private StatusCodeResult? ValidateRequest(ref string filename) + private StatusCodeResult? ValidateRequest(string filename) { if (string.IsNullOrEmpty(filename)) return StatusCode((int) HttpStatusCode.InternalServerError); @@ -73,7 +68,7 @@ public ReportController(ILogger logger, IOptionsSnapshot GetHtml(string filename, CancellationToken ct) { - if (ValidateRequest(ref filename) is { } errorResponse) + if (ValidateRequest(filename) is { } errorResponse) return errorResponse; if (await _dbContext.FileEntities.FirstOrDefaultAsync(x => x.Id!.FileId == filename, ct) is not { } file) @@ -82,26 +77,20 @@ private async Task GetHtml(string filename, CancellationToken ct) if (Request.GetTypedHeaders().AcceptEncoding.Any(x => x.Value.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))) { Response.Headers.ContentEncoding = "gzip"; - return File(file.DataCompressed, "text/html; charset=utf-8", true); + return File(file.DataCompressed, "text/html; charset=utf-8", false); } - return File(await _gZipCompressor.DecompressAsync(file.DataCompressed, ct), "text/html; charset=utf-8", true); + return File(await _gZipCompressor.DecompressAsync(file.DataCompressed, ct), "text/html; charset=utf-8", false); } private async Task GetJson(string filename, CancellationToken ct) { - if (ValidateRequest(ref filename) is { } errorResponse) + if (ValidateRequest(filename) is { } errorResponse) return errorResponse; if (await _dbContext.JsonEntities.FirstOrDefaultAsync(x => x.Id!.FileId == filename, ct) is not { } file) return StatusCode(StatusCodes.Status404NotFound); - if (Request.GetTypedHeaders().AcceptEncoding.Any(x => x.Value.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))) - { - Response.Headers.ContentEncoding = "gzip"; - return File(await _gZipCompressor.CompressAsync(new MemoryStream(Encoding.UTF8.GetBytes(file.CrashReport)), ct), "application/json; charset=utf-8", true); - - } - return File(new MemoryStream(Encoding.UTF8.GetBytes(file.CrashReport)), "application/json; charset=utf-8", true); + return Content(file.CrashReport, "application/json; charset=utf-8"); } [AllowAnonymous] diff --git a/src/BUTR.CrashReportServer/Services/GZipCompressor.cs b/src/BUTR.CrashReportServer/Services/GZipCompressor.cs index 4a28783..6b80242 100644 --- a/src/BUTR.CrashReportServer/Services/GZipCompressor.cs +++ b/src/BUTR.CrashReportServer/Services/GZipCompressor.cs @@ -28,6 +28,7 @@ public async Task CompressAsync(Stream decompressedStream, Cancell var compressedStream = _streamManager.GetStream(); await using var zipStream = new GZipStream(compressedStream, CompressionMode.Compress, true); await decompressedStream.CopyToAsync(zipStream, ct); + compressedStream.Seek(0, SeekOrigin.Begin); return compressedStream; } @@ -42,6 +43,7 @@ public async Task DecompressAsync(Stream compressedStream, Cancell var decompressedStream = _streamManager.GetStream(); await using var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress, true); await zipStream.CopyToAsync(decompressedStream, ct); + decompressedStream.Seek(0, SeekOrigin.Begin); return decompressedStream; } } \ No newline at end of file diff --git a/src/BUTR.CrashReportServer/Startup.cs b/src/BUTR.CrashReportServer/Startup.cs index 789a10f..65836ce 100644 --- a/src/BUTR.CrashReportServer/Startup.cs +++ b/src/BUTR.CrashReportServer/Startup.cs @@ -17,11 +17,13 @@ using System; using System.IO; +using System.IO.Compression; using System.Linq; using System.Reflection; using System.Security.Cryptography; using System.Text.Json; using System.Text.Json.Serialization; +using Microsoft.AspNetCore.ResponseCompression; namespace BUTR.CrashReportServer; @@ -112,6 +114,22 @@ public void ConfigureServices(IServiceCollection services) opts.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); }); + services.AddResponseCompression(opts => + { + opts.EnableForHttps = true; + opts.Providers.Add(); + opts.Providers.Add(); + }); + services.Configure(options => + { + options.Level = CompressionLevel.Fastest; + }); + + services.Configure(options => + { + options.Level = CompressionLevel.SmallestSize; + }); + services.AddResponseCaching(); } @@ -128,6 +146,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseRouting(); app.UseResponseCaching(); + app.UseResponseCompression(); app.UseAuthentication(); app.UseAuthorization();