< Summary - Kestrun — Combined Coverage

Information
Class: Public.Server.Set-KrServerLimit
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Server/Set-KrServerLimit.ps1
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
58%
Covered lines: 24
Uncovered lines: 17
Coverable lines: 41
Total lines: 167
Line coverage: 58.5%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Server/Set-KrServerLimit.ps1

#LineLine coverage
 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#>
 70function 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
 1105        $Server = Resolve-KestrunServer -Server $Server
 1106        if ($null -eq $Server) {
 0107            throw 'Server is not initialized. Please ensure the server is configured before setting options.'
 108        }
 109    }
 110    process {
 1111        $options = $Server.Options
 1112        if ($null -eq $options) {
 0113            throw 'Server is not initialized.Please ensure the server is configured before setting limits.'
 114        }
 1115        if ($MaxRequestBodySize -gt 0) {
 1116            Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MaxRequestBodySize to {MaxRequestBod
 1117            $options.ServerLimits.MaxRequestBodySize = $MaxRequestBodySize
 118        }
 1119        if ($MaxConcurrentConnections -gt 0) {
 1120            Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MaxConcurrentConnections to {MaxConc
 1121            $options.ServerLimits.MaxConcurrentConnections = $MaxConcurrentConnections
 122        }
 1123        if ($MaxRequestHeaderCount -gt 0) {
 1124            Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MaxRequestHeaderCount to {MaxRequest
 1125            $options.ServerLimits.MaxRequestHeaderCount = $MaxRequestHeaderCount
 126        }
 1127        if ($KeepAliveTimeoutSeconds -gt 0) {
 1128            Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting KeepAliveTimeout to {KeepAliveTimeou
 1129            $options.ServerLimits.KeepAliveTimeout = [TimeSpan]::FromSeconds($KeepAliveTimeoutSeconds)
 130        }
 1131        if ($MaxRequestBufferSize -gt 0) {
 0132            Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MaxRequestBufferSize to {MaxRequestB
 0133            $options.ServerLimits.MaxRequestBufferSize = $MaxRequestBufferSize
 134        }
 1135        if ($MaxRequestHeadersTotalSize -gt 0) {
 0136            Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MaxRequestHeadersTotalSize to {MaxRe
 0137            $options.ServerLimits.MaxRequestHeadersTotalSize = $MaxRequestHeadersTotalSize
 138        }
 1139        if ($MaxRequestLineSize -gt 0) {
 0140            Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MaxRequestLineSize to {MaxRequestLin
 0141            $options.ServerLimits.MaxRequestLineSize = $MaxRequestLineSize
 142        }
 1143        if ($MaxResponseBufferSize -gt 0) {
 0144            Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MaxResponseBufferSize to {MaxRespons
 0145            $options.ServerLimits.MaxResponseBufferSize = $MaxResponseBufferSize
 146        }
 1147        if ($null -ne $MinRequestBodyDataRate) {
 0148            Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MinRequestBodyDataRate to {MinReques
 0149            $options.ServerLimits.MinRequestBodyDataRate = $MinRequestBodyDataRate
 150        }
 1151        if ($null -ne $MinResponseDataRate) {
 0152            Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting MinResponseDataRate to {MinResponseD
 0153            $options.ServerLimits.MinResponseDataRate = $MinResponseDataRate
 154        }
 1155        if ($null -ne $RequestHeadersTimeout) {
 0156            Write-KrLog -Logger $Server.HostLogger -Level Verbose -Message "Setting RequestHeadersTimeout to {RequestHea
 0157            $options.ServerLimits.RequestHeadersTimeout = [TimeSpan]::FromSeconds($RequestHeadersTimeoutSeconds)
 158        }
 159
 1160        if ($PassThru.IsPresent) {
 161            # if the PassThru switch is specified, return the server instance
 162            # Return the modified server instance
 0163            return $Server
 164        }
 165    }
 166}
 167

Methods/Properties

Set-KrServerLimit()