/
HttpListener.cs
326 lines (288 loc) · 10.8 KB
/
HttpListener.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Collections;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Security.Authentication.ExtendedProtection;
using System.Text;
using System.Threading.Tasks;
namespace System.Net
{
public sealed unsafe partial class HttpListener : IDisposable
{
public delegate ExtendedProtectionPolicy ExtendedProtectionSelector(HttpListenerRequest request);
private readonly object _internalLock;
private volatile State _state; // _state is set only within lock blocks, but often read outside locks.
private readonly HttpListenerPrefixCollection _prefixes;
internal Hashtable _uriPrefixes = new Hashtable();
private bool _ignoreWriteExceptions;
private readonly ServiceNameStore _defaultServiceNames;
private readonly HttpListenerTimeoutManager _timeoutManager;
private ExtendedProtectionPolicy _extendedProtectionPolicy;
private AuthenticationSchemeSelector? _authenticationDelegate;
private AuthenticationSchemes _authenticationScheme = AuthenticationSchemes.Anonymous;
private ExtendedProtectionSelector? _extendedProtectionSelectorDelegate;
private string? _realm;
internal ICollection PrefixCollection => _uriPrefixes.Keys;
public HttpListener()
{
_state = State.Stopped;
_internalLock = new object();
_defaultServiceNames = new ServiceNameStore();
_timeoutManager = new HttpListenerTimeoutManager(this);
_prefixes = new HttpListenerPrefixCollection(this);
// default: no CBT checks on any platform (appcompat reasons); applies also to PolicyEnforcement
// config element
_extendedProtectionPolicy = new ExtendedProtectionPolicy(PolicyEnforcement.Never);
}
public AuthenticationSchemeSelector? AuthenticationSchemeSelectorDelegate
{
get => _authenticationDelegate;
set
{
CheckDisposed();
_authenticationDelegate = value;
}
}
[DisallowNull]
public ExtendedProtectionSelector? ExtendedProtectionSelectorDelegate
{
get => _extendedProtectionSelectorDelegate;
set
{
CheckDisposed();
ArgumentNullException.ThrowIfNull(value);
_extendedProtectionSelectorDelegate = value;
}
}
public AuthenticationSchemes AuthenticationSchemes
{
get => _authenticationScheme;
set
{
CheckDisposed();
_authenticationScheme = value;
}
}
public ExtendedProtectionPolicy ExtendedProtectionPolicy
{
get => _extendedProtectionPolicy;
set
{
CheckDisposed();
ArgumentNullException.ThrowIfNull(value);
if (value.CustomChannelBinding != null)
{
throw new ArgumentException(SR.net_listener_cannot_set_custom_cbt, nameof(value));
}
_extendedProtectionPolicy = value;
}
}
public ServiceNameCollection DefaultServiceNames => _defaultServiceNames.ServiceNames;
public HttpListenerPrefixCollection Prefixes
{
get
{
CheckDisposed();
return _prefixes;
}
}
internal void AddPrefix(string uriPrefix)
{
ArgumentNullException.ThrowIfNull(uriPrefix);
string? registeredPrefix;
try
{
CheckDisposed();
int i;
if (string.Compare(uriPrefix, 0, "http://", 0, 7, StringComparison.OrdinalIgnoreCase) == 0)
{
i = 7;
}
else if (string.Compare(uriPrefix, 0, "https://", 0, 8, StringComparison.OrdinalIgnoreCase) == 0)
{
i = 8;
}
else
{
throw new ArgumentException(SR.net_listener_scheme, nameof(uriPrefix));
}
bool inSquareBrakets = false;
int j = i;
while (j < uriPrefix.Length && uriPrefix[j] != '/' && (uriPrefix[j] != ':' || inSquareBrakets))
{
if (uriPrefix[j] == '[')
{
if (inSquareBrakets)
{
j = i;
break;
}
inSquareBrakets = true;
}
if (inSquareBrakets && uriPrefix[j] == ']')
{
inSquareBrakets = false;
}
j++;
}
if (i == j)
{
throw new ArgumentException(SR.net_listener_host, nameof(uriPrefix));
}
if (!uriPrefix.EndsWith('/'))
{
throw new ArgumentException(SR.net_listener_slash, nameof(uriPrefix));
}
registeredPrefix = CreateRegisteredPrefix(uriPrefix, j, i);
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"mapped uriPrefix: {uriPrefix} to registeredPrefix: {registeredPrefix}");
if (_state == State.Started)
{
AddPrefixCore(registeredPrefix);
}
_uriPrefixes[uriPrefix] = registeredPrefix;
_defaultServiceNames.Add(uriPrefix);
}
catch (Exception exception)
{
if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, exception);
throw;
}
static string CreateRegisteredPrefix(string uriPrefix, int j, int i)
{
int length = uriPrefix.Length;
if (uriPrefix[j] != ':')
{
length += i == 7 ? ":80".Length : ":443".Length;
}
return string.Create(length, (uriPrefix, j, i), static (destination, state) =>
{
if (state.uriPrefix[state.j] == ':')
{
state.uriPrefix.CopyTo(destination);
}
else
{
int indexOfNextCopy = state.j;
state.uriPrefix.AsSpan(0, indexOfNextCopy).CopyTo(destination);
if (state.i == 7)
{
":80".CopyTo(destination.Slice(indexOfNextCopy));
indexOfNextCopy += 3;
}
else
{
":443".CopyTo(destination.Slice(indexOfNextCopy));
indexOfNextCopy += 4;
}
state.uriPrefix.AsSpan(state.j).CopyTo(destination.Slice(indexOfNextCopy));
}
int toLowerLength = destination.IndexOf(':');
if (toLowerLength < 0)
{
toLowerLength = destination.Length;
}
OperationStatus operationStatus = Ascii.ToLowerInPlace(destination.Slice(0, toLowerLength), out _);
Debug.Assert(operationStatus == OperationStatus.Done);
});
}
}
internal bool ContainsPrefix(string uriPrefix) => _uriPrefixes.Contains(uriPrefix);
internal bool RemovePrefix(string uriPrefix)
{
try
{
CheckDisposed();
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"uriPrefix: {uriPrefix}");
ArgumentNullException.ThrowIfNull(uriPrefix);
if (!_uriPrefixes.Contains(uriPrefix))
{
return false;
}
if (_state == State.Started)
{
RemovePrefixCore((string)_uriPrefixes[uriPrefix]!);
}
_uriPrefixes.Remove(uriPrefix);
_defaultServiceNames.Remove(uriPrefix);
}
catch (Exception exception)
{
if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, exception);
throw;
}
return true;
}
internal void RemoveAll(bool clear)
{
CheckDisposed();
// go through the uri list and unregister for each one of them
if (_uriPrefixes.Count > 0)
{
if (_state == State.Started)
{
foreach (string registeredPrefix in _uriPrefixes.Values)
{
RemovePrefixCore(registeredPrefix);
}
}
if (clear)
{
_uriPrefixes.Clear();
_defaultServiceNames.Clear();
}
}
}
public string? Realm
{
get => _realm;
set
{
CheckDisposed();
_realm = value;
}
}
public bool IsListening => _state == State.Started;
public bool IgnoreWriteExceptions
{
get => _ignoreWriteExceptions;
set
{
CheckDisposed();
_ignoreWriteExceptions = value;
}
}
public Task<HttpListenerContext> GetContextAsync()
{
return Task.Factory.FromAsync(
(callback, state) => ((HttpListener)state!).BeginGetContext(callback, state),
iar => ((HttpListener)iar!.AsyncState!).EndGetContext(iar),
this);
}
public void Close()
{
try
{
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info("HttpListenerRequest::Close()");
((IDisposable)this).Dispose();
}
catch (Exception exception)
{
if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, $"Close {exception}");
throw;
}
}
internal void CheckDisposed()
{
ObjectDisposedException.ThrowIf(_state == State.Closed, this);
}
private enum State
{
Stopped,
Started,
Closed,
}
void IDisposable.Dispose() => Dispose();
}
}