diff --git a/.gitignore b/.gitignore index f86f68b748..e30a491fca 100644 --- a/.gitignore +++ b/.gitignore @@ -48,6 +48,7 @@ x64/ *.pdb *.lib *.idb +*.TMP src/*/AspNetCore/aspnetcoremodule.h src/*/AspNetCore/aspnetcore_msg.h diff --git a/src/AspNetCoreModuleV2/AspNetCore/Inc/applicationinfo.h b/src/AspNetCoreModuleV2/AspNetCore/Inc/applicationinfo.h index f47b51786b..96532217e6 100644 --- a/src/AspNetCoreModuleV2/AspNetCore/Inc/applicationinfo.h +++ b/src/AspNetCoreModuleV2/AspNetCore/Inc/applicationinfo.h @@ -11,17 +11,9 @@ HRESULT (WINAPI * PFN_ASPNETCORE_CREATE_APPLICATION)( _In_ IHttpServer *pServer, _In_ ASPNETCORE_CONFIG *pConfig, - _Out_ APPLICATION **pApplication + _Out_ IAPPLICATION **pApplication ); -typedef -HRESULT -(WINAPI * PFN_ASPNETCORE_CREATE_REQUEST_HANDLER)( - _In_ IHttpContext *pHttpContext, - _In_ HTTP_MODULE_ID *pModuleId, - _In_ APPLICATION *pApplication, - _Out_ REQUEST_HANDLER **pRequestHandler - ); // // The key used for hash-table lookups, consists of the port on which the http process is created. // @@ -71,8 +63,7 @@ public: m_cRefs(1), m_fAppOfflineFound(FALSE), m_pAppOfflineHtm(NULL), m_pFileWatcherEntry(NULL), m_pConfiguration(NULL), - m_pfnAspNetCoreCreateApplication(NULL), - m_pfnAspNetCoreCreateRequestHandler(NULL) + m_pfnAspNetCoreCreateApplication(NULL) { InitializeSRWLock(&m_srwLock); } @@ -136,7 +127,7 @@ public: // Otherwise memory leak // VOID - ExtractApplication(APPLICATION** ppApplication) + ExtractApplication(IAPPLICATION** ppApplication) { AcquireSRWLockShared(&m_srwLock); if (m_pApplication != NULL) @@ -156,12 +147,6 @@ public: HRESULT EnsureApplicationCreated(); - PFN_ASPNETCORE_CREATE_REQUEST_HANDLER - QueryCreateRequestHandler() - { - return m_pfnAspNetCoreCreateRequestHandler; - } - private: HRESULT FindRequestHandlerAssembly(); HRESULT FindNativeAssemblyFromGlobalLocation(STRU* struFilename); @@ -175,11 +160,10 @@ private: APP_OFFLINE_HTM *m_pAppOfflineHtm; FILE_WATCHER_ENTRY *m_pFileWatcherEntry; ASPNETCORE_CONFIG *m_pConfiguration; - APPLICATION *m_pApplication; + IAPPLICATION *m_pApplication; SRWLOCK m_srwLock; IHttpServer *m_pServer; PFN_ASPNETCORE_CREATE_APPLICATION m_pfnAspNetCoreCreateApplication; - PFN_ASPNETCORE_CREATE_REQUEST_HANDLER m_pfnAspNetCoreCreateRequestHandler; }; class APPLICATION_INFO_HASH : diff --git a/src/AspNetCoreModuleV2/AspNetCore/Inc/proxymodule.h b/src/AspNetCoreModuleV2/AspNetCore/Inc/proxymodule.h index 7e5f30a8eb..982dd8c970 100644 --- a/src/AspNetCoreModuleV2/AspNetCore/Inc/proxymodule.h +++ b/src/AspNetCoreModuleV2/AspNetCore/Inc/proxymodule.h @@ -46,8 +46,8 @@ class ASPNET_CORE_PROXY_MODULE : public CHttpModule private: APPLICATION_INFO *m_pApplicationInfo; - APPLICATION *m_pApplication; - REQUEST_HANDLER *m_pHandler; + IAPPLICATION *m_pApplication; + IREQUEST_HANDLER *m_pHandler; }; class ASPNET_CORE_PROXY_MODULE_FACTORY : public IHttpModuleFactory diff --git a/src/AspNetCoreModuleV2/AspNetCore/src/applicationinfo.cpp b/src/AspNetCoreModuleV2/AspNetCore/src/applicationinfo.cpp index 63efb9e889..20d8ef7915 100644 --- a/src/AspNetCoreModuleV2/AspNetCore/src/applicationinfo.cpp +++ b/src/AspNetCoreModuleV2/AspNetCore/src/applicationinfo.cpp @@ -15,7 +15,7 @@ APPLICATION_INFO::~APPLICATION_INFO() { // Mark the entry as invalid, // StopMonitor will close the file handle and trigger a FCN - // the entry will delete itself when processing this FCN + // the entry will delete itself when processing this FCN m_pFileWatcherEntry->MarkEntryInValid(); m_pFileWatcherEntry->StopMonitor(); m_pFileWatcherEntry->DereferenceFileWatcherEntry(); @@ -161,7 +161,7 @@ APPLICATION_INFO::UpdateAppOfflineFileHandle() STACK_STRU(strEventMsg, 256); if (SUCCEEDED(strEventMsg.SafeSnwprintf( ASPNETCORE_EVENT_RECYCLE_APPOFFLINE_MSG, - m_pApplication->QueryConfig()->QueryApplicationPath()->QueryStr()))) + m_pConfiguration->QueryApplicationPath()->QueryStr()))) { UTILITY::LogEvent(g_hEventLog, EVENTLOG_INFORMATION_TYPE, @@ -181,7 +181,7 @@ APPLICATION_INFO::EnsureApplicationCreated() { HRESULT hr = S_OK; BOOL fLocked = FALSE; - APPLICATION* pApplication = NULL; + IAPPLICATION* pApplication = NULL; STACK_STRU(struFileName, 300); // >MAX_PATH STRU struHostFxrDllLocation; @@ -314,24 +314,15 @@ APPLICATION_INFO::FindRequestHandlerAssembly() hr = HRESULT_FROM_WIN32(GetLastError()); goto Finished; } - - g_pfnAspNetCoreCreateRequestHandler = (PFN_ASPNETCORE_CREATE_REQUEST_HANDLER) - GetProcAddress(g_hAspnetCoreRH, "CreateRequestHandler"); - if (g_pfnAspNetCoreCreateRequestHandler == NULL) - { - hr = HRESULT_FROM_WIN32(GetLastError()); - goto Finished; - } g_fAspnetcoreRHAssemblyLoaded = TRUE; } Finished: // // Question: we remember the load failure so that we will not try again. - // User needs to check whether the fuction pointer is NULL + // User needs to check whether the fuction pointer is NULL // m_pfnAspNetCoreCreateApplication = g_pfnAspNetCoreCreateApplication; - m_pfnAspNetCoreCreateRequestHandler = g_pfnAspNetCoreCreateRequestHandler; if (!g_fAspnetcoreRHLoadedError && FAILED(hr)) { g_fAspnetcoreRHLoadedError = TRUE; @@ -401,11 +392,11 @@ Finished: return hr; } -// +// // Tries to find aspnetcorerh.dll from the application // Calls into hostfxr.dll to find it. // Will leave hostfxr.dll loaded as it will be used again to call hostfxr_main. -// +// HRESULT APPLICATION_INFO::FindNativeAssemblyFromHostfxr( STRU* struFilename @@ -440,7 +431,7 @@ APPLICATION_INFO::FindNativeAssemblyFromHostfxr( if (pFnHostFxrSearchDirectories == NULL) { // Host fxr version is incorrect (need a higher version). - // TODO log error + // TODO log error hr = E_FAIL; goto Finished; } @@ -540,7 +531,7 @@ Finished: VOID APPLICATION_INFO::RecycleApplication() { - APPLICATION* pApplication = NULL; + IAPPLICATION* pApplication = NULL; HANDLE hThread = INVALID_HANDLE_VALUE; BOOL fLockAcquired = FALSE; @@ -551,7 +542,7 @@ APPLICATION_INFO::RecycleApplication() if (m_pApplication != NULL) { pApplication = m_pApplication; - if (pApplication->QueryConfig()->QueryHostingModel() == HOSTING_OUT_PROCESS) + if (m_pConfiguration->QueryHostingModel() == HOSTING_OUT_PROCESS) { // // For inprocess, need to set m_pApplication to NULL first to @@ -588,7 +579,7 @@ APPLICATION_INFO::RecycleApplication() g_pHttpServer->RecycleProcess(L"On Demand by AspNetCore Module for recycle application failure"); } } - else + else { // Closing a thread handle does not terminate the associated thread or remove the thread object. CloseHandle(hThread); @@ -606,7 +597,7 @@ VOID APPLICATION_INFO::DoRecycleApplication( LPVOID lpParam) { - APPLICATION* pApplication = static_cast(lpParam); + IAPPLICATION* pApplication = static_cast(lpParam); // No lock required @@ -624,7 +615,7 @@ APPLICATION_INFO::DoRecycleApplication( VOID APPLICATION_INFO::ShutDownApplication() { - APPLICATION* pApplication = NULL; + IAPPLICATION* pApplication = NULL; BOOL fLockAcquired = FALSE; // pApplication can be NULL due to app_offline diff --git a/src/AspNetCoreModuleV2/AspNetCore/src/dllmain.cpp b/src/AspNetCoreModuleV2/AspNetCore/src/dllmain.cpp index 2e4f87e542..56829cb382 100644 --- a/src/AspNetCoreModuleV2/AspNetCore/src/dllmain.cpp +++ b/src/AspNetCoreModuleV2/AspNetCore/src/dllmain.cpp @@ -21,7 +21,6 @@ DWORD g_dwDebugFlags = 0; PCSTR g_szDebugLabel = "ASPNET_CORE_MODULE"; PCWSTR g_pwzAspnetcoreRequestHandlerName = L"aspnetcorerh.dll"; PFN_ASPNETCORE_CREATE_APPLICATION g_pfnAspNetCoreCreateApplication; -PFN_ASPNETCORE_CREATE_REQUEST_HANDLER g_pfnAspNetCoreCreateRequestHandler; VOID StaticCleanup() @@ -49,7 +48,7 @@ BOOL WINAPI DllMain(HMODULE hModule, break; case DLL_PROCESS_DETACH: // IIS can cause dll detach to occur before we receive global notifications - // For example, when we switch the bitness of the worker process, + // For example, when we switch the bitness of the worker process, // this is a bug in IIS. To try to avoid AVs, we will set a global flag g_fInShutdown = TRUE; StaticCleanup(); @@ -204,7 +203,7 @@ HRESULT hr = E_OUTOFMEMORY; goto Finished; } - + hr = pApplicationManager->Initialize(); if(FAILED(hr)) { diff --git a/src/AspNetCoreModuleV2/AspNetCore/src/precomp.hxx b/src/AspNetCoreModuleV2/AspNetCore/src/precomp.hxx index 574244ba35..bd07247484 100644 --- a/src/AspNetCoreModuleV2/AspNetCore/src/precomp.hxx +++ b/src/AspNetCoreModuleV2/AspNetCore/src/precomp.hxx @@ -104,7 +104,7 @@ inline bool IsSpace(char ch) #include "..\..\CommonLib\environmentvariablehash.h" #include "..\..\CommonLib\aspnetcoreconfig.h" #include "..\..\CommonLib\hostfxr_utility.h" -#include "..\..\CommonLib\application.h" +#include "..\..\CommonLib\iapplication.h" #include "..\..\CommonLib\utility.h" #include "..\..\CommonLib\debugutil.h" #include "..\..\CommonLib\requesthandler.h" @@ -138,7 +138,7 @@ FORCEINLINE HRESULT HRESULT_FROM_GETLASTERROR() { - return ( GetLastError() != NO_ERROR ) + return ( GetLastError() != NO_ERROR ) ? HRESULT_FROM_WIN32( GetLastError() ) : E_FAIL; } @@ -155,5 +155,4 @@ extern SRWLOCK g_srwLock; extern PCWSTR g_pwzAspnetcoreRequestHandlerName; extern HANDLE g_hEventLog; extern PFN_ASPNETCORE_CREATE_APPLICATION g_pfnAspNetCoreCreateApplication; -extern PFN_ASPNETCORE_CREATE_REQUEST_HANDLER g_pfnAspNetCoreCreateRequestHandler; #pragma warning( error : 4091) diff --git a/src/AspNetCoreModuleV2/AspNetCore/src/proxymodule.cxx b/src/AspNetCoreModuleV2/AspNetCore/src/proxymodule.cxx index 8ab880339a..0c295fbd0c 100644 --- a/src/AspNetCoreModuleV2/AspNetCore/src/proxymodule.cxx +++ b/src/AspNetCoreModuleV2/AspNetCore/src/proxymodule.cxx @@ -81,7 +81,7 @@ ASPNET_CORE_PROXY_MODULE::OnExecuteRequestHandler( ASPNETCORE_CONFIG *pConfig = NULL; APPLICATION_MANAGER *pApplicationManager = NULL; REQUEST_NOTIFICATION_STATUS retVal = RQ_NOTIFICATION_CONTINUE; - APPLICATION* pApplication = NULL; + IAPPLICATION* pApplication = NULL; STACK_STRU(struFileName, 256); if (g_fInShutdown) { @@ -162,9 +162,8 @@ ASPNET_CORE_PROXY_MODULE::OnExecuteRequestHandler( } // Create RequestHandler and process the request - hr = m_pApplicationInfo->QueryCreateRequestHandler()(pHttpContext, + hr = pApplication->CreateHandler(pHttpContext, (HTTP_MODULE_ID*) &g_pModuleId, - pApplication, &m_pHandler); if (FAILED(hr)) @@ -174,7 +173,7 @@ ASPNET_CORE_PROXY_MODULE::OnExecuteRequestHandler( retVal = m_pHandler->OnExecuteRequestHandler(); -Finished: +Finished: if (FAILED(hr)) { retVal = RQ_NOTIFICATION_FINISH_REQUEST; diff --git a/src/AspNetCoreModuleV2/CommonLib/CommonLib.vcxproj b/src/AspNetCoreModuleV2/CommonLib/CommonLib.vcxproj index 1ab1ef971c..525198f1b6 100644 --- a/src/AspNetCoreModuleV2/CommonLib/CommonLib.vcxproj +++ b/src/AspNetCoreModuleV2/CommonLib/CommonLib.vcxproj @@ -171,12 +171,14 @@ + + @@ -184,11 +186,9 @@ - - Create diff --git a/src/AspNetCoreModuleV2/CommonLib/application.cpp b/src/AspNetCoreModuleV2/CommonLib/application.cpp deleted file mode 100644 index b68c093266..0000000000 --- a/src/AspNetCoreModuleV2/CommonLib/application.cpp +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the MIT License. See License.txt in the project root for license information. - -#include "stdafx.h" - -APPLICATION::APPLICATION( - _In_ IHttpServer* pHttpServer, - _In_ ASPNETCORE_CONFIG* pConfig) : - m_cRefs(1), - m_pConfig(pConfig), - m_status(APPLICATION_STATUS::UNKNOWN) -{ - UNREFERENCED_PARAMETER(pHttpServer); -} - -APPLICATION::~APPLICATION() -{ -} - -APPLICATION_STATUS -APPLICATION::QueryStatus() -{ - return m_status; -} - -ASPNETCORE_CONFIG* -APPLICATION::QueryConfig() -{ - return m_pConfig; -} - -VOID -APPLICATION::ReferenceApplication() -const -{ - InterlockedIncrement(&m_cRefs); -} - -VOID -APPLICATION::DereferenceApplication() -const -{ - DBG_ASSERT(m_cRefs != 0); - - LONG cRefs = 0; - if ((cRefs = InterlockedDecrement(&m_cRefs)) == 0) - { - delete this; - } -} diff --git a/src/AspNetCoreModuleV2/CommonLib/application.h b/src/AspNetCoreModuleV2/CommonLib/application.h index 43c9dafd0c..212fb27f63 100644 --- a/src/AspNetCoreModuleV2/CommonLib/application.h +++ b/src/AspNetCoreModuleV2/CommonLib/application.h @@ -3,51 +3,45 @@ #pragma once -enum APPLICATION_STATUS -{ - UNKNOWN = 0, - STARTING, - RUNNING, - SHUTDOWN, - FAIL -}; +#include "stdafx.h" -class ASPNETCORE_CONFIG; - -class APPLICATION +class APPLICATION : public IAPPLICATION { + public: - APPLICATION( - _In_ IHttpServer* pHttpServer, - _In_ ASPNETCORE_CONFIG* pConfig); - - virtual - VOID - ShutDown() = 0; - - virtual - VOID - Recycle() = 0; - - virtual - ~APPLICATION(); APPLICATION_STATUS - QueryStatus(); + QueryStatus() override + { + return m_status; + } - ASPNETCORE_CONFIG* - QueryConfig(); + APPLICATION() + : m_cRefs(1) + { + } VOID - ReferenceApplication() - const; + ReferenceApplication() override + { + InterlockedIncrement(&m_cRefs); + } VOID - DereferenceApplication() - const; + DereferenceApplication() override + { + DBG_ASSERT(m_cRefs != 0); + + LONG cRefs = 0; + if ((cRefs = InterlockedDecrement(&m_cRefs)) == 0) + { + delete this; + } + } protected: - mutable LONG m_cRefs; - volatile APPLICATION_STATUS m_status; - ASPNETCORE_CONFIG* m_pConfig; + volatile APPLICATION_STATUS m_status = APPLICATION_STATUS::UNKNOWN; + +private: + mutable LONG m_cRefs; }; diff --git a/src/AspNetCoreModuleV2/CommonLib/iapplication.h b/src/AspNetCoreModuleV2/CommonLib/iapplication.h new file mode 100644 index 0000000000..95d726ead7 --- /dev/null +++ b/src/AspNetCoreModuleV2/CommonLib/iapplication.h @@ -0,0 +1,47 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +#pragma once + +enum APPLICATION_STATUS +{ + UNKNOWN = 0, + STARTING, + RUNNING, + SHUTDOWN, + FAIL +}; + +class IAPPLICATION +{ +public: + virtual + VOID + ShutDown() = 0; + + virtual + VOID + Recycle() = 0; + + virtual + ~IAPPLICATION() = 0 { }; + + virtual + APPLICATION_STATUS + QueryStatus() = 0; + + virtual + VOID + ReferenceApplication() = 0; + + virtual + VOID + DereferenceApplication() = 0; + + virtual + HRESULT + CreateHandler( + _In_ IHttpContext *pHttpContext, + _In_ HTTP_MODULE_ID *pModuleId, + _Out_ IREQUEST_HANDLER **pRequestHandler) = 0; +}; diff --git a/src/AspNetCoreModuleV2/CommonLib/irequesthandler.h b/src/AspNetCoreModuleV2/CommonLib/irequesthandler.h new file mode 100644 index 0000000000..fce1913c32 --- /dev/null +++ b/src/AspNetCoreModuleV2/CommonLib/irequesthandler.h @@ -0,0 +1,48 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +#pragma once + +#include "stdafx.h" + +// +// Pure abstract class +// +class IREQUEST_HANDLER +{ +public: + + virtual + REQUEST_NOTIFICATION_STATUS + OnExecuteRequestHandler() = 0; + + virtual + REQUEST_NOTIFICATION_STATUS + OnAsyncCompletion( + DWORD cbCompletion, + HRESULT hrCompletionStatus + ) = 0; + + virtual + VOID + TerminateRequest( + bool fClientInitiated + ) = 0; + + virtual + ~IREQUEST_HANDLER( + VOID + ) = 0 { } + + virtual + VOID + ReferenceRequestHandler( + VOID + ) = 0; + + virtual + VOID + DereferenceRequestHandler( + VOID + ) = 0; +}; diff --git a/src/AspNetCoreModuleV2/CommonLib/requesthandler.cxx b/src/AspNetCoreModuleV2/CommonLib/requesthandler.cxx deleted file mode 100644 index bcf1887d35..0000000000 --- a/src/AspNetCoreModuleV2/CommonLib/requesthandler.cxx +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the MIT License. See License.txt in the project root for license information. - -#include "stdafx.h" - -REQUEST_HANDLER::REQUEST_HANDLER( - _In_ IHttpContext *pW3Context, - _In_ HTTP_MODULE_ID *pModuleId, - _In_ APPLICATION *pApplication) - : m_cRefs(1) -{ - m_pW3Context = pW3Context; - m_pApplication = pApplication; - m_pModuleId = *pModuleId; -} - - -REQUEST_HANDLER::~REQUEST_HANDLER() -{ -} - -VOID -REQUEST_HANDLER::ReferenceRequestHandler( - VOID -) const -{ - InterlockedIncrement(&m_cRefs); -} - - -VOID -REQUEST_HANDLER::DereferenceRequestHandler( - VOID -) const -{ - DBG_ASSERT(m_cRefs != 0); - - LONG cRefs = 0; - if ((cRefs = InterlockedDecrement(&m_cRefs)) == 0) - { - delete this; - } - -} diff --git a/src/AspNetCoreModuleV2/CommonLib/requesthandler.h b/src/AspNetCoreModuleV2/CommonLib/requesthandler.h index 28f4fb725e..ad12b90733 100644 --- a/src/AspNetCoreModuleV2/CommonLib/requesthandler.h +++ b/src/AspNetCoreModuleV2/CommonLib/requesthandler.h @@ -4,56 +4,32 @@ #pragma once #include "stdafx.h" -#include "application.h" // -// Abstract class +// Pure abstract class // -class REQUEST_HANDLER +class REQUEST_HANDLER: public virtual IREQUEST_HANDLER { + public: - REQUEST_HANDLER( - _In_ IHttpContext *pW3Context, - _In_ HTTP_MODULE_ID *pModuleId, - _In_ APPLICATION *pApplication - ); - - virtual - REQUEST_NOTIFICATION_STATUS - OnExecuteRequestHandler() = 0; - - virtual - REQUEST_NOTIFICATION_STATUS - OnAsyncCompletion( - DWORD cbCompletion, - HRESULT hrCompletionStatus - ) = 0; - - virtual VOID - TerminateRequest( - bool fClientInitiated - ) = 0; - - virtual - ~REQUEST_HANDLER( - VOID - ); + ReferenceRequestHandler() override + { + InterlockedIncrement(&m_cRefs); + } VOID - ReferenceRequestHandler( - VOID - ) const; + DereferenceRequestHandler() override + { + DBG_ASSERT(m_cRefs != 0); - virtual - VOID - DereferenceRequestHandler( - VOID - ) const; + LONG cRefs = 0; + if ((cRefs = InterlockedDecrement(&m_cRefs)) == 0) + { + delete this; + } + } -protected: - mutable LONG m_cRefs; - IHttpContext* m_pW3Context; - APPLICATION* m_pApplication; - HTTP_MODULE_ID m_pModuleId; -}; \ No newline at end of file +private: + mutable LONG m_cRefs = 1; +}; diff --git a/src/AspNetCoreModuleV2/CommonLib/stdafx.h b/src/AspNetCoreModuleV2/CommonLib/stdafx.h index 69ad058f1f..30186ed4ad 100644 --- a/src/AspNetCoreModuleV2/CommonLib/stdafx.h +++ b/src/AspNetCoreModuleV2/CommonLib/stdafx.h @@ -25,10 +25,14 @@ #include "environmentvariablehash.h" #include "utility.h" #include "aspnetcoreconfig.h" -#include "application.h" -#include "requesthandler.h" #include "fx_ver.h" #include "hostfxr_utility.h" #include "resources.h" #include "aspnetcore_msg.h" +#include "irequesthandler.h" +#include "iapplication.h" +#include "requesthandler.h" +#include "application.h" + + diff --git a/src/AspNetCoreModuleV2/RequestHandler/Source.def b/src/AspNetCoreModuleV2/RequestHandler/Source.def index 889bd1a39b..86c8c2a268 100644 --- a/src/AspNetCoreModuleV2/RequestHandler/Source.def +++ b/src/AspNetCoreModuleV2/RequestHandler/Source.def @@ -2,5 +2,4 @@ LIBRARY aspnetcorerh EXPORTS CreateApplication - CreateRequestHandler diff --git a/src/AspNetCoreModuleV2/RequestHandler/disconnectcontext.h b/src/AspNetCoreModuleV2/RequestHandler/disconnectcontext.h index e43a49c0a0..e421fe11e3 100644 --- a/src/AspNetCoreModuleV2/RequestHandler/disconnectcontext.h +++ b/src/AspNetCoreModuleV2/RequestHandler/disconnectcontext.h @@ -21,7 +21,7 @@ public: VOID NotifyDisconnect() { - REQUEST_HANDLER *pInitialValue = (REQUEST_HANDLER*) + IREQUEST_HANDLER *pInitialValue = (IREQUEST_HANDLER*) InterlockedExchangePointer((PVOID*)&m_pHandler, NULL); if (pInitialValue != NULL) @@ -33,7 +33,7 @@ public: VOID SetHandler( - REQUEST_HANDLER *pHandler + IREQUEST_HANDLER *pHandler ) { // @@ -61,7 +61,7 @@ public: VOID ) { - REQUEST_HANDLER *pInitialValue = (REQUEST_HANDLER*) + IREQUEST_HANDLER *pInitialValue = (IREQUEST_HANDLER*) InterlockedExchangePointer((PVOID*)&m_pHandler, NULL); if (pInitialValue != NULL) @@ -74,5 +74,5 @@ private: ~ASYNC_DISCONNECT_CONTEXT() {} - REQUEST_HANDLER * m_pHandler; + IREQUEST_HANDLER * m_pHandler; }; \ No newline at end of file diff --git a/src/AspNetCoreModuleV2/RequestHandler/dllmain.cxx b/src/AspNetCoreModuleV2/RequestHandler/dllmain.cxx index 3cfdef0798..5014e3a3ef 100644 --- a/src/AspNetCoreModuleV2/RequestHandler/dllmain.cxx +++ b/src/AspNetCoreModuleV2/RequestHandler/dllmain.cxx @@ -187,10 +187,10 @@ EnsureOutOfProcessInitializtion() } // - // Don't set non-blocking callbacks WINHTTP_OPTION_ASSURED_NON_BLOCKING_CALLBACKS, + // Don't set non-blocking callbacks WINHTTP_OPTION_ASSURED_NON_BLOCKING_CALLBACKS, // as we will call WinHttpQueryDataAvailable to get response on the same thread // that we received callback from Winhttp on completing sending/forwarding the request - // + // // // Setup the callback function @@ -277,11 +277,11 @@ __stdcall CreateApplication( _In_ IHttpServer *pServer, _In_ ASPNETCORE_CONFIG *pConfig, - _Out_ APPLICATION **ppApplication + _Out_ IAPPLICATION **ppApplication ) { HRESULT hr = S_OK; - APPLICATION *pApplication = NULL; + IAPPLICATION *pApplication = NULL; // Initialze some global variables here InitializeGlobalConfiguration(pServer); @@ -303,7 +303,8 @@ CreateApplication( goto Finished; } - pApplication = new OUT_OF_PROCESS_APPLICATION(pServer, pConfig); + + pApplication = new OUT_OF_PROCESS_APPLICATION(pConfig); if (pApplication == NULL) { hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY); @@ -329,41 +330,3 @@ CreateApplication( Finished: return hr; } - -HRESULT -__stdcall -CreateRequestHandler( - _In_ IHttpContext *pHttpContext, - _In_ HTTP_MODULE_ID *pModuleId, - _In_ APPLICATION *pApplication, - _Out_ REQUEST_HANDLER **pRequestHandler -) -{ - HRESULT hr = S_OK; - REQUEST_HANDLER* pHandler = NULL; - ASPNETCORE_CONFIG* pConfig = pApplication->QueryConfig(); - DBG_ASSERT(pConfig); - - if (pConfig->QueryHostingModel() == APP_HOSTING_MODEL::HOSTING_IN_PROCESS) - { - pHandler = new IN_PROCESS_HANDLER(pHttpContext, pModuleId, pApplication); - } - else if (pConfig->QueryHostingModel() == APP_HOSTING_MODEL::HOSTING_OUT_PROCESS) - { - pHandler = new FORWARDING_HANDLER(pHttpContext, pModuleId, pApplication); - } - else - { - return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); - } - - if (pHandler == NULL) - { - hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY); - } - else - { - *pRequestHandler = pHandler; - } - return hr; -} diff --git a/src/AspNetCoreModuleV2/RequestHandler/inprocess/inprocessapplication.cpp b/src/AspNetCoreModuleV2/RequestHandler/inprocess/inprocessapplication.cpp index 59d3d08a84..ca599c9389 100644 --- a/src/AspNetCoreModuleV2/RequestHandler/inprocess/inprocessapplication.cpp +++ b/src/AspNetCoreModuleV2/RequestHandler/inprocess/inprocessapplication.cpp @@ -5,7 +5,6 @@ IN_PROCESS_APPLICATION* IN_PROCESS_APPLICATION::s_Application = NULL; IN_PROCESS_APPLICATION::IN_PROCESS_APPLICATION( IHttpServer* pHttpServer, ASPNETCORE_CONFIG* pConfig) : - APPLICATION(pHttpServer, pConfig), m_pHttpServer(pHttpServer), m_ProcessExitCode(0), m_hLogFileHandle(INVALID_HANDLE_VALUE), @@ -17,7 +16,8 @@ IN_PROCESS_APPLICATION::IN_PROCESS_APPLICATION( m_fInitialized(FALSE), m_fShutdownCalledFromNative(FALSE), m_fShutdownCalledFromManaged(FALSE), - m_srwLock() + m_srwLock(), + m_pConfig(pConfig) { // is it guaranteed that we have already checked app offline at this point? // If so, I don't think there is much to do here. @@ -985,3 +985,28 @@ IN_PROCESS_APPLICATION::FilterException(unsigned int, struct _EXCEPTION_POINTERS // TODO, log error based on exception code. return EXCEPTION_EXECUTE_HANDLER; } + +ASPNETCORE_CONFIG* +IN_PROCESS_APPLICATION::QueryConfig() const +{ + return m_pConfig; +} + +HRESULT +IN_PROCESS_APPLICATION::CreateHandler( + _In_ IHttpContext *pHttpContext, + _In_ HTTP_MODULE_ID *pModuleId, + _Out_ IREQUEST_HANDLER **pRequestHandler) +{ + HRESULT hr = S_OK; + IREQUEST_HANDLER* pHandler = NULL; + pHandler = new IN_PROCESS_HANDLER(pHttpContext, pModuleId, this); + + if (pHandler == NULL) + { + hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY); + } + + *pRequestHandler = pHandler; + return hr; +} diff --git a/src/AspNetCoreModuleV2/RequestHandler/inprocess/inprocessapplication.h b/src/AspNetCoreModuleV2/RequestHandler/inprocess/inprocessapplication.h index ea234382dd..6c27d8e202 100644 --- a/src/AspNetCoreModuleV2/RequestHandler/inprocess/inprocessapplication.h +++ b/src/AspNetCoreModuleV2/RequestHandler/inprocess/inprocessapplication.h @@ -33,6 +33,14 @@ public: VOID ); + __override + HRESULT + CreateHandler( + _In_ IHttpContext *pHttpContext, + _In_ HTTP_MODULE_ID *pModuleId, + _Out_ IREQUEST_HANDLER **pRequestHandler) + override; + // Executes the .NET Core process HRESULT ExecuteApplication( @@ -98,6 +106,9 @@ public: return s_Application; } + ASPNETCORE_CONFIG* + QueryConfig() const; + private: static DWORD @@ -155,6 +166,8 @@ private: DWORD m_dwStdErrReadTotal; static IN_PROCESS_APPLICATION* s_Application; + ASPNETCORE_CONFIG* m_pConfig; + VOID SetStdOut( VOID diff --git a/src/AspNetCoreModuleV2/RequestHandler/inprocess/inprocesshandler.cpp b/src/AspNetCoreModuleV2/RequestHandler/inprocess/inprocesshandler.cpp index c98d84744c..a8bce9b974 100644 --- a/src/AspNetCoreModuleV2/RequestHandler/inprocess/inprocesshandler.cpp +++ b/src/AspNetCoreModuleV2/RequestHandler/inprocess/inprocesshandler.cpp @@ -3,8 +3,10 @@ IN_PROCESS_HANDLER::IN_PROCESS_HANDLER( _In_ IHttpContext *pW3Context, _In_ HTTP_MODULE_ID *pModuleId, - _In_ APPLICATION *pApplication -): REQUEST_HANDLER(pW3Context, pModuleId, pApplication) + _In_ IN_PROCESS_APPLICATION *pApplication +): m_pW3Context(pW3Context), + m_pApplication(pApplication), + m_pModuleId(*pModuleId) { m_fManagedRequestComplete = FALSE; } @@ -20,7 +22,7 @@ IN_PROCESS_HANDLER::OnExecuteRequestHandler() { // First get the in process Application HRESULT hr; - hr = ((IN_PROCESS_APPLICATION*)m_pApplication)->LoadManagedApplication(); + hr = m_pApplication->LoadManagedApplication(); if (FAILED(hr)) { // TODO remove com_error? @@ -39,7 +41,7 @@ IN_PROCESS_HANDLER::OnExecuteRequestHandler() } // FREB log - + if (ANCMEvents::ANCM_START_APPLICATION_SUCCESS::IsEnabled(m_pW3Context->GetTraceContext())) { ANCMEvents::ANCM_START_APPLICATION_SUCCESS::RaiseEvent( @@ -49,7 +51,7 @@ IN_PROCESS_HANDLER::OnExecuteRequestHandler() } //SetHttpSysDisconnectCallback(); - return ((IN_PROCESS_APPLICATION*)m_pApplication)->OnExecuteRequest(m_pW3Context, this); + return m_pApplication->OnExecuteRequest(m_pW3Context, this); } __override @@ -59,15 +61,9 @@ IN_PROCESS_HANDLER::OnAsyncCompletion( HRESULT hrCompletionStatus ) { - IN_PROCESS_APPLICATION* application = (IN_PROCESS_APPLICATION*)m_pApplication; - if (application == NULL) - { - return RQ_NOTIFICATION_FINISH_REQUEST; - } - // OnAsyncCompletion must call into the application if there was a error. We will redo calls // to Read/Write if we called cancelIo on the IHttpContext. - return application->OnAsyncCompletion(cbCompletion, hrCompletionStatus, this); + return m_pApplication->OnAsyncCompletion(cbCompletion, hrCompletionStatus, this); } VOID diff --git a/src/AspNetCoreModuleV2/RequestHandler/inprocess/inprocesshandler.h b/src/AspNetCoreModuleV2/RequestHandler/inprocess/inprocesshandler.h index 1ea1a8dbc5..344bde1bb0 100644 --- a/src/AspNetCoreModuleV2/RequestHandler/inprocess/inprocesshandler.h +++ b/src/AspNetCoreModuleV2/RequestHandler/inprocess/inprocesshandler.h @@ -1,5 +1,7 @@ #pragma once +class IN_PROCESS_APPLICATION; + class IN_PROCESS_HANDLER : public REQUEST_HANDLER { public: @@ -7,27 +9,26 @@ public: _In_ IHttpContext *pW3Context, _In_ HTTP_MODULE_ID *pModuleId, - _In_ APPLICATION *pApplication); + _In_ IN_PROCESS_APPLICATION *pApplication); - ~IN_PROCESS_HANDLER(); + ~IN_PROCESS_HANDLER() override; __override REQUEST_NOTIFICATION_STATUS - OnExecuteRequestHandler(); + OnExecuteRequestHandler() override; __override REQUEST_NOTIFICATION_STATUS OnAsyncCompletion( DWORD cbCompletion, HRESULT hrCompletionStatus - ); + ) override; __override VOID TerminateRequest( bool fClientInitiated - - ); + ) override; PVOID QueryManagedHttpContext( @@ -69,4 +70,8 @@ private: IHttpContext* m_pHttpContext; BOOL m_fManagedRequestComplete; REQUEST_NOTIFICATION_STATUS m_requestNotificationStatus; -}; \ No newline at end of file + + IHttpContext* m_pW3Context; + IN_PROCESS_APPLICATION* m_pApplication; + HTTP_MODULE_ID m_pModuleId; +}; diff --git a/src/AspNetCoreModuleV2/RequestHandler/outofprocess/forwardinghandler.cpp b/src/AspNetCoreModuleV2/RequestHandler/outofprocess/forwardinghandler.cpp index 9b8c9466c8..f15be2a3f3 100644 --- a/src/AspNetCoreModuleV2/RequestHandler/outofprocess/forwardinghandler.cpp +++ b/src/AspNetCoreModuleV2/RequestHandler/outofprocess/forwardinghandler.cpp @@ -18,10 +18,10 @@ PROTOCOL_CONFIG FORWARDING_HANDLER::sm_ProtocolConfig; RESPONSE_HEADER_HASH * FORWARDING_HANDLER::sm_pResponseHeaderHash = NULL; FORWARDING_HANDLER::FORWARDING_HANDLER( - _In_ IHttpContext *pW3Context, - _In_ HTTP_MODULE_ID *pModuleId, - _In_ APPLICATION *pApplication -) : REQUEST_HANDLER(pW3Context, pModuleId, pApplication), + _In_ IHttpContext *pW3Context, + _In_ HTTP_MODULE_ID *pModuleId, + _In_ OUT_OF_PROCESS_APPLICATION *pApplication +) : IREQUEST_HANDLER(), m_Signature(FORWARDING_HANDLER_SIGNATURE), m_RequestStatus(FORWARDER_START), m_fClientDisconnected(FALSE), @@ -39,7 +39,11 @@ FORWARDING_HANDLER::FORWARDING_HANDLER( m_fDoneAsyncCompletion(FALSE), m_fHttpHandleInClose(FALSE), m_fWebSocketHandleInClose(FALSE), - m_fServerResetConn(FALSE) + m_fServerResetConn(FALSE), + m_cRefs(1), + m_pW3Context(pW3Context), + m_pApplication(pApplication), + m_pModuleId(*pModuleId) { #ifdef DEBUG DebugPrintf(ASPNETCORE_DEBUG_FLAG_INFO, @@ -67,9 +71,9 @@ FORWARDING_HANDLER::~FORWARDING_HANDLER( // // Disconnect notification cleanup would happen first, before // the FORWARDING_HANDLER instance got removed from m_pSharedhandler list. - // The m_pServer cleanup would happen afterwards, since there may be a + // The m_pServer cleanup would happen afterwards, since there may be a // call pending from SHARED_HANDLER to FORWARDING_HANDLER::SetStatusAndHeaders() - // + // DBG_ASSERT(m_pDisconnect == NULL); RemoveRequest(); @@ -472,7 +476,7 @@ REQUEST_NOTIFICATION_STATUS if (FAILED(hr)) { - // This failure could happen when client disconnect happens or backend server fails + // This failure could happen when client disconnect happens or backend server fails // after websocket upgrade goto Failure; } @@ -827,8 +831,8 @@ FORWARDING_HANDLER::GetHeaders( PCSTR pszFinalHeader; USHORT cchCurrentHeader; DWORD cchFinalHeader; - BOOL fSecure = FALSE; // dummy. Used in SplitUrl. Value will not be used - // as ANCM always use http protocol to communicate with backend + BOOL fSecure = FALSE; // dummy. Used in SplitUrl. Value will not be used + // as ANCM always use http protocol to communicate with backend STRU struDestination; STRU struUrl; STACK_STRA(strTemp, 64); @@ -858,7 +862,7 @@ FORWARDING_HANDLER::GetHeaders( } // // Strip all headers starting with MS-ASPNETCORE. - // These headers are generated by the asp.net core module and + // These headers are generated by the asp.net core module and // passed to the process it creates. // @@ -1143,7 +1147,7 @@ FORWARDING_HANDLER::CreateWinHttpRequest( { dwTimeout = pProtocol->QueryTimeout(); } - + if (!WinHttpSetTimeouts(m_hRequest, dwTimeout, //resolve timeout dwTimeout, // connect timeout @@ -2007,7 +2011,7 @@ FORWARDING_HANDLER::OnWinHttpCompletionStatusReadComplete( // hr = pResponse->Flush(TRUE, // fAsync TRUE, // fMoreData - NULL); // pcbSent + NULL); // pcbSent if (FAILED(hr)) { goto Finished; diff --git a/src/AspNetCoreModuleV2/RequestHandler/outofprocess/forwardinghandler.h b/src/AspNetCoreModuleV2/RequestHandler/outofprocess/forwardinghandler.h index 427540f2d3..458d07dba4 100644 --- a/src/AspNetCoreModuleV2/RequestHandler/outofprocess/forwardinghandler.h +++ b/src/AspNetCoreModuleV2/RequestHandler/outofprocess/forwardinghandler.h @@ -4,6 +4,7 @@ extern DWORD g_OptionalWinHttpFlags; extern HINSTANCE g_hWinHttpModule; extern HINSTANCE g_hAspNetCoreModule; +class OUT_OF_PROCESS_APPLICATION; enum FORWARDING_REQUEST_STATUS { @@ -23,7 +24,7 @@ public: _In_ IHttpContext *pW3Context, _In_ HTTP_MODULE_ID *pModuleId, - _In_ APPLICATION *pApplication); + _In_ OUT_OF_PROCESS_APPLICATION *pApplication); ~FORWARDING_HANDLER(); @@ -193,7 +194,7 @@ private: volatile BOOL m_fDoneAsyncCompletion; volatile BOOL m_fHasError; // - // WinHttp may hit AV under race if handle got closed more than once simultaneously + // WinHttp may hit AV under race if handle got closed more than once simultaneously // Use two bool variables to guard // volatile BOOL m_fHttpHandleInClose; @@ -230,4 +231,9 @@ private: static TRACE_LOG * sm_pTraceLog; static STRA sm_pStra502ErrorMsg; + + mutable LONG m_cRefs; + IHttpContext* m_pW3Context; + OUT_OF_PROCESS_APPLICATION* m_pApplication; + HTTP_MODULE_ID m_pModuleId; }; diff --git a/src/AspNetCoreModuleV2/RequestHandler/outofprocess/outprocessapplication.cpp b/src/AspNetCoreModuleV2/RequestHandler/outofprocess/outprocessapplication.cpp index e9222ba95e..ba6e3c9b4f 100644 --- a/src/AspNetCoreModuleV2/RequestHandler/outofprocess/outprocessapplication.cpp +++ b/src/AspNetCoreModuleV2/RequestHandler/outofprocess/outprocessapplication.cpp @@ -1,9 +1,8 @@ #include "..\precomp.hxx" OUT_OF_PROCESS_APPLICATION::OUT_OF_PROCESS_APPLICATION( - IHttpServer* pHttpServer, ASPNETCORE_CONFIG* pConfig) : - APPLICATION(pHttpServer, pConfig) + m_pConfig(pConfig) { m_status = APPLICATION_STATUS::RUNNING; m_pProcessManager = NULL; @@ -53,6 +52,12 @@ OUT_OF_PROCESS_APPLICATION::GetProcess( return m_pProcessManager->GetProcess(m_pConfig, ppServerProcess); } +ASPNETCORE_CONFIG* +OUT_OF_PROCESS_APPLICATION::QueryConfig() const +{ + return m_pConfig; +} + __override VOID OUT_OF_PROCESS_APPLICATION::ShutDown() @@ -76,3 +81,21 @@ OUT_OF_PROCESS_APPLICATION::Recycle() ShutDown(); } +HRESULT +OUT_OF_PROCESS_APPLICATION::CreateHandler( + _In_ IHttpContext *pHttpContext, + _In_ HTTP_MODULE_ID *pModuleId, + _Out_ IREQUEST_HANDLER **pRequestHandler) +{ + HRESULT hr = S_OK; + IREQUEST_HANDLER* pHandler = NULL; + pHandler = new FORWARDING_HANDLER(pHttpContext, pModuleId, this); + + if (pHandler == NULL) + { + hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY); + } + + *pRequestHandler = pHandler; + return hr; +} diff --git a/src/AspNetCoreModuleV2/RequestHandler/outofprocess/outprocessapplication.h b/src/AspNetCoreModuleV2/RequestHandler/outofprocess/outprocessapplication.h index f8c30a69cc..cd6f935098 100644 --- a/src/AspNetCoreModuleV2/RequestHandler/outofprocess/outprocessapplication.h +++ b/src/AspNetCoreModuleV2/RequestHandler/outofprocess/outprocessapplication.h @@ -4,9 +4,10 @@ class OUT_OF_PROCESS_APPLICATION : public APPLICATION { public: - OUT_OF_PROCESS_APPLICATION(IHttpServer* pHttpServer, ASPNETCORE_CONFIG *pConfig); + OUT_OF_PROCESS_APPLICATION(ASPNETCORE_CONFIG *pConfig); - ~OUT_OF_PROCESS_APPLICATION(); + __override + ~OUT_OF_PROCESS_APPLICATION() override; HRESULT Initialize(); @@ -18,13 +19,30 @@ public: __override VOID - ShutDown(); + ShutDown() + override; __override VOID - Recycle(); + Recycle() + override; + + __override + HRESULT + CreateHandler( + _In_ IHttpContext *pHttpContext, + _In_ HTTP_MODULE_ID *pModuleId, + _Out_ IREQUEST_HANDLER **pRequestHandler) + override; + + ASPNETCORE_CONFIG* + QueryConfig() + const; private: + PROCESS_MANAGER * m_pProcessManager; SRWLOCK rwlock; + + ASPNETCORE_CONFIG* m_pConfig; }; diff --git a/src/AspNetCoreModuleV2/RequestHandler/precomp.hxx b/src/AspNetCoreModuleV2/RequestHandler/precomp.hxx index 2fa43aac17..64bfd09ae7 100644 --- a/src/AspNetCoreModuleV2/RequestHandler/precomp.hxx +++ b/src/AspNetCoreModuleV2/RequestHandler/precomp.hxx @@ -72,6 +72,7 @@ #include ".\outofprocess\forwardinghandler.h" #include ".\outofprocess\outprocessapplication.h" #include ".\outofprocess\winhttphelper.h" +#include ".\outofprocess\outprocessapplication.h" #ifdef max #undef max diff --git a/test/CommonLibTests/stdafx.h b/test/CommonLibTests/stdafx.h index e4ee515033..feb79f19fe 100644 --- a/test/CommonLibTests/stdafx.h +++ b/test/CommonLibTests/stdafx.h @@ -43,7 +43,7 @@ #include "hostfxr_utility.h" #include "environmentvariablehash.h" #include "aspnetcoreconfig.h" -#include "application.h" +#include "iapplication.h" #include "utility.h" #include "debugutil.h" #include "requesthandler.h"