forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpSelfHostConfiguration.cs
More file actions
418 lines (368 loc) · 15.3 KB
/
HttpSelfHostConfiguration.cs
File metadata and controls
418 lines (368 loc) · 15.3 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// 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.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.IdentityModel.Selectors;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Security;
using System.Web.Http.SelfHost.Channels;
using System.Web.Http.SelfHost.Properties;
using System.Web.Http.SelfHost.ServiceModel;
using System.Web.Http.SelfHost.ServiceModel.Channels;
namespace System.Web.Http.SelfHost
{
/// <summary>
/// The configuration class for Http Services
/// </summary>
public class HttpSelfHostConfiguration : HttpConfiguration
{
private const int DefaultMaxConcurrentRequests = 100;
private const int DefaultMaxBufferSize = 64 * 1024;
private const int DefaultReceivedMessageSize = 64 * 1024;
private const int MinConcurrentRequests = 1;
private const int MinBufferSize = 1;
private const int MinReceivedMessageSize = 1;
private static readonly TimeSpan DefaultReceiveTimeout = new TimeSpan(0, 10, 0);
private static readonly TimeSpan DefaultSendTimeout = new TimeSpan(0, 1, 0);
private Uri _baseAddress;
private int _maxConcurrentRequests;
private ServiceCredentials _credentials = new ServiceCredentials();
private HttpClientCredentialType _clientCredentialType = HttpClientCredentialType.None;
private TransferMode _transferMode;
private int _maxBufferSize = DefaultMaxBufferSize;
private bool _maxBufferSizeIsInitialized;
private long _maxReceivedMessageSize = DefaultReceivedMessageSize;
private TimeSpan _receiveTimeout = DefaultReceiveTimeout;
private TimeSpan _sendTimeout = DefaultSendTimeout;
private HostNameComparisonMode _hostNameComparisonMode;
/// <summary>
/// Initializes a new instance of the <see cref="HttpSelfHostConfiguration"/> class.
/// </summary>
/// <param name="baseAddress">The base address.</param>
public HttpSelfHostConfiguration(string baseAddress)
: this(CreateBaseAddress(baseAddress))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpSelfHostConfiguration"/> class.
/// </summary>
/// <param name="baseAddress">The base address.</param>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "caller owns object")]
public HttpSelfHostConfiguration(Uri baseAddress)
: base(new HttpRouteCollection(ValidateBaseAddress(baseAddress).AbsolutePath))
{
_baseAddress = ValidateBaseAddress(baseAddress);
_maxConcurrentRequests = MultiplyByProcessorCount(DefaultMaxConcurrentRequests);
_maxBufferSize = TransportDefaults.MaxBufferSize;
_maxReceivedMessageSize = TransportDefaults.MaxReceivedMessageSize;
}
/// <summary>
/// Gets the base address.
/// </summary>
/// <value>
/// The base address.
/// </value>
public Uri BaseAddress
{
get { return _baseAddress; }
}
/// <summary>
/// Gets or sets the upper limit of how many concurrent <see cref="T:System.Net.Http.HttpRequestMessage"/> instances
/// can be processed at any given time. The default is 100 times the number of CPU cores.
/// </summary>
/// <value>
/// The maximum concurrent <see cref="T:System.Net.Http.HttpRequestMessage"/> instances processed at any given time.
/// </value>
public int MaxConcurrentRequests
{
get { return _maxConcurrentRequests; }
set
{
if (value < MinConcurrentRequests)
{
throw Error.ArgumentMustBeGreaterThanOrEqualTo("value", value, MinConcurrentRequests);
}
_maxConcurrentRequests = value;
}
}
/// <summary>
/// Gets or sets the transfer mode.
/// </summary>
/// <value>
/// The transfer mode.
/// </value>
public TransferMode TransferMode
{
get { return _transferMode; }
set
{
TransferModeHelper.Validate(value, "value");
_transferMode = value;
}
}
/// <summary>
/// Specifies how the host name should be used in URI comparisons when dispatching an incoming message.
/// </summary>
public HostNameComparisonMode HostNameComparisonMode
{
get { return _hostNameComparisonMode; }
set
{
HostNameComparisonModeHelper.Validate(value, "value");
_hostNameComparisonMode = value;
}
}
/// <summary>
/// Gets or sets the size of the max buffer.
/// </summary>
/// <value>
/// The size of the max buffer.
/// </value>
public int MaxBufferSize
{
get
{
if (_maxBufferSizeIsInitialized || TransferMode != TransferMode.Buffered)
{
return _maxBufferSize;
}
long maxReceivedMessageSize = MaxReceivedMessageSize;
if (maxReceivedMessageSize > Int32.MaxValue)
{
return Int32.MaxValue;
}
return (int)maxReceivedMessageSize;
}
set
{
if (value < MinBufferSize)
{
throw Error.ArgumentMustBeGreaterThanOrEqualTo("value", value, MinBufferSize);
}
_maxBufferSizeIsInitialized = true;
_maxBufferSize = value;
}
}
/// <summary>
/// Gets or sets the size of the max received message.
/// </summary>
/// <value>
/// The size of the max received message.
/// </value>
public long MaxReceivedMessageSize
{
get { return _maxReceivedMessageSize; }
set
{
if (value < MinReceivedMessageSize)
{
throw Error.ArgumentMustBeGreaterThanOrEqualTo("value", value, MinReceivedMessageSize);
}
_maxReceivedMessageSize = value;
}
}
/// <summary>
/// Gets or sets the interval of time that a connection can remain inactive, during which no application messages are received, before it is dropped.
/// </summary>
/// <value>
/// The interval of time that a connection can remain inactive, during which no application messages are received, before it is dropped.
/// </value>
public TimeSpan ReceiveTimeout
{
get { return _receiveTimeout; }
set
{
if (value < TimeSpan.Zero)
{
throw Error.ArgumentMustBeGreaterThanOrEqualTo("value", value, TimeSpan.Zero);
}
_receiveTimeout = value;
}
}
/// <summary>
/// Gets or sets the interval of time provided for a write operation to complete before the transport raises an exception.
/// </summary>
/// <value>
/// The interval of time provided for a write operation to complete before the transport raises an exception.
/// </value>
public TimeSpan SendTimeout
{
get { return _sendTimeout; }
set
{
if (value < TimeSpan.Zero)
{
throw Error.ArgumentMustBeGreaterThanOrEqualTo("value", value, TimeSpan.Zero);
}
_sendTimeout = value;
}
}
/// <summary>
/// Gets or sets UserNamePasswordValidator so that it can be used to validate the username and password
/// sent over HTTP or HTTPS
/// </summary>
/// <value>
/// The server certificate.
/// </value>
public UserNamePasswordValidator UserNamePasswordValidator
{
get { return _credentials.UserNameAuthentication.CustomUserNamePasswordValidator; }
set
{
if (value == null)
{
throw Error.PropertyNull();
}
_clientCredentialType = HttpClientCredentialType.Basic;
_credentials.UserNameAuthentication.CustomUserNamePasswordValidator = value;
_credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
}
}
/// <summary>
/// Gets or sets X509CertificateValidator so that it can be used to validate the client certificate
/// sent over HTTPS
/// </summary>
/// <value>
/// The server certificate.
/// </value>
public X509CertificateValidator X509CertificateValidator
{
get { return _credentials.ClientCertificate.Authentication.CustomCertificateValidator; }
set
{
if (value == null)
{
throw Error.PropertyNull();
}
_clientCredentialType = HttpClientCredentialType.Certificate;
_credentials.ClientCertificate.Authentication.CustomCertificateValidator = value;
_credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.Custom;
}
}
/// <summary>
/// Gets/Sets the ClientCredentialType that server is expecting.
/// </summary>
/// <value>
/// The default value is HttpClientCredentialType.None.
/// </value>
public HttpClientCredentialType ClientCredentialType
{
get { return _clientCredentialType; }
set { _clientCredentialType = value; }
}
/// <summary>
/// Internal method called to configure <see cref="HttpBinding"/> settings.
/// </summary>
/// <param name="httpBinding">Http binding.</param>
/// <returns>The <see cref="BindingParameterCollection"/> to use when building the <see cref="IChannelListener"/> or null if no binding parameters are present.</returns>
internal BindingParameterCollection ConfigureBinding(HttpBinding httpBinding)
{
return OnConfigureBinding(httpBinding);
}
/// <summary>
/// Called to apply the configuration on the endpoint level.
/// </summary>
/// <param name="httpBinding">Http endpoint.</param>
/// <returns>The <see cref="BindingParameterCollection"/> to use when building the <see cref="IChannelListener"/> or null if no binding parameters are present.</returns>
protected virtual BindingParameterCollection OnConfigureBinding(HttpBinding httpBinding)
{
if (httpBinding == null)
{
throw Error.ArgumentNull("httpBinding");
}
if (_clientCredentialType != HttpClientCredentialType.Basic && _credentials.UserNameAuthentication.CustomUserNamePasswordValidator != null)
{
throw Error.InvalidOperation(SRResources.CannotUseOtherClientCredentialTypeWithUserNamePasswordValidator);
}
if (_clientCredentialType != HttpClientCredentialType.Certificate && _credentials.ClientCertificate.Authentication.CustomCertificateValidator != null)
{
throw Error.InvalidOperation(SRResources.CannotUseOtherClientCredentialTypeWithX509CertificateValidator);
}
httpBinding.MaxBufferSize = MaxBufferSize;
httpBinding.MaxReceivedMessageSize = MaxReceivedMessageSize;
httpBinding.TransferMode = TransferMode;
httpBinding.HostNameComparisonMode = HostNameComparisonMode;
httpBinding.ReceiveTimeout = ReceiveTimeout;
httpBinding.SendTimeout = SendTimeout;
// Set up binding parameters
if (_baseAddress.Scheme == Uri.UriSchemeHttps)
{
// we need to use SSL
httpBinding.Security = new HttpBindingSecurity()
{
Mode = HttpBindingSecurityMode.Transport,
};
}
if (_clientCredentialType != HttpClientCredentialType.None)
{
if (httpBinding.Security == null || httpBinding.Security.Mode == HttpBindingSecurityMode.None)
{
// Basic over HTTP case
httpBinding.Security = new HttpBindingSecurity()
{
Mode = HttpBindingSecurityMode.TransportCredentialOnly,
};
}
httpBinding.Security.Transport.ClientCredentialType = _clientCredentialType;
}
if (UserNamePasswordValidator != null || X509CertificateValidator != null)
{
// those are the only two things that affect service credentials
return AddCredentialsToBindingParameters();
}
else
{
return null;
}
}
private BindingParameterCollection AddCredentialsToBindingParameters()
{
BindingParameterCollection bindingParameters = new BindingParameterCollection();
bindingParameters.Add(_credentials);
return bindingParameters;
}
private static Uri CreateBaseAddress(string baseAddress)
{
if (baseAddress == null)
{
throw Error.ArgumentNull("baseAddress");
}
return new Uri(baseAddress, UriKind.RelativeOrAbsolute);
}
private static Uri ValidateBaseAddress(Uri baseAddress)
{
if (baseAddress == null)
{
throw Error.ArgumentNull("baseAddress");
}
if (!baseAddress.IsAbsoluteUri)
{
throw Error.ArgumentUriNotAbsolute("baseAddress", baseAddress);
}
if (!String.IsNullOrEmpty(baseAddress.Query) || !String.IsNullOrEmpty(baseAddress.Fragment))
{
throw Error.ArgumentUriHasQueryOrFragment("baseAddress", baseAddress);
}
if (!ReferenceEquals(baseAddress.Scheme, Uri.UriSchemeHttp) && !ReferenceEquals(baseAddress.Scheme, Uri.UriSchemeHttps))
{
throw Error.ArgumentUriNotHttpOrHttpsScheme("baseAddress", baseAddress);
}
return baseAddress;
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We never want to fail here so we have to catch all exceptions.")]
internal static int MultiplyByProcessorCount(int value)
{
Contract.Assert(value > 0);
try
{
return Math.Max(Environment.ProcessorCount * value, value);
}
catch
{
return value;
}
}
}
}