| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Configures advanced options and operational limits for a Kestrun server instance. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | The Set-KrServerOptions function allows fine-grained configuration of a Kestrun server instance. |
| | | 6 | | It enables administrators to control server behavior, resource usage, and protocol compliance by |
| | | 7 | | setting limits on request sizes, connection counts, timeouts, and other operational parameters. |
| | | 8 | | Each parameter is optional and, if not specified, the server will use its built-in default value. |
| | | 9 | | .PARAMETER Server |
| | | 10 | | The Kestrun server instance to configure. This parameter is mandatory and must be a valid server object. |
| | | 11 | | .PARAMETER AllowSynchronousIO |
| | | 12 | | If set to $true, allows synchronous IO operations on the server. |
| | | 13 | | Synchronous IO can impact scalability and is generally discouraged. |
| | | 14 | | Default: $false. |
| | | 15 | | .PARAMETER DisableResponseHeaderCompression |
| | | 16 | | If set to $true, disables compression of HTTP response headers. |
| | | 17 | | Default: $false. |
| | | 18 | | .PARAMETER DenyServerHeader |
| | | 19 | | If set to $true, removes the 'Server' HTTP header from responses for improved privacy and security. |
| | | 20 | | Default: $false. |
| | | 21 | | .PARAMETER AllowAlternateSchemes |
| | | 22 | | If set to $true, allows alternate URI schemes (other than HTTP/HTTPS) in requests. |
| | | 23 | | Default: $false. |
| | | 24 | | .PARAMETER AllowHostHeaderOverride |
| | | 25 | | If set to $true, permits overriding the Host header in incoming requests. |
| | | 26 | | Default: $false. |
| | | 27 | | .PARAMETER DisableStringReuse |
| | | 28 | | If set to $true, disables internal string reuse optimizations, which may increase memory usage but can help with |
| | | 29 | | Default: $false. |
| | | 30 | | .PARAMETER MaxRunspaces |
| | | 31 | | Specifies the maximum number of runspaces to use for script execution. |
| | | 32 | | This can help control resource usage and concurrency in script execution. |
| | | 33 | | Default: 2x CPU cores or as specified in the KestrunOptions. |
| | | 34 | | .PARAMETER MinRunspaces |
| | | 35 | | Specifies the minimum number of runspaces to use for script execution. |
| | | 36 | | This ensures that at least a certain number of runspaces are always available for processing requests. |
| | | 37 | | Default: 1. |
| | | 38 | | .PARAMETER PassThru |
| | | 39 | | If specified, the cmdlet will return the modified server instance after applying the limits. |
| | | 40 | | .EXAMPLE |
| | | 41 | | Set-KrServerOptions -Server $srv -MaxRequestBodySize 1000000 |
| | | 42 | | Configures the server instance $srv to limit request body size to 1,000,000 bytes. |
| | | 43 | | .EXAMPLE |
| | | 44 | | Set-KrServerOptions -Server $srv -AllowSynchronousIO |
| | | 45 | | Configures the server instance $srv to allow synchronous IO operations. |
| | | 46 | | .NOTES |
| | | 47 | | All parameters are optional except for -Server. |
| | | 48 | | Defaults are based on typical Kestrun server settings as of the latest release. |
| | | 49 | | #> |
| | | 50 | | function Set-KrServerOptions { |
| | | 51 | | [KestrunRuntimeApi('Definition')] |
| | | 52 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] |
| | | 53 | | [CmdletBinding()] |
| | | 54 | | param( |
| | | 55 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 56 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 57 | | [Parameter()] |
| | | 58 | | [switch]$AllowSynchronousIO, |
| | | 59 | | [Parameter()] |
| | | 60 | | [switch]$DisableResponseHeaderCompression , |
| | | 61 | | [Parameter()] |
| | | 62 | | [switch]$DenyServerHeader, |
| | | 63 | | [Parameter()] |
| | | 64 | | [switch]$AllowAlternateSchemes, |
| | | 65 | | [Parameter()] |
| | | 66 | | [switch]$AllowHostHeaderOverride, |
| | | 67 | | [Parameter()] |
| | | 68 | | [switch]$DisableStringReuse, |
| | | 69 | | [Parameter()] |
| | | 70 | | [int]$MaxRunspaces, |
| | | 71 | | [Parameter()] |
| | | 72 | | [int]$MinRunspaces = 1, |
| | | 73 | | [Parameter()] |
| | | 74 | | [switch]$PassThru |
| | | 75 | | ) |
| | | 76 | | begin { |
| | | 77 | | # Ensure the server instance is resolved |
| | 0 | 78 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 79 | | } |
| | | 80 | | process { |
| | 0 | 81 | | $options = $Server.Options |
| | 0 | 82 | | if ($null -eq $options) { |
| | 0 | 83 | | throw 'Server is not initialized. Please ensure the server is configured before setting options.' |
| | | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | if ($AllowSynchronousIO.IsPresent) { |
| | 0 | 87 | | Write-KrLog -Logger $Server.Logger -Level Verbose -Message "Setting ServerOptions.AllowSynchronousIO to {All |
| | 0 | 88 | | $options.ServerOptions.AllowSynchronousIO = $AllowSynchronousIO.IsPresent |
| | | 89 | | } |
| | 0 | 90 | | if ($DisableResponseHeaderCompression.IsPresent) { |
| | 0 | 91 | | Write-KrLog -Logger $Server.Logger -Level Verbose -Message "Setting ServerOptions.AllowResponseHeaderCompres |
| | | 92 | | -Values $false |
| | 0 | 93 | | $options.ServerOptions.AllowResponseHeaderCompression = $false |
| | | 94 | | } |
| | 0 | 95 | | if ($DenyServerHeader.IsPresent) { |
| | 0 | 96 | | Write-KrLog -Logger $Server.Logger -Level Verbose -Message "Setting ServerOptions.AddServerHeader to {AddSer |
| | 0 | 97 | | $options.ServerOptions.AddServerHeader = $false |
| | | 98 | | } |
| | 0 | 99 | | if ($AllowAlternateSchemes.IsPresent) { |
| | 0 | 100 | | Write-KrLog -Logger $Server.Logger -Level Verbose -Message "Setting ServerOptions.AllowAlternateSchemes to { |
| | 0 | 101 | | $options.ServerOptions.AllowAlternateSchemes = $true |
| | | 102 | | } |
| | 0 | 103 | | if ($AllowHostHeaderOverride.IsPresent) { |
| | 0 | 104 | | Write-KrLog -Logger $Server.Logger -Level Verbose -Message "Setting ServerOptions.AllowHostHeaderOverride to |
| | 0 | 105 | | $options.ServerOptions.AllowHostHeaderOverride = $true |
| | | 106 | | } |
| | 0 | 107 | | if ($DisableStringReuse.IsPresent) { |
| | 0 | 108 | | Write-KrLog -Logger $Server.Logger -Level Verbose -Message "Setting ServerOptions.DisableStringReuse to {Dis |
| | 0 | 109 | | $options.ServerOptions.DisableStringReuse = $true |
| | | 110 | | } |
| | 0 | 111 | | if ($MaxRunspaces -gt 0) { |
| | 0 | 112 | | Write-KrLog -Logger $Server.Logger -Level Verbose -Message "Setting MaxRunspaces to {MaxRunspaces}" -Values |
| | 0 | 113 | | $options.MaxRunspaces = $MaxRunspaces |
| | | 114 | | } |
| | 0 | 115 | | if ($MinRunspaces -gt 0) { |
| | 0 | 116 | | Write-KrLog -Logger $Server.Logger -Level Verbose -Message "Setting MinRunspaces to {MinRunspaces}" -Values |
| | 0 | 117 | | $options.MinRunspaces = $MinRunspaces |
| | | 118 | | } |
| | | 119 | | |
| | 0 | 120 | | if ($PassThru.IsPresent) { |
| | | 121 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 122 | | return $Server |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | |