| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds caching headers to the HTTP response. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This cmdlet allows you to add caching headers to the HTTP response in a route script block. |
| | | 6 | | It provides various parameters to customize the caching behavior, such as setting max-age, |
| | | 7 | | no-cache, no-store, and other cache control directives. |
| | | 8 | | .PARAMETER NoCache |
| | | 9 | | If specified, the 'no-cache' directive will be added to the Cache-Control header. |
| | | 10 | | .PARAMETER NoStore |
| | | 11 | | If specified, the 'no-store' directive will be added to the Cache-Control header. |
| | | 12 | | .PARAMETER MaxAge |
| | | 13 | | If specified, sets the 'max-age' directive in seconds for the Cache-Control header. |
| | | 14 | | .PARAMETER SharedMaxAge |
| | | 15 | | If specified, sets the 's-maxage' directive in seconds for the Cache-Control header |
| | | 16 | | (used by shared caches). |
| | | 17 | | .PARAMETER MaxStale |
| | | 18 | | If specified, the 'max-stale' directive will be added to the Cache-Control header. |
| | | 19 | | .PARAMETER MaxStaleLimit |
| | | 20 | | If specified, sets the limit in seconds for the 'max-stale' directive in the Cache-Control header. |
| | | 21 | | .PARAMETER MinFresh |
| | | 22 | | If specified, sets the 'min-fresh' directive in seconds for the Cache-Control header. |
| | | 23 | | .PARAMETER NoTransform |
| | | 24 | | If specified, the 'no-transform' directive will be added to the Cache-Control header. |
| | | 25 | | .PARAMETER OnlyIfCached |
| | | 26 | | If specified, the 'only-if-cached' directive will be added to the Cache-Control header. |
| | | 27 | | .PARAMETER Public |
| | | 28 | | If specified, the 'public' directive will be added to the Cache-Control header. |
| | | 29 | | .PARAMETER Private |
| | | 30 | | If specified, the 'private' directive will be added to the Cache-Control header. |
| | | 31 | | .PARAMETER MustRevalidate |
| | | 32 | | If specified, the 'must-revalidate' directive will be added to the Cache-Control header. |
| | | 33 | | .PARAMETER ProxyRevalidate |
| | | 34 | | If specified, the 'proxy-revalidate' directive will be added to the Cache-Control header. |
| | | 35 | | .EXAMPLE |
| | | 36 | | Add-KrCacheResponse -NoCache -MaxAge 3600 -Public |
| | | 37 | | This example adds caching headers to the response, setting the 'no-cache' directive, |
| | | 38 | | a 'max-age' of 3600 seconds, and marking the response as 'public'. |
| | | 39 | | .EXAMPLE |
| | | 40 | | Add-KrCacheResponse -NoStore -Private -MustRevalidate |
| | | 41 | | This example adds caching headers to the response, setting the 'no-store' directive, |
| | | 42 | | marking the response as 'private', and adding the 'must-revalidate' directive. |
| | | 43 | | .NOTES |
| | | 44 | | This cmdlet is used to add caching headers to the response in a route script block, |
| | | 45 | | allowing you to control how responses are cached by clients and intermediate caches. |
| | | 46 | | It must be used within a route script block where the $Context variable is available. |
| | | 47 | | #> |
| | | 48 | | function Add-KrCacheResponse { |
| | | 49 | | [KestrunRuntimeApi('Route')] |
| | | 50 | | [CmdletBinding()] |
| | | 51 | | param( |
| | | 52 | | [Parameter()] |
| | | 53 | | [switch]$NoCache, |
| | | 54 | | [Parameter()] |
| | | 55 | | [switch]$NoStore, |
| | | 56 | | [Parameter()] |
| | | 57 | | [ValidateRange(1, [int]::MaxValue)] |
| | | 58 | | [int]$MaxAge, |
| | | 59 | | [Parameter()] |
| | | 60 | | [ValidateRange(1, [int]::MaxValue)] |
| | | 61 | | [int]$SharedMaxAge, |
| | | 62 | | [Parameter()] |
| | | 63 | | [switch]$MaxStale, |
| | | 64 | | [Parameter()] |
| | | 65 | | [ValidateRange(1, [int]::MaxValue)] |
| | | 66 | | [int]$MaxStaleLimit, |
| | | 67 | | [Parameter()] |
| | | 68 | | [ValidateRange(1, [int]::MaxValue)] |
| | | 69 | | [int]$MinFresh, |
| | | 70 | | [Parameter()] |
| | | 71 | | [switch]$NoTransform, |
| | | 72 | | [Parameter()] |
| | | 73 | | [switch]$OnlyIfCached, |
| | | 74 | | [Parameter()] |
| | | 75 | | [switch]$Public, |
| | | 76 | | [Parameter()] |
| | | 77 | | [switch]$Private, |
| | | 78 | | [Parameter()] |
| | | 79 | | [switch]$MustRevalidate, |
| | | 80 | | [Parameter()] |
| | | 81 | | [switch]$ProxyRevalidate |
| | | 82 | | ) |
| | | 83 | | # Only works inside a route script block where $Context is available |
| | 0 | 84 | | if ($null -ne $Context.Response) { |
| | | 85 | | # Define default cache control headers if not provided |
| | 0 | 86 | | $cacheControl = [Microsoft.Net.Http.Headers.CacheControlHeaderValue]::new(); |
| | 0 | 87 | | if ($PSBoundParameters.ContainsKey('NoCache')) { $cacheControl.NoCache = $NoCache.IsPresent } |
| | 0 | 88 | | if ($PSBoundParameters.ContainsKey('NoStore')) { $cacheControl.NoStore = $NoStore.IsPresent } |
| | 0 | 89 | | if ($PSBoundParameters.ContainsKey('MaxAge')) { $cacheControl.MaxAge = [TimeSpan]::FromSeconds($MaxAge) } |
| | 0 | 90 | | if ($PSBoundParameters.ContainsKey('SharedMaxAge')) { $cacheControl.SharedMaxAge = [TimeSpan]::FromSeconds($Shar |
| | 0 | 91 | | if ($PSBoundParameters.ContainsKey('MaxStale')) { $cacheControl.MaxStale = $MaxStale.IsPresent } |
| | 0 | 92 | | if ($PSBoundParameters.ContainsKey('MaxStaleLimit')) { $cacheControl.MaxStaleLimit = [TimeSpan]::FromSeconds($Ma |
| | 0 | 93 | | if ($PSBoundParameters.ContainsKey('MinFresh')) { $cacheControl.MinFresh = [TimeSpan]::FromSeconds($MinFresh) } |
| | 0 | 94 | | if ($PSBoundParameters.ContainsKey('NoTransform')) { $cacheControl.NoTransform = $NoTransform.IsPresent } |
| | 0 | 95 | | if ($PSBoundParameters.ContainsKey('OnlyIfCached')) { $cacheControl.OnlyIfCached = $OnlyIfCached.IsPresent } |
| | 0 | 96 | | if ($PSBoundParameters.ContainsKey('Public')) { $cacheControl.Public = $Public.IsPresent } |
| | 0 | 97 | | if ($PSBoundParameters.ContainsKey('Private')) { $cacheControl.Private = $Private.IsPresent } |
| | 0 | 98 | | if ($PSBoundParameters.ContainsKey('MustRevalidate')) { $cacheControl.MustRevalidate = $MustRevalidate.IsPresent |
| | 0 | 99 | | if ($PSBoundParameters.ContainsKey('ProxyRevalidate')) { $cacheControl.ProxyRevalidate = $ProxyRevalidate.IsPres |
| | | 100 | | |
| | | 101 | | # Apply the cache control headers to the response |
| | 0 | 102 | | $Context.Response.CacheControl = $cacheControl |
| | | 103 | | } else { |
| | 0 | 104 | | Write-KrOutsideRouteWarning |
| | | 105 | | } |
| | | 106 | | } |