| | | 1 | | |
| | | 2 | | using Kestrun.Middleware; |
| | | 3 | | using Microsoft.Net.Http.Headers; |
| | | 4 | | using Serilog.Events; |
| | | 5 | | |
| | | 6 | | namespace Kestrun.Hosting; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides extension methods for configuring static file, default file, favicon, and file server middleware in Kestrun |
| | | 10 | | /// </summary> |
| | | 11 | | public static class KestrunHostStaticFilesExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Adds default files middleware to the application. |
| | | 15 | | /// This middleware serves default files like index.html when a directory is requested. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 18 | | /// <param name="cfg">Configuration options for the default files middleware.</param> |
| | | 19 | | /// <returns>The current KestrunHost instance.</returns> |
| | | 20 | | public static KestrunHost AddDefaultFiles(this KestrunHost host, DefaultFilesOptions? cfg) |
| | | 21 | | { |
| | 2 | 22 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 23 | | { |
| | 0 | 24 | | host.Logger.Debug("Adding Default Files with configuration: {@Config}", cfg); |
| | | 25 | | } |
| | | 26 | | |
| | 2 | 27 | | if (cfg == null) |
| | | 28 | | { |
| | 1 | 29 | | return host.AddDefaultFiles(); // no config, use defaults |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | // Convert DefaultFilesOptions to an Action<DefaultFilesOptions> |
| | 1 | 33 | | return host.AddDefaultFiles(options => |
| | 1 | 34 | | { |
| | 0 | 35 | | CopyDefaultFilesOptions(cfg, options); |
| | 1 | 36 | | }); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Adds default files middleware to the application. |
| | | 41 | | /// This middleware serves default files like index.html when a directory is requested. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 44 | | /// <param name="cfg">Configuration options for the default files middleware.</param> |
| | | 45 | | /// <returns>The current KestrunHost instance.</returns> |
| | | 46 | | public static KestrunHost AddDefaultFiles(this KestrunHost host, Action<DefaultFilesOptions>? cfg = null) |
| | | 47 | | { |
| | 5 | 48 | | return host.Use(app => |
| | 5 | 49 | | { |
| | 1 | 50 | | var options = new DefaultFilesOptions(); |
| | 1 | 51 | | cfg?.Invoke(options); |
| | 1 | 52 | | _ = app.UseDefaultFiles(options); |
| | 6 | 53 | | }); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Adds a directory browser middleware to the application. |
| | | 58 | | /// This middleware enables directory browsing for a specified request path. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 61 | | /// <param name="requestPath">The request path to enable directory browsing for.</param> |
| | | 62 | | /// <returns>The current KestrunHost instance.</returns> |
| | | 63 | | public static KestrunHost AddDirectoryBrowser(this KestrunHost host, string requestPath) |
| | | 64 | | { |
| | 0 | 65 | | return host.Use(app => |
| | 0 | 66 | | { |
| | 0 | 67 | | _ = app.UseDirectoryBrowser(requestPath); |
| | 0 | 68 | | }); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Adds a favicon middleware to the application. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 75 | | /// <param name="iconPath">The path to the favicon file. If null, uses the default favicon.</param> |
| | | 76 | | /// <returns>The current KestrunHost instance.</returns> |
| | | 77 | | public static KestrunHost AddFavicon(this KestrunHost host, string? iconPath = null) |
| | | 78 | | { |
| | 4 | 79 | | return host.Use(app => |
| | 4 | 80 | | { |
| | 2 | 81 | | _ = app.UseFavicon(iconPath); |
| | 6 | 82 | | }); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Copies static file options from one object to another. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="src">The source static file options.</param> |
| | | 90 | | /// <param name="dest">The destination static file options.</param> |
| | | 91 | | /// <remarks> |
| | | 92 | | /// This method copies properties from the source static file options to the destination static file options. |
| | | 93 | | /// </remarks> |
| | | 94 | | private static void CopyStaticFileOptions(StaticFileOptions? src, StaticFileOptions dest) |
| | | 95 | | { |
| | | 96 | | // If no source, return a new empty options object |
| | 0 | 97 | | if (src == null || dest == null) |
| | | 98 | | { |
| | 0 | 99 | | return; |
| | | 100 | | } |
| | | 101 | | // Copy properties from source to destination |
| | 0 | 102 | | dest.ContentTypeProvider = src.ContentTypeProvider; |
| | 0 | 103 | | dest.OnPrepareResponse = src.OnPrepareResponse; |
| | 0 | 104 | | dest.ServeUnknownFileTypes = src.ServeUnknownFileTypes; |
| | 0 | 105 | | dest.DefaultContentType = src.DefaultContentType; |
| | 0 | 106 | | dest.FileProvider = src.FileProvider; |
| | 0 | 107 | | dest.RequestPath = src.RequestPath; |
| | 0 | 108 | | dest.RedirectToAppendTrailingSlash = src.RedirectToAppendTrailingSlash; |
| | 0 | 109 | | dest.HttpsCompression = src.HttpsCompression; |
| | 0 | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Copies default files options from one object to another. |
| | | 114 | | /// This method is used to ensure that the default files options are correctly configured. |
| | | 115 | | /// </summary> |
| | | 116 | | /// <param name="src">The source default files options.</param> |
| | | 117 | | /// <param name="dest">The destination default files options.</param> |
| | | 118 | | /// <remarks> |
| | | 119 | | /// This method copies properties from the source default files options to the destination default files options. |
| | | 120 | | /// </remarks> |
| | | 121 | | private static void CopyDefaultFilesOptions(DefaultFilesOptions? src, DefaultFilesOptions dest) |
| | | 122 | | { |
| | | 123 | | // If no source, return a new empty options object |
| | 0 | 124 | | if (src == null || dest == null) |
| | | 125 | | { |
| | 0 | 126 | | return; |
| | | 127 | | } |
| | | 128 | | // Copy properties from source to destination |
| | 0 | 129 | | dest.DefaultFileNames.Clear(); |
| | 0 | 130 | | foreach (var name in src.DefaultFileNames) |
| | | 131 | | { |
| | 0 | 132 | | dest.DefaultFileNames.Add(name); |
| | | 133 | | } |
| | | 134 | | |
| | 0 | 135 | | dest.FileProvider = src.FileProvider; |
| | 0 | 136 | | dest.RequestPath = src.RequestPath; |
| | 0 | 137 | | dest.RedirectToAppendTrailingSlash = src.RedirectToAppendTrailingSlash; |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Adds a file server middleware to the application. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 144 | | /// <param name="cfg">Configuration options for the file server middleware.</param> |
| | | 145 | | /// <param name="cacheControl">Optional cache control headers to apply to static file responses.</param> |
| | | 146 | | /// <returns>The current KestrunHost instance.</returns> |
| | | 147 | | /// <remarks> |
| | | 148 | | /// This middleware serves static files and default files from a specified file provider. |
| | | 149 | | /// If no configuration is provided, it uses default settings. |
| | | 150 | | /// </remarks> |
| | | 151 | | public static KestrunHost AddFileServer(this KestrunHost host, FileServerOptions? cfg, CacheControlHeaderValue? cach |
| | | 152 | | { |
| | 2 | 153 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 154 | | { |
| | 0 | 155 | | host.Logger.Debug("Adding File Server with configuration: {@Config}", cfg); |
| | | 156 | | } |
| | | 157 | | |
| | 2 | 158 | | if (cfg == null) |
| | | 159 | | { |
| | 1 | 160 | | return host.AddFileServer(); // no config, use defaults |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | // Convert FileServerOptions to an Action<FileServerOptions> |
| | 1 | 164 | | return host.AddFileServer(options => |
| | 1 | 165 | | { |
| | 0 | 166 | | options.EnableDefaultFiles = cfg.EnableDefaultFiles; |
| | 0 | 167 | | options.EnableDirectoryBrowsing = cfg.EnableDirectoryBrowsing; |
| | 0 | 168 | | options.FileProvider = cfg.FileProvider; |
| | 0 | 169 | | options.RequestPath = cfg.RequestPath; |
| | 0 | 170 | | options.RedirectToAppendTrailingSlash = cfg.RedirectToAppendTrailingSlash; |
| | 0 | 171 | | CopyDefaultFilesOptions(cfg.DefaultFilesOptions, options.DefaultFilesOptions); |
| | 0 | 172 | | if (cfg.DirectoryBrowserOptions != null) |
| | 1 | 173 | | { |
| | 0 | 174 | | options.DirectoryBrowserOptions.FileProvider = cfg.DirectoryBrowserOptions.FileProvider; |
| | 0 | 175 | | options.DirectoryBrowserOptions.RequestPath = cfg.DirectoryBrowserOptions.RequestPath; |
| | 0 | 176 | | options.DirectoryBrowserOptions.RedirectToAppendTrailingSlash = cfg.DirectoryBrowserOptions.RedirectToAp |
| | 1 | 177 | | } |
| | 1 | 178 | | |
| | 0 | 179 | | CopyStaticFileOptions(cfg.StaticFileOptions, options.StaticFileOptions); |
| | 0 | 180 | | if (cacheControl != null) |
| | 1 | 181 | | { |
| | 0 | 182 | | options.StaticFileOptions.OnPrepareResponse = ctx => |
| | 0 | 183 | | { |
| | 0 | 184 | | // Apply the provided cache control if one was given |
| | 0 | 185 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | 0 | 186 | | { |
| | 0 | 187 | | host.Logger.Debug("Setting Cache-Control header to: {@CacheControl}", cacheControl); |
| | 0 | 188 | | } |
| | 0 | 189 | | ctx.Context.Response.Headers.CacheControl = cacheControl.ToString(); |
| | 0 | 190 | | }; |
| | 1 | 191 | | } |
| | 0 | 192 | | else if (host.DefaultCacheControl != null) |
| | 1 | 193 | | { |
| | 0 | 194 | | options.StaticFileOptions.OnPrepareResponse = ctx => |
| | 0 | 195 | | { |
| | 0 | 196 | | // Apply the host-wide default cache control if no specific one was provided |
| | 0 | 197 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | 0 | 198 | | { |
| | 0 | 199 | | host.Logger.Debug("Setting host-wide default cache Cache-Control: {@DefaultCacheControl}", host. |
| | 0 | 200 | | } |
| | 0 | 201 | | ctx.Context.Response.Headers.CacheControl = host.DefaultCacheControl.ToString(); |
| | 0 | 202 | | }; |
| | 1 | 203 | | } |
| | 1 | 204 | | }); |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Adds a file server middleware to the application. |
| | | 209 | | /// This middleware serves static files and default files from a specified file provider. |
| | | 210 | | /// </summary> |
| | | 211 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 212 | | /// <param name="cfg">Configuration options for the file server middleware.</param> |
| | | 213 | | /// <returns>The current KestrunHost instance.</returns> |
| | | 214 | | public static KestrunHost AddFileServer(this KestrunHost host, Action<FileServerOptions>? cfg = null) |
| | | 215 | | { |
| | 7 | 216 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 217 | | { |
| | 0 | 218 | | host.Logger.Debug("Adding File Server with Action<configuration>"); |
| | | 219 | | } |
| | | 220 | | |
| | 7 | 221 | | return host.Use(app => |
| | 7 | 222 | | { |
| | 2 | 223 | | var options = new FileServerOptions(); |
| | 2 | 224 | | cfg?.Invoke(options); |
| | 2 | 225 | | _ = app.UseFileServer(options); |
| | 9 | 226 | | }); |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | /// <summary> |
| | | 230 | | /// Adds static files to the application. |
| | | 231 | | /// This overload allows you to specify configuration options. |
| | | 232 | | /// </summary> |
| | | 233 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 234 | | /// <param name="cfg">The static file options to configure.</param> |
| | | 235 | | /// <returns>The current KestrunHost instance.</returns> |
| | | 236 | | public static KestrunHost AddStaticFiles(this KestrunHost host, Action<StaticFileOptions>? cfg = null) |
| | | 237 | | { |
| | 8 | 238 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 239 | | { |
| | 0 | 240 | | host.Logger.Debug("Adding static files with configuration: {Config}", cfg); |
| | | 241 | | } |
| | | 242 | | |
| | 8 | 243 | | return host.Use(app => |
| | 8 | 244 | | { |
| | 3 | 245 | | if (cfg == null) |
| | 8 | 246 | | { |
| | 0 | 247 | | _ = app.UseStaticFiles(); |
| | 8 | 248 | | } |
| | 8 | 249 | | else |
| | 8 | 250 | | { |
| | 3 | 251 | | var options = new StaticFileOptions(); |
| | 3 | 252 | | cfg(options); |
| | 8 | 253 | | |
| | 3 | 254 | | _ = app.UseStaticFiles(options); |
| | 8 | 255 | | } |
| | 11 | 256 | | }); |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | /// <summary> |
| | | 260 | | /// Adds static files to the application. |
| | | 261 | | /// This overload allows you to specify configuration options. |
| | | 262 | | /// </summary> |
| | | 263 | | /// <param name="host">The KestrunHost instance to configure.</param> |
| | | 264 | | /// <param name="options">The static file options to configure.</param> |
| | | 265 | | /// <param name="cacheControl">Optional cache control headers to apply to static file responses.</param> |
| | | 266 | | /// <returns>The current KestrunHost instance.</returns> |
| | | 267 | | public static KestrunHost AddStaticFiles(this KestrunHost host, StaticFileOptions options, CacheControlHeaderValue? |
| | | 268 | | { |
| | 2 | 269 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 270 | | { |
| | 0 | 271 | | host.Logger.Debug("Adding static files with options: {@Options} and cache control: {@CacheControl}", options |
| | | 272 | | } |
| | | 273 | | |
| | 2 | 274 | | if (options == null) |
| | | 275 | | { |
| | 1 | 276 | | return host.AddStaticFiles(); // no options, use defaults |
| | | 277 | | } |
| | | 278 | | |
| | | 279 | | // reuse the delegate overload so the pipeline logic stays in one place |
| | 1 | 280 | | return host.AddStaticFiles(o => |
| | 1 | 281 | | { |
| | 1 | 282 | | // copy only the properties callers are likely to set |
| | 0 | 283 | | CopyStaticFileOptions(options, o); |
| | 0 | 284 | | if (cacheControl != null) |
| | 1 | 285 | | { |
| | 0 | 286 | | o.OnPrepareResponse = ctx => |
| | 0 | 287 | | { |
| | 0 | 288 | | // Apply the provided cache control if one was given |
| | 0 | 289 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | 0 | 290 | | { |
| | 0 | 291 | | host.Logger.Debug("Setting Cache-Control header to: {@CacheControl}", cacheControl); |
| | 0 | 292 | | } |
| | 0 | 293 | | ctx.Context.Response.Headers.CacheControl = cacheControl.ToString(); |
| | 0 | 294 | | }; |
| | 1 | 295 | | } |
| | 0 | 296 | | else if (host.DefaultCacheControl != null) |
| | 1 | 297 | | { |
| | 0 | 298 | | o.OnPrepareResponse = ctx => |
| | 0 | 299 | | { |
| | 0 | 300 | | // Apply the host-wide default cache control if no specific one was provided |
| | 0 | 301 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | 0 | 302 | | { |
| | 0 | 303 | | host.Logger.Debug("Setting host-wide default cache Cache-Control: {@DefaultCacheControl}", host. |
| | 0 | 304 | | } |
| | 0 | 305 | | ctx.Context.Response.Headers.CacheControl = host.DefaultCacheControl.ToString(); |
| | 0 | 306 | | }; |
| | 1 | 307 | | } |
| | 1 | 308 | | }); |
| | | 309 | | } |
| | | 310 | | } |