| | | 1 | | using Kestrun.Middleware; |
| | | 2 | | using Microsoft.AspNetCore.ResponseCaching; |
| | | 3 | | using Microsoft.AspNetCore.ResponseCompression; |
| | | 4 | | using Microsoft.Net.Http.Headers; |
| | | 5 | | using Serilog.Events; |
| | | 6 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 7 | | using Microsoft.AspNetCore.HttpsPolicy; |
| | | 8 | | |
| | | 9 | | namespace Kestrun.Hosting; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Provides extension methods for configuring common HTTP middleware in Kestrun. |
| | | 13 | | /// </summary> |
| | | 14 | | public static class KestrunHttpMiddlewareExtensions |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Adds Apache-style common access logging using a configured <see cref="CommonAccessLogOptions"/> instance. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="host">The <see cref="KestrunHost"/> instance to configure.</param> |
| | | 20 | | /// <param name="configure">Optional pre-configured <see cref="CommonAccessLogOptions"/> instance.</param> |
| | | 21 | | /// <returns>The configured <see cref="KestrunHost"/> instance.</returns> |
| | | 22 | | public static KestrunHost AddCommonAccessLog(this KestrunHost host, CommonAccessLogOptions configure) |
| | | 23 | | { |
| | 0 | 24 | | return host.AddCommonAccessLog(opts => |
| | 0 | 25 | | { |
| | 0 | 26 | | opts.Level = configure.Level; |
| | 0 | 27 | | opts.IncludeQueryString = configure.IncludeQueryString; |
| | 0 | 28 | | opts.IncludeProtocol = configure.IncludeProtocol; |
| | 0 | 29 | | opts.IncludeElapsedMilliseconds = configure.IncludeElapsedMilliseconds; |
| | 0 | 30 | | opts.UseUtcTimestamp = configure.UseUtcTimestamp; |
| | 0 | 31 | | opts.TimestampFormat = configure.TimestampFormat; |
| | 0 | 32 | | opts.ClientAddressHeader = configure.ClientAddressHeader; |
| | 0 | 33 | | opts.TimeProvider = configure.TimeProvider; |
| | 0 | 34 | | opts.Logger = configure.Logger; |
| | 0 | 35 | | }); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Adds Apache-style common access logging using <see cref="CommonAccessLogMiddleware"/>. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="host">The <see cref="KestrunHost"/> instance to configure.</param> |
| | | 42 | | /// <param name="configure">Optional delegate to configure <see cref="CommonAccessLogOptions"/>.</param> |
| | | 43 | | /// <returns>The configured <see cref="KestrunHost"/> instance.</returns> |
| | | 44 | | public static KestrunHost AddCommonAccessLog(this KestrunHost host, Action<CommonAccessLogOptions>? configure = null |
| | | 45 | | { |
| | 1 | 46 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 47 | | { |
| | 1 | 48 | | host.Logger.Debug( |
| | 1 | 49 | | "Adding common access log middleware (custom configuration supplied: {HasConfig})", |
| | 1 | 50 | | configure != null); |
| | | 51 | | } |
| | | 52 | | |
| | 1 | 53 | | _ = host.AddService(services => |
| | 1 | 54 | | { |
| | 1 | 55 | | // Ensure a Serilog.ILogger is available for middleware constructor injection. |
| | 1 | 56 | | // We don't overwrite a user-provided registration. |
| | 1 | 57 | | services.TryAddSingleton(_ => host.Logger); |
| | 1 | 58 | | |
| | 1 | 59 | | var builder = services.AddOptions<CommonAccessLogOptions>(); |
| | 1 | 60 | | if (configure != null) |
| | 1 | 61 | | { |
| | 1 | 62 | | _ = builder.Configure(configure); |
| | 1 | 63 | | } |
| | 2 | 64 | | }); |
| | | 65 | | |
| | 2 | 66 | | return host.Use(app => app.UseMiddleware<CommonAccessLogMiddleware>()); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Adds response compression to the application. |
| | | 73 | | /// This overload allows you to specify configuration options. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 76 | | /// <param name="options">The configuration options for response compression.</param> |
| | | 77 | | /// <returns>The current KestrunHost instance.</returns> |
| | | 78 | | public static KestrunHost AddResponseCompression(this KestrunHost host, ResponseCompressionOptions? options) |
| | | 79 | | { |
| | 2 | 80 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 81 | | { |
| | 0 | 82 | | host.Logger.Debug("Adding response compression with options: {@Options}", options); |
| | | 83 | | } |
| | | 84 | | |
| | 2 | 85 | | if (options == null) |
| | | 86 | | { |
| | 1 | 87 | | return host.AddResponseCompression(); // no options, use defaults |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | // delegate shim – re‑use the existing pipeline |
| | 1 | 91 | | return host.AddResponseCompression(o => |
| | 1 | 92 | | { |
| | 0 | 93 | | o.EnableForHttps = options.EnableForHttps; |
| | 0 | 94 | | o.MimeTypes = options.MimeTypes; |
| | 0 | 95 | | o.ExcludedMimeTypes = options.ExcludedMimeTypes; |
| | 1 | 96 | | // copy provider lists, levels, etc. if you expose them |
| | 0 | 97 | | foreach (var p in options.Providers) |
| | 1 | 98 | | { |
| | 0 | 99 | | o.Providers.Add(p); |
| | 1 | 100 | | } |
| | 1 | 101 | | }); |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Adds response compression to the application. |
| | | 106 | | /// This overload allows you to specify configuration options. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 109 | | /// <param name="cfg">The configuration options for response compression.</param> |
| | | 110 | | /// <returns>The current KestrunHost instance.</returns> |
| | | 111 | | public static KestrunHost AddResponseCompression(this KestrunHost host, Action<ResponseCompressionOptions>? cfg = nu |
| | | 112 | | { |
| | 5 | 113 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 114 | | { |
| | 0 | 115 | | host.Logger.Debug("Adding response compression with configuration: {HasConfig}", cfg != null); |
| | | 116 | | } |
| | | 117 | | // Service side |
| | 5 | 118 | | _ = host.AddService(services => |
| | 5 | 119 | | { |
| | 1 | 120 | | _ = cfg == null ? services.AddResponseCompression() : services.AddResponseCompression(cfg); |
| | 5 | 121 | | // replace the default provider with our opt-out decorator |
| | 1 | 122 | | _ = services.AddSingleton<IResponseCompressionProvider, Compression.KestrunResponseCompressionProvider>(); |
| | 6 | 123 | | }); |
| | | 124 | | |
| | | 125 | | // Middleware side |
| | 5 | 126 | | return host.Use(app => app.UseResponseCompression()); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Adds response caching to the application. |
| | | 131 | | /// This overload allows you to specify configuration options. |
| | | 132 | | /// </summary> |
| | | 133 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 134 | | /// <param name="options">The configuration options for response caching.</param> |
| | | 135 | | /// <param name="cacheControl"> |
| | | 136 | | /// Optional default Cache-Control to apply (only if the response didn't set one). |
| | | 137 | | /// </param> |
| | | 138 | | public static KestrunHost AddResponseCaching(this KestrunHost host, ResponseCachingOptions options, CacheControlHead |
| | | 139 | | { |
| | 0 | 140 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 141 | | { |
| | 0 | 142 | | host.Logger.Debug("Adding response caching with options: {@Options}", options); |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | // delegate shim – re‑use the existing pipeline |
| | 0 | 146 | | return host.AddResponseCaching(o => |
| | 0 | 147 | | { |
| | 0 | 148 | | o.SizeLimit = options.SizeLimit; |
| | 0 | 149 | | o.MaximumBodySize = options.MaximumBodySize; |
| | 0 | 150 | | o.UseCaseSensitivePaths = options.UseCaseSensitivePaths; |
| | 0 | 151 | | }, cacheControl); |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Validates inputs and performs initial logging for response caching configuration. |
| | | 156 | | /// </summary> |
| | | 157 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 158 | | /// <param name="cfg">Optional configuration for response caching.</param> |
| | | 159 | | /// <param name="cacheControl">Optional default Cache-Control to apply.</param> |
| | | 160 | | internal static void ValidateCachingInput(KestrunHost host, Action<ResponseCachingOptions>? cfg, CacheControlHeaderV |
| | | 161 | | { |
| | 2 | 162 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 163 | | { |
| | 1 | 164 | | host.Logger.Debug("Adding response caching with Action<ResponseCachingOptions>{HasConfig} and Cache-Control: |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | // Remember the default Cache-Control if provided |
| | 2 | 168 | | if (cacheControl is not null) |
| | | 169 | | { |
| | 1 | 170 | | host.Logger.Information("Setting default Cache-Control: {CacheControl}", cacheControl.ToString()); |
| | | 171 | | // Save for reference |
| | 1 | 172 | | host.DefaultCacheControl = cacheControl; |
| | | 173 | | } |
| | 2 | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Registers response caching services with the dependency injection container. |
| | | 178 | | /// </summary> |
| | | 179 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 180 | | /// <param name="cfg">Optional configuration for response caching.</param> |
| | | 181 | | internal static void RegisterCachingServices(KestrunHost host, Action<ResponseCachingOptions>? cfg) |
| | | 182 | | { |
| | 2 | 183 | | _ = host.AddService(services => |
| | 2 | 184 | | { |
| | 0 | 185 | | _ = cfg == null ? services.AddResponseCaching() : services.AddResponseCaching(cfg); |
| | 2 | 186 | | }); |
| | 2 | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Applies cache control headers to the HTTP response if conditions are met. |
| | | 191 | | /// </summary> |
| | | 192 | | /// <param name="context">The HTTP context.</param> |
| | | 193 | | /// <param name="cacheControl">The cache control header value to apply.</param> |
| | | 194 | | /// <param name="logger">The Serilog logger instance for debugging.</param> |
| | | 195 | | /// <returns>True if headers were applied, false otherwise.</returns> |
| | | 196 | | internal static bool ApplyCacheHeaders(HttpContext context, CacheControlHeaderValue? cacheControl, Serilog.ILogger l |
| | | 197 | | { |
| | | 198 | | // Gate: only for successful cacheable responses on GET/HEAD |
| | 6 | 199 | | var method = context.Request.Method; |
| | 6 | 200 | | if (!(HttpMethods.IsGet(method) || HttpMethods.IsHead(method))) |
| | | 201 | | { |
| | 1 | 202 | | return false; |
| | | 203 | | } |
| | | 204 | | |
| | 5 | 205 | | var status = context.Response.StatusCode; |
| | 5 | 206 | | if (status is < 200 or >= 300) |
| | | 207 | | { |
| | 1 | 208 | | return false; |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | // ResponseCaching won't cache if Set-Cookie is present; don't add headers in that case |
| | 4 | 212 | | if (context.Response.Headers.ContainsKey(HeaderNames.SetCookie)) |
| | | 213 | | { |
| | 1 | 214 | | return false; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | // Only apply default Cache-Control if none was set and caller provided one |
| | 3 | 218 | | if (cacheControl is not null) |
| | | 219 | | { |
| | 2 | 220 | | context.Response.Headers.CacheControl = cacheControl.ToString(); |
| | | 221 | | |
| | | 222 | | // If you expect compression variability elsewhere, add Vary only if absent |
| | 2 | 223 | | if (!context.Response.Headers.ContainsKey(HeaderNames.Vary)) |
| | | 224 | | { |
| | 2 | 225 | | context.Response.Headers.Append(HeaderNames.Vary, "Accept-Encoding"); |
| | | 226 | | } |
| | | 227 | | |
| | 2 | 228 | | if (logger.IsEnabled(LogEventLevel.Debug)) |
| | | 229 | | { |
| | 0 | 230 | | logger.Debug("Applied default Cache-Control: {CacheControl}", cacheControl.ToString()); |
| | | 231 | | } |
| | 2 | 232 | | return true; |
| | | 233 | | } |
| | | 234 | | else |
| | | 235 | | { |
| | 1 | 236 | | if (logger.IsEnabled(LogEventLevel.Debug)) |
| | | 237 | | { |
| | 0 | 238 | | logger.Debug("No default cache Control provided; skipping."); |
| | | 239 | | } |
| | 1 | 240 | | return false; |
| | | 241 | | } |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | /// <summary> |
| | | 245 | | /// Creates the caching middleware that applies cache headers. |
| | | 246 | | /// </summary> |
| | | 247 | | /// <param name="host">The KestrunHost instance.</param> |
| | | 248 | | /// <param name="cacheControl">Optional cache control header value.</param> |
| | | 249 | | /// <returns>The configured middleware action.</returns> |
| | | 250 | | internal static Action<IApplicationBuilder> CreateCachingMiddleware(KestrunHost host, CacheControlHeaderValue? cache |
| | | 251 | | { |
| | 1 | 252 | | return app => |
| | 1 | 253 | | { |
| | 0 | 254 | | _ = app.UseResponseCaching(); |
| | 0 | 255 | | _ = app.Use(async (context, next) => |
| | 0 | 256 | | { |
| | 0 | 257 | | try |
| | 0 | 258 | | { |
| | 0 | 259 | | _ = ApplyCacheHeaders(context, cacheControl, host.Logger); |
| | 0 | 260 | | } |
| | 0 | 261 | | catch (Exception ex) |
| | 0 | 262 | | { |
| | 0 | 263 | | // Never let caching decoration break the response |
| | 0 | 264 | | host.Logger.Warning(ex, "Failed to apply default cache headers."); |
| | 0 | 265 | | } |
| | 0 | 266 | | finally |
| | 0 | 267 | | { |
| | 0 | 268 | | await next(context); |
| | 0 | 269 | | } |
| | 0 | 270 | | }); |
| | 1 | 271 | | }; |
| | | 272 | | } |
| | | 273 | | |
| | | 274 | | /// <summary> |
| | | 275 | | /// Adds response caching to the application. |
| | | 276 | | /// This overload allows you to specify a configuration delegate. |
| | | 277 | | /// </summary> |
| | | 278 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 279 | | /// <param name="cfg">Optional configuration for response caching.</param> |
| | | 280 | | /// <param name="cacheControl">Optional default Cache-Control to apply (only if the response didn't set one).</param |
| | | 281 | | /// <returns> The updated KestrunHost instance. </returns> |
| | | 282 | | public static KestrunHost AddResponseCaching(this KestrunHost host, Action<ResponseCachingOptions>? cfg = null, |
| | | 283 | | CacheControlHeaderValue? cacheControl = null) |
| | | 284 | | { |
| | 0 | 285 | | ValidateCachingInput(host, cfg, cacheControl); |
| | 0 | 286 | | RegisterCachingServices(host, cfg); |
| | 0 | 287 | | return host.Use(CreateCachingMiddleware(host, cacheControl)); |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | |
| | | 291 | | /// <summary> |
| | | 292 | | /// Adds HTTPS redirection to the application using the specified <see cref="HttpsRedirectionOptions"/>. |
| | | 293 | | /// </summary> |
| | | 294 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 295 | | /// <param name="cfg">The HTTPS redirection options.</param> |
| | | 296 | | /// <returns>The updated KestrunHost instance.</returns> |
| | | 297 | | public static KestrunHost AddHttpsRedirection(this KestrunHost host, HttpsRedirectionOptions cfg) |
| | | 298 | | { |
| | 2 | 299 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 300 | | { |
| | 0 | 301 | | host.Logger.Debug("Adding HTTPS redirection with configuration: {@Config}", cfg); |
| | | 302 | | } |
| | | 303 | | |
| | 2 | 304 | | if (cfg == null) |
| | | 305 | | { |
| | 1 | 306 | | return host.AddHttpsRedirection(); // fallback to parameterless overload |
| | | 307 | | } |
| | | 308 | | |
| | 1 | 309 | | _ = host.AddService(services => |
| | 1 | 310 | | { |
| | 0 | 311 | | _ = services.AddHttpsRedirection(opts => |
| | 0 | 312 | | { |
| | 0 | 313 | | opts.RedirectStatusCode = cfg.RedirectStatusCode; |
| | 0 | 314 | | opts.HttpsPort = cfg.HttpsPort; |
| | 0 | 315 | | }); |
| | 1 | 316 | | }); |
| | | 317 | | |
| | 1 | 318 | | return host.Use(app => app.UseHttpsRedirection()); |
| | | 319 | | } |
| | | 320 | | |
| | | 321 | | /// <summary> |
| | | 322 | | /// Adds HTTPS redirection to the application using the specified configuration delegate. |
| | | 323 | | /// </summary> |
| | | 324 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 325 | | /// <param name="cfg">The configuration delegate for HTTPS redirection options.</param> |
| | | 326 | | /// <returns>The updated KestrunHost instance.</returns> |
| | | 327 | | public static KestrunHost AddHttpsRedirection(this KestrunHost host, Action<HttpsRedirectionOptions>? cfg = null) |
| | | 328 | | { |
| | 3 | 329 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 330 | | { |
| | 0 | 331 | | host.Logger.Debug("Adding HTTPS redirection with configuration: {HasConfig}", cfg != null); |
| | | 332 | | } |
| | | 333 | | |
| | | 334 | | // Register the HTTPS redirection service |
| | 3 | 335 | | _ = host.AddService(services => |
| | 3 | 336 | | { |
| | 0 | 337 | | _ = services.AddHttpsRedirection(cfg ?? (_ => { })); // Always pass a delegate |
| | 3 | 338 | | }); |
| | | 339 | | |
| | | 340 | | // Apply the middleware |
| | 3 | 341 | | return host.Use(app => app.UseHttpsRedirection()); |
| | | 342 | | } |
| | | 343 | | |
| | | 344 | | |
| | | 345 | | /// <summary> |
| | | 346 | | /// Adds HSTS to the application using the specified <see cref="HstsOptions"/>. |
| | | 347 | | /// </summary> |
| | | 348 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 349 | | /// <param name="opts">The delegate for configuring HSTS options.</param> |
| | | 350 | | /// <returns>The updated KestrunHost instance.</returns> |
| | | 351 | | public static KestrunHost AddHsts(this KestrunHost host, Action<HstsOptions>? opts = null) |
| | | 352 | | { |
| | 4 | 353 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 354 | | { |
| | 1 | 355 | | host.Logger.Debug("Adding HSTS with configuration: {HasConfig}", opts != null); |
| | | 356 | | } |
| | | 357 | | |
| | | 358 | | // Register the HSTS service |
| | 4 | 359 | | _ = host.AddService(services => |
| | 4 | 360 | | { |
| | 0 | 361 | | _ = services.AddHsts(opts ?? (_ => { })); // Always pass a delegate |
| | 4 | 362 | | }); |
| | | 363 | | |
| | | 364 | | // Apply the middleware |
| | 4 | 365 | | return host.Use(app => app.UseHsts()); |
| | | 366 | | } |
| | | 367 | | |
| | | 368 | | /// <summary> |
| | | 369 | | /// Adds HSTS to the application using the specified <see cref="HstsOptions"/>. |
| | | 370 | | /// </summary> |
| | | 371 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 372 | | /// <param name="opts">The HSTS options.</param> |
| | | 373 | | /// <returns>The updated KestrunHost instance.</returns> |
| | | 374 | | public static KestrunHost AddHsts(this KestrunHost host, HstsOptions opts) |
| | | 375 | | { |
| | 4 | 376 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 377 | | { |
| | 0 | 378 | | host.Logger.Debug("Adding HSTS with configuration: {@Config}", opts); |
| | | 379 | | } |
| | | 380 | | |
| | 4 | 381 | | if (opts == null) |
| | | 382 | | { |
| | 1 | 383 | | return host.AddHsts(); // fallback to parameterless overload |
| | | 384 | | } |
| | | 385 | | |
| | 3 | 386 | | _ = host.AddService(services => |
| | 3 | 387 | | { |
| | 0 | 388 | | _ = services.AddHsts(o => |
| | 0 | 389 | | { |
| | 0 | 390 | | o.Preload = opts.Preload; |
| | 0 | 391 | | o.IncludeSubDomains = opts.IncludeSubDomains; |
| | 0 | 392 | | o.MaxAge = opts.MaxAge; |
| | 0 | 393 | | o.ExcludedHosts.Clear(); |
| | 0 | 394 | | foreach (var h in opts.ExcludedHosts) |
| | 0 | 395 | | { |
| | 0 | 396 | | o.ExcludedHosts.Add(h); |
| | 0 | 397 | | } |
| | 0 | 398 | | }); |
| | 3 | 399 | | }); |
| | | 400 | | |
| | 3 | 401 | | return host.Use(app => app.UseHsts()); |
| | | 402 | | } |
| | | 403 | | } |