Adds InProcessHandler to calls, Refactor to remove IHttpContext completely

This commit is contained in:
Justin Kotalik 2017-11-20 18:09:08 -08:00
parent 90d5f124b6
commit f27190a1f1
5 changed files with 45 additions and 45 deletions

View File

@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
internal static extern bool CloseHandle(IntPtr handle); internal static extern bool CloseHandle(IntPtr handle);
public const int S_OK = 0; public const int S_OK = 0;
private const string AspNetCoreModuleDll = "aspnetcore.dll"; private const string AspNetCoreModuleDll = "aspnetcorerh.dll";
public enum REQUEST_NOTIFICATION_STATUS public enum REQUEST_NOTIFICATION_STATUS
{ {
@ -32,47 +32,47 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
RQ_NOTIFICATION_FINISH_REQUEST RQ_NOTIFICATION_FINISH_REQUEST
} }
public delegate REQUEST_NOTIFICATION_STATUS PFN_REQUEST_HANDLER(IntPtr pHttpContext, IntPtr pvRequestContext); public delegate REQUEST_NOTIFICATION_STATUS PFN_REQUEST_HANDLER(IntPtr pInProcessHandler, IntPtr pvRequestContext);
public delegate bool PFN_SHUTDOWN_HANDLER(IntPtr pvRequestContext); public delegate bool PFN_SHUTDOWN_HANDLER(IntPtr pvRequestContext);
public delegate REQUEST_NOTIFICATION_STATUS PFN_ASYNC_COMPLETION(IntPtr pvManagedHttpContext, int hr, int bytes); public delegate REQUEST_NOTIFICATION_STATUS PFN_ASYNC_COMPLETION(IntPtr pvManagedHttpContext, int hr, int bytes);
public delegate REQUEST_NOTIFICATION_STATUS PFN_WEBSOCKET_ASYNC_COMPLETION(IntPtr pHttpContext, IntPtr completionInfo, IntPtr pvCompletionContext); public delegate REQUEST_NOTIFICATION_STATUS PFN_WEBSOCKET_ASYNC_COMPLETION(IntPtr pInProcessHandler, IntPtr completionInfo, IntPtr pvCompletionContext);
// TODO make this all internal // TODO make this all internal
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
public static extern int http_post_completion(IntPtr pHttpContext, int cbBytes); public static extern int http_post_completion(IntPtr pInProcessHandler, int cbBytes);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
public static extern int http_set_completion_status(IntPtr pHttpContext, REQUEST_NOTIFICATION_STATUS rquestNotificationStatus); public static extern int http_set_completion_status(IntPtr pInProcessHandler, REQUEST_NOTIFICATION_STATUS rquestNotificationStatus);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
public static extern void http_indicate_completion(IntPtr pHttpContext, REQUEST_NOTIFICATION_STATUS notificationStatus); public static extern void http_indicate_completion(IntPtr pInProcessHandler, REQUEST_NOTIFICATION_STATUS notificationStatus);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
public static extern void register_callbacks(PFN_REQUEST_HANDLER request_callback, PFN_SHUTDOWN_HANDLER shutdown_callback, PFN_ASYNC_COMPLETION managed_context_handler, IntPtr pvRequestContext, IntPtr pvShutdownContext); public static extern void register_callbacks(PFN_REQUEST_HANDLER request_callback, PFN_SHUTDOWN_HANDLER shutdown_callback, PFN_ASYNC_COMPLETION managed_context_handler, IntPtr pvRequestContext, IntPtr pvShutdownContext);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
internal unsafe static extern int http_write_response_bytes(IntPtr pHttpContext, HttpApiTypes.HTTP_DATA_CHUNK* pDataChunks, int nChunks, out bool fCompletionExpected); internal unsafe static extern int http_write_response_bytes(IntPtr pInProcessHandler, HttpApiTypes.HTTP_DATA_CHUNK* pDataChunks, int nChunks, out bool fCompletionExpected);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
public unsafe static extern int http_flush_response_bytes(IntPtr pHttpContext, out bool fCompletionExpected); public unsafe static extern int http_flush_response_bytes(IntPtr pInProcessHandler, out bool fCompletionExpected);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
internal unsafe static extern HttpApiTypes.HTTP_REQUEST_V2* http_get_raw_request(IntPtr pHttpContext); internal unsafe static extern HttpApiTypes.HTTP_REQUEST_V2* http_get_raw_request(IntPtr pInProcessHandler);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
internal unsafe static extern HttpApiTypes.HTTP_RESPONSE_V2* http_get_raw_response(IntPtr pHttpContext); internal unsafe static extern HttpApiTypes.HTTP_RESPONSE_V2* http_get_raw_response(IntPtr pInProcessHandler);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
public unsafe static extern void http_set_response_status_code(IntPtr pHttpContext, ushort statusCode, byte* pszReason); public unsafe static extern void http_set_response_status_code(IntPtr pInProcessHandler, ushort statusCode, byte* pszReason);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
public unsafe static extern int http_read_request_bytes(IntPtr pHttpContext, byte* pvBuffer, int cbBuffer, out int dwBytesReceived, out bool fCompletionExpected); public unsafe static extern int http_read_request_bytes(IntPtr pInProcessHandler, byte* pvBuffer, int cbBuffer, out int dwBytesReceived, out bool fCompletionExpected);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
public unsafe static extern bool http_get_completion_info(IntPtr pCompletionInfo, out int cbBytes, out int hr); public unsafe static extern bool http_get_completion_info(IntPtr pCompletionInfo, out int cbBytes, out int hr);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
public unsafe static extern bool http_set_managed_context(IntPtr pHttpContext, IntPtr pvManagedContext); public unsafe static extern bool http_set_managed_context(IntPtr pInProcessHandler, IntPtr pvManagedContext);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
public unsafe static extern int http_get_application_properties(ref IISConfigurationData iiConfigData); public unsafe static extern int http_get_application_properties(ref IISConfigurationData iiConfigData);
@ -81,10 +81,10 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
public unsafe static extern bool http_shutdown(); public unsafe static extern bool http_shutdown();
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
public unsafe static extern int http_websockets_read_bytes(IntPtr pHttpContext, byte* pvBuffer, int cbBuffer, PFN_WEBSOCKET_ASYNC_COMPLETION pfnCompletionCallback, IntPtr pvCompletionContext, out int dwBytesReceived, out bool fCompletionExpected); public unsafe static extern int http_websockets_read_bytes(IntPtr pInProcessHandler, byte* pvBuffer, int cbBuffer, PFN_WEBSOCKET_ASYNC_COMPLETION pfnCompletionCallback, IntPtr pvCompletionContext, out int dwBytesReceived, out bool fCompletionExpected);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
internal unsafe static extern int http_websockets_write_bytes(IntPtr pHttpContext, HttpApiTypes.HTTP_DATA_CHUNK* pDataChunks, int nChunks, PFN_WEBSOCKET_ASYNC_COMPLETION pfnCompletionCallback, IntPtr pvCompletionContext, out bool fCompletionExpected); internal unsafe static extern int http_websockets_write_bytes(IntPtr pInProcessHandler, HttpApiTypes.HTTP_DATA_CHUNK* pDataChunks, int nChunks, PFN_WEBSOCKET_ASYNC_COMPLETION pfnCompletionCallback, IntPtr pvCompletionContext, out bool fCompletionExpected);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
public unsafe static extern int http_enable_websockets(IntPtr pHttpContext); public unsafe static extern int http_enable_websockets(IntPtr pHttpContext);
@ -93,13 +93,13 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
public unsafe static extern int http_cancel_io(IntPtr pHttpContext); public unsafe static extern int http_cancel_io(IntPtr pHttpContext);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
public unsafe static extern int http_response_set_unknown_header(IntPtr pHttpContext, byte* pszHeaderName, byte* pszHeaderValue, ushort usHeaderValueLength, bool fReplace); public unsafe static extern int http_response_set_unknown_header(IntPtr pInProcessHandler, byte* pszHeaderName, byte* pszHeaderValue, ushort usHeaderValueLength, bool fReplace);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
internal unsafe static extern int http_response_set_known_header(IntPtr pHttpContext, int headerId, byte* pHeaderValue, ushort length, bool fReplace); internal unsafe static extern int http_response_set_known_header(IntPtr pInProcessHandler, int headerId, byte* pHeaderValue, ushort length, bool fReplace);
[DllImport(AspNetCoreModuleDll)] [DllImport(AspNetCoreModuleDll)]
public unsafe static extern int http_get_authentication_information(IntPtr pHttpContext, [MarshalAs(UnmanagedType.BStr)] out string authType, out IntPtr token); public unsafe static extern int http_get_authentication_information(IntPtr pInProcessHandler, [MarshalAs(UnmanagedType.BStr)] out string authType, out IntPtr token);
[DllImport("kernel32.dll")] [DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string lpModuleName); public static extern IntPtr GetModuleHandle(string lpModuleName);

