From ee2e46a614fdc9aab49e2af23962d50898aa50eb Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Tue, 10 Apr 2018 09:08:13 -0700 Subject: [PATCH] What about now? --- ...ft.AspNetCore.Server.IISIntegration.csproj | 4 +- .../Server/IISHttpContext.cs | 73 ++++++++++--------- .../Server/IISHttpContextOfT.cs | 2 + .../Server/IISHttpServer.cs | 16 ++-- 4 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration/Microsoft.AspNetCore.Server.IISIntegration.csproj b/src/Microsoft.AspNetCore.Server.IISIntegration/Microsoft.AspNetCore.Server.IISIntegration.csproj index 3180ce1915..f5efc09f7a 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration/Microsoft.AspNetCore.Server.IISIntegration.csproj +++ b/src/Microsoft.AspNetCore.Server.IISIntegration/Microsoft.AspNetCore.Server.IISIntegration.csproj @@ -1,8 +1,8 @@ - + ASP.NET Core components for working with the IIS AspNetCoreModule. - netstandard2.0;netcoreapp2.1 + netstandard2.0 $(NoWarn);CS1591 true aspnetcore;iis diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContext.cs b/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContext.cs index 1eed1d6c20..fa04d66e6c 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContext.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContext.cs @@ -31,6 +31,8 @@ namespace Microsoft.AspNetCore.Server.IISIntegration protected readonly IntPtr _pInProcessHandler; + private readonly IISOptions _options; + private bool _reading; // To know whether we are currently in a read operation. private volatile bool _hasResponseStarted; @@ -62,13 +64,45 @@ namespace Microsoft.AspNetCore.Server.IISIntegration internal unsafe IISHttpContext(MemoryPool memoryPool, IntPtr pInProcessHandler, IISOptions options, IISHttpServer server) : base((HttpApiTypes.HTTP_REQUEST*)NativeMethods.HttpGetRawRequest(pInProcessHandler)) { - _thisHandle = GCHandle.Alloc(this); - _memoryPool = memoryPool; _pInProcessHandler = pInProcessHandler; + _options = options; _server = server; + } + + public Version HttpVersion { get; set; } + public string Scheme { get; set; } + public string Method { get; set; } + public string PathBase { get; set; } + public string Path { get; set; } + public string QueryString { get; set; } + public string RawTarget { get; set; } + public CancellationToken RequestAborted { get; set; } + public bool HasResponseStarted => _hasResponseStarted; + public IPAddress RemoteIpAddress { get; set; } + public int RemotePort { get; set; } + public IPAddress LocalIpAddress { get; set; } + public int LocalPort { get; set; } + public string RequestConnectionId { get; set; } + public string TraceIdentifier { get; set; } + public ClaimsPrincipal User { get; set; } + internal WindowsPrincipal WindowsUser { get; set; } + public Stream RequestBody { get; set; } + public Stream ResponseBody { get; set; } + public Pipe Input { get; set; } + public OutputProducer Output { get; set; } + + public IHeaderDictionary RequestHeaders { get; set; } + public IHeaderDictionary ResponseHeaders { get; set; } + private HeaderCollection HttpResponseHeaders { get; set; } + internal HttpApiTypes.HTTP_VERB KnownMethod { get; private set; } + + protected void InitializeContext() + { + _thisHandle = GCHandle.Alloc(this); + + NativeMethods.HttpSetManagedContext(_pInProcessHandler, (IntPtr)_thisHandle); - NativeMethods.HttpSetManagedContext(pInProcessHandler, (IntPtr)_thisHandle); Method = GetVerb(); RawTarget = GetRawUrl(); @@ -100,10 +134,10 @@ namespace Microsoft.AspNetCore.Server.IISIntegration HttpResponseHeaders = new HeaderCollection(); ResponseHeaders = HttpResponseHeaders; - if (options.ForwardWindowsAuthentication) + if (_options.ForwardWindowsAuthentication) { WindowsUser = GetWindowsPrincipal(); - if (options.AutomaticAuthentication) + if (_options.AutomaticAuthentication) { User = WindowsUser; } @@ -111,7 +145,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration ResetFeatureCollection(); - if (!_server.IsWebSocketAvailible(pInProcessHandler)) + if (!_server.IsWebSocketAvailible(_pInProcessHandler)) { _currentIHttpUpgradeFeature = null; } @@ -130,33 +164,6 @@ namespace Microsoft.AspNetCore.Server.IISIntegration Output = new OutputProducer(pipe); } - public Version HttpVersion { get; set; } - public string Scheme { get; set; } - public string Method { get; set; } - public string PathBase { get; set; } - public string Path { get; set; } - public string QueryString { get; set; } - public string RawTarget { get; set; } - public CancellationToken RequestAborted { get; set; } - public bool HasResponseStarted => _hasResponseStarted; - public IPAddress RemoteIpAddress { get; set; } - public int RemotePort { get; set; } - public IPAddress LocalIpAddress { get; set; } - public int LocalPort { get; set; } - public string RequestConnectionId { get; set; } - public string TraceIdentifier { get; set; } - public ClaimsPrincipal User { get; set; } - internal WindowsPrincipal WindowsUser { get; set; } - public Stream RequestBody { get; set; } - public Stream ResponseBody { get; set; } - public Pipe Input { get; set; } - public OutputProducer Output { get; set; } - - public IHeaderDictionary RequestHeaders { get; set; } - public IHeaderDictionary ResponseHeaders { get; set; } - private HeaderCollection HttpResponseHeaders { get; set; } - internal HttpApiTypes.HTTP_VERB KnownMethod { get; } - public int StatusCode { get { return _statusCode; } diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContextOfT.cs b/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContextOfT.cs index 312deb4552..0867c3dd06 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContextOfT.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContextOfT.cs @@ -22,6 +22,8 @@ namespace Microsoft.AspNetCore.Server.IISIntegration public override async Task ProcessRequestAsync() { + InitializeContext(); + var context = default(TContext); var success = true; diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpServer.cs b/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpServer.cs index ce32f8d401..49518926ce 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpServer.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpServer.cs @@ -144,21 +144,15 @@ namespace Microsoft.AspNetCore.Server.IISIntegration var server = (IISHttpServer)GCHandle.FromIntPtr(pvRequestContext).Target; Interlocked.Increment(ref server._outstandingRequests); -#if NETCOREAPP2_1 - ThreadPool.QueueUserWorkItem<(IISHttpServer, IntPtr)>( - state => _ = HandleRequest(state.Item1, state.Item2), - (server, pInProcessHandler), - preferLocal: false); -#else - ThreadPool.QueueUserWorkItem( - state => _ = HandleRequest(server, pInProcessHandler)); -#endif + var context = server._iisContextFactory.CreateHttpContext(pInProcessHandler); + + ThreadPool.QueueUserWorkItem(state => _ = HandleRequest((IISHttpContext)state), context); + return NativeMethods.REQUEST_NOTIFICATION_STATUS.RQ_NOTIFICATION_PENDING; } - private static async Task HandleRequest(IISHttpServer server, IntPtr handler) + private static async Task HandleRequest(IISHttpContext context) { - var context = server._iisContextFactory.CreateHttpContext(handler); var result = await context.ProcessRequestAsync(); CompleteRequest(context, result); }