| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Registers a static file server to serve files from a specified path. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This cmdlet allows you to serve static files from a specified path using the Kestrun server. |
| | | 6 | | It can be used to serve files like images, stylesheets, and scripts. |
| | | 7 | | .PARAMETER Server |
| | | 8 | | The Kestrun server instance to which the static file service will be added. |
| | | 9 | | .PARAMETER Options |
| | | 10 | | The StaticFileOptions to configure the static file service. |
| | | 11 | | .PARAMETER RootPath |
| | | 12 | | The root path from which to serve static files. |
| | | 13 | | .PARAMETER RequestPath |
| | | 14 | | The path at which the static file service will be registered. |
| | | 15 | | .PARAMETER HttpsCompression |
| | | 16 | | The HTTPS compression mode to use for the static files. |
| | | 17 | | .PARAMETER ServeUnknownFileTypes |
| | | 18 | | If specified, allows serving files with unknown MIME types. |
| | | 19 | | .PARAMETER DefaultContentType |
| | | 20 | | The default content type to use for files served by the static file service. |
| | | 21 | | .PARAMETER RedirectToAppendTrailingSlash |
| | | 22 | | If specified, redirects requests to append a trailing slash to the URL. |
| | | 23 | | .PARAMETER ContentTypeMap |
| | | 24 | | A hashtable mapping file extensions to MIME types. |
| | | 25 | | .PARAMETER NoCache |
| | | 26 | | If specified, adds a 'no-cache' directive to the Cache-Control header. |
| | | 27 | | .PARAMETER NoStore |
| | | 28 | | If specified, adds a 'no-store' directive to the Cache-Control header. |
| | | 29 | | .PARAMETER MaxAge |
| | | 30 | | If specified, sets the 'max-age' directive in seconds for the Cache-Control header. |
| | | 31 | | .PARAMETER SharedMaxAge |
| | | 32 | | If specified, sets the 's-maxage' directive in seconds for the Cache-Control header |
| | | 33 | | (used by shared caches). |
| | | 34 | | .PARAMETER MaxStale |
| | | 35 | | If specified, adds a 'max-stale' directive to the Cache-Control header. |
| | | 36 | | .PARAMETER MaxStaleLimit |
| | | 37 | | If specified, sets the limit in seconds for the 'max-stale' directive in the Cache-Control header. |
| | | 38 | | .PARAMETER MinFresh |
| | | 39 | | If specified, sets the 'min-fresh' directive in seconds for the Cache-Control header. |
| | | 40 | | .PARAMETER NoTransform |
| | | 41 | | If specified, adds a 'no-transform' directive to the Cache-Control header. |
| | | 42 | | .PARAMETER OnlyIfCached |
| | | 43 | | If specified, adds an 'only-if-cached' directive to the Cache-Control header. |
| | | 44 | | .PARAMETER Public |
| | | 45 | | If specified, adds a 'public' directive to the Cache-Control header. |
| | | 46 | | .PARAMETER Private |
| | | 47 | | If specified, adds a 'private' directive to the Cache-Control header. |
| | | 48 | | .PARAMETER MustRevalidate |
| | | 49 | | If specified, adds a 'must-revalidate' directive to the Cache-Control header. |
| | | 50 | | .PARAMETER ProxyRevalidate |
| | | 51 | | If specified, adds a 'proxy-revalidate' directive to the Cache-Control header. |
| | | 52 | | .PARAMETER PassThru |
| | | 53 | | If specified, the cmdlet will return the modified server instance after adding the static file service. |
| | | 54 | | .EXAMPLE |
| | | 55 | | $server | Add-KrStaticFilesMiddleware -RequestPath '/static' -HttpsCompression -ServeUnknownFileTypes -DefaultConten |
| | | 56 | | This example adds a static file service to the server for the path '/static', enabling HTTPS compression, allowing s |
| | | 57 | | setting the default content type to 'application/octet-stream', and redirecting requests to append a trailing slash. |
| | | 58 | | .EXAMPLE |
| | | 59 | | $server | Add-KrStaticFilesMiddleware -Options $options |
| | | 60 | | This example adds a static file service to the server using the specified StaticFileOptions. |
| | | 61 | | .EXAMPLE |
| | | 62 | | $server | Add-KrStaticFilesMiddleware -RequestPath '/static' -MaxAge 600 -Public -MustRevalidate |
| | | 63 | | This example adds a static file service to the server for the path '/static', setting caching headers with a max-age |
| | | 64 | | marking the response as public, and adding the must-revalidate directive. |
| | | 65 | | .LINK |
| | | 66 | | https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.staticfileoptions?view=aspnetcore-8.0 |
| | | 67 | | .NOTES |
| | | 68 | | ContentTypeProvider and ContentTypeProviderOptions are not supported yet. |
| | | 69 | | #> |
| | | 70 | | function Add-KrStaticFilesMiddleware { |
| | | 71 | | [KestrunRuntimeApi('Definition')] |
| | | 72 | | [CmdletBinding(defaultParameterSetName = 'Items')] |
| | | 73 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 74 | | param( |
| | | 75 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 76 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 77 | | |
| | | 78 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | | 79 | | [Microsoft.AspNetCore.Builder.StaticFileOptions]$Options, |
| | | 80 | | |
| | | 81 | | [Parameter(ParameterSetName = 'Items')] |
| | | 82 | | [string]$RootPath, |
| | | 83 | | |
| | | 84 | | [Parameter(ParameterSetName = 'Items')] |
| | | 85 | | [string]$RequestPath, |
| | | 86 | | |
| | | 87 | | [Parameter(ParameterSetName = 'Items')] |
| | | 88 | | [Microsoft.AspNetCore.Http.Features.HttpsCompressionMode]$HttpsCompression, |
| | | 89 | | |
| | | 90 | | [Parameter(ParameterSetName = 'Items')] |
| | | 91 | | [switch]$ServeUnknownFileTypes, |
| | | 92 | | |
| | | 93 | | [Parameter(ParameterSetName = 'Items')] |
| | | 94 | | [string]$DefaultContentType, |
| | | 95 | | |
| | | 96 | | [Parameter(ParameterSetName = 'Items')] |
| | | 97 | | [switch]$RedirectToAppendTrailingSlash, |
| | | 98 | | |
| | | 99 | | [Parameter(ParameterSetName = 'Items')] |
| | | 100 | | [hashtable]$ContentTypeMap, |
| | | 101 | | |
| | | 102 | | [Parameter()] |
| | | 103 | | [switch]$NoCache, |
| | | 104 | | [Parameter()] |
| | | 105 | | [switch]$NoStore, |
| | | 106 | | [Parameter()] |
| | | 107 | | [ValidateRange(1, [int]::MaxValue)] |
| | | 108 | | [int]$MaxAge, |
| | | 109 | | [Parameter()] |
| | | 110 | | [ValidateRange(1, [int]::MaxValue)] |
| | | 111 | | [int]$SharedMaxAge, |
| | | 112 | | [Parameter()] |
| | | 113 | | [switch]$MaxStale, |
| | | 114 | | [Parameter()] |
| | | 115 | | [ValidateRange(1, [int]::MaxValue)] |
| | | 116 | | [int]$MaxStaleLimit, |
| | | 117 | | [Parameter()] |
| | | 118 | | [ValidateRange(1, [int]::MaxValue)] |
| | | 119 | | [int]$MinFresh, |
| | | 120 | | [Parameter()] |
| | | 121 | | [switch]$NoTransform, |
| | | 122 | | [Parameter()] |
| | | 123 | | [switch]$OnlyIfCached, |
| | | 124 | | [Parameter()] |
| | | 125 | | [switch]$Public, |
| | | 126 | | [Parameter()] |
| | | 127 | | [switch]$Private, |
| | | 128 | | [Parameter()] |
| | | 129 | | [switch]$MustRevalidate, |
| | | 130 | | [Parameter()] |
| | | 131 | | [switch]$ProxyRevalidate, |
| | | 132 | | |
| | | 133 | | [Parameter()] |
| | | 134 | | [switch]$PassThru |
| | | 135 | | ) |
| | | 136 | | begin { |
| | | 137 | | # Ensure the server instance is resolved |
| | 0 | 138 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 139 | | } |
| | | 140 | | process { |
| | 0 | 141 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| | 0 | 142 | | $Options = [Microsoft.AspNetCore.Builder.StaticFileOptions]::new() |
| | 0 | 143 | | if (-not [string]::IsNullOrEmpty($RootPath)) { |
| | 0 | 144 | | $resolvedPath = Resolve-KrPath $RootPath -KestrunRoot |
| | 0 | 145 | | $Options.FileProvider = [Microsoft.Extensions.FileProviders.PhysicalFileProvider]::new($resolvedPath) |
| | | 146 | | } |
| | 0 | 147 | | if (-not [string]::IsNullOrEmpty($RequestPath)) { |
| | 0 | 148 | | $Options.RequestPath = [Microsoft.AspNetCore.Http.PathString]::new($RequestPath.TrimEnd('/')) |
| | | 149 | | } |
| | 0 | 150 | | if ($ServeUnknownFileTypes.IsPresent) { |
| | 0 | 151 | | $Options.ServeUnknownFileTypes = $true |
| | | 152 | | } |
| | 0 | 153 | | if ($PSBoundParameters.ContainsKey('HttpsCompression')) { |
| | 0 | 154 | | $Options.HttpsCompression = $HttpsCompression |
| | | 155 | | } |
| | 0 | 156 | | if (-not [string]::IsNullOrEmpty($DefaultContentType)) { |
| | 0 | 157 | | $Options.DefaultContentType = $DefaultContentType |
| | | 158 | | } |
| | 0 | 159 | | if ($RedirectToAppendTrailingSlash.IsPresent) { |
| | 0 | 160 | | $Options.RedirectToAppendTrailingSlash = $true |
| | | 161 | | } |
| | 0 | 162 | | if ($ContentTypeMap) { |
| | 0 | 163 | | $provider = [Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider]::new() |
| | 0 | 164 | | foreach ($k in $ContentTypeMap.Keys) { |
| | 0 | 165 | | $ext = if ($k -like '.*') { $k } else { ".$k" } |
| | 0 | 166 | | $mime = [string]$ContentTypeMap[$k] |
| | 0 | 167 | | if ([string]::IsNullOrWhiteSpace($mime)) { continue } |
| | 0 | 168 | | $provider.Mappings[$ext] = $mime |
| | | 169 | | } |
| | 0 | 170 | | $Options.StaticFileOptions.ContentTypeProvider = $provider |
| | | 171 | | } |
| | | 172 | | } |
| | 0 | 173 | | if ($PSBoundParameters.ContainsKey('NoCache') -or ($PSBoundParameters.ContainsKey('NoStore')) -or ($PSBoundParam |
| | 0 | 174 | | ($PSBoundParameters.ContainsKey('SharedMaxAge')) -or ($PSBoundParameters.ContainsKey('MaxStale')) -or |
| | 0 | 175 | | ($PSBoundParameters.ContainsKey('MaxStaleLimit')) -or ($PSBoundParameters.ContainsKey('MinFresh')) -or |
| | 0 | 176 | | ($PSBoundParameters.ContainsKey('NoTransform')) -or ($PSBoundParameters.ContainsKey('OnlyIfCached')) -or |
| | 0 | 177 | | ($PSBoundParameters.ContainsKey('Public')) -or ($PSBoundParameters.ContainsKey('Private')) -or |
| | 0 | 178 | | ($PSBoundParameters.ContainsKey('MustRevalidate')) -or ($PSBoundParameters.ContainsKey('ProxyRevalidate')) |
| | | 179 | | ) { |
| | 0 | 180 | | $cacheControl = [Microsoft.Net.Http.Headers.CacheControlHeaderValue]::new(); |
| | 0 | 181 | | if ($PSBoundParameters.ContainsKey('NoCache')) { $cacheControl.NoCache = $NoCache.IsPresent } |
| | 0 | 182 | | if ($PSBoundParameters.ContainsKey('NoStore')) { $cacheControl.NoStore = $NoStore.IsPresent } |
| | 0 | 183 | | if ($PSBoundParameters.ContainsKey('MaxAge')) { $cacheControl.MaxAge = [TimeSpan]::FromSeconds($MaxAge) } |
| | 0 | 184 | | if ($PSBoundParameters.ContainsKey('SharedMaxAge')) { $cacheControl.SharedMaxAge = [TimeSpan]::FromSeconds($ |
| | 0 | 185 | | if ($PSBoundParameters.ContainsKey('MaxStale')) { $cacheControl.MaxStale = $MaxStale.IsPresent } |
| | 0 | 186 | | if ($PSBoundParameters.ContainsKey('MaxStaleLimit')) { $cacheControl.MaxStaleLimit = [TimeSpan]::FromSeconds |
| | 0 | 187 | | if ($PSBoundParameters.ContainsKey('MinFresh')) { $cacheControl.MinFresh = [TimeSpan]::FromSeconds($MinFresh |
| | 0 | 188 | | if ($PSBoundParameters.ContainsKey('NoTransform')) { $cacheControl.NoTransform = $NoTransform.IsPresent } |
| | 0 | 189 | | if ($PSBoundParameters.ContainsKey('OnlyIfCached')) { $cacheControl.OnlyIfCached = $OnlyIfCached.IsPresent } |
| | 0 | 190 | | if ($PSBoundParameters.ContainsKey('Public')) { $cacheControl.Public = $Public.IsPresent } |
| | 0 | 191 | | if ($PSBoundParameters.ContainsKey('Private')) { $cacheControl.Private = $Private.IsPresent } |
| | 0 | 192 | | if ($PSBoundParameters.ContainsKey('MustRevalidate')) { $cacheControl.MustRevalidate = $MustRevalidate.IsPre |
| | 0 | 193 | | if ($PSBoundParameters.ContainsKey('ProxyRevalidate')) { $cacheControl.ProxyRevalidate = $ProxyRevalidate.Is |
| | | 194 | | # Add the static file service to the server with caching options |
| | 0 | 195 | | [Kestrun.Hosting.KestrunHostStaticFilesExtensions]::AddStaticFiles($Server, $Options, $cacheControl) | Out-N |
| | | 196 | | } else { |
| | | 197 | | # Add the static file service to the server without caching options |
| | 0 | 198 | | [Kestrun.Hosting.KestrunHostStaticFilesExtensions]::AddStaticFiles($Server, $Options) | Out-Null |
| | | 199 | | } |
| | | 200 | | |
| | 0 | 201 | | if ($PassThru.IsPresent) { |
| | | 202 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 203 | | return $Server |
| | | 204 | | } |
| | | 205 | | } |
| | | 206 | | } |
| | | 207 | | |