HttpApplicationFactory.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //
  2. // System.Web.HttpApplicationFactory
  3. //
  4. // Author:
  5. // Patrik Torstensson ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (c) 2002,2003 Ximian, Inc. (http://www.ximian.com)
  9. //
  10. using System;
  11. using System.Collections;
  12. using System.IO;
  13. using System.Reflection;
  14. using System.Web;
  15. using System.Web.Compilation;
  16. using System.Web.SessionState;
  17. namespace System.Web {
  18. class HttpApplicationFactory {
  19. private string _appFilename;
  20. private Type _appType;
  21. private bool _appInitialized;
  22. private bool _appFiredEnd;
  23. private Stack _appFreePublicList;
  24. private int _appFreePublicInstances;
  25. static private int _appMaxFreePublicInstances = 32;
  26. private HttpApplicationState _state;
  27. static Hashtable appEventNames;
  28. static IHttpHandler custApplication;
  29. static private HttpApplicationFactory s_Factory = new HttpApplicationFactory();
  30. static HttpApplicationFactory ()
  31. {
  32. appEventNames = new Hashtable ();
  33. appEventNames.Add ("Application_BeginRequest", null);
  34. appEventNames.Add ("Application_AuthenticateRequest", null);
  35. appEventNames.Add ("Application_AuthorizeRequest", null);
  36. appEventNames.Add ("Application_ResolveRequestCache", null);
  37. appEventNames.Add ("Application_AcquireRequestState", null);
  38. appEventNames.Add ("Application_PreRequestHandlerExecute", null);
  39. appEventNames.Add ("Application_PostRequestHandlerExecute", null);
  40. appEventNames.Add ("Application_ReleaseRequestState", null);
  41. appEventNames.Add ("Application_UpdateRequestCache", null);
  42. appEventNames.Add ("Application_EndRequest", null);
  43. appEventNames.Add ("Application_PreSendRequestHeaders", null);
  44. appEventNames.Add ("Application_PreSendRequestContent", null);
  45. appEventNames.Add ("Application_Disposed", null);
  46. appEventNames.Add ("Application_Error", null);
  47. }
  48. public HttpApplicationFactory() {
  49. _appInitialized = false;
  50. _appFiredEnd = false;
  51. _appFreePublicList = new Stack();
  52. _appFreePublicInstances = 0;
  53. }
  54. static private string GetAppFilename (HttpContext context)
  55. {
  56. string physicalAppPath = context.Request.PhysicalApplicationPath;
  57. string appFilePath = Path.Combine (physicalAppPath, "Global.asax");
  58. if (File.Exists (appFilePath))
  59. return appFilePath;
  60. return Path.Combine (physicalAppPath, "global.asax");
  61. }
  62. private void CompileApp(HttpContext context) {
  63. if (File.Exists(_appFilename)) {
  64. // Setup filemonitor for all filedepend also. CacheDependency?
  65. _appType = GlobalAsaxCompiler.CompileApplicationType (_appFilename, context);
  66. if (_appType == null) {
  67. string msg = String.Format ("Error compiling application file ({0}).", _appFilename);
  68. throw new ApplicationException (msg);
  69. }
  70. } else {
  71. _appType = typeof (System.Web.HttpApplication);
  72. _state = new HttpApplicationState ();
  73. }
  74. }
  75. static bool IsEventHandler (MethodInfo m)
  76. {
  77. if (m.ReturnType != typeof (void))
  78. return false;
  79. ParameterInfo [] pi = m.GetParameters ();
  80. if (pi.Length != 2)
  81. return false;
  82. if (pi [0].ParameterType != typeof (object) ||
  83. pi [1].ParameterType != typeof (EventArgs))
  84. return false;
  85. return appEventNames.ContainsKey (m.Name);
  86. }
  87. static void AddEvent (MethodInfo method, Hashtable appTypeEventHandlers)
  88. {
  89. string name = method.Name;
  90. ArrayList list;
  91. list = appTypeEventHandlers [name] as ArrayList;
  92. if (list == null) {
  93. list = new ArrayList ();
  94. appTypeEventHandlers [name] = list;
  95. }
  96. list.Add (method);
  97. }
  98. static Hashtable GetApplicationTypeEvents (HttpApplication app)
  99. {
  100. Type appType = app.GetType ();
  101. Hashtable appTypeEventHandlers = new Hashtable ();
  102. ArrayList evtMethods = new ArrayList ();
  103. BindingFlags flags = BindingFlags.Public |
  104. BindingFlags.NonPublic |
  105. BindingFlags.DeclaredOnly |
  106. BindingFlags.Instance |
  107. BindingFlags.Static;
  108. MethodInfo [] methods = appType.GetMethods (flags);
  109. foreach (MethodInfo m in methods) {
  110. if (IsEventHandler (m))
  111. AddEvent (m, appTypeEventHandlers);
  112. }
  113. Type baseType = appType.BaseType;
  114. if (baseType == typeof (HttpApplication))
  115. return appTypeEventHandlers;
  116. flags = BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
  117. methods = appType.GetMethods (flags);
  118. foreach (MethodInfo m in methods) {
  119. if (IsEventHandler (m))
  120. AddEvent (m, appTypeEventHandlers);
  121. }
  122. return appTypeEventHandlers;
  123. }
  124. void FireEvents (string method_name, object state_context, object [] args)
  125. {
  126. /*
  127. if (methods == null || methods.Count == 0)
  128. return;
  129. foreach (MethodInfo method in methods)
  130. method.Invoke (this, args);
  131. */
  132. }
  133. void FireOnAppStart (HttpContext context)
  134. {
  135. FireEvents ("Application_OnStart", context, new object [] {this, EventArgs.Empty});
  136. }
  137. void FireOnAppEnd ()
  138. {
  139. FireEvents ("Application_OnEnd", null, new object [] {this, EventArgs.Empty});
  140. }
  141. void FireOnSessionStart (HttpSessionState state, object source, EventArgs args)
  142. {
  143. FireEvents ("Session_OnStart", state, new object [] {source, args});
  144. }
  145. void FireOnSessionEnd (HttpSessionState state, object source, EventArgs args)
  146. {
  147. FireEvents ("Session_OnEnd", state, new object [] {source, args});
  148. }
  149. private void InitializeFactory(HttpContext context) {
  150. // TODO: Should we be impersonating here? We are reading a app file.. security issue?
  151. _appFilename = GetAppFilename(context);
  152. CompileApp(context);
  153. FireOnAppStart(context);
  154. }
  155. private void Dispose() {
  156. ArrayList torelease = new ArrayList();
  157. lock (_appFreePublicList) {
  158. while (_appFreePublicList.Count > 0) {
  159. torelease.Add(_appFreePublicList.Pop());
  160. _appFreePublicInstances--;
  161. }
  162. }
  163. if (torelease.Count > 0) {
  164. foreach (Object obj in torelease) {
  165. ((HttpApplication) obj).Cleanup();
  166. }
  167. }
  168. if (!_appFiredEnd) {
  169. lock (this) {
  170. if (!_appFiredEnd) {
  171. FireOnAppEnd();
  172. _appFiredEnd = true;
  173. }
  174. }
  175. }
  176. }
  177. internal static IHttpHandler GetInstance(HttpContext context)
  178. {
  179. if (custApplication != null)
  180. return custApplication;
  181. if (!s_Factory._appInitialized) {
  182. lock (s_Factory) {
  183. if (!s_Factory._appInitialized) {
  184. s_Factory.InitializeFactory(context);
  185. s_Factory._appInitialized = true;
  186. }
  187. }
  188. }
  189. return s_Factory.GetPublicInstance(context);
  190. }
  191. internal static void RecycleInstance(HttpApplication app) {
  192. if (!s_Factory._appInitialized)
  193. throw new InvalidOperationException("Factory not intialized");
  194. s_Factory.RecyclePublicInstance(app);
  195. }
  196. internal static void AttachEvents (HttpApplication app)
  197. {
  198. Type appType = app.GetType ();
  199. Hashtable appTypeEventHandlers = GetApplicationTypeEvents (app);
  200. foreach (string key in appTypeEventHandlers.Keys) {
  201. if (key == "Application_OnStart" || key == "Application_OnEnd" ||
  202. key == "Session_OnStart" || key == "Session_OnEnd")
  203. continue;
  204. int pos = key.IndexOf ('_');
  205. if (pos == -1 || key.Length <= pos + 1)
  206. continue;
  207. EventInfo evt = appType.GetEvent (key.Substring (pos + 1));
  208. if (evt == null)
  209. continue;
  210. ArrayList list = appTypeEventHandlers [key] as ArrayList;
  211. if (list == null || list.Count == 0)
  212. continue;
  213. foreach (MethodInfo method in list)
  214. evt.AddEventHandler (app, Delegate.CreateDelegate (typeof (EventHandler), method));
  215. }
  216. }
  217. private IHttpHandler GetPublicInstance(HttpContext context) {
  218. HttpApplication app = null;
  219. lock (_appFreePublicList) {
  220. if (_appFreePublicInstances > 0) {
  221. app = (HttpApplication) _appFreePublicList.Pop();
  222. _appFreePublicInstances--;
  223. }
  224. }
  225. if (app == null) {
  226. // Create non-public object
  227. app = (HttpApplication) HttpRuntime.CreateInternalObject(_appType);
  228. app.Startup(context, HttpApplicationFactory.ApplicationState);
  229. }
  230. return (IHttpHandler) app;
  231. }
  232. internal void RecyclePublicInstance(HttpApplication app) {
  233. lock (_appFreePublicList) {
  234. if (_appFreePublicInstances < _appMaxFreePublicInstances) {
  235. _appFreePublicList.Push(app);
  236. _appFreePublicInstances++;
  237. app = null;
  238. }
  239. }
  240. if (app != null) {
  241. app.Cleanup();
  242. }
  243. }
  244. static internal HttpApplicationState ApplicationState {
  245. get {
  246. if (null == s_Factory._state) {
  247. s_Factory._state = new HttpApplicationState();
  248. }
  249. return s_Factory._state;
  250. }
  251. }
  252. internal static void EndApplication() {
  253. s_Factory.Dispose();
  254. }
  255. internal static void StartSession(HttpSessionState state, object source, EventArgs args) {
  256. s_Factory.FireOnSessionStart(state, source, args);
  257. }
  258. static void EndSession(HttpSessionState state, object source, EventArgs args) {
  259. s_Factory.FireOnSessionEnd(state, source, args);
  260. }
  261. public static void SetCustomApplication (IHttpHandler customApplication)
  262. {
  263. custApplication = customApplication;
  264. }
  265. }
  266. }