| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Enables Status Code Pages for a Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Wraps KestrunHostStatusCodePagesExtensions to configure how 4xx–5xx responses |
| | | 6 | | produce a body: default text, redirect, re-execute, template body, options object, |
| | | 7 | | or a scripted handler via LanguageOptions (PowerShell/C#). |
| | | 8 | | .PARAMETER Server |
| | | 9 | | The Kestrun server instance (resolved if omitted via Resolve-KestrunServer). |
| | | 10 | | .PARAMETER LocationFormat |
| | | 11 | | The location URL for Redirect mode (e.g. "/error/{0}"). |
| | | 12 | | .PARAMETER PathFormat |
| | | 13 | | The path to re-execute for ReExecute mode (e.g. "/error/{0}"). |
| | | 14 | | .PARAMETER QueryFormat |
| | | 15 | | The query string to append for ReExecute mode (e.g. "?code={0}"). |
| | | 16 | | .PARAMETER ContentType |
| | | 17 | | The content type for Template mode (e.g. "text/plain" or "application/json"). |
| | | 18 | | .PARAMETER BodyFormat |
| | | 19 | | The body format string for Template mode (e.g. "Oops {0}"). |
| | | 20 | | .PARAMETER LanguageOptions |
| | | 21 | | A pre-configured LanguageOptions object for custom scripted handling. |
| | | 22 | | .PARAMETER ScriptBlock |
| | | 23 | | A PowerShell script block for custom scripted handling. The script block |
| | | 24 | | receives a single parameter: the HttpContext object. |
| | | 25 | | .PARAMETER Code |
| | | 26 | | A code string for custom scripted handling. |
| | | 27 | | .PARAMETER Language |
| | | 28 | | The scripting language for the code string (PowerShell, CSharp, VisualBasic). |
| | | 29 | | .PARAMETER CodeFilePath |
| | | 30 | | A file path to a code file for custom scripted handling (.ps1, .cs, .vb). |
| | | 31 | | .PARAMETER ExtraRefs |
| | | 32 | | Additional assembly references for custom scripted handling. |
| | | 33 | | .PARAMETER Arguments |
| | | 34 | | Additional arguments (key-value pairs) for custom scripted handling. |
| | | 35 | | .PARAMETER PassThru |
| | | 36 | | If specified, the function will return the created route object. |
| | | 37 | | .EXAMPLE |
| | | 38 | | Enable-KrStatusCodePage -Mode Default |
| | | 39 | | .EXAMPLE |
| | | 40 | | Enable-KrStatusCodePage -Mode Redirect -Location "/error/{0}" |
| | | 41 | | .EXAMPLE |
| | | 42 | | Enable-KrStatusCodePage -Mode ReExecute -Path "/error/{0}" -Query "?code={0}" |
| | | 43 | | .EXAMPLE |
| | | 44 | | Enable-KrStatusCodePage -Mode Template -ContentType "text/plain" -BodyFormat "Oops {0}" |
| | | 45 | | .EXAMPLE |
| | | 46 | | $opts = [Microsoft.AspNetCore.Diagnostics.StatusCodePagesOptions]::new() |
| | | 47 | | Enable-KrStatusCodePage -Mode Options -Options $opts |
| | | 48 | | .EXAMPLE |
| | | 49 | | $lo = [Kestrun.Hosting.Options.LanguageOptions]::new() |
| | | 50 | | $lo.ExtraImports = @('System.Net') |
| | | 51 | | $lo.Code = { |
| | | 52 | | param($Context) |
| | | 53 | | $statusCode = $Context.Response.StatusCode |
| | | 54 | | $reasonPhrase = [System.Net.HttpStatusCode]::Parse([int]$statusCode).ToString() |
| | | 55 | | $message = "Custom handler: Status code $statusCode - $reasonPhrase" |
| | | 56 | | $Context.Response.ContentType = 'text/plain' |
| | | 57 | | $Context.Response.WriteAsync($message) | Out-Null |
| | | 58 | | } |
| | | 59 | | Enable-KrStatusCodePage -Mode LanguageOptions -LanguageOptions $lo |
| | | 60 | | .EXAMPLE |
| | | 61 | | Enable-KrStatusCodePage -Mode ScriptBlock -ScriptBlock { |
| | | 62 | | param($Context) |
| | | 63 | | $statusCode = $Context.Response.StatusCode |
| | | 64 | | $reasonPhrase = [System.Net.HttpStatusCode]::Parse([int]$statusCode).ToString() |
| | | 65 | | $message = "Custom handler: Status code $statusCode - $reasonPhrase" |
| | | 66 | | $Context.Response.ContentType = 'text/plain' |
| | | 67 | | $Context.Response.WriteAsync($message) | Out-Null |
| | | 68 | | } |
| | | 69 | | .EXAMPLE |
| | | 70 | | Enable-KrStatusCodePage -Mode Code -Language PowerShell -Code { |
| | | 71 | | param($Context) |
| | | 72 | | $statusCode = $Context.Response.StatusCode |
| | | 73 | | $reasonPhrase = [System.Net.HttpStatusCode]::Parse([int]$statusCode).ToString() |
| | | 74 | | $message = "Custom handler: Status code $statusCode - $reasonPhrase" |
| | | 75 | | $Context.Response.ContentType = 'text/plain' |
| | | 76 | | $Context.Response.WriteAsync($message) | Out-Null |
| | | 77 | | } |
| | | 78 | | .EXAMPLE |
| | | 79 | | Enable-KrStatusCodePage -Mode CodeFilePath -CodeFilePath 'C:\Scripts\StatusCodeHandler.ps1' |
| | | 80 | | .NOTES |
| | | 81 | | This function is part of the Kestrun PowerShell module and is used to manage Kestrun servers |
| | | 82 | | and their middleware components. |
| | | 83 | | #> |
| | | 84 | | function Enable-KrStatusCodePage { |
| | | 85 | | [KestrunRuntimeApi('Definition')] |
| | | 86 | | [CmdletBinding(DefaultParameterSetName = 'Default')] |
| | | 87 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 88 | | param( |
| | | 89 | | [Parameter(ValueFromPipeline = $true)] |
| | | 90 | | [Kestrun.Hosting.KestrunHost] $Server, |
| | | 91 | | |
| | | 92 | | # Redirect |
| | | 93 | | [Parameter(Mandatory = $true, ParameterSetName = 'Redirect')] |
| | | 94 | | [string] $LocationFormat, |
| | | 95 | | |
| | | 96 | | # Re-execute |
| | | 97 | | [Parameter(Mandatory = $true, ParameterSetName = 'ReExecute')] |
| | | 98 | | [string] $PathFormat, |
| | | 99 | | [Parameter(ParameterSetName = 'ReExecute')] |
| | | 100 | | [string] $QueryFormat, |
| | | 101 | | |
| | | 102 | | # Template body |
| | | 103 | | [Parameter(Mandatory = $true, ParameterSetName = 'Template')] |
| | | 104 | | [string] $ContentType, |
| | | 105 | | [Parameter(Mandatory = $true, ParameterSetName = 'Template')] |
| | | 106 | | [string] $BodyFormat, |
| | | 107 | | |
| | | 108 | | # Custom via LanguageOptions or ScriptBlock |
| | | 109 | | [Parameter(ParameterSetName = 'LanguageOptions')] |
| | | 110 | | [Kestrun.Hosting.Options.LanguageOptions] $LanguageOptions, |
| | | 111 | | |
| | | 112 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | | 113 | | [scriptblock] $ScriptBlock, |
| | | 114 | | |
| | | 115 | | [Parameter(Mandatory = $true, ParameterSetName = 'Code')] |
| | | 116 | | [Alias('CodeBlock')] |
| | | 117 | | [string]$Code, |
| | | 118 | | [Parameter(Mandatory = $true, ParameterSetName = 'Code')] |
| | | 119 | | [Kestrun.Scripting.ScriptLanguage]$Language, |
| | | 120 | | |
| | | 121 | | [Parameter(Mandatory = $true, ParameterSetName = 'CodeFilePath')] |
| | | 122 | | [string]$CodeFilePath, |
| | | 123 | | |
| | | 124 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | | 125 | | [Parameter(ParameterSetName = 'Code')] |
| | | 126 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 127 | | [System.Reflection.Assembly[]]$ExtraRefs = $null, |
| | | 128 | | |
| | | 129 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | | 130 | | [Parameter(ParameterSetName = 'Code')] |
| | | 131 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 132 | | [hashtable]$Arguments, |
| | | 133 | | |
| | | 134 | | [switch] $PassThru |
| | | 135 | | ) |
| | | 136 | | begin { |
| | 0 | 137 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 138 | | } |
| | | 139 | | process { |
| | 0 | 140 | | $statusCodeOptions = [Kestrun.Hosting.Options.StatusCodeOptions]::new($Server) |
| | 0 | 141 | | switch ($PSCmdlet.ParameterSetName) { |
| | | 142 | | 'Default' { |
| | | 143 | | # Nothing to configure; use default plain text handler |
| | | 144 | | } |
| | | 145 | | 'Template' { |
| | 0 | 146 | | if ([string]::IsNullOrWhiteSpace($ContentType)) { throw '-ContentType is required for Mode=Template.' } |
| | 0 | 147 | | if ([string]::IsNullOrWhiteSpace($BodyFormat)) { throw '-BodyFormat is required for Mode=Template.' } |
| | 0 | 148 | | $statusCodeOptions.ContentType = $ContentType |
| | 0 | 149 | | $statusCodeOptions.BodyFormat = $BodyFormat |
| | | 150 | | } |
| | | 151 | | 'Redirect' { |
| | 0 | 152 | | if ([string]::IsNullOrWhiteSpace($LocationFormat)) { throw '-LocationFormat is required for Mode=Redirec |
| | 0 | 153 | | $statusCodeOptions.LocationFormat = $LocationFormat |
| | | 154 | | } |
| | | 155 | | 'ReExecute' { |
| | 0 | 156 | | if ([string]::IsNullOrWhiteSpace($PathFormat)) { throw '-Path is required for Mode=ReExecute.' } |
| | 0 | 157 | | $statusCodeOptions.PathFormat = $PathFormat |
| | 0 | 158 | | $statusCodeOptions.QueryFormat = $QueryFormat |
| | | 159 | | } |
| | | 160 | | 'LanguageOptions' { |
| | 0 | 161 | | $statusCodeOptions.LanguageOptions = $LanguageOptions |
| | | 162 | | } |
| | | 163 | | default { |
| | 0 | 164 | | $lo = [Kestrun.Hosting.Options.LanguageOptions]::new() |
| | 0 | 165 | | $lo.ExtraImports = $ExtraImports |
| | 0 | 166 | | $lo.ExtraRefs = $ExtraRefs |
| | 0 | 167 | | if ($null -ne $Arguments) { |
| | 0 | 168 | | $dict = [System.Collections.Generic.Dictionary[string, object]]::new() |
| | 0 | 169 | | foreach ($key in $Arguments.Keys) { |
| | 0 | 170 | | $dict[$key] = $Arguments[$key] |
| | | 171 | | } |
| | 0 | 172 | | $lo.Arguments = $dict |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | switch ($PSCmdlet.ParameterSetName) { |
| | | 176 | | 'ScriptBlock' { |
| | 0 | 177 | | $lo.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | 0 | 178 | | $lo.Code = $ScriptBlock.ToString() |
| | | 179 | | } |
| | | 180 | | 'Code' { |
| | 0 | 181 | | $lo.Language = $Language |
| | 0 | 182 | | $lo.Code = $Code |
| | | 183 | | } |
| | | 184 | | 'CodeFilePath' { |
| | 0 | 185 | | if (-not (Test-Path -Path $CodeFilePath)) { |
| | 0 | 186 | | throw "The specified code file path does not exist: $CodeFilePath" |
| | | 187 | | } |
| | 0 | 188 | | $extension = Split-Path -Path $CodeFilePath -Extension |
| | 0 | 189 | | switch ($extension) { |
| | | 190 | | '.ps1' { |
| | 0 | 191 | | $lo.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | | 192 | | } |
| | | 193 | | '.cs' { |
| | 0 | 194 | | $lo.Language = [Kestrun.Scripting.ScriptLanguage]::CSharp |
| | | 195 | | } |
| | | 196 | | '.vb' { |
| | 0 | 197 | | $lo.Language = [Kestrun.Scripting.ScriptLanguage]::VisualBasic |
| | | 198 | | } |
| | | 199 | | default { |
| | 0 | 200 | | throw "Unsupported '$extension' code file extension." |
| | | 201 | | } |
| | | 202 | | } |
| | 0 | 203 | | $lo.Code = Get-Content -Path $CodeFilePath -Raw |
| | | 204 | | } |
| | | 205 | | default { |
| | 0 | 206 | | throw "Unrecognized ParameterSetName: $($PSCmdlet.ParameterSetName)" |
| | | 207 | | } |
| | | 208 | | } |
| | 0 | 209 | | $statusCodeOptions.LanguageOptions = $lo |
| | | 210 | | } |
| | | 211 | | } |
| | | 212 | | # Assign the configured options to the server instance |
| | 0 | 213 | | $Server.StatusCodeOptions = $statusCodeOptions |
| | | 214 | | |
| | 0 | 215 | | if ($PassThru.IsPresent) { |
| | | 216 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 217 | | return $Server |
| | | 218 | | } |
| | | 219 | | } |
| | | 220 | | } |