View File

@ -280,7 +280,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
StatusCode = StatusCodes.Status101SwitchingProtocols; StatusCode = StatusCodes.Status101SwitchingProtocols;
ReasonPhrase = ReasonPhrases.GetReasonPhrase(StatusCodes.Status101SwitchingProtocols); ReasonPhrase = ReasonPhrases.GetReasonPhrase(StatusCodes.Status101SwitchingProtocols);
await UpgradeAsync(); await UpgradeAsync();
NativeMethods.http_enable_websockets(_pHttpContext); NativeMethods.http_enable_websockets(_pInProcessHandler);
_wasUpgraded = true; _wasUpgraded = true;
_readWebSocketsOperation = new IISAwaitable(); _readWebSocketsOperation = new IISAwaitable();

View File

@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
private static bool UpgradeAvailable = (Environment.OSVersion.Version >= new Version(6, 2)); private static bool UpgradeAvailable = (Environment.OSVersion.Version >= new Version(6, 2));
protected readonly IntPtr _pHttpContext; protected readonly IntPtr _pInProcessHandler;
private bool _wasUpgraded; private bool _wasUpgraded;
private int _statusCode; private int _statusCode;
@ -63,15 +63,15 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
private const string NegotiateString = "Negotiate"; private const string NegotiateString = "Negotiate";
private const string BasicString = "Basic"; private const string BasicString = "Basic";
internal unsafe IISHttpContext(MemoryPool memoryPool, IntPtr pHttpContext, IISOptions options) internal unsafe IISHttpContext(MemoryPool memoryPool, IntPtr pInProcessHandler, IISOptions options)
: base((HttpApiTypes.HTTP_REQUEST*)NativeMethods.http_get_raw_request(pHttpContext)) : base((HttpApiTypes.HTTP_REQUEST*)NativeMethods.http_get_raw_request(pInProcessHandler))
{ {
_thisHandle = GCHandle.Alloc(this); _thisHandle = GCHandle.Alloc(this);
_memoryPool = memoryPool; _memoryPool = memoryPool;
_pHttpContext = pHttpContext; _pInProcessHandler = pInProcessHandler;
NativeMethods.http_set_managed_context(_pHttpContext, (IntPtr)_thisHandle); NativeMethods.http_set_managed_context(pInProcessHandler, (IntPtr)_thisHandle);
unsafe unsafe
{ {
Method = GetVerb(); Method = GetVerb();
@ -204,7 +204,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
unsafe unsafe
{ {
var hr = 0; var hr = 0;
hr = NativeMethods.http_flush_response_bytes(_pHttpContext, out var fCompletionExpected); hr = NativeMethods.http_flush_response_bytes(_pInProcessHandler, out var fCompletionExpected);
if (!fCompletionExpected) if (!fCompletionExpected)
{ {
_operation.Complete(hr, 0); _operation.Complete(hr, 0);
@ -381,7 +381,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
fixed (byte* pReasonPhrase = reasonPhraseBytes) fixed (byte* pReasonPhrase = reasonPhraseBytes)
{ {
// This copies data into the underlying buffer // This copies data into the underlying buffer
NativeMethods.http_set_response_status_code(_pHttpContext, (ushort)StatusCode, pReasonPhrase); NativeMethods.http_set_response_status_code(_pInProcessHandler, (ushort)StatusCode, pReasonPhrase);
} }
HttpResponseHeaders.IsReadOnly = true; HttpResponseHeaders.IsReadOnly = true;
@ -399,7 +399,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{ {
fixed (byte* pHeaderValue = headerValueBytes) fixed (byte* pHeaderValue = headerValueBytes)
{ {
NativeMethods.http_response_set_unknown_header(_pHttpContext, pHeaderName, pHeaderValue, (ushort)headerValueBytes.Length, fReplace: false); NativeMethods.http_response_set_unknown_header(_pInProcessHandler, pHeaderName, pHeaderValue, (ushort)headerValueBytes.Length, fReplace: false);
} }
} }
} }
@ -411,7 +411,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
var headerValueBytes = Encoding.UTF8.GetBytes(headerValues[i]); var headerValueBytes = Encoding.UTF8.GetBytes(headerValues[i]);
fixed (byte* pHeaderValue = headerValueBytes) fixed (byte* pHeaderValue = headerValueBytes)
{ {
NativeMethods.http_response_set_known_header(_pHttpContext, knownHeaderIndex, pHeaderValue, (ushort)headerValueBytes.Length, fReplace: false); NativeMethods.http_response_set_known_header(_pInProcessHandler, knownHeaderIndex, pHeaderValue, (ushort)headerValueBytes.Length, fReplace: false);
} }
} }
} }
@ -592,11 +592,11 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
chunk.fromMemory.BufferLength = (uint)buffer.Length; chunk.fromMemory.BufferLength = (uint)buffer.Length;
if (_wasUpgraded) if (_wasUpgraded)
{ {
hr = NativeMethods.http_websockets_write_bytes(_pHttpContext, pDataChunks, nChunks, IISAwaitable.WriteCallback, (IntPtr)_thisHandle, out fCompletionExpected); hr = NativeMethods.http_websockets_write_bytes(_pInProcessHandler, pDataChunks, nChunks, IISAwaitable.WriteCallback, (IntPtr)_thisHandle, out fCompletionExpected);
} }
else else
{ {
hr = NativeMethods.http_write_response_bytes(_pHttpContext, pDataChunks, nChunks, out fCompletionExpected); hr = NativeMethods.http_write_response_bytes(_pInProcessHandler, pDataChunks, nChunks, out fCompletionExpected);
} }
} }
} }
@ -625,11 +625,11 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
} }
if (_wasUpgraded) if (_wasUpgraded)
{ {
hr = NativeMethods.http_websockets_write_bytes(_pHttpContext, pDataChunks, nChunks, IISAwaitable.WriteCallback, (IntPtr)_thisHandle, out fCompletionExpected); hr = NativeMethods.http_websockets_write_bytes(_pInProcessHandler, pDataChunks, nChunks, IISAwaitable.WriteCallback, (IntPtr)_thisHandle, out fCompletionExpected);
} }
else else
{ {
hr = NativeMethods.http_write_response_bytes(_pHttpContext, pDataChunks, nChunks, out fCompletionExpected); hr = NativeMethods.http_write_response_bytes(_pInProcessHandler, pDataChunks, nChunks, out fCompletionExpected);
} }
// Free the handles // Free the handles
foreach (var handle in handles) foreach (var handle in handles)
@ -659,7 +659,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
private unsafe IISAwaitable ReadAsync(int length) private unsafe IISAwaitable ReadAsync(int length)
{ {
var hr = NativeMethods.http_read_request_bytes( var hr = NativeMethods.http_read_request_bytes(
_pHttpContext, _pInProcessHandler,
(byte*)_inputHandle.Pointer, (byte*)_inputHandle.Pointer,
length, length,
out var dwReceivedBytes, out var dwReceivedBytes,
@ -677,7 +677,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
int dwReceivedBytes; int dwReceivedBytes;
bool fCompletionExpected; bool fCompletionExpected;
hr = NativeMethods.http_websockets_read_bytes( hr = NativeMethods.http_websockets_read_bytes(
_pHttpContext, _pInProcessHandler,
(byte*)_inputHandle.Pointer, (byte*)_inputHandle.Pointer,
length, length,
IISAwaitable.ReadCallback, IISAwaitable.ReadCallback,
@ -790,13 +790,13 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{ {
Debug.Assert(!_operation.HasContinuation, "Pending async operation!"); Debug.Assert(!_operation.HasContinuation, "Pending async operation!");
var hr = NativeMethods.http_set_completion_status(_pHttpContext, requestNotificationStatus); var hr = NativeMethods.http_set_completion_status(_pInProcessHandler, requestNotificationStatus);
if (hr != NativeMethods.S_OK) if (hr != NativeMethods.S_OK)
{ {
throw Marshal.GetExceptionForHR(hr); throw Marshal.GetExceptionForHR(hr);
} }
hr = NativeMethods.http_post_completion(_pHttpContext, 0); hr = NativeMethods.http_post_completion(_pInProcessHandler, 0);
if (hr != NativeMethods.S_OK) if (hr != NativeMethods.S_OK)
{ {
throw Marshal.GetExceptionForHR(hr); throw Marshal.GetExceptionForHR(hr);
@ -805,7 +805,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
public void IndicateCompletion(NativeMethods.REQUEST_NOTIFICATION_STATUS notificationStatus) public void IndicateCompletion(NativeMethods.REQUEST_NOTIFICATION_STATUS notificationStatus)
{ {
NativeMethods.http_indicate_completion(_pHttpContext, notificationStatus); NativeMethods.http_indicate_completion(_pInProcessHandler, notificationStatus);
} }
internal void OnAsyncCompletion(int hr, int cbBytes) internal void OnAsyncCompletion(int hr, int cbBytes)
@ -873,7 +873,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
private WindowsPrincipal GetWindowsPrincipal() private WindowsPrincipal GetWindowsPrincipal()
{ {
var hr = NativeMethods.http_get_authentication_information(_pHttpContext, out var authenticationType, out var token); var hr = NativeMethods.http_get_authentication_information(_pInProcessHandler, out var authenticationType, out var token);
if (hr == 0 && token != IntPtr.Zero && authenticationType != null) if (hr == 0 && token != IntPtr.Zero && authenticationType != null)
{ {

View File

@ -14,8 +14,8 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{ {
private readonly IHttpApplication<TContext> _application; private readonly IHttpApplication<TContext> _application;
public IISHttpContextOfT(MemoryPool memoryPool, IHttpApplication<TContext> application, IntPtr pHttpContext, IISOptions options) public IISHttpContextOfT(MemoryPool memoryPool, IHttpApplication<TContext> application, IntPtr pInProcessHandler, IISOptions options)
: base(memoryPool, pHttpContext, options) : base(memoryPool, pInProcessHandler, options)
{ {
_application = application; _application = application;
} }

View File

@ -73,12 +73,12 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
_memoryPool.Dispose(); _memoryPool.Dispose();
} }
private static NativeMethods.REQUEST_NOTIFICATION_STATUS HandleRequest(IntPtr pHttpContext, IntPtr pvRequestContext) private static NativeMethods.REQUEST_NOTIFICATION_STATUS HandleRequest(IntPtr pInProcessHandler, IntPtr pvRequestContext)
{ {
// Unwrap the server so we can create an http context and process the request // Unwrap the server so we can create an http context and process the request
var server = (IISHttpServer)GCHandle.FromIntPtr(pvRequestContext).Target; var server = (IISHttpServer)GCHandle.FromIntPtr(pvRequestContext).Target;
var context = server._iisContextFactory.CreateHttpContext(pHttpContext); var context = server._iisContextFactory.CreateHttpContext(pInProcessHandler);
var task = context.ProcessRequestAsync(); var task = context.ProcessRequestAsync();
@ -136,9 +136,9 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
_options = options; _options = options;
} }
public IISHttpContext CreateHttpContext(IntPtr pHttpContext) public IISHttpContext CreateHttpContext(IntPtr pInProcessHandler)
{ {
return new IISHttpContextOfT<T>(_memoryPool, _application, pHttpContext, _options); return new IISHttpContextOfT<T>(_memoryPool, _application, pInProcessHandler, _options);
} }
} }
} }
@ -146,6 +146,6 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
// Over engineering to avoid allocations... // Over engineering to avoid allocations...
internal interface IISContextFactory internal interface IISContextFactory
{ {
IISHttpContext CreateHttpContext(IntPtr pHttpContext); IISHttpContext CreateHttpContext(IntPtr pInProcessHandler);
} }
} }