Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1583 lines
46 KiB

  1. /*===================================================================
  2. Microsoft Denali
  3. Microsoft Confidential.
  4. Copyright 1996-1999 Microsoft Corporation. All Rights Reserved.
  5. Component: Main
  6. File: denali.cpp
  7. Owner: AndyMorr
  8. This file contains the I S A P I C A L L B A C K A P I S
  9. ===================================================================*/
  10. #include "denpre.h"
  11. #pragma hdrstop
  12. #undef DEFAULT_TRACE_FLAGS
  13. #define DEFAULT_TRACE_FLAGS (DEBUG_ERROR)
  14. #include "gip.h"
  15. #include "mtacb.h"
  16. #include "perfdata.h"
  17. #include "activdbg.h"
  18. #include "dlldatax.h"
  19. #include "debugger.h"
  20. #include "dbgutil.h"
  21. #include "randgen.h"
  22. #include "aspdmon.h"
  23. #include "tlbcache.h"
  24. #include "thrdgate.h"
  25. #include "ie449.h"
  26. #include "memcls.h"
  27. #include "memchk.h"
  28. // Globals
  29. BOOL g_fShutDownInProgress = FALSE;
  30. BOOL g_fInitStarted = FALSE;
  31. char g_szExtensionDesc[] = "Microsoft Active Server Pages 2.0";
  32. GLOB gGlob;
  33. BOOL g_fFirstHit = TRUE;
  34. char g_pszASPModuleName[] = "ASP";
  35. DECLARE_DEBUG_PRINTS_OBJECT();
  36. #ifdef _NO_TRACING_
  37. DECLARE_DEBUG_VARIABLE();
  38. #else
  39. #include <initguid.h>
  40. DEFINE_GUID(IisAspGuid,
  41. 0x784d8902, 0xaa8c, 0x11d2, 0x92, 0x5e, 0x00, 0xc0, 0x4f, 0x72, 0xd9, 0x0e);
  42. #endif
  43. DECLARE_PLATFORM_TYPE();
  44. // Out of process flag
  45. BOOL g_fOOP = FALSE;
  46. // session id cookie
  47. char g_szSessionIDCookieName[CCH_SESSION_ID_COOKIE+1];
  48. CRITICAL_SECTION g_csEventlogLock;
  49. CRITICAL_SECTION g_csFirstHitLock;
  50. HINSTANCE g_hODBC32Lib;
  51. // Added to support CacheExtensions
  52. HINSTANCE g_hDenali = (HINSTANCE)0;
  53. HINSTANCE g_hinstDLL = (HINSTANCE)0;
  54. extern LONG g_nSessionObjectsActive;
  55. // Cached BSTRs
  56. BSTR g_bstrApplication = NULL;
  57. BSTR g_bstrRequest = NULL;
  58. BSTR g_bstrResponse = NULL;
  59. BSTR g_bstrServer = NULL;
  60. BSTR g_bstrCertificate = NULL;
  61. BSTR g_bstrSession = NULL;
  62. BSTR g_bstrScriptingNamespace = NULL;
  63. BSTR g_bstrObjectContext = NULL;
  64. // Forward references
  65. HRESULT GlobInit();
  66. HRESULT GlobUnInit();
  67. HRESULT CacheStdTypeInfos();
  68. HRESULT UnCacheStdTypeInfos();
  69. HRESULT InitCachedBSTRs();
  70. HRESULT UnInitCachedBSTRs();
  71. HRESULT ShutDown();
  72. HRESULT SendHtmlSubstitute(CIsapiReqInfo *pIReq);
  73. void MakeAspCookieName(char *);
  74. BOOL FirstHitInit(CIsapiReqInfo *pIReq);
  75. // ATL support
  76. #if _IIS_5_1
  77. CWamModule _Module;
  78. #elif _IIS_6_0
  79. CComModule _Module;
  80. #else
  81. #error "Neither _IIS_6_0 nor _IIS_5_1 is defined"
  82. #endif
  83. BEGIN_OBJECT_MAP(ObjectMap)
  84. END_OBJECT_MAP()
  85. /*===================================================================
  86. DllMain - Moved from clsfctry.cpp
  87. Main entry point into the DLL. Called by system on DLL load
  88. and unload.
  89. Returns:
  90. TRUE on success
  91. Side effects:
  92. None.
  93. ===================================================================*/
  94. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpvReserved)
  95. {
  96. /* Obsolete
  97. // Let the Proxy code get a crack at it
  98. if (!PrxDllMain(hinstDLL, dwReason, lpvReserved))
  99. return FALSE;
  100. */
  101. switch(dwReason)
  102. {
  103. case DLL_PROCESS_ATTACH:
  104. // hang onto the hinstance so we can use it to get to our string resources
  105. //
  106. g_hinstDLL = hinstDLL;
  107. // Here's an interesting optimization:
  108. // The following tells the system NOT to call us for Thread attach/detach
  109. // since we dont handle those calls anyway, this will speed things up a bit.
  110. // If this turns out to be a problem for some reason (cant imagine why),
  111. // just remove this again.
  112. DisableThreadLibraryCalls(hinstDLL);
  113. break;
  114. case DLL_PROCESS_DETACH:
  115. break;
  116. case DLL_THREAD_ATTACH:
  117. break;
  118. case DLL_THREAD_DETACH:
  119. break;
  120. }
  121. return TRUE;
  122. }
  123. /*===================================================================
  124. DWORD HandleHit
  125. Given the CIsapiReqInfo construct a hit object to be queued
  126. for execution
  127. Parameters:
  128. pIReq - CIsapiReqInfo
  129. Returns:
  130. HSE_STATUS_PENDING if function is successful in queuing request
  131. HSE_STATUS_ERROR if not successful
  132. ===================================================================*/
  133. DWORD HandleHit(CIsapiReqInfo *pIReq)
  134. {
  135. int errorId = 0;
  136. BOOL fRejected = FALSE;
  137. BOOL fCompleted = FALSE;
  138. HRESULT hr = S_OK;
  139. /*
  140. * We cant read the metabase until we have the WAM_EXEC_INFO, which
  141. * we dont have at DllInit time. Therefore, we postpone reading the
  142. * metabase until now, but we do it only on the first hit.
  143. */
  144. if (g_fFirstHit)
  145. {
  146. EnterCriticalSection(&g_csFirstHitLock);
  147. // If someone initied while we were waiting for the CS,
  148. // then noop
  149. if (g_fFirstHit)
  150. {
  151. BOOL fT;
  152. fT = FirstHitInit(pIReq);
  153. Assert(fT);
  154. g_fFirstHit = FALSE;
  155. }
  156. LeaveCriticalSection(&g_csFirstHitLock);
  157. }
  158. #ifndef PERF_DISABLE
  159. if (!g_fPerfInited) // Init PERFMON data on first request
  160. {
  161. // FYI: leverage same CS as first hit lock
  162. EnterCriticalSection(&g_csFirstHitLock);
  163. // If someone initied while we were waiting for the CS,
  164. // then noop
  165. if (!g_fPerfInited)
  166. {
  167. if (SUCCEEDED(InitPerfDataOnFirstRequest(pIReq)))
  168. {
  169. g_fPerfInited = TRUE;
  170. }
  171. else
  172. {
  173. g_fPerfInited = FALSE;
  174. // call this again since InitPerfDataOnFirstRequest will uninit
  175. // and erase the criticalsection which will then AV when we try to increment counters
  176. PreInitPerfData();
  177. }
  178. }
  179. LeaveCriticalSection(&g_csFirstHitLock);
  180. }
  181. g_PerfData.Incr_REQTOTAL();
  182. #endif
  183. if (Glob(fNeedUpdate))
  184. {
  185. if (SUCCEEDED(StartISAThreadBracket(pIReq)))
  186. {
  187. // Update uses pIReq - need to bracket
  188. gGlob.Update(pIReq);
  189. EndISAThreadBracket(pIReq);
  190. }
  191. }
  192. if (IsShutDownInProgress())
  193. hr = E_FAIL;
  194. // Enforce the limit of concurrent browser requests
  195. if (SUCCEEDED(hr) && Glob(dwRequestQueueMax) &&
  196. (*g_PerfData.PDWCounter(ID_REQCURRENT) >= Glob(dwRequestQueueMax)))
  197. {
  198. hr = E_FAIL;
  199. fRejected = TRUE;
  200. }
  201. if (SUCCEEDED(hr))
  202. hr = CHitObj::NewBrowserRequest(pIReq, &fRejected, &fCompleted, &errorId);
  203. if (SUCCEEDED(hr))
  204. return fCompleted ? HSE_STATUS_SUCCESS_AND_KEEP_CONN : HSE_STATUS_PENDING;
  205. if (fRejected)
  206. {
  207. if (Glob(fEnableAspHtmlFallBack))
  208. {
  209. // Instead of rejecting the request try to find
  210. // XXX_ASP.HTM file in the same directory and dump its contents
  211. hr = SendHtmlSubstitute(pIReq);
  212. if (hr == S_OK)
  213. return HSE_STATUS_SUCCESS_AND_KEEP_CONN; // HTML substitute sent
  214. else if (FAILED(hr))
  215. return HSE_STATUS_ERROR; // error sending
  216. // HTML substitute not found
  217. }
  218. errorId = IDE_SERVER_TOO_BUSY;
  219. #ifndef PERF_DISABLE
  220. g_PerfData.Incr_REQREJECTED();
  221. #endif
  222. }
  223. if (SUCCEEDED(StartISAThreadBracket(pIReq)))
  224. {
  225. // Uses pIReq -- need to bracket
  226. Handle500Error(errorId, pIReq);
  227. EndISAThreadBracket(pIReq);
  228. }
  229. return HSE_STATUS_ERROR;
  230. }
  231. /*===================================================================
  232. BOOL DllInit
  233. Initialize Denali if not invoked by RegSvr32. Only do inits here
  234. that dont require Glob values loaded from the metabase. For any
  235. inits that require values loaded into Glob from the metabase, use
  236. FirstHitInit.
  237. Returns:
  238. TRUE on successful initialization
  239. ===================================================================*/
  240. BOOL DllInit()
  241. {
  242. HRESULT hr;
  243. const CHAR szASPDebugRegLocation[] =
  244. "System\\CurrentControlSet\\Services\\W3Svc\\ASP";
  245. #if _IIS_5_1
  246. InitializeIISRTL();
  247. DBGPRINTF((DBG_CONTEXT, "ASP Init -- IISRTL\n"));
  248. #endif
  249. #ifndef _NO_TRACING_
  250. CREATE_DEBUG_PRINT_OBJECT( g_pszASPModuleName, IisAspGuid);
  251. #else
  252. CREATE_DEBUG_PRINT_OBJECT( g_pszASPModuleName);
  253. #endif
  254. if ( !VALID_DEBUG_PRINT_OBJECT())
  255. {
  256. return ( FALSE);
  257. }
  258. #ifdef _NO_TRACING_
  259. LOAD_DEBUG_FLAGS_FROM_REG_STR(szASPDebugRegLocation, (DEBUG_ERROR));
  260. #endif
  261. #ifdef SCRIPT_STATS
  262. ReadRegistrySettings();
  263. #endif // SCRIPT_STATS
  264. // Create ASP RefTrace Logs
  265. IF_DEBUG(TEMPLATE) CTemplate::gm_pTraceLog = CreateRefTraceLog(5000, 0);
  266. IF_DEBUG(SESSION) CSession::gm_pTraceLog = CreateRefTraceLog(5000, 0);
  267. IF_DEBUG(APPLICATION) CAppln::gm_pTraceLog = CreateRefTraceLog(5000, 0);
  268. IF_DEBUG(FCN) CASPDirMonitorEntry::gm_pTraceLog = CreateRefTraceLog(500, 0);
  269. CAppln::gm_pTraceLog = CreateRefTraceLog(5000, 0);
  270. if (FAILED(PreInitPerfData()))
  271. return FALSE;
  272. DBGPRINTF((DBG_CONTEXT, "ASP Init -- PerfMon Data PreInit\n"));
  273. ErrInitCriticalSection( &g_csEventlogLock, hr );
  274. if (FAILED(hr))
  275. return FALSE;
  276. ErrInitCriticalSection( &g_csFirstHitLock, hr );
  277. if (FAILED(hr))
  278. return FALSE;
  279. #ifdef DENALI_MEMCHK
  280. if (FAILED(DenaliMemoryInit()))
  281. return FALSE;
  282. #else
  283. if (FAILED(AspMemInit()))
  284. return FALSE;
  285. #endif
  286. DBGPRINTF((DBG_CONTEXT, "ASP Init -- Denali Memory Init\n"));
  287. g_pDirMonitor = new CDirMonitor;
  288. if (g_pDirMonitor == NULL) {
  289. return FALSE;
  290. }
  291. #if _IIS_5_1
  292. if (AtqInitialize(0) == FALSE)
  293. return FALSE;
  294. DBGPRINTF((DBG_CONTEXT, "ASP Init -- ATQ Initialized\n"));
  295. #endif
  296. if (FAILED(GlobInit()))
  297. return FALSE;
  298. DBGPRINTF((DBG_CONTEXT, "ASP Init -- Glob Init\n"));
  299. if (FAILED(InitMemCls()))
  300. return FALSE;
  301. DBGPRINTF((DBG_CONTEXT, "ASP Init -- Per-Class Cache Init\n"));
  302. if (FAILED(InitCachedBSTRs()))
  303. return FALSE;
  304. DBGPRINTF((DBG_CONTEXT, "ASP Init -- Cached BSTRs Init\n"));
  305. if (FAILED(TxnSupportInit()))
  306. return FALSE;
  307. DBGPRINTF((DBG_CONTEXT, "ASP Init -- Txn Support Init\n"));
  308. if (FAILED(CacheStdTypeInfos()))
  309. return FALSE;
  310. DBGPRINTF((DBG_CONTEXT, "ASP Init -- Cache Std TypeInfos\n"));
  311. if (FAILED(g_TypelibCache.Init()))
  312. return FALSE;
  313. DBGPRINTF((DBG_CONTEXT, "ASP Init -- Typelib Cache Init\n"));
  314. if (FAILED(ErrHandleInit()))
  315. return FALSE;
  316. DBGPRINTF((DBG_CONTEXT, "ASP Init -- Err Handler Init\n"));
  317. srand( (unsigned int) time(NULL) );
  318. if (FAILED(g_SessionIdGenerator.Init())) // seed session id
  319. return FALSE;
  320. // Init new Exposed Session Id variable
  321. if (FAILED(g_ExposedSessionIdGenerator.Init(g_SessionIdGenerator))) // seed exposed session id
  322. return FALSE;
  323. DBGPRINTF((DBG_CONTEXT, "ASP Init -- SessionID Generator Init\n"));
  324. MakeAspCookieName(g_szSessionIDCookieName);
  325. if (FAILED(InitRandGenerator()))
  326. return FALSE;
  327. DBGPRINTF((DBG_CONTEXT, "ASP Init -- RandGen Init\n"));
  328. if (FAILED(g_ApplnMgr.Init()))
  329. return FALSE;
  330. DBGPRINTF((DBG_CONTEXT, "ASP Init -- Appln Mgr Init\n"));
  331. if (FAILED(Init449()))
  332. return FALSE;
  333. DBGPRINTF((DBG_CONTEXT, "ASP Init -- 449 Mgr Init\n"));
  334. // Note: Template cache manager is inited in two phases. Do first here.
  335. if (FAILED(g_TemplateCache.Init()))
  336. return FALSE;
  337. DBGPRINTF((DBG_CONTEXT, "ASP Init -- Template Cache Init\n"));
  338. if (FAILED(g_IncFileMap.Init()))
  339. return FALSE;
  340. DBGPRINTF((DBG_CONTEXT, "ASP Init -- Inc File Users Init\n"));
  341. if (FIsWinNT()) // No Change notification on Windows 95
  342. {
  343. if (FAILED(g_FileAppMap.Init()))
  344. return FALSE;
  345. DBGPRINTF((DBG_CONTEXT, "ASP Init -- File-Application Map Init\n"));
  346. }
  347. if (FAILED(g_ScriptManager.Init()))
  348. return FALSE;
  349. DBGPRINTF((DBG_CONTEXT, "ASP Init -- Script Manager Init\n"));
  350. if (FAILED(CTemplate::InitClass()))
  351. return FALSE;
  352. DBGPRINTF((DBG_CONTEXT, "ASP Init -- CTemplate Init Class\n"));
  353. if (FIsWinNT()) // No GIPs on Win95
  354. {
  355. if (FAILED(g_GIPAPI.Init()))
  356. return FALSE;
  357. DBGPRINTF((DBG_CONTEXT, "ASP Init -- Global Interface API Init\n"));
  358. }
  359. if (FAILED(InitMTACallbacks()))
  360. return FALSE;
  361. DBGPRINTF((DBG_CONTEXT, "ASP Init -- MTA Callbacks Init\n"));
  362. if (!RequestSupportInit())
  363. return FALSE;
  364. DBGPRINTF((DBG_CONTEXT, "ASP Init -- Request Support Init\n"));
  365. DBGPRINTF((DBG_CONTEXT, "ASP Init -- Denali DLL Initialized\n"));
  366. #ifdef LOG_FCNOTIFICATIONS
  367. LfcnCreateLogFile();
  368. #endif //LOG_FCNOTIFICATIONS
  369. return TRUE;
  370. }
  371. /*===================================================================
  372. BOOL FirstHitInit
  373. Initialize any ASP values that can not be inited at DllInit time.
  374. Returns:
  375. TRUE on successful initialization
  376. ===================================================================*/
  377. BOOL FirstHitInit
  378. (
  379. CIsapiReqInfo *pIReq
  380. )
  381. {
  382. HRESULT hr;
  383. /*
  384. * In the out of proc case, being able to call the metabase relies on having
  385. * told WAM that we are a "smart" client
  386. */
  387. if (SUCCEEDED(StartISAThreadBracket(pIReq)))
  388. {
  389. // ReadConfigFromMD uses pIReq - need to bracket
  390. hr = ReadConfigFromMD(pIReq, NULL, TRUE);
  391. // Initialize Debugging
  392. if (FIsWinNT() && RevertToSelf()) // No Debugging on Win95
  393. {
  394. // Don't care whether debugging initializaiton succeeds or not. The most likely
  395. // falure is debugger not installed on the machine.
  396. //
  397. if (SUCCEEDED(InitDebuggingAndCreateActivity(pIReq)))
  398. {
  399. DBGPRINTF((DBG_CONTEXT, "FirstHitInit: Debugging Initialized\n"));
  400. }
  401. else
  402. {
  403. DBGPRINTF((DBG_CONTEXT, "FirstHitInit: Debugger Initialization Failed\n"));
  404. }
  405. DBG_REQUIRE( SetThreadToken(NULL, pIReq->QueryImpersonationToken()) );
  406. }
  407. EndISAThreadBracket(pIReq);
  408. }
  409. else
  410. {
  411. hr = E_FAIL;
  412. }
  413. if (FAILED(hr))
  414. goto LExit;
  415. DBGPRINTF((DBG_CONTEXT, "FirstHitInit: Metadata loaded successfully\n"));
  416. // Do FirstHitInit for the Template Cache Manager. Primarily initializes
  417. // the Persisted Template Cache
  418. if (FAILED(hr = g_TemplateCache.FirstHitInit()))
  419. goto LExit;
  420. DBGPRINTF((DBG_CONTEXT, "FirstHitInit: Template Cache Initialized\n"));
  421. // Configure MTS
  422. if (FAILED(hr = ViperConfigure(Glob(dwProcessorThreadMax), Glob(fAllowOutOfProcCmpnts))))
  423. goto LExit;
  424. DBGPRINTF((DBG_CONTEXT, "FirstHitInit: MTS configured\n"));
  425. // Configure Thread Gate
  426. THREADGATE_CONFIG tgc;
  427. tgc.fEnabled = Glob(fThreadGateEnabled);
  428. tgc.msTimeSlice = Glob(dwThreadGateTimeSlice);
  429. tgc.msSleepDelay = Glob(dwThreadGateSleepDelay);
  430. tgc.nSleepMax = Glob(dwThreadGateSleepMax);
  431. tgc.nLoadLow = Glob(dwThreadGateLoadLow);
  432. tgc.nLoadHigh = Glob(dwThreadGateLoadHigh);
  433. tgc.nMinProcessorThreads = 1;
  434. tgc.nMaxProcessorThreads = Glob(dwProcessorThreadMax);
  435. if (FAILED(hr = InitThreadGate(&tgc)))
  436. goto LExit;
  437. DBGPRINTF((DBG_CONTEXT, "Thread Gate Init\n"));
  438. DBGPRINTF((DBG_CONTEXT, "ASP First Hit Initialization complete\n"));
  439. LExit:
  440. Assert(SUCCEEDED(hr));
  441. return SUCCEEDED(hr);
  442. }
  443. /*===================================================================
  444. void DllUnInit
  445. UnInitialize Denali DLL if not invoked by RegSvr32
  446. Returns:
  447. NONE
  448. Side effects:
  449. NONE
  450. ===================================================================*/
  451. void DllUnInit( void )
  452. {
  453. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- %d Apps %d Sessions %d Requests\n",
  454. g_nApplications, g_nSessions, g_nBrowserRequests));
  455. g_fShutDownInProgress = TRUE;
  456. ShutDown();
  457. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- ShutDown Processing\n" ));
  458. UnInitMTACallbacks();
  459. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- MTA Callbacks\n" ));
  460. UnInitRandGenerator();
  461. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- RandGen\n"));
  462. UnInitThreadGate();
  463. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- Thread Gate\n"));
  464. UnInit449();
  465. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- 449 Mgr\n"));
  466. g_ApplnMgr.UnInit();
  467. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- Application Manager\n" ));
  468. g_ScriptManager.UnInit();
  469. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- Script Manager\n" ));
  470. g_TemplateCache.UnInit();
  471. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- Template Cache\n" ));
  472. g_IncFileMap.UnInit();
  473. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- IncFileMap\n" ));
  474. if (FIsWinNT()) {
  475. // No change notification on windows 95
  476. g_FileAppMap.UnInit();
  477. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- File-Application Map\n" ));
  478. if (g_pDirMonitor) {
  479. g_pDirMonitor->Cleanup();
  480. DBGPRINTF((DBG_CONTEXT, "ASP UNInit -- Directory Monitor\n" ));
  481. }
  482. }
  483. CTemplate::UnInitClass();
  484. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- CTemplate\n" ));
  485. g_TypelibCache.UnInit();
  486. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- Typelib Cache\n"));
  487. UnCacheStdTypeInfos();
  488. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- TypeInfos\n" ));
  489. TxnSupportUnInit();
  490. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- Txn Support\n" ));
  491. if (FIsWinNT())
  492. {
  493. // No GIPs on Win95
  494. g_GIPAPI.UnInit();
  495. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- GIP\n" ));
  496. }
  497. ErrHandleUnInit();
  498. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- ErrHandler\n" ));
  499. GlobUnInit();
  500. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- Glob\n" ));
  501. UnInitCachedBSTRs();
  502. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- Cached BSTRs\n" ));
  503. //////////////////////////////////////////////////////////
  504. // Wait for the actual session objects to be destroyed.
  505. // The g_nSessions global tracks the init/uninit of session
  506. // objects but not the memory itself. This presents a
  507. // problem when something outside of ASP holds a reference
  508. // to a session object or one of the contained intrinsics.
  509. // One case of this is the revoking of a git'd transaction
  510. // object. Turns out the revoke can happen asynchronously.
  511. //
  512. // NOTE!!! - This needs to be done BEFORE uniniting the
  513. // mem classes since these objects are in the acache.
  514. LONG lastCount = g_nSessionObjectsActive;
  515. DWORD loopCount = 50;
  516. while( (g_nSessionObjectsActive > 0) && (loopCount--) )
  517. {
  518. if (lastCount != g_nSessionObjectsActive) {
  519. lastCount = g_nSessionObjectsActive;
  520. loopCount = 50;
  521. }
  522. Sleep( 100 );
  523. }
  524. UnInitMemCls();
  525. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- Per-Class Cache\n" ));
  526. #if _IIS_5_1
  527. AtqTerminate();
  528. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- ATQ\n" ));
  529. #endif
  530. // Destroy ASP RefTrace Logs
  531. IF_DEBUG(TEMPLATE) DestroyRefTraceLog(CTemplate::gm_pTraceLog);
  532. IF_DEBUG(SESSION) DestroyRefTraceLog(CSession::gm_pTraceLog);
  533. IF_DEBUG(APPLICATION) DestroyRefTraceLog(CAppln::gm_pTraceLog);
  534. IF_DEBUG(FCN) DestroyRefTraceLog(CASPDirMonitorEntry::gm_pTraceLog);
  535. _Module.Term();
  536. delete g_pDirMonitor;
  537. g_pDirMonitor = NULL;
  538. // UnInitODBC();
  539. // Note: the memmgr uses perf counters, so must be uninited before the perf counters are uninited
  540. #ifdef DENALI_MEMCHK
  541. DenaliMemoryUnInit();
  542. #else
  543. AspMemUnInit();
  544. #endif
  545. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- Memory Manager\n" ));
  546. UnInitPerfData();
  547. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- Perf Counters\n" ));
  548. #if _IIS_5_1
  549. TerminateIISRTL();
  550. DBGPRINTF((DBG_CONTEXT, "ASP UnInit -- IISRTL\n" ));
  551. #endif
  552. DBGPRINTF((DBG_CONTEXT, "ASP Uninitialized\n" ));
  553. #ifdef LOG_FCNOTIFICATIONS
  554. LfcnUnmapLogFile();
  555. #endif //LOG_FCNOTIFICATIONS
  556. // Deleting the following CS's must be last. Dont put anything after this
  557. DeleteCriticalSection( &g_csFirstHitLock );
  558. DeleteCriticalSection( &g_csEventlogLock );
  559. DELETE_DEBUG_PRINT_OBJECT();
  560. }
  561. /*===================================================================
  562. GetExtensionVersion
  563. Mandatory server extension call which returns the version number of
  564. the ISAPI spec that we were built with.
  565. Returns:
  566. TRUE on success
  567. Side effects:
  568. None.
  569. ===================================================================*/
  570. BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pextver)
  571. {
  572. // This DLL can be inited only once
  573. if (g_fShutDownInProgress ||
  574. InterlockedExchange((LPLONG)&g_fInitStarted, TRUE))
  575. {
  576. SetLastError(ERROR_BUSY);
  577. return FALSE;
  578. }
  579. if (!DllInit())
  580. {
  581. SetLastError(ERROR_BUSY);
  582. return FALSE;
  583. }
  584. pextver->dwExtensionVersion =
  585. MAKELONG(HSE_VERSION_MAJOR, HSE_VERSION_MINOR);
  586. strcpy(pextver->lpszExtensionDesc, g_szExtensionDesc);
  587. return TRUE;
  588. }
  589. /*===================================================================
  590. HttpExtensionProc
  591. Main entry point into the DLL for the (ActiveX) Internet Information Server.
  592. Returns:
  593. DWord indicating status of request.
  594. HSE_STATUS_PENDING for normal return
  595. (This indicates that we will process the request, but havent yet.)
  596. Side effects:
  597. None.
  598. ===================================================================*/
  599. DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK *pECB)
  600. {
  601. #ifdef SCRIPT_STATS
  602. InterlockedIncrement(&g_cHttpExtensionsExecuting);
  603. #endif // SCRIPT_STATS
  604. CIsapiReqInfo *pIReq = new CIsapiReqInfo(pECB);
  605. if (pIReq == NULL) {
  606. SetLastError(ERROR_OUTOFMEMORY);
  607. return HSE_STATUS_ERROR;
  608. }
  609. #ifndef PERF_DISABLE
  610. g_PerfData.Add_REQTOTALBYTEIN
  611. (
  612. pIReq->QueryCchQueryString()
  613. #if _IIS_5_1
  614. + pIReq->QueryCchPathTranslated()
  615. #elif _IIS_6_0
  616. + strlen( pIReq->ECB()->lpszPathTranslated )
  617. #endif
  618. + pIReq->QueryCbTotalBytes()
  619. );
  620. #endif
  621. DWORD dw = HandleHit(pIReq);
  622. #ifdef SCRIPT_STATS
  623. InterlockedDecrement(&g_cHttpExtensionsExecuting);
  624. #endif // SCRIPT_STATS
  625. pIReq->Release();
  626. return dw;
  627. }
  628. /*===================================================================
  629. TerminateExtension
  630. IIS is supposed to call this entry point to unload ISAPI DLLs.
  631. Returns:
  632. NONE
  633. Side effects:
  634. Uninitializes the Denali ISAPI DLL if asked to.
  635. ===================================================================*/
  636. BOOL WINAPI TerminateExtension( DWORD dwFlag )
  637. {
  638. if ( dwFlag == HSE_TERM_ADVISORY_UNLOAD )
  639. return TRUE;
  640. if ( dwFlag == HSE_TERM_MUST_UNLOAD )
  641. {
  642. // If already shutdown don't uninit twice.
  643. if (g_fShutDownInProgress)
  644. return TRUE;
  645. // make sure this is a CoInitialize()'d thread
  646. HRESULT hr = CoInitialize(NULL);
  647. if (hr == RPC_E_CHANGED_MODE)
  648. {
  649. // already coinitialized MUTLITREADED - OK
  650. DllUnInit();
  651. }
  652. else if (SUCCEEDED(hr))
  653. {
  654. DllUnInit();
  655. // need to CoUninit() because CoInit() Succeeded
  656. CoUninitialize();
  657. }
  658. return TRUE;
  659. }
  660. return FALSE;
  661. }
  662. /*===================================================================
  663. HRESULT ShutDown
  664. ASP Processing ShutDown logic. (Moved from ThreadManager::UnInit())
  665. Returns:
  666. HRESULT - S_OK on success
  667. Side effects:
  668. May be slow. Kills all requests/sessions/applications
  669. ===================================================================*/
  670. HRESULT ShutDown()
  671. {
  672. long iT;
  673. const DWORD dwtLongWait = 1000; // 1 sec
  674. const DWORD dwtShortWait = 100; // 1/10 sec
  675. DBGPRINTF((DBG_CONTEXT, "ASP Shutdown: %d apps (%d restarting), %d sessions\n",
  676. g_nApplications, g_nApplicationsRestarting, g_nSessions ));
  677. //////////////////////////////////////////////////////////
  678. // Stop change notification on files in template cache
  679. if (FIsWinNT())
  680. {
  681. g_TemplateCache.ShutdownCacheChangeNotification();
  682. }
  683. //////////////////////////////////////////////////////////
  684. // Shut down debugging, which will have the effect of
  685. // resuming scripts stopped at a breakpoint.
  686. //
  687. // (otherwise stopping running scripts will hang later)
  688. if (g_pPDM)
  689. {
  690. g_TemplateCache.RemoveApplicationFromDebuggerUI(NULL); // remove all document nodes
  691. UnInitDebugging(); // kill PDM
  692. DBGPRINTF((DBG_CONTEXT, "ASP Shutdown: PDM Closed\n" ));
  693. }
  694. //////////////////////////////////////////////////////////
  695. // Drain down all pending browser requests
  696. if (g_nBrowserRequests > 0)
  697. {
  698. // Give them a little time each
  699. for (iT = 2*g_nBrowserRequests; g_nBrowserRequests > 0 && iT > 0; iT--)
  700. Sleep(dwtShortWait);
  701. if (g_nBrowserRequests > 0)
  702. {
  703. // Still there - kill scripts and wait again
  704. g_ScriptManager.EmptyRunningScriptList();
  705. for (iT = 2*g_nBrowserRequests; g_nBrowserRequests > 0 && iT > 0; iT--)
  706. Sleep(dwtShortWait);
  707. }
  708. }
  709. DBGPRINTF((DBG_CONTEXT, "ASP Shutdown: Requests drained: %d remaining\n",
  710. g_nBrowserRequests));
  711. //////////////////////////////////////////////////////////
  712. // Kill any remaining engines running scripts
  713. g_ScriptManager.EmptyRunningScriptList();
  714. DBGPRINTF((DBG_CONTEXT, "ASP Shutdown: Scripts killed\n"));
  715. //////////////////////////////////////////////////////////
  716. // Wait till there are no appications restarting
  717. g_ApplnMgr.Lock();
  718. while (g_nApplicationsRestarting > 0)
  719. {
  720. g_ApplnMgr.UnLock();
  721. Sleep(dwtShortWait);
  722. g_ApplnMgr.Lock();
  723. }
  724. g_ApplnMgr.UnLock();
  725. DBGPRINTF((DBG_CONTEXT, "ASP Shutdown: 0 applications restarting\n"));
  726. //////////////////////////////////////////////////////////
  727. // Make this thread's priority higher than that of worker threads
  728. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
  729. //////////////////////////////////////////////////////////
  730. // For each application queue up all its sessions for deletion
  731. CApplnIterator ApplnIterator;
  732. ApplnIterator.Start();
  733. CAppln *pAppln;
  734. while (pAppln = ApplnIterator.Next())
  735. {
  736. // remove link to ATQ scheduler (even if killing of sessions fails)
  737. pAppln->PSessionMgr()->UnScheduleSessionKiller();
  738. for (iT = pAppln->GetNumSessions(); iT > 0; iT--)
  739. {
  740. pAppln->PSessionMgr()->DeleteAllSessions(TRUE);
  741. if (pAppln->GetNumSessions() == 0) // all gone?
  742. break;
  743. Sleep(dwtShortWait);
  744. }
  745. }
  746. ApplnIterator.Stop();
  747. DBGPRINTF((DBG_CONTEXT, "ASP Shutdown: All sessions queued up for deletion. nSessions=%d\n",
  748. g_nSessions));
  749. //////////////////////////////////////////////////////////
  750. // Wait till all sessions are gone (UnInited)
  751. while (g_nSessions > 0)
  752. {
  753. // Wait for a maximum of 0.1 sec x # of sessions
  754. for (iT = g_nSessions; g_nSessions > 0 && iT > 0; iT--)
  755. Sleep(dwtShortWait);
  756. if (g_nSessions > 0)
  757. g_ScriptManager.EmptyRunningScriptList(); // Kill runaway Session_OnEnd scripts
  758. }
  759. DBGPRINTF((DBG_CONTEXT, "ASP Shutdown: Finished waiting for sessions to go away. nSessions=%d\n",
  760. g_nSessions));
  761. //////////////////////////////////////////////////////////
  762. // Queue up all application objects for deletion
  763. g_ApplnMgr.DeleteAllApplications();
  764. DBGPRINTF((DBG_CONTEXT, "ASP Shutdown: All applications queued up for deletion. nApplications=%d\n",
  765. g_nApplications));
  766. //////////////////////////////////////////////////////////
  767. // Wait till all applications are gone (UnInited)
  768. while (g_nApplications > 0)
  769. {
  770. // Wait for a maximum of 1 sec x # of applications
  771. for (iT = g_nApplications; g_nApplications > 0 && iT > 0; iT--)
  772. Sleep(dwtLongWait);
  773. if (g_nApplications > 0)
  774. g_ScriptManager.EmptyRunningScriptList(); // Kill runaway Applications_OnEnd scripts
  775. }
  776. DBGPRINTF((DBG_CONTEXT, "ASP Shutdown: Finished waiting for applications to go away. nApplications=%d\n",
  777. g_nApplications));
  778. /////////////////////////////////////////////////////////
  779. // Wait on the CViperAsyncRequest objects. COM holds the
  780. // final reference to these so we need to let the activity
  781. // threads release any outstanding references before we
  782. // exit.
  783. while( g_nViperRequests > 0 )
  784. {
  785. Sleep( dwtShortWait );
  786. }
  787. //////////////////////////////////////////////////////////
  788. // Free up libraries to force call of DllCanUnloadNow()
  789. // Component writers should put cleanup code in the DllCanUnloadNow() entry point.
  790. CoFreeUnusedLibraries();
  791. //////////////////////////////////////////////////////////
  792. // Kill Debug Activity if any
  793. if (g_pDebugActivity)
  794. delete g_pDebugActivity;
  795. DBGPRINTF((DBG_CONTEXT, "ASP Shutdown: Debug Activity destroyed\n"));
  796. //////////////////////////////////////////////////////////
  797. return S_OK;
  798. }
  799. /*===================================================================
  800. HRESULT GlobInit
  801. Get all interesting global values (mostly from registry)
  802. Returns:
  803. HRESULT - S_OK on success
  804. Side effects:
  805. fills in glob. May be slow
  806. ===================================================================*/
  807. HRESULT GlobInit()
  808. {
  809. //
  810. // BUGBUG - This really needs to be provided either through
  811. // a server support function or via the wamexec
  812. //
  813. char szModule[MAX_PATH+1];
  814. if (GetModuleFileNameA(NULL, szModule, MAX_PATH) > 0)
  815. {
  816. int cch = strlen(szModule);
  817. if (cch > 12 && stricmp(szModule+cch-12, "inetinfo.exe") == 0)
  818. {
  819. g_fOOP = FALSE;
  820. }
  821. else if ( cch > 8 && stricmp( szModule+cch-8, "w3wp.exe" ) == 0 )
  822. {
  823. g_fOOP = FALSE;
  824. }
  825. else
  826. {
  827. g_fOOP = TRUE;
  828. }
  829. }
  830. // Init gGlob
  831. return gGlob.GlobInit();
  832. }
  833. /*===================================================================
  834. GlobUnInit
  835. It is a macro now. see glob.h
  836. Returns:
  837. HRESULT - S_OK on success
  838. Side effects:
  839. memory freed.
  840. ===================================================================*/
  841. HRESULT GlobUnInit()
  842. {
  843. return gGlob.GlobUnInit();
  844. }
  845. /*===================================================================
  846. InitCachedBSTRs
  847. Pre-create frequently used BSTRs
  848. ===================================================================*/
  849. HRESULT InitCachedBSTRs()
  850. {
  851. g_bstrApplication = SysAllocString(WSZ_OBJ_APPLICATION);
  852. g_bstrRequest = SysAllocString(WSZ_OBJ_REQUEST);
  853. g_bstrResponse = SysAllocString(WSZ_OBJ_RESPONSE);
  854. g_bstrServer = SysAllocString(WSZ_OBJ_SERVER);
  855. g_bstrCertificate = SysAllocString(WSZ_OBJ_CERTIFICATE);
  856. g_bstrSession = SysAllocString(WSZ_OBJ_SESSION);
  857. g_bstrScriptingNamespace = SysAllocString(WSZ_OBJ_SCRIPTINGNAMESPACE);
  858. g_bstrObjectContext = SysAllocString(WSZ_OBJ_OBJECTCONTEXT);
  859. return
  860. (
  861. g_bstrApplication &&
  862. g_bstrRequest &&
  863. g_bstrResponse &&
  864. g_bstrServer &&
  865. g_bstrCertificate &&
  866. g_bstrSession &&
  867. g_bstrScriptingNamespace &&
  868. g_bstrObjectContext
  869. )
  870. ? S_OK : E_OUTOFMEMORY;
  871. }
  872. /*===================================================================
  873. UnInitCachedBSTRs
  874. Delete frequently used BSTRs
  875. ===================================================================*/
  876. HRESULT UnInitCachedBSTRs()
  877. {
  878. if (g_bstrApplication)
  879. {
  880. SysFreeString(g_bstrApplication);
  881. g_bstrApplication = NULL;
  882. }
  883. if (g_bstrRequest)
  884. {
  885. SysFreeString(g_bstrRequest);
  886. g_bstrRequest = NULL;
  887. }
  888. if (g_bstrResponse)
  889. {
  890. SysFreeString(g_bstrResponse);
  891. g_bstrResponse = NULL;
  892. }
  893. if (g_bstrServer)
  894. {
  895. SysFreeString(g_bstrServer);
  896. g_bstrServer = NULL;
  897. }
  898. if (g_bstrCertificate)
  899. {
  900. SysFreeString(g_bstrCertificate);
  901. g_bstrCertificate = NULL;
  902. }
  903. if (g_bstrSession)
  904. {
  905. SysFreeString(g_bstrSession);
  906. g_bstrSession = NULL;
  907. }
  908. if (g_bstrScriptingNamespace)
  909. {
  910. SysFreeString(g_bstrScriptingNamespace);
  911. g_bstrScriptingNamespace = NULL;
  912. }
  913. if (g_bstrObjectContext)
  914. {
  915. SysFreeString(g_bstrObjectContext);
  916. g_bstrObjectContext = NULL;
  917. }
  918. return S_OK;
  919. }
  920. // Cached typeinfo's
  921. ITypeInfo *g_ptinfoIDispatch = NULL; // Cache IDispatch typeinfo
  922. ITypeInfo *g_ptinfoIUnknown = NULL; // Cache IUnknown typeinfo
  923. ITypeInfo *g_ptinfoIStringList = NULL; // Cache IStringList typeinfo
  924. ITypeInfo *g_ptinfoIRequestDictionary = NULL; // Cache IRequestDictionary typeinfo
  925. ITypeInfo *g_ptinfoIReadCookie = NULL; // Cache IReadCookie typeinfo
  926. ITypeInfo *g_ptinfoIWriteCookie = NULL; // Cache IWriteCookie typeinfo
  927. /*===================================================================
  928. CacheStdTypeInfos
  929. This is kindofa funny OA-threading bug workaround and perf improvement.
  930. Because we know that they typinfo's for IUnknown and IDispatch are
  931. going to be used like mad, we will load them on startup and keep
  932. them addref'ed. Without this, OA would be loading and unloading
  933. their typeinfos on almost every Invoke.
  934. Also, cache denali's typelib so everyone can get at it, and
  935. cache tye typeinfo's of all our non-top-level intrinsics.
  936. Returns:
  937. HRESULT - S_OK on success
  938. Side effects:
  939. ===================================================================*/
  940. HRESULT CacheStdTypeInfos()
  941. {
  942. HRESULT hr = S_OK;
  943. ITypeLib *pITypeLib = NULL;
  944. CMBCSToWChar convStr;
  945. /*
  946. * Load the typeinfos for IUnk and IDisp
  947. */
  948. hr = LoadRegTypeLib(IID_StdOle,
  949. STDOLE2_MAJORVERNUM,
  950. STDOLE2_MINORVERNUM,
  951. STDOLE2_LCID,
  952. &pITypeLib);
  953. if (hr != S_OK)
  954. {
  955. hr = LoadTypeLibEx(OLESTR("stdole2.tlb"), REGKIND_DEFAULT, &pITypeLib);
  956. }
  957. hr = pITypeLib->GetTypeInfoOfGuid(IID_IDispatch, &g_ptinfoIDispatch);
  958. if (SUCCEEDED(hr))
  959. {
  960. hr = pITypeLib->GetTypeInfoOfGuid(IID_IUnknown, &g_ptinfoIUnknown);
  961. }
  962. pITypeLib->Release();
  963. pITypeLib = NULL;
  964. if (FAILED(hr))
  965. goto LFail;
  966. /*
  967. * Load denali's typelibs. Save them in Glob.
  968. */
  969. /*
  970. * The type libraries are registered under 0 (neutral),
  971. * and 9 (English) with no specific sub-language, which
  972. * would make them 407 or 409 and such.
  973. * If we become sensitive to sub-languages, then use the
  974. * full LCID instead of just the LANGID as done here.
  975. */
  976. char szPath[MAX_PATH];
  977. // Get the path for denali so we can look for the TLB there.
  978. if (!GetModuleFileNameA(g_hinstDLL, szPath, sizeof(szPath)/sizeof(char)))
  979. return E_FAIL;
  980. if (FAILED(hr = convStr.Init(szPath)))
  981. goto LFail;
  982. hr = LoadTypeLibEx(convStr.GetString(), REGKIND_DEFAULT, &pITypeLib);
  983. // Since it's presumably in our DLL, make sure that we loaded it.
  984. Assert (SUCCEEDED(hr));
  985. if (FAILED(hr))
  986. goto LFail;
  987. // Save it in Glob
  988. gGlob.m_pITypeLibDenali = pITypeLib;
  989. /*
  990. * Now cache the typeinfo's of all non-top-level intrinsics
  991. * This is for the OA workaround and for performance.
  992. */
  993. hr = pITypeLib->GetTypeInfoOfGuid(IID_IStringList, &g_ptinfoIStringList);
  994. if (FAILED(hr))
  995. goto LFail;
  996. hr = pITypeLib->GetTypeInfoOfGuid(IID_IRequestDictionary, &g_ptinfoIRequestDictionary);
  997. if (FAILED(hr))
  998. goto LFail;
  999. hr = pITypeLib->GetTypeInfoOfGuid(IID_IReadCookie, &g_ptinfoIReadCookie);
  1000. if (FAILED(hr))
  1001. goto LFail;
  1002. hr = pITypeLib->GetTypeInfoOfGuid(IID_IWriteCookie, &g_ptinfoIWriteCookie);
  1003. if (FAILED(hr))
  1004. goto LFail;
  1005. LFail:
  1006. return(hr);
  1007. }
  1008. /*===================================================================
  1009. UnCacheStdTypeInfos
  1010. Release the typeinfo's we have cached for IUnknown and IDispatch
  1011. and the denali typelib and the other cached stuff.
  1012. Returns:
  1013. HRESULT - S_OK on success
  1014. Side effects:
  1015. ===================================================================*/
  1016. HRESULT UnCacheStdTypeInfos()
  1017. {
  1018. ITypeInfo **ppTypeInfo;
  1019. // Release the typeinfos for IUnk and IDisp
  1020. if (g_ptinfoIDispatch)
  1021. {
  1022. g_ptinfoIDispatch->Release();
  1023. g_ptinfoIDispatch = NULL;
  1024. }
  1025. if (g_ptinfoIUnknown)
  1026. {
  1027. g_ptinfoIUnknown->Release();
  1028. g_ptinfoIDispatch = NULL;
  1029. }
  1030. // Let go of the cached Denali typelibs
  1031. Glob(pITypeLibDenali)->Release();
  1032. // Let go of other cached typeinfos
  1033. g_ptinfoIStringList->Release();
  1034. g_ptinfoIRequestDictionary->Release();
  1035. g_ptinfoIReadCookie->Release();
  1036. g_ptinfoIWriteCookie->Release();
  1037. return(S_OK);
  1038. }
  1039. /*===================================================================
  1040. InitODBC
  1041. Based on the registry Entry value for StartConnectionPool
  1042. we will initialize the ODBC Connection Pool.
  1043. This initialization requires no un-init call
  1044. StartConnectionPool = TRUE - turn on connection pooling
  1045. StartConnectionPool = FALSE
  1046. Returns:
  1047. Void - no return value
  1048. ===================================================================*/
  1049. /*void InitODBC(void)
  1050. {
  1051. if (!Glob(fStartConnectionPool))
  1052. return;
  1053. signed short rc;
  1054. // define the function pointer type
  1055. typedef signed short(CALLBACK *PFNSQLSetEnvAttr)
  1056. (
  1057. DWORD, DWORD, void*, DWORD
  1058. );
  1059. // declare the function pointer
  1060. PFNSQLSetEnvAttr pfnSQLSetEnvAttr;
  1061. g_hODBC32Lib = LoadLibrary("odbc32.dll");
  1062. if(g_hODBC32Lib)
  1063. {
  1064. pfnSQLSetEnvAttr = (PFNSQLSetEnvAttr) GetProcAddress(g_hODBC32Lib, "SQLSetEnvAttr");
  1065. if (pfnSQLSetEnvAttr)
  1066. rc = (*pfnSQLSetEnvAttr)(0UL, 201UL, (void*)2UL, 0UL);
  1067. // rc = SQLSetEnvAttr(SQL_NULL_HENV, SQL_ATTR_CONNECTION_POOLING, (void*)SQL_CP_ONE_PER_HENV,0)
  1068. // I did not want to include all of the SQL h files so I hard-coded the values
  1069. // for the constants
  1070. }
  1071. }
  1072. */
  1073. /*===================================================================
  1074. UnInitODBC
  1075. Based on the registry Entry value for StartConnectionPool
  1076. we will free the odbc32.dll that was loaded with InitODBC
  1077. StartConnectionPool = TRUE - turn on connection pooling
  1078. StartConnectionPool = FALSE
  1079. Returns:
  1080. Void - no return value
  1081. ===================================================================*/
  1082. /*
  1083. void UnInitODBC(void)
  1084. {
  1085. if (!Glob(fStartConnectionPool))
  1086. return;
  1087. if (g_hODBC32Lib)
  1088. FreeLibrary(g_hODBC32Lib);
  1089. }
  1090. */
  1091. /*===================================================================
  1092. MakeAspCookieName
  1093. Creates the asp cookie name as concatination of standard prefix and
  1094. current process id.
  1095. This function is called once per ASP.DLL (oon DllInit())
  1096. Parameters:
  1097. szCookie [out] Put cookie name here
  1098. Returns:
  1099. NOTHING
  1100. ===================================================================*/
  1101. static void MakeAspCookieName(char *szCookie)
  1102. {
  1103. // Standard prefix
  1104. strcpy(szCookie, SZ_SESSION_ID_COOKIE_PREFIX);
  1105. szCookie += CCH_SESSION_ID_COOKIE_PREFIX;
  1106. // Process ID
  1107. sprintf(szCookie, "%08x", GetCurrentProcessId());
  1108. // Uppercase
  1109. strupr(szCookie);
  1110. // Change digits to letters
  1111. static const char *pszDigitsToLetters[2] = {"GHIJKLMNOP","QRSTUVWXYZ"};
  1112. for (int i = 0; i < 8; i++)
  1113. {
  1114. int ch = szCookie[i];
  1115. if (ch >= '0' && ch <= '9')
  1116. szCookie[i] = pszDigitsToLetters[rand() % 2][ch - '0'];
  1117. }
  1118. }
  1119. /*===================================================================
  1120. SendHtmlSubstitute
  1121. Send the html file named XXX_ASP.HTM instead of rejecting the
  1122. request.
  1123. Parameters:
  1124. pIReq CIsapiReqInfo
  1125. Returns:
  1126. HRESULT (S_FALSE = no html substitute found)
  1127. ===================================================================*/
  1128. HRESULT SendHtmlSubstitute(CIsapiReqInfo *pIReq)
  1129. {
  1130. TCHAR *szAspPath = pIReq->QueryPszPathTranslated();
  1131. DWORD cchAspPath = pIReq->QueryCchPathTranslated();
  1132. // verify file name
  1133. if (cchAspPath < 4 || cchAspPath > MAX_PATH ||
  1134. _tcsicmp(szAspPath + cchAspPath - 4, _T(".asp")) != 0)
  1135. {
  1136. return S_FALSE;
  1137. }
  1138. // construct path of the html file
  1139. TCHAR szHtmPath[MAX_PATH+5];
  1140. DWORD cchHtmPath = cchAspPath + 4;
  1141. _tcscpy(szHtmPath, szAspPath);
  1142. szHtmPath[cchAspPath - 4] = _T('_');
  1143. _tcscpy(szHtmPath + cchAspPath, _T(".htm"));
  1144. // check if the html file exists
  1145. if (FAILED(AspGetFileAttributes(szHtmPath)))
  1146. return S_FALSE;
  1147. return CResponse::SyncWriteFile(pIReq, szHtmPath);
  1148. }
  1149. #ifdef LOG_FCNOTIFICATIONS
  1150. // UNDONE get this from registry
  1151. LPSTR g_szNotifyLogFile = "C:\\Temp\\AspNotify.Log";
  1152. HANDLE g_hfileNotifyLog;
  1153. HANDLE g_hmapNotifyLog;
  1154. char* g_pchNotifyLogStart;
  1155. char* g_pchNotifyLogCurrent;
  1156. LPSTR g_szNotifyPrefix = "File change notification: ";
  1157. LPSTR g_szCreateHandlePrefix = "Create handle: ";
  1158. void LfcnCreateLogFile()
  1159. {
  1160. DWORD dwErrCode;
  1161. if(INVALID_HANDLE_VALUE != (g_hfileNotifyLog =
  1162. CreateFile(
  1163. g_szNotifyLogFile, // file name
  1164. GENERIC_READ | GENERIC_WRITE, // access (read-write) mode
  1165. FILE_SHARE_READ, // share mode
  1166. NULL, // pointer to security descriptor
  1167. CREATE_ALWAYS, // how to create
  1168. FILE_ATTRIBUTE_NORMAL, // file attributes
  1169. NULL // handle to file with attributes to copy
  1170. )))
  1171. {
  1172. BYTE rgb[0x10000];
  1173. DWORD cb = sizeof( rgb );
  1174. DWORD cbWritten = 0;
  1175. // FillMemory( rgb, cb, 0xAB );
  1176. WriteFile(
  1177. g_hfileNotifyLog, // handle to file to write to
  1178. rgb, // pointer to data to write to file
  1179. cb, // number of bytes to write
  1180. &cbWritten, // pointer to number of bytes written
  1181. NULL // pointer to structure needed for overlapped I/O
  1182. );
  1183. if(NULL != (g_hmapNotifyLog =
  1184. CreateFileMapping(
  1185. g_hfileNotifyLog, // handle to file to map
  1186. NULL, // optional security attributes
  1187. PAGE_READWRITE, // protection for mapping object
  1188. 0, // high-order 32 bits of object size
  1189. 100, // low-order 32 bits of object size
  1190. NULL // name of file-mapping object
  1191. )))
  1192. {
  1193. if(NULL != (g_pchNotifyLogStart =
  1194. (char*) MapViewOfFile(
  1195. g_hmapNotifyLog, // file-mapping object to map into address space
  1196. FILE_MAP_WRITE, // access mode
  1197. 0, // high-order 32 bits of file offset
  1198. 0, // low-order 32 bits of file offset
  1199. 0 // number of bytes to map
  1200. )))
  1201. {
  1202. *g_pchNotifyLogStart = '\0';
  1203. g_pchNotifyLogCurrent = g_pchNotifyLogStart;
  1204. LfcnAppendLog( "ASP change-notifications log file \r\n" );
  1205. LfcnAppendLog( "================================= \r\n" );
  1206. DBGPRINTF((DBG_CONTEXT, "Notifications log file created and mapped.\r\n" ));
  1207. return;
  1208. }
  1209. }
  1210. }
  1211. dwErrCode = GetLastError();
  1212. DBGERROR((DBG_CONTEXT, "Failed to create notifications log file; last error was %d\r\n", szErrCode));
  1213. }
  1214. void LfcnCopyAdvance(char** ppchDest, const char* sz)
  1215. {
  1216. // UNDONE make this robust (WriteFile to extend file?)
  1217. strcpy( *ppchDest, sz );
  1218. *ppchDest += strlen( sz );
  1219. }
  1220. void LfcnAppendLog(const char* sz)
  1221. {
  1222. LfcnCopyAdvance( &g_pchNotifyLogCurrent, sz );
  1223. DBGPRINTF((DBG_CONTEXT, "%s", sz));
  1224. }
  1225. void LfcnLogNotification(char* szFile)
  1226. {
  1227. LfcnAppendLog( g_szNotifyPrefix );
  1228. LfcnAppendLog( szFile );
  1229. LfcnAppendLog( "\r\n" );
  1230. }
  1231. void LfcnLogHandleCreation(int i, char* szApp)
  1232. {
  1233. char szIndex[5];
  1234. _itoa( i, szIndex, 10);
  1235. LfcnAppendLog( g_szCreateHandlePrefix );
  1236. LfcnAppendLog( szIndex );
  1237. LfcnAppendLog( "\t" );
  1238. LfcnAppendLog( szApp );
  1239. LfcnAppendLog( "\r\n" );
  1240. }
  1241. void LfcnUnmapLogFile()
  1242. {
  1243. if(g_pchNotifyLogStart != NULL)
  1244. UnmapViewOfFile(g_pchNotifyLogStart);
  1245. if(g_hmapNotifyLog!= NULL)
  1246. CloseHandle(g_hmapNotifyLog);
  1247. if(g_hfileNotifyLog != NULL && g_hfileNotifyLog != INVALID_HANDLE_VALUE)
  1248. CloseHandle( g_hfileNotifyLog );
  1249. g_pchNotifyLogStart = NULL;
  1250. g_hmapNotifyLog = NULL;
  1251. g_hfileNotifyLog = NULL;
  1252. }
  1253. #endif //LOG_FCNOTIFICATIONS
  1254. #if _IIS_5_1
  1255. LONG CWamModule::Lock()
  1256. {
  1257. return CComModule::Lock();
  1258. }
  1259. LONG CWamModule::Unlock()
  1260. {
  1261. return CComModule::Unlock();
  1262. }
  1263. #endif