| | | 1 | | using Kestrun.Logging; |
| | | 2 | | using Microsoft.AspNetCore.RequestDecompression; |
| | | 3 | | using Microsoft.Net.Http.Headers; |
| | | 4 | | using Serilog.Events; |
| | | 5 | | |
| | | 6 | | namespace Kestrun.Hosting.Compression; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Extension methods for request decompression middleware. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class KrRequestDecompressionExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Adds request decompression middleware with optional configuration. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="host">The Kestrun host.</param> |
| | | 17 | | /// <param name="configure">Optional configuration delegate.</param> |
| | | 18 | | /// <returns>The configured host.</returns> |
| | | 19 | | public static KestrunHost AddRequestDecompression(this KestrunHost host, Action<RequestDecompressionOptions>? config |
| | | 20 | | { |
| | 2 | 21 | | ArgumentNullException.ThrowIfNull(host); |
| | | 22 | | |
| | 2 | 23 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 24 | | { |
| | 0 | 25 | | host.Logger.Debug("Adding request decompression (custom config: {HasConfig})", configure != null); |
| | | 26 | | } |
| | | 27 | | |
| | 2 | 28 | | _ = host.AddService(services => |
| | 2 | 29 | | { |
| | 2 | 30 | | _ = configure == null ? services.AddRequestDecompression() : services.AddRequestDecompression(configure); |
| | 4 | 31 | | }); |
| | | 32 | | |
| | 4 | 33 | | return host.Use(app => app.UseRequestDecompression()); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Adds request decompression middleware using allowed encodings. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="host">The Kestrun host.</param> |
| | | 40 | | /// <param name="allowedEncodings">The allowed encodings (gzip, deflate, br).</param> |
| | | 41 | | /// <returns>The configured host.</returns> |
| | | 42 | | public static KestrunHost AddRequestDecompression(this KestrunHost host, IEnumerable<string>? allowedEncodings) |
| | | 43 | | { |
| | 2 | 44 | | if (allowedEncodings == null) |
| | | 45 | | { |
| | 0 | 46 | | return host.AddRequestDecompression(); |
| | | 47 | | } |
| | | 48 | | |
| | 2 | 49 | | var encodingSet = new HashSet<string>(allowedEncodings, StringComparer.OrdinalIgnoreCase); |
| | | 50 | | |
| | 4 | 51 | | _ = host.Use(app => app.Use(async (ctx, next) => |
| | 4 | 52 | | { |
| | 2 | 53 | | var encHeader = ctx.Request.Headers[HeaderNames.ContentEncoding].ToString(); |
| | 2 | 54 | | if (!string.IsNullOrWhiteSpace(encHeader)) |
| | 4 | 55 | | { |
| | 2 | 56 | | var encodings = encHeader.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntr |
| | 7 | 57 | | foreach (var encoding in encodings) |
| | 4 | 58 | | { |
| | 2 | 59 | | if (encoding.Equals("identity", StringComparison.OrdinalIgnoreCase)) |
| | 4 | 60 | | { |
| | 4 | 61 | | continue; |
| | 4 | 62 | | } |
| | 4 | 63 | | |
| | 2 | 64 | | if (!encodingSet.Contains(encoding)) |
| | 4 | 65 | | { |
| | 1 | 66 | | host.Logger.WarningSanitized("Rejected request Content-Encoding: {Encoding}", encoding); |
| | 1 | 67 | | ctx.Response.StatusCode = StatusCodes.Status415UnsupportedMediaType; |
| | 1 | 68 | | await ctx.Response.WriteAsync("Unsupported Content-Encoding.", ctx.RequestAborted).ConfigureAwai |
| | 1 | 69 | | return; |
| | 4 | 70 | | } |
| | 4 | 71 | | } |
| | 4 | 72 | | } |
| | 4 | 73 | | |
| | 1 | 74 | | await next().ConfigureAwait(false); |
| | 6 | 75 | | })); |
| | | 76 | | |
| | 2 | 77 | | return host.AddRequestDecompression(); |
| | | 78 | | } |
| | | 79 | | } |