This repository has been archived by the owner on Mar 19, 2019. It is now read-only.
/
HttpSysOptions.cs
199 lines (174 loc) · 7.74 KB
/
HttpSysOptions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Globalization;
using Microsoft.AspNetCore.Http.Features;
namespace Microsoft.AspNetCore.Server.HttpSys
{
public class HttpSysOptions
{
private const Http503VerbosityLevel DefaultRejectionVerbosityLevel = Http503VerbosityLevel.Basic; // Http.sys default.
private const long DefaultRequestQueueLength = 1000; // Http.sys default.
internal static readonly int DefaultMaxAccepts = 5 * Environment.ProcessorCount;
// Matches the default maxAllowedContentLength in IIS (~28.6 MB)
// https://www.iis.net/configreference/system.webserver/security/requestfiltering/requestlimits#005
private const long DefaultMaxRequestBodySize = 30000000;
private Http503VerbosityLevel _rejectionVebosityLevel = DefaultRejectionVerbosityLevel;
// The native request queue
private long _requestQueueLength = DefaultRequestQueueLength;
private long? _maxConnections;
private RequestQueue _requestQueue;
private UrlGroup _urlGroup;
private long? _maxRequestBodySize = DefaultMaxRequestBodySize;
public HttpSysOptions()
{
}
/// <summary>
/// The maximum number of concurrent accepts.
/// </summary>
public int MaxAccepts { get; set; } = DefaultMaxAccepts;
/// <summary>
/// Attempts kernel mode caching for responses with eligible headers. The response may not include
/// Set-Cookie, Vary, or Pragma headers. It must include a Cache-Control header with Public and
/// either a Shared-Max-Age or Max-Age value, or an Expires header.
/// </summary>
public bool EnableResponseCaching { get; set; } = true;
/// <summary>
/// The url prefixes to register with Http.Sys. These may be modified at any time prior to disposing
/// the listener.
/// </summary>
public UrlPrefixCollection UrlPrefixes { get; } = new UrlPrefixCollection();
/// <summary>
/// Http.Sys authentication settings. These may be modified at any time prior to disposing
/// the listener.
/// </summary>
public AuthenticationManager Authentication { get; } = new AuthenticationManager();
/// <summary>
/// Exposes the Http.Sys timeout configurations. These may also be configured in the registry.
/// These may be modified at any time prior to disposing the listener.
/// </summary>
public TimeoutManager Timeouts { get; } = new TimeoutManager();
/// <summary>
/// Gets or Sets if response body writes that fail due to client disconnects should throw exceptions or
/// complete normally. The default is false.
/// </summary>
public bool ThrowWriteExceptions { get; set; }
/// <summary>
/// Gets or sets the maximum number of concurrent connections to accept, -1 for infinite, or null to
/// use the machine wide setting from the registry. The default value is null.
/// </summary>
public long? MaxConnections
{
get => _maxConnections;
set
{
if (value.HasValue && value < -1)
{
throw new ArgumentOutOfRangeException(nameof(value), value, "The value must be positive, or -1 for infiniate.");
}
if (value.HasValue && _urlGroup != null)
{
_urlGroup.SetMaxConnections(value.Value);
}
_maxConnections = value;
}
}
/// <summary>
/// Gets or sets the maximum number of requests that will be queued up in Http.Sys.
/// </summary>
public long RequestQueueLimit
{
get
{
return _requestQueueLength;
}
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(nameof(value), value, "The value must be greater than zero.");
}
if (_requestQueue != null)
{
_requestQueue.SetLengthLimit(_requestQueueLength);
}
// Only store it if it succeeds or hasn't started yet
_requestQueueLength = value;
}
}
/// <summary>
/// Gets or sets the maximum allowed size of any request body in bytes.
/// When set to null, the maximum request body size is unlimited.
/// This limit has no effect on upgraded connections which are always unlimited.
/// This can be overridden per-request via <see cref="IHttpMaxRequestBodySizeFeature"/>.
/// </summary>
/// <remarks>
/// Defaults to 30,000,000 bytes, which is approximately 28.6MB.
/// </remarks>
public long? MaxRequestBodySize
{
get => _maxRequestBodySize;
set
{
if (value < 0)
{
throw new ArgumentOutOfRangeException(nameof(value), value, "The value must be greater or equal to zero.");
}
_maxRequestBodySize = value;
}
}
/// <summary>
/// Gets or sets a value that controls whether synchronous IO is allowed for the HttpContext.Request.Body and HttpContext.Response.Body.
/// The default is `true`.
/// </summary>
public bool AllowSynchronousIO { get; set; } = true;
/// <summary>
/// Gets or sets a value that controls how http.sys reacts when rejecting requests due to throttling conditions - like when the request
/// queue limit is reached. The default in http.sys is "Basic" which means http.sys is just resetting the TCP connection. IIS uses Limited
/// as its default behavior which will result in sending back a 503 - Service Unavailable back to the client.
/// </summary>
public Http503VerbosityLevel Http503Verbosity
{
get
{
return _rejectionVebosityLevel;
}
set
{
if (value < Http503VerbosityLevel.Basic || value > Http503VerbosityLevel.Full)
{
string message = String.Format(
CultureInfo.InvariantCulture,
"The value must be one of the values defined in the '{0}' enum.",
typeof(Http503VerbosityLevel).Name);
throw new ArgumentOutOfRangeException(nameof(value), value, message);
}
if (_requestQueue != null)
{
_requestQueue.SetRejectionVerbosity(value);
}
// Only store it if it succeeds or hasn't started yet
_rejectionVebosityLevel = value;
}
}
internal void Apply(UrlGroup urlGroup, RequestQueue requestQueue)
{
_urlGroup = urlGroup;
_requestQueue = requestQueue;
if (_maxConnections.HasValue)
{
_urlGroup.SetMaxConnections(_maxConnections.Value);
}
if (_requestQueueLength != DefaultRequestQueueLength)
{
_requestQueue.SetLengthLimit(_requestQueueLength);
}
if (_rejectionVebosityLevel != DefaultRejectionVerbosityLevel)
{
_requestQueue.SetRejectionVerbosity(_rejectionVebosityLevel);
}
Authentication.SetUrlGroupSecurity(urlGroup);
Timeouts.SetUrlGroupTimeouts(urlGroup);
}
}
}