application.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the MIT License. See License.txt in the project root for license information.
  3. #pragma once
  4. #include <string>
  5. #include "iapplication.h"
  6. #include "ntassert.h"
  7. #include "SRWExclusiveLock.h"
  8. #include "SRWSharedLock.h"
  9. #include "exceptions.h"
  10. #include "HandleWrapper.h"
  11. class APPLICATION : public IAPPLICATION
  12. {
  13. public:
  14. // Non-copyable
  15. APPLICATION(const APPLICATION&) = delete;
  16. const APPLICATION& operator=(const APPLICATION&) = delete;
  17. HRESULT
  18. TryCreateHandler(
  19. _In_ IHttpContext *pHttpContext,
  20. _Outptr_result_maybenull_ IREQUEST_HANDLER **pRequestHandler) override
  21. {
  22. *pRequestHandler = nullptr;
  23. SRWSharedLock stopLock(m_stopLock);
  24. // If we have acquired the stopLock, we don't need to acquire the data lock
  25. // as m_fStoppedCalled is only set by Stop.
  26. if (m_fStopCalled)
  27. {
  28. return S_FALSE;
  29. }
  30. TraceContextScope traceScope(pHttpContext->GetTraceContext());
  31. return CreateHandler(pHttpContext, pRequestHandler);
  32. }
  33. virtual
  34. HRESULT
  35. CreateHandler(
  36. _In_ IHttpContext *pHttpContext,
  37. _Outptr_opt_ IREQUEST_HANDLER **pRequestHandler) = 0;
  38. APPLICATION(const IHttpApplication& pHttpApplication)
  39. : m_fStopCalled(false),
  40. m_cRefs(1),
  41. m_applicationPhysicalPath(pHttpApplication.GetApplicationPhysicalPath()),
  42. m_applicationConfigPath(pHttpApplication.GetAppConfigPath()),
  43. m_applicationId(pHttpApplication.GetApplicationId())
  44. {
  45. InitializeSRWLock(&m_stopLock);
  46. InitializeSRWLock(&m_dataLock);
  47. m_applicationVirtualPath = ToVirtualPath(m_applicationConfigPath);
  48. }
  49. VOID
  50. Stop(bool fServerInitiated) override
  51. {
  52. SRWExclusiveLock stopLock(m_stopLock);
  53. {
  54. SRWExclusiveLock dataLock(m_dataLock);
  55. if (m_fStopCalled)
  56. {
  57. return;
  58. }
  59. m_fStopCalled = true;
  60. }
  61. StopInternal(fServerInitiated);
  62. }
  63. virtual
  64. VOID
  65. StopInternal(bool fServerInitiated)
  66. {
  67. UNREFERENCED_PARAMETER(fServerInitiated);
  68. }
  69. VOID
  70. ReferenceApplication() noexcept override
  71. {
  72. DBG_ASSERT(m_cRefs > 0);
  73. InterlockedIncrement(&m_cRefs);
  74. }
  75. VOID
  76. DereferenceApplication() noexcept override
  77. {
  78. DBG_ASSERT(m_cRefs > 0);
  79. if (InterlockedDecrement(&m_cRefs) == 0)
  80. {
  81. delete this;
  82. }
  83. }
  84. const std::wstring&
  85. QueryApplicationId() const noexcept
  86. {
  87. return m_applicationId;
  88. }
  89. const std::wstring&
  90. QueryApplicationPhysicalPath() const noexcept
  91. {
  92. return m_applicationPhysicalPath;
  93. }
  94. const std::wstring&
  95. QueryApplicationVirtualPath() const noexcept
  96. {
  97. return m_applicationVirtualPath;
  98. }
  99. const std::wstring&
  100. QueryConfigPath() const noexcept
  101. {
  102. return m_applicationConfigPath;
  103. }
  104. protected:
  105. SRWLOCK m_stopLock{};
  106. SRWLOCK m_dataLock {};
  107. bool m_fStopCalled;
  108. private:
  109. mutable LONG m_cRefs;
  110. std::wstring m_applicationPhysicalPath;
  111. std::wstring m_applicationVirtualPath;
  112. std::wstring m_applicationConfigPath;
  113. std::wstring m_applicationId;
  114. static std::wstring ToVirtualPath(const std::wstring& configurationPath)
  115. {
  116. auto segments = 0;
  117. auto position = configurationPath.find('/');
  118. // Skip first 4 segments of config path
  119. while (segments != 3 && position != std::wstring::npos)
  120. {
  121. segments++;
  122. position = configurationPath.find('/', position + 1);
  123. }
  124. if (position != std::wstring::npos)
  125. {
  126. return configurationPath.substr(position);
  127. }
  128. return L"/";
  129. }
  130. };