| | 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 |
| 1 | 78 | | $Server = Resolve-KestrunServer -Server $Server |
| 1 | 79 | | if ($null -eq $Server) { |
| 0 | 80 | | throw 'Server is not initialized. Please ensure the server is configured before setting options.' |
| | 81 | | } |
| | 82 | | } |
| | 83 | | process { |
| 1 | 84 | | $options = $Server.Options |
| 1 | 85 | | if ($null -eq $options) { |
| 0 | 86 | | throw 'Server is not initialized. Please ensure the server is configured before setting options.' |
| | 87 | | } |
| | 88 | |
|
| 1 | 89 | | if ($AllowSynchronousIO.IsPresent) { |
| 0 | 90 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting ServerOptions.AllowSynchronousIO to |
| 0 | 91 | | $options.ServerOptions.AllowSynchronousIO = $AllowSynchronousIO.IsPresent |
| | 92 | | } |
| 1 | 93 | | if ($DisableResponseHeaderCompression.IsPresent) { |
| 0 | 94 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting ServerOptions.AllowResponseHeaderCom |
| | 95 | | -Properties $false |
| 0 | 96 | | $options.ServerOptions.AllowResponseHeaderCompression = $false |
| | 97 | | } |
| 1 | 98 | | if ($DenyServerHeader.IsPresent) { |
| 1 | 99 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting ServerOptions.AddServerHeader to {Ad |
| 1 | 100 | | $options.ServerOptions.AddServerHeader = $false |
| | 101 | | } |
| 1 | 102 | | if ($AllowAlternateSchemes.IsPresent) { |
| 0 | 103 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting ServerOptions.AllowAlternateSchemes |
| 0 | 104 | | $options.ServerOptions.AllowAlternateSchemes = $true |
| | 105 | | } |
| 1 | 106 | | if ($AllowHostHeaderOverride.IsPresent) { |
| 0 | 107 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting ServerOptions.AllowHostHeaderOverrid |
| 0 | 108 | | $options.ServerOptions.AllowHostHeaderOverride = $true |
| | 109 | | } |
| 1 | 110 | | if ($DisableStringReuse.IsPresent) { |
| 0 | 111 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting ServerOptions.DisableStringReuse to |
| 0 | 112 | | $options.ServerOptions.DisableStringReuse = $true |
| | 113 | | } |
| 1 | 114 | | if ($MaxRunspaces -gt 0) { |
| 0 | 115 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MaxRunspaces to {MaxRunspaces}" -Pro |
| 0 | 116 | | $options.MaxRunspaces = $MaxRunspaces |
| | 117 | | } |
| 1 | 118 | | if ($MinRunspaces -gt 0) { |
| 1 | 119 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MinRunspaces to {MinRunspaces}" -Pro |
| 1 | 120 | | $options.MinRunspaces = $MinRunspaces |
| | 121 | | } |
| | 122 | |
|
| 1 | 123 | | if ($PassThru.IsPresent) { |
| | 124 | | # if the PassThru switch is specified, return the server instance |
| | 125 | | # Return the modified server instance |
| 0 | 126 | | return $Server |
| | 127 | | } |
| | 128 | | } |
| | 129 | | } |
| | 130 | |
|