Skip to content

Commit e3ed98e

Browse files
committed
Use a single request object
- Merged NativeRequestContext, RequestContext and FeatureContext into a single object.
1 parent 4ea55fc commit e3ed98e

File tree

5 files changed

+139
-140
lines changed

5 files changed

+139
-140
lines changed

src/Servers/HttpSys/src/AsyncAcceptContext.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal unsafe class AsyncAcceptContext : IValueTaskSource<RequestContext>, IDi
2222
RunContinuationsAsynchronously = false
2323
};
2424

25-
private NativeRequestContext _nativeRequestContext;
25+
private RequestContext _requestContext;
2626

2727
internal AsyncAcceptContext(HttpSysListener server)
2828
{
@@ -70,16 +70,17 @@ private static void IOCompleted(AsyncAcceptContext asyncContext, uint errorCode,
7070
// points to it we need to hook up our authentication handling code here.
7171
try
7272
{
73-
var nativeContext = asyncContext._nativeRequestContext;
73+
var requestContext = asyncContext._requestContext;
7474

75-
if (server.ValidateRequest(nativeContext) && server.ValidateAuth(nativeContext))
75+
if (server.ValidateRequest(requestContext) && server.ValidateAuth(requestContext))
7676
{
7777
// It's important that we clear the native request context before we set the result
7878
// we want to reuse this object for future accepts.
79-
asyncContext._nativeRequestContext = null;
79+
asyncContext._requestContext = null;
8080

81-
var requestContext = new RequestContext(server, nativeContext);
82-
asyncContext._mrvts.SetResult(requestContext);
81+
requestContext.Initialize();
82+
83+
asyncContext._tcs.SetResult(requestContext);
8384

8485
complete = true;
8586
}
@@ -92,14 +93,14 @@ private static void IOCompleted(AsyncAcceptContext asyncContext, uint errorCode,
9293
{
9394
if (!complete)
9495
{
95-
asyncContext.AllocateNativeRequest(size: asyncContext._nativeRequestContext.Size);
96+
asyncContext.AllocateNativeRequest(size: asyncContext._requestContext.Size);
9697
}
9798
}
9899
}
99100
else
100101
{
101102
// (uint)backingBuffer.Length - AlignmentPadding
102-
asyncContext.AllocateNativeRequest(numBytes, asyncContext._nativeRequestContext.RequestId);
103+
asyncContext.AllocateNativeRequest(numBytes, asyncContext._requestContext.RequestId);
103104
}
104105

105106
// We need to issue a new request, either because auth failed, or because our buffer was too small the first time.
@@ -124,8 +125,8 @@ private static void IOCompleted(AsyncAcceptContext asyncContext, uint errorCode,
124125

125126
private static unsafe void IOWaitCallback(uint errorCode, uint numBytes, NativeOverlapped* nativeOverlapped)
126127
{
127-
var asyncResult = (AsyncAcceptContext)ThreadPoolBoundHandle.GetNativeOverlappedState(nativeOverlapped);
128-
IOCompleted(asyncResult, errorCode, numBytes);
128+
var acceptContext = (AsyncAcceptContext)ThreadPoolBoundHandle.GetNativeOverlappedState(nativeOverlapped);
129+
IOCompleted(acceptContext, errorCode, numBytes);
129130
}
130131

131132
private uint QueueBeginGetContext()
@@ -138,21 +139,21 @@ private uint QueueBeginGetContext()
138139
uint bytesTransferred = 0;
139140
statusCode = HttpApi.HttpReceiveHttpRequest(
140141
Server.RequestQueue.Handle,
141-
_nativeRequestContext.RequestId,
142+
_requestContext.RequestId,
142143
// Small perf impact by not using HTTP_RECEIVE_REQUEST_FLAG_COPY_BODY
143144
// if the request sends header+body in a single TCP packet
144145
(uint)HttpApiTypes.HTTP_FLAGS.NONE,
145-
_nativeRequestContext.NativeRequest,
146-
_nativeRequestContext.Size,
146+
_requestContext.NativeRequest,
147+
_requestContext.Size,
147148
&bytesTransferred,
148149
_overlapped);
149150

150-
if (statusCode == UnsafeNclNativeMethods.ErrorCodes.ERROR_INVALID_PARAMETER && _nativeRequestContext.RequestId != 0)
151+
if (statusCode == UnsafeNclNativeMethods.ErrorCodes.ERROR_INVALID_PARAMETER && _requestContext.RequestId != 0)
151152
{
152153
// we might get this if somebody stole our RequestId,
153154
// set RequestId to 0 and start all over again with the buffer we just allocated
154155
// BUGBUG: how can someone steal our request ID? seems really bad and in need of fix.
155-
_nativeRequestContext.RequestId = 0;
156+
_requestContext.RequestId = 0;
156157
retry = true;
157158
}
158159
else if (statusCode == UnsafeNclNativeMethods.ErrorCodes.ERROR_MORE_DATA)
@@ -176,17 +177,16 @@ private uint QueueBeginGetContext()
176177

177178
private void AllocateNativeRequest(uint? size = null, ulong requestId = 0)
178179
{
179-
_nativeRequestContext?.ReleasePins();
180-
_nativeRequestContext?.Dispose();
180+
_requestContext?.ReleasePins();
181+
_requestContext?.Dispose();
181182

182183
var boundHandle = Server.RequestQueue.BoundHandle;
183-
184184
if (_overlapped != null)
185185
{
186186
boundHandle.FreeNativeOverlapped(_overlapped);
187187
}
188188

189-
_nativeRequestContext = new NativeRequestContext(Server.MemoryPool, size, requestId);
189+
_requestContext = new RequestContext(Server, size, requestId);
190190
_overlapped = boundHandle.AllocateNativeOverlapped(_preallocatedOverlapped);
191191
}
192192

src/Servers/HttpSys/src/RequestProcessing/Request.cs

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ namespace Microsoft.AspNetCore.Server.HttpSys
2020
{
2121
internal sealed class Request
2222
{
23-
private NativeRequestContext _nativeRequestContext;
24-
2523
private X509Certificate2 _clientCert;
2624
// TODO: https://github.com/aspnet/HttpSysServer/issues/231
2725
// private byte[] _providedTokenBindingId;
@@ -39,26 +37,25 @@ internal sealed class Request
3937

4038
private bool _isDisposed = false;
4139

42-
internal Request(RequestContext requestContext, NativeRequestContext nativeRequestContext)
40+
internal Request(RequestContext requestContext)
4341
{
4442
// TODO: Verbose log
4543
RequestContext = requestContext;
46-
_nativeRequestContext = nativeRequestContext;
4744
_contentBoundaryType = BoundaryType.None;
4845

49-
RequestId = nativeRequestContext.RequestId;
50-
UConnectionId = nativeRequestContext.ConnectionId;
51-
SslStatus = nativeRequestContext.SslStatus;
46+
RequestId = requestContext.RequestId;
47+
UConnectionId = requestContext.ConnectionId;
48+
SslStatus = requestContext.SslStatus;
5249

53-
KnownMethod = nativeRequestContext.VerbId;
54-
Method = _nativeRequestContext.GetVerb();
50+
KnownMethod = requestContext.VerbId;
51+
Method = requestContext.GetVerb();
5552

56-
RawUrl = nativeRequestContext.GetRawUrl();
53+
RawUrl = requestContext.GetRawUrl();
5754

58-
var cookedUrl = nativeRequestContext.GetCookedUrl();
55+
var cookedUrl = requestContext.GetCookedUrl();
5956
QueryString = cookedUrl.GetQueryString() ?? string.Empty;
6057

61-
var rawUrlInBytes = _nativeRequestContext.GetRawUrlInBytes();
58+
var rawUrlInBytes = requestContext.GetRawUrlInBytes();
6259
var originalPath = RequestUriBuilder.DecodeAndUnescapePath(rawUrlInBytes);
6360

6461
PathBase = string.Empty;
@@ -72,7 +69,7 @@ internal Request(RequestContext requestContext, NativeRequestContext nativeReque
7269
}
7370
else
7471
{
75-
var prefix = requestContext.Server.Options.UrlPrefixes.GetPrefix((int)nativeRequestContext.UrlContext);
72+
var prefix = requestContext.Server.Options.UrlPrefixes.GetPrefix((int)requestContext.UrlContext);
7673
// Prefix may be null if the requested has been transfered to our queue
7774
if (!(prefix is null))
7875
{
@@ -97,11 +94,11 @@ internal Request(RequestContext requestContext, NativeRequestContext nativeReque
9794
}
9895
}
9996

100-
ProtocolVersion = _nativeRequestContext.GetVersion();
97+
ProtocolVersion = RequestContext.GetVersion();
10198

102-
Headers = new RequestHeaders(_nativeRequestContext);
99+
Headers = new RequestHeaders(RequestContext);
103100

104-
User = _nativeRequestContext.GetUser();
101+
User = RequestContext.GetUser();
105102

106103
if (IsHttps)
107104
{
@@ -111,7 +108,7 @@ internal Request(RequestContext requestContext, NativeRequestContext nativeReque
111108
// GetTlsTokenBindingInfo(); TODO: https://github.com/aspnet/HttpSysServer/issues/231
112109

113110
// Finished directly accessing the HTTP_REQUEST structure.
114-
_nativeRequestContext.ReleasePins();
111+
RequestContext.ReleasePins();
115112
// TODO: Verbose log parameters
116113
}
117114

@@ -222,7 +219,7 @@ private AspNetCore.HttpSys.Internal.SocketAddress RemoteEndPoint
222219
{
223220
if (_remoteEndPoint == null)
224221
{
225-
_remoteEndPoint = _nativeRequestContext.GetRemoteEndPoint();
222+
_remoteEndPoint = RequestContext.GetRemoteEndPoint();
226223
}
227224

228225
return _remoteEndPoint;
@@ -235,7 +232,7 @@ private AspNetCore.HttpSys.Internal.SocketAddress LocalEndPoint
235232
{
236233
if (_localEndPoint == null)
237234
{
238-
_localEndPoint = _nativeRequestContext.GetLocalEndPoint();
235+
_localEndPoint = RequestContext.GetLocalEndPoint();
239236
}
240237

241238
return _localEndPoint;
@@ -278,15 +275,15 @@ public IReadOnlyDictionary<int, ReadOnlyMemory<byte>> RequestInfo
278275
{
279276
if (_requestInfo == null)
280277
{
281-
_requestInfo = _nativeRequestContext.GetRequestInfo();
278+
_requestInfo = RequestContext.GetRequestInfo();
282279
}
283280
return _requestInfo;
284281
}
285282
}
286283

287284
private void GetTlsHandshakeResults()
288285
{
289-
var handshake = _nativeRequestContext.GetTlsHandshake();
286+
var handshake = RequestContext.GetTlsHandshake();
290287

291288
Protocol = handshake.Protocol;
292289
// The OS considers client and server TLS as different enum values. SslProtocols choose to combine those for some reason.
@@ -332,7 +329,7 @@ public X509Certificate2 ClientCertificate
332329
{
333330
try
334331
{
335-
_clientCert = _nativeRequestContext.GetClientCertificate();
332+
_clientCert = RequestContext.GetClientCertificate();
336333
}
337334
catch (CryptographicException ce)
338335
{
@@ -426,27 +423,22 @@ private unsafe void GetTlsTokenBindingInfo()
426423
*/
427424
internal uint GetChunks(ref int dataChunkIndex, ref uint dataChunkOffset, byte[] buffer, int offset, int size)
428425
{
429-
return _nativeRequestContext.GetChunks(ref dataChunkIndex, ref dataChunkOffset, buffer, offset, size);
426+
return RequestContext.GetChunks(ref dataChunkIndex, ref dataChunkOffset, buffer, offset, size);
430427
}
431428

432429
// should only be called from RequestContext
433430
internal void Dispose()
434431
{
435-
// TODO: Verbose log
436-
_isDisposed = true;
437-
_nativeRequestContext.Dispose();
438-
(User?.Identity as WindowsIdentity)?.Dispose();
439-
if (_nativeStream != null)
440-
{
441-
_nativeStream.Dispose();
442-
}
443-
}
444-
445-
private void CheckDisposed()
446-
{
447-
if (_isDisposed)
432+
if (!_isDisposed)
448433
{
449-
throw new ObjectDisposedException(this.GetType().FullName);
434+
// TODO: Verbose log
435+
_isDisposed = true;
436+
RequestContext.Dispose();
437+
(User?.Identity as WindowsIdentity)?.Dispose();
438+
if (_nativeStream != null)
439+
{
440+
_nativeStream.Dispose();
441+
}
450442
}
451443
}
452444

0 commit comments

Comments
 (0)