| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Configures advanced options and operational limits for a Kestrun server instance. |
| | 4 | | .DESCRIPTION |
| | 5 | | This function allows administrators to fine-tune the behavior of a Kestrun server by setting various |
| | 6 | | operational limits and options. |
| | 7 | | .PARAMETER Server |
| | 8 | | The Kestrun server instance to configure.This parameter is mandatory and must be a valid server object. |
| | 9 | | .PARAMETER MaxRequestBodySize |
| | 10 | | Specifies the maximum allowed size of the HTTP request body in bytes. |
| | 11 | | Requests exceeding this size will be rejected. |
| | 12 | | Default: 30,000,000 bytes (28.6 MB). |
| | 13 | | .PARAMETER MaxConcurrentConnections |
| | 14 | | Sets the maximum number of concurrent client connections allowed to the server. |
| | 15 | | Additional connection attempts will be queued or rejected. |
| | 16 | | Default: Unlimited (no explicit limit). |
| | 17 | | .PARAMETER MaxRequestHeaderCount |
| | 18 | | Defines the maximum number of HTTP headers permitted in a single request. |
| | 19 | | Requests with more headers will be rejected. |
| | 20 | | Default: 100. |
| | 21 | | .PARAMETER KeepAliveTimeoutSeconds |
| | 22 | | Specifies the duration, in seconds, that a connection is kept alive when idle before being closed. |
| | 23 | | Default: 120 seconds. |
| | 24 | | .PARAMETER MaxRequestBufferSize |
| | 25 | | Sets the maximum size, in bytes, of the buffer used for reading HTTP requests. |
| | 26 | | Default: 1048576 bytes (1 MB). |
| | 27 | | .PARAMETER MaxRequestHeadersTotalSize |
| | 28 | | Specifies the maximum combined size, in bytes, of all HTTP request headers. |
| | 29 | | Requests exceeding this size will be rejected. |
| | 30 | | Default: 32768 bytes (32 KB). |
| | 31 | | .PARAMETER MaxRequestLineSize |
| | 32 | | Sets the maximum allowed length, in bytes, of the HTTP request line (method, URI, and version). |
| | 33 | | Default: 8192 bytes (8 KB). |
| | 34 | | .PARAMETER MaxResponseBufferSize |
| | 35 | | Specifies the maximum size, in bytes, of the buffer used for sending HTTP responses. |
| | 36 | | Default: 65536 bytes (64 KB). |
| | 37 | | .PARAMETER MinRequestBodyDataRate |
| | 38 | | Defines the minimum data rate, in bytes per second, required for receiving the request body. |
| | 39 | | If the rate falls below this threshold, the connection may be closed. |
| | 40 | | Default: 240 bytes/second. |
| | 41 | | .PARAMETER MinResponseDataRate |
| | 42 | | Sets the minimum data rate, in bytes per second, required for sending the response. |
| | 43 | | Default: 240 bytes/second. |
| | 44 | | .PARAMETER RequestHeadersTimeoutSeconds |
| | 45 | | Specifies the maximum time, in seconds, allowed to receive the complete set of request headers. |
| | 46 | | Default: 30 seconds. |
| | 47 | | .PARAMETER PassThru |
| | 48 | | If specified, the cmdlet will return the modified server instance after applying the limits. |
| | 49 | | .OUTPUTS |
| | 50 | | [Kestrun.Hosting.KestrunHost] |
| | 51 | | The modified Kestrun server instance after applying the limits. |
| | 52 | | .EXAMPLE |
| | 53 | | Set-KrServerLimit -Server $server -MaxRequestBodySize 30000000 |
| | 54 | | Applies the specified limits to the Kestrun server instance. |
| | 55 | | .EXAMPLE |
| | 56 | | Set-KrServerLimit -Server $server -MinRequestBodyDataRate 240 |
| | 57 | | Sets the minimum data rate for receiving the request body. |
| | 58 | | .EXAMPLE |
| | 59 | | Set-KrServerLimit -Server $server -MaxResponseBufferSize 65536 |
| | 60 | | Sets the maximum size of the buffer used for sending HTTP responses. |
| | 61 | | .EXAMPLE |
| | 62 | | Set-KrServerLimit -Server $server -MinResponseDataRate 240 |
| | 63 | | Sets the minimum data rate for sending the response. |
| | 64 | | .EXAMPLE |
| | 65 | | Set-KrServerLimit -Server $server -MaxRequestBodySize 30000000 |
| | 66 | | Applies the specified limits to the Kestrun server instance. |
| | 67 | | .NOTES |
| | 68 | | This cmdlet modifies the server instance's configuration to enforce the specified limits. |
| | 69 | | #> |
| | 70 | | function Set-KrServerLimit { |
| | 71 | | [KestrunRuntimeApi('Definition')] |
| | 72 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] |
| | 73 | | [CmdletBinding()] |
| | 74 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | 75 | | param( |
| | 76 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | 77 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | 78 | | [Parameter()] |
| | 79 | | [long]$MaxRequestBodySize , # Default is 30,000,000 |
| | 80 | | [Parameter()] |
| | 81 | | [int]$MaxConcurrentConnections , |
| | 82 | | [Parameter()] |
| | 83 | | [int]$MaxRequestHeaderCount , # Default is 100 |
| | 84 | | [Parameter()] |
| | 85 | | [int]$KeepAliveTimeoutSeconds , # Default is 130 seconds |
| | 86 | | [Parameter()] |
| | 87 | | [long]$MaxRequestBufferSize , #default is 1,048,576 bytes (1 MB). |
| | 88 | | [Parameter()] |
| | 89 | | [int]$MaxRequestHeadersTotalSize , # Default is 32,768 bytes (32 KB) |
| | 90 | | [Parameter()] |
| | 91 | | [int]$MaxRequestLineSize , # Default is 8,192 bytes (8 KB) |
| | 92 | | [Parameter()] |
| | 93 | | [long]$MaxResponseBufferSize , # Default is 65,536 bytes (64 KB). |
| | 94 | | [Parameter()] |
| | 95 | | [Microsoft.AspNetCore.Server.Kestrel.Core.MinDataRate]$MinRequestBodyDataRate , # Defaults to 240 bytes/second w |
| | 96 | | [Parameter()] |
| | 97 | | [Microsoft.AspNetCore.Server.Kestrel.Core.MinDataRate]$MinResponseDataRate, # Defaults to 240 bytes/second with |
| | 98 | | [Parameter()] |
| | 99 | | [int]$RequestHeadersTimeoutSeconds, # Default is 30 seconds. |
| | 100 | | [Parameter()] |
| | 101 | | [switch]$PassThru |
| | 102 | | ) |
| | 103 | | begin { |
| | 104 | | # Ensure the server instance is resolved |
| 1 | 105 | | $Server = Resolve-KestrunServer -Server $Server |
| 1 | 106 | | if ($null -eq $Server) { |
| 0 | 107 | | throw 'Server is not initialized. Please ensure the server is configured before setting options.' |
| | 108 | | } |
| | 109 | | } |
| | 110 | | process { |
| 1 | 111 | | $options = $Server.Options |
| 1 | 112 | | if ($null -eq $options) { |
| 0 | 113 | | throw 'Server is not initialized.Please ensure the server is configured before setting limits.' |
| | 114 | | } |
| 1 | 115 | | if ($MaxRequestBodySize -gt 0) { |
| 1 | 116 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MaxRequestBodySize to {MaxRequestBod |
| 1 | 117 | | $options.ServerLimits.MaxRequestBodySize = $MaxRequestBodySize |
| | 118 | | } |
| 1 | 119 | | if ($MaxConcurrentConnections -gt 0) { |
| 1 | 120 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MaxConcurrentConnections to {MaxConc |
| 1 | 121 | | $options.ServerLimits.MaxConcurrentConnections = $MaxConcurrentConnections |
| | 122 | | } |
| 1 | 123 | | if ($MaxRequestHeaderCount -gt 0) { |
| 1 | 124 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MaxRequestHeaderCount to {MaxRequest |
| 1 | 125 | | $options.ServerLimits.MaxRequestHeaderCount = $MaxRequestHeaderCount |
| | 126 | | } |
| 1 | 127 | | if ($KeepAliveTimeoutSeconds -gt 0) { |
| 1 | 128 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting KeepAliveTimeout to {KeepAliveTimeou |
| 1 | 129 | | $options.ServerLimits.KeepAliveTimeout = [TimeSpan]::FromSeconds($KeepAliveTimeoutSeconds) |
| | 130 | | } |
| 1 | 131 | | if ($MaxRequestBufferSize -gt 0) { |
| 0 | 132 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MaxRequestBufferSize to {MaxRequestB |
| 0 | 133 | | $options.ServerLimits.MaxRequestBufferSize = $MaxRequestBufferSize |
| | 134 | | } |
| 1 | 135 | | if ($MaxRequestHeadersTotalSize -gt 0) { |
| 0 | 136 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MaxRequestHeadersTotalSize to {MaxRe |
| 0 | 137 | | $options.ServerLimits.MaxRequestHeadersTotalSize = $MaxRequestHeadersTotalSize |
| | 138 | | } |
| 1 | 139 | | if ($MaxRequestLineSize -gt 0) { |
| 0 | 140 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MaxRequestLineSize to {MaxRequestLin |
| 0 | 141 | | $options.ServerLimits.MaxRequestLineSize = $MaxRequestLineSize |
| | 142 | | } |
| 1 | 143 | | if ($MaxResponseBufferSize -gt 0) { |
| 0 | 144 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MaxResponseBufferSize to {MaxRespons |
| 0 | 145 | | $options.ServerLimits.MaxResponseBufferSize = $MaxResponseBufferSize |
| | 146 | | } |
| 1 | 147 | | if ($null -ne $MinRequestBodyDataRate) { |
| 0 | 148 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MinRequestBodyDataRate to {MinReques |
| 0 | 149 | | $options.ServerLimits.MinRequestBodyDataRate = $MinRequestBodyDataRate |
| | 150 | | } |
| 1 | 151 | | if ($null -ne $MinResponseDataRate) { |
| 0 | 152 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MinResponseDataRate to {MinResponseD |
| 0 | 153 | | $options.ServerLimits.MinResponseDataRate = $MinResponseDataRate |
| | 154 | | } |
| 1 | 155 | | if ($null -ne $RequestHeadersTimeout) { |
| 0 | 156 | | Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting RequestHeadersTimeout to {RequestHea |
| 0 | 157 | | $options.ServerLimits.RequestHeadersTimeout = [TimeSpan]::FromSeconds($RequestHeadersTimeoutSeconds) |
| | 158 | | } |
| | 159 | |
|
| 1 | 160 | | if ($PassThru.IsPresent) { |
| | 161 | | # if the PassThru switch is specified, return the server instance |
| | 162 | | # Return the modified server instance |
| 0 | 163 | | return $Server |
| | 164 | | } |
| | 165 | | } |
| | 166 | | } |
| | 167 | |
|