HttpApplication.cs 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. //
  2. // System.Web.HttpApplication
  3. //
  4. // Authors:
  5. // Patrik Torstensson ([email protected])
  6. // Tim Coleman ([email protected])
  7. // Gonzalo Paniagua Javier ([email protected])
  8. //
  9. // (c) Copyright 2002-2003 Ximian, Inc. (http://www.ximian.com)
  10. // (c) Copyright 2004 Novell, Inc. (http://www.novell.com)
  11. //
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Collections;
  34. using System.ComponentModel;
  35. using System.Globalization;
  36. using System.IO;
  37. using System.Threading;
  38. using System.Security.Principal;
  39. using System.Runtime.Remoting.Messaging;
  40. using System.Web.UI;
  41. using System.Web.Configuration;
  42. using System.Web.SessionState;
  43. namespace System.Web
  44. {
  45. [ToolboxItem(true)]
  46. public class HttpApplication : IHttpAsyncHandler, IHttpHandler, IComponent, IDisposable
  47. {
  48. #region Event Handlers
  49. // Async event holders
  50. AsyncEvents _acquireRequestStateAsync;
  51. AsyncEvents _authenticateRequestAsync;
  52. AsyncEvents _endRequestAsync;
  53. AsyncEvents _beginRequestAsync;
  54. AsyncEvents _authorizeRequestAsync;
  55. AsyncEvents _updateRequestCacheAsync;
  56. AsyncEvents _resolveRequestCacheAsync;
  57. AsyncEvents _releaseRequestStateAsync;
  58. AsyncEvents _preRequestHandlerExecuteAsync;
  59. AsyncEvents _postRequestHandlerExecuteAsync;
  60. // ID objects used to indentify the event
  61. static object AcquireRequestStateId = new Object ();
  62. static object AuthenticateRequestId = new Object ();
  63. static object DefaultAuthenticationId = new Object ();
  64. static object EndRequestId = new Object ();
  65. static object DisposedId = new Object ();
  66. static object BeginRequestId = new Object ();
  67. static object AuthorizeRequestId = new Object ();
  68. static object UpdateRequestCacheId = new Object ();
  69. static object ResolveRequestCacheId = new Object ();
  70. static object ReleaseRequestStateId = new Object ();
  71. static object PreSendRequestContentId = new Object ();
  72. static object PreSendRequestHeadersId = new Object ();
  73. static object PreRequestHandlerExecuteId = new Object ();
  74. static object PostRequestHandlerExecuteId = new Object ();
  75. static object ErrorId = new Object ();
  76. // List of events
  77. private EventHandlerList _Events;
  78. public event EventHandler AcquireRequestState {
  79. add { Events.AddHandler (AcquireRequestStateId, value); }
  80. remove { Events.RemoveHandler (AcquireRequestStateId, value); }
  81. }
  82. public event EventHandler AuthenticateRequest {
  83. add { Events.AddHandler (AuthenticateRequestId, value); }
  84. remove { Events.RemoveHandler (AuthenticateRequestId, value); }
  85. }
  86. public event EventHandler AuthorizeRequest {
  87. add { Events.AddHandler (AuthorizeRequestId, value); }
  88. remove { Events.RemoveHandler (AuthorizeRequestId, value); }
  89. }
  90. public event EventHandler BeginRequest {
  91. add { Events.AddHandler (BeginRequestId, value); }
  92. remove { Events.RemoveHandler (BeginRequestId, value); }
  93. }
  94. public event EventHandler Disposed {
  95. add { Events.AddHandler (DisposedId, value); }
  96. remove { Events.RemoveHandler (DisposedId, value); }
  97. }
  98. public event EventHandler EndRequest {
  99. add { Events.AddHandler (EndRequestId, value); }
  100. remove { Events.RemoveHandler (EndRequestId, value); }
  101. }
  102. public event EventHandler Error {
  103. add { Events.AddHandler (ErrorId, value); }
  104. remove { Events.RemoveHandler (ErrorId, value); }
  105. }
  106. public event EventHandler PostRequestHandlerExecute {
  107. add { Events.AddHandler (PostRequestHandlerExecuteId, value); }
  108. remove { Events.RemoveHandler (PostRequestHandlerExecuteId, value); }
  109. }
  110. public event EventHandler PreRequestHandlerExecute {
  111. add { Events.AddHandler (PreRequestHandlerExecuteId, value); }
  112. remove { Events.RemoveHandler (PreRequestHandlerExecuteId, value); }
  113. }
  114. public event EventHandler PreSendRequestContent {
  115. add { Events.AddHandler (PreSendRequestContentId, value); }
  116. remove { Events.RemoveHandler (PreSendRequestContentId, value); }
  117. }
  118. public event EventHandler ReleaseRequestState {
  119. add { Events.AddHandler (ReleaseRequestStateId, value); }
  120. remove { Events.RemoveHandler (ReleaseRequestStateId, value); }
  121. }
  122. public event EventHandler ResolveRequestCache
  123. {
  124. add { Events.AddHandler (ResolveRequestCacheId, value); }
  125. remove { Events.RemoveHandler (ResolveRequestCacheId, value); }
  126. }
  127. public event EventHandler UpdateRequestCache {
  128. add { Events.AddHandler (UpdateRequestCacheId, value); }
  129. remove { Events.RemoveHandler (UpdateRequestCacheId, value); }
  130. }
  131. public event EventHandler PreSendRequestHeaders {
  132. add { Events.AddHandler (PreSendRequestHeadersId, value); }
  133. remove { Events.RemoveHandler (PreSendRequestHeadersId, value); }
  134. }
  135. internal event EventHandler DefaultAuthentication {
  136. add { Events.AddHandler (DefaultAuthenticationId, value); }
  137. remove { Events.RemoveHandler (DefaultAuthenticationId, value); }
  138. }
  139. public void AddOnAcquireRequestStateAsync (BeginEventHandler beg, EndEventHandler end)
  140. {
  141. if (null == _acquireRequestStateAsync)
  142. _acquireRequestStateAsync = new AsyncEvents ();
  143. _acquireRequestStateAsync.Add (beg, end);
  144. }
  145. public void AddOnAuthenticateRequestAsync(BeginEventHandler beg, EndEventHandler end)
  146. {
  147. if (null == _authenticateRequestAsync)
  148. _authenticateRequestAsync = new AsyncEvents ();
  149. _authenticateRequestAsync.Add (beg, end);
  150. }
  151. public void AddOnAuthorizeRequestAsync (BeginEventHandler beg, EndEventHandler end)
  152. {
  153. if (null == _authorizeRequestAsync)
  154. _authorizeRequestAsync = new AsyncEvents ();
  155. _authorizeRequestAsync.Add (beg, end);
  156. }
  157. public void AddOnBeginRequestAsync (BeginEventHandler beg, EndEventHandler end)
  158. {
  159. if (null == _beginRequestAsync)
  160. _beginRequestAsync = new AsyncEvents ();
  161. _beginRequestAsync.Add (beg, end);
  162. }
  163. public void AddOnEndRequestAsync (BeginEventHandler beg, EndEventHandler end)
  164. {
  165. if (null == _endRequestAsync)
  166. _endRequestAsync = new AsyncEvents ();
  167. _endRequestAsync.Add (beg, end);
  168. }
  169. public void AddOnPostRequestHandlerExecuteAsync (BeginEventHandler beg, EndEventHandler end)
  170. {
  171. if (null == _postRequestHandlerExecuteAsync)
  172. _postRequestHandlerExecuteAsync = new AsyncEvents ();
  173. _postRequestHandlerExecuteAsync.Add (beg, end);
  174. }
  175. public void AddOnPreRequestHandlerExecuteAsync (BeginEventHandler beg, EndEventHandler end)
  176. {
  177. if (null == _preRequestHandlerExecuteAsync)
  178. _preRequestHandlerExecuteAsync = new AsyncEvents ();
  179. _preRequestHandlerExecuteAsync.Add (beg, end);
  180. }
  181. public void AddOnReleaseRequestStateAsync (BeginEventHandler beg, EndEventHandler end)
  182. {
  183. if (null == _releaseRequestStateAsync)
  184. _releaseRequestStateAsync = new AsyncEvents ();
  185. _releaseRequestStateAsync.Add (beg, end);
  186. }
  187. public void AddOnResolveRequestCacheAsync (BeginEventHandler beg, EndEventHandler end)
  188. {
  189. if (null == _resolveRequestCacheAsync)
  190. _resolveRequestCacheAsync = new AsyncEvents ();
  191. _resolveRequestCacheAsync.Add (beg, end);
  192. }
  193. public void AddOnUpdateRequestCacheAsync (BeginEventHandler beg, EndEventHandler end)
  194. {
  195. if (null == _updateRequestCacheAsync)
  196. _updateRequestCacheAsync = new AsyncEvents ();
  197. _updateRequestCacheAsync.Add (beg, end);
  198. }
  199. #endregion
  200. #region Recycle Helper
  201. class HandlerFactory
  202. {
  203. public IHttpHandler Handler;
  204. public IHttpHandlerFactory Factory;
  205. public HandlerFactory (IHttpHandler handler, IHttpHandlerFactory factory)
  206. {
  207. this.Handler = handler;
  208. this.Factory = factory;
  209. }
  210. }
  211. #endregion
  212. #region State Machine
  213. interface IStateHandler
  214. {
  215. void Execute();
  216. bool CompletedSynchronously { get; }
  217. bool PossibleToTimeout { get; }
  218. }
  219. class EventState : IStateHandler
  220. {
  221. HttpApplication _app;
  222. EventHandler _event;
  223. public EventState (HttpApplication app, EventHandler evt)
  224. {
  225. _app = app;
  226. _event = evt;
  227. }
  228. public void Execute ()
  229. {
  230. if (null != _event)
  231. _event (_app, EventArgs.Empty);
  232. }
  233. public bool CompletedSynchronously {
  234. get { return true; }
  235. }
  236. public bool PossibleToTimeout {
  237. get { return true; }
  238. }
  239. }
  240. class AsyncEventState : IStateHandler
  241. {
  242. HttpApplication _app;
  243. BeginEventHandler _begin;
  244. EndEventHandler _end;
  245. AsyncCallback _callback;
  246. bool _async;
  247. public AsyncEventState (HttpApplication app,
  248. BeginEventHandler begin,
  249. EndEventHandler end)
  250. {
  251. _async = false;
  252. _app = app;
  253. _begin = begin;
  254. _end = end;
  255. _callback = new AsyncCallback (OnAsyncReady);
  256. }
  257. public void Execute ()
  258. {
  259. _async = true;
  260. IAsyncResult ar = _begin (_app, EventArgs.Empty, _callback, null);
  261. if (ar.CompletedSynchronously) {
  262. _async = false;
  263. _end (ar);
  264. }
  265. }
  266. public bool CompletedSynchronously {
  267. get { return !_async; }
  268. }
  269. public bool PossibleToTimeout {
  270. get {
  271. // We can't cancel a async event
  272. return false;
  273. }
  274. }
  275. private void OnAsyncReady (IAsyncResult ar)
  276. {
  277. if (ar.CompletedSynchronously)
  278. return;
  279. Exception error = null;
  280. try {
  281. // Invoke end handler
  282. _end(ar);
  283. } catch (Exception exc) {
  284. // Flow this error to the next state (handle during state execution)
  285. error = exc;
  286. }
  287. _app._state.ExecuteNextAsync (error);
  288. }
  289. }
  290. class AsyncEvents
  291. {
  292. ArrayList _events;
  293. class EventRecord {
  294. public EventRecord(BeginEventHandler beg, EndEventHandler end)
  295. {
  296. Begin = beg;
  297. End = end;
  298. }
  299. public BeginEventHandler Begin;
  300. public EndEventHandler End;
  301. }
  302. public AsyncEvents ()
  303. {
  304. _events = new ArrayList ();
  305. }
  306. public void Add (BeginEventHandler begin, EndEventHandler end)
  307. {
  308. _events.Add (new EventRecord (begin, end));
  309. }
  310. public void GetAsStates (HttpApplication app, ArrayList states)
  311. {
  312. foreach (object obj in _events)
  313. states.Add (new AsyncEventState (app,
  314. ((EventRecord) obj).Begin,
  315. ((EventRecord) obj).End));
  316. }
  317. }
  318. class ExecuteHandlerState : IStateHandler
  319. {
  320. HttpApplication _app;
  321. AsyncCallback _callback;
  322. IHttpAsyncHandler _handler;
  323. bool _async;
  324. public ExecuteHandlerState (HttpApplication app)
  325. {
  326. _app = app;
  327. _callback = new AsyncCallback (OnAsyncReady);
  328. }
  329. private void OnAsyncReady (IAsyncResult ar)
  330. {
  331. if (ar.CompletedSynchronously)
  332. return;
  333. Exception error = null;
  334. try {
  335. // Invoke end handler
  336. _handler.EndProcessRequest(ar);
  337. } catch (Exception exc) {
  338. // Flow this error to the next state (handle during state execution)
  339. error = exc;
  340. }
  341. _handler = null;
  342. _app._state.ExecuteNextAsync (error);
  343. }
  344. public void Execute ()
  345. {
  346. IHttpHandler handler = _app.Context.Handler;
  347. if (handler == null)
  348. return;
  349. // Check if we can execute async
  350. if (handler is IHttpAsyncHandler) {
  351. _async = true;
  352. _handler = (IHttpAsyncHandler) handler;
  353. IAsyncResult ar = _handler.BeginProcessRequest (_app.Context,
  354. _callback,
  355. null);
  356. if (ar.CompletedSynchronously) {
  357. _async = false;
  358. _handler = null;
  359. ((IHttpAsyncHandler) handler).EndProcessRequest (ar);
  360. }
  361. } else {
  362. _async = false;
  363. // Sync handler
  364. handler.ProcessRequest (_app.Context);
  365. }
  366. }
  367. public bool CompletedSynchronously {
  368. get { return !_async; }
  369. }
  370. public bool PossibleToTimeout {
  371. get {
  372. if (_app.Context.Handler is IHttpAsyncHandler)
  373. return false;
  374. return true;
  375. }
  376. }
  377. }
  378. class CreateHandlerState : IStateHandler
  379. {
  380. HttpApplication _app;
  381. public CreateHandlerState (HttpApplication app)
  382. {
  383. _app = app;
  384. }
  385. public void Execute ()
  386. {
  387. _app.Context.Handler = _app.CreateHttpHandler ( _app.Context,
  388. _app.Request.RequestType,
  389. _app.Request.FilePath,
  390. _app.Request.PhysicalPath);
  391. }
  392. public bool CompletedSynchronously {
  393. get { return true; }
  394. }
  395. public bool PossibleToTimeout {
  396. get { return false; }
  397. }
  398. }
  399. class FilterHandlerState : IStateHandler
  400. {
  401. HttpApplication _app;
  402. public FilterHandlerState (HttpApplication app)
  403. {
  404. _app = app;
  405. }
  406. public void Execute ()
  407. {
  408. _app.Context.Response.DoFilter (true);
  409. }
  410. public bool CompletedSynchronously {
  411. get { return true; }
  412. }
  413. public bool PossibleToTimeout {
  414. get { return true; }
  415. }
  416. }
  417. class StateMachine
  418. {
  419. HttpApplication _app;
  420. WaitCallback _asynchandler;
  421. IStateHandler [] _handlers;
  422. int _currentStateIdx;
  423. int _endStateIdx;
  424. int _endRequestStateIdx;
  425. int _countSteps;
  426. int _countSyncSteps;
  427. // Helper to create the states for a normal event
  428. private void GetAsStates (object Event, ArrayList states)
  429. {
  430. // Get list of clients for the sync events
  431. Delegate evnt = _app.Events [Event];
  432. if (evnt == null)
  433. return;
  434. System.Delegate [] clients = evnt.GetInvocationList();
  435. foreach (Delegate client in clients)
  436. states.Add (new EventState (_app, (EventHandler) client));
  437. }
  438. internal StateMachine (HttpApplication app)
  439. {
  440. _app = app;
  441. }
  442. internal void Init ()
  443. {
  444. _asynchandler = new WaitCallback (OnAsyncCallback);
  445. // Create a arraylist of states to execute
  446. ArrayList states = new ArrayList ();
  447. // BeginRequest
  448. if (null != _app._beginRequestAsync)
  449. _app._beginRequestAsync.GetAsStates (_app, states);
  450. GetAsStates (HttpApplication.BeginRequestId, states);
  451. // AuthenticateRequest
  452. if (null != _app._authenticateRequestAsync)
  453. _app._authenticateRequestAsync.GetAsStates (_app, states);
  454. GetAsStates (HttpApplication.AuthenticateRequestId, states);
  455. // DefaultAuthentication
  456. EventHandler defaultAuthHandler = (EventHandler) _app.Events [HttpApplication.DefaultAuthenticationId];
  457. states.Add (new EventState (_app, defaultAuthHandler));
  458. // AuthorizeRequest
  459. if (null != _app._authorizeRequestAsync)
  460. _app._authorizeRequestAsync.GetAsStates (_app, states);
  461. GetAsStates (HttpApplication.AuthorizeRequestId, states);
  462. // ResolveRequestCache
  463. if (null != _app._resolveRequestCacheAsync)
  464. _app._resolveRequestCacheAsync.GetAsStates (_app, states);
  465. GetAsStates (HttpApplication.ResolveRequestCacheId, states);
  466. // [A handler (a page corresponding to the request URL) is created at this point.]
  467. states.Add (new CreateHandlerState (_app));
  468. // AcquireRequestState
  469. if (null != _app._acquireRequestStateAsync)
  470. _app._acquireRequestStateAsync.GetAsStates (_app, states);
  471. GetAsStates (HttpApplication.AcquireRequestStateId, states);
  472. // PreRequestHandlerExecute
  473. if (null != _app._preRequestHandlerExecuteAsync)
  474. _app._preRequestHandlerExecuteAsync.GetAsStates (_app, states);
  475. GetAsStates (HttpApplication.PreRequestHandlerExecuteId, states);
  476. // [The handler is executed.]
  477. states.Add (new ExecuteHandlerState (_app));
  478. // PostRequestHandlerExecute
  479. if (null != _app._postRequestHandlerExecuteAsync)
  480. _app._postRequestHandlerExecuteAsync.GetAsStates (_app, states);
  481. GetAsStates (HttpApplication.PostRequestHandlerExecuteId, states);
  482. // ReleaseRequestState
  483. if (null != _app._releaseRequestStateAsync)
  484. _app._releaseRequestStateAsync.GetAsStates (_app, states);
  485. GetAsStates (HttpApplication.ReleaseRequestStateId, states);
  486. // [Response filters, if any, filter the output.]
  487. states.Add (new FilterHandlerState (_app));
  488. // UpdateRequestCache
  489. if (null != _app._updateRequestCacheAsync)
  490. _app._updateRequestCacheAsync.GetAsStates (_app, states);
  491. GetAsStates (HttpApplication.UpdateRequestCacheId, states);
  492. // EndRequest
  493. _endRequestStateIdx = states.Count;
  494. if (null != _app._endRequestAsync)
  495. _app._endRequestAsync.GetAsStates (_app, states);
  496. GetAsStates (HttpApplication.EndRequestId, states);
  497. // Make list ready to execute
  498. _handlers = new IStateHandler [states.Count];
  499. states.CopyTo (_handlers);
  500. }
  501. internal void Reset ()
  502. {
  503. _countSyncSteps = 0;
  504. _countSteps = 0;
  505. _currentStateIdx = -1;
  506. _endStateIdx = _handlers.Length - 1;
  507. }
  508. internal void Start ()
  509. {
  510. Reset ();
  511. ExecuteNextAsync (null);
  512. }
  513. internal void ExecuteNextAsync (Exception lasterror)
  514. {
  515. if (!Thread.CurrentThread.IsThreadPoolThread)
  516. ThreadPool.QueueUserWorkItem (_asynchandler, lasterror);
  517. else
  518. ExecuteNext (lasterror);
  519. }
  520. internal void ExecuteNext (Exception lasterror)
  521. {
  522. bool ready_sync = false;
  523. IStateHandler handler;
  524. bool timeoutPossible = false;
  525. lock (_app) {
  526. _app.OnStateExecuteEnter ();
  527. try {
  528. do {
  529. if (null != lasterror) {
  530. _app.HandleError (lasterror);
  531. lasterror = null;
  532. }
  533. // Check if request flow is to be stopped
  534. if ((_app.GetLastError () != null || _app._CompleteRequest) &&
  535. _currentStateIdx < _endRequestStateIdx) {
  536. _currentStateIdx = _endRequestStateIdx;
  537. // MS does not filter on error
  538. _app._Context.Response.DoFilter (false);
  539. } else if (_currentStateIdx < _endStateIdx) {
  540. // Get next state handler
  541. _currentStateIdx++;
  542. }
  543. handler = _handlers [_currentStateIdx];
  544. _countSteps++;
  545. timeoutPossible = handler.PossibleToTimeout;
  546. if (timeoutPossible)
  547. HttpRuntime.TimeoutManager.Add (_app.Context);
  548. lasterror = ExecuteState (handler, ref ready_sync);
  549. if (ready_sync)
  550. _countSyncSteps++;
  551. } while (ready_sync && _currentStateIdx < _endStateIdx);
  552. if (null != lasterror)
  553. _app.HandleError (lasterror);
  554. } finally {
  555. if (timeoutPossible)
  556. HttpRuntime.TimeoutManager.Remove (_app.Context);
  557. _app.OnStateExecuteLeave ();
  558. }
  559. }
  560. // Finish the request off..
  561. if (lasterror != null || _currentStateIdx == _endStateIdx) {
  562. _app._asyncWebResult.Complete ((_countSyncSteps == _countSteps),
  563. null,
  564. null);
  565. _app.Context.Handler = null;
  566. _app.Context.ApplicationInstance = null;
  567. _app.RecycleHandlers ();
  568. _app._asyncWebResult = null;
  569. HttpApplicationFactory.RecycleInstance (_app);
  570. }
  571. }
  572. private void OnAsyncCallback (object obj)
  573. {
  574. ExecuteNext ((Exception) obj);
  575. }
  576. private Exception ExecuteState (IStateHandler state, ref bool readysync)
  577. {
  578. Exception lasterror = null;
  579. try {
  580. if (state.PossibleToTimeout) {
  581. _app.Context.BeginTimeoutPossible ();
  582. }
  583. try {
  584. state.Execute ();
  585. } finally {
  586. if (state.PossibleToTimeout) {
  587. _app.Context.EndTimeoutPossible ();
  588. }
  589. }
  590. if (state.PossibleToTimeout) {
  591. // Async Execute
  592. _app.Context.TryWaitForTimeout ();
  593. }
  594. readysync = state.CompletedSynchronously;
  595. } catch (ThreadAbortException obj) {
  596. object o = obj.ExceptionState;
  597. Type type = (o != null) ? o.GetType () : null;
  598. if (type == typeof (StepTimeout)) {
  599. Thread.ResetAbort ();
  600. lasterror = new HttpException ("The request timed out.");
  601. _app.CompleteRequest ();
  602. } else if (type == typeof (StepCompleteRequest)) {
  603. Thread.ResetAbort ();
  604. _app.CompleteRequest ();
  605. }
  606. } catch (Exception obj) {
  607. lasterror = obj;
  608. }
  609. return lasterror;
  610. }
  611. }
  612. #endregion
  613. #region Fields
  614. StateMachine _state;
  615. bool _CompleteRequest;
  616. HttpContext _Context;
  617. Exception _lastError;
  618. HttpContext _savedContext;
  619. IPrincipal _savedUser;
  620. HttpAsyncResult _asyncWebResult;
  621. ISite _Site;
  622. HttpModuleCollection _ModuleCollection;
  623. HttpSessionState _Session;
  624. HttpApplicationState _appState;
  625. string assemblyLocation;
  626. ArrayList _recycleHandlers;
  627. bool _InPreRequestResponseMode;
  628. CultureInfo appCulture;
  629. CultureInfo appUICulture;
  630. CultureInfo prevAppCulture;
  631. CultureInfo prevAppUICulture;
  632. #endregion
  633. #region Constructor
  634. public HttpApplication ()
  635. {
  636. assemblyLocation = GetType ().Assembly.Location;
  637. }
  638. #endregion
  639. #region Methods
  640. internal IHttpHandler CreateHttpHandler (HttpContext context,
  641. string type,
  642. string file,
  643. string path)
  644. {
  645. HandlerFactoryConfiguration handler =
  646. HttpContext.GetAppConfig ("system.web/httpHandlers")
  647. as HandlerFactoryConfiguration;
  648. if (handler == null)
  649. throw new HttpException ("Cannot get system.web/httpHandlers handler.");
  650. object result = handler.FindHandler (type, file).Create ();
  651. if (result is IHttpHandler)
  652. return (IHttpHandler) result;
  653. if (result is IHttpHandlerFactory) {
  654. IHttpHandlerFactory factory = (IHttpHandlerFactory) result;
  655. try {
  656. IHttpHandler ret = factory.GetHandler (context, type, file, path);
  657. if (null != ret) {
  658. if (null == _recycleHandlers)
  659. _recycleHandlers = new ArrayList();
  660. _recycleHandlers.Add (new HandlerFactory (ret, factory));
  661. }
  662. return ret;
  663. } catch (DirectoryNotFoundException) {
  664. throw new HttpException (404, "Cannot find '" + file + "'.");
  665. } catch (FileNotFoundException fnf) {
  666. string fname = fnf.FileName;
  667. if (fname != null && fname != "") {
  668. file = Path.GetFileName (fname);
  669. }
  670. throw new HttpException (404, "Cannot find '" + file + "'.");
  671. }
  672. }
  673. throw new HttpException ("Handler not found");
  674. }
  675. internal void RecycleHandlers ()
  676. {
  677. if (null == _recycleHandlers || _recycleHandlers.Count == 0)
  678. return;
  679. foreach (HandlerFactory item in _recycleHandlers)
  680. item.Factory.ReleaseHandler (item.Handler);
  681. _recycleHandlers.Clear ();
  682. }
  683. internal void InitModules ()
  684. {
  685. ModulesConfiguration modules;
  686. modules = (ModulesConfiguration) HttpContext.GetAppConfig ("system.web/httpModules");
  687. if (null == modules)
  688. throw new HttpException (
  689. HttpRuntime.FormatResourceString ("missing_modules_config"));
  690. _ModuleCollection = modules.CreateCollection ();
  691. if (_ModuleCollection == null)
  692. return;
  693. int pos, count;
  694. count = _ModuleCollection.Count;
  695. for (pos = 0; pos != count; pos++)
  696. ((IHttpModule) _ModuleCollection.Get (pos)).Init (this);
  697. }
  698. internal void InitCulture ()
  699. {
  700. GlobalizationConfiguration cfg = GlobalizationConfiguration.GetInstance (null);
  701. if (cfg != null) {
  702. appCulture = cfg.Culture;
  703. appUICulture = cfg.UICulture;
  704. }
  705. }
  706. void SaveThreadCulture ()
  707. {
  708. Thread th = Thread.CurrentThread;
  709. if (appCulture != null) {
  710. prevAppCulture = th.CurrentCulture;
  711. th.CurrentCulture = appCulture;
  712. }
  713. if (appUICulture != null) {
  714. prevAppUICulture = th.CurrentUICulture;
  715. th.CurrentUICulture = appUICulture;
  716. }
  717. }
  718. void RestoreThreadCulture ()
  719. {
  720. Thread th = Thread.CurrentThread;
  721. if (prevAppCulture != null) {
  722. th.CurrentCulture = prevAppCulture;
  723. prevAppCulture = null;
  724. }
  725. if (prevAppUICulture != null) {
  726. th.CurrentUICulture = prevAppUICulture;
  727. prevAppUICulture = null;
  728. }
  729. }
  730. internal void OnStateExecuteEnter ()
  731. {
  732. SaveThreadCulture ();
  733. _savedContext = HttpContext.Context;
  734. HttpContext.Context = _Context;
  735. SetPrincipal (Context.User);
  736. }
  737. internal void OnStateExecuteLeave ()
  738. {
  739. RestoreThreadCulture ();
  740. HttpContext.Context = _savedContext;
  741. RestorePrincipal ();
  742. }
  743. internal void SetPrincipal (IPrincipal principal)
  744. {
  745. // Don't overwrite _savedUser if called from inside a step
  746. if (_savedUser == null)
  747. _savedUser = Thread.CurrentPrincipal;
  748. Thread.CurrentPrincipal = principal;
  749. }
  750. internal void RestorePrincipal ()
  751. {
  752. if (_savedUser == null)
  753. return;
  754. Thread.CurrentPrincipal = _savedUser;
  755. _savedUser = null;
  756. }
  757. internal void ClearError ()
  758. {
  759. _lastError = null;
  760. }
  761. internal Exception GetLastError ()
  762. {
  763. if (_Context == null)
  764. return _lastError;
  765. return _Context.Error;
  766. }
  767. internal void HandleError (Exception obj)
  768. {
  769. EventHandler handler;
  770. bool fire = true;
  771. if (null != _Context) {
  772. if (null != _Context.Error)
  773. fire = false;
  774. _Context.AddError (obj);
  775. } else {
  776. if (null != _lastError)
  777. fire = false;
  778. _lastError = obj;
  779. }
  780. if (!fire)
  781. return;
  782. // Fire OnError event here
  783. handler = (EventHandler) Events [HttpApplication.ErrorId];
  784. if (null != handler) {
  785. try {
  786. handler (this, EventArgs.Empty);
  787. } catch (Exception excp) {
  788. if (null != _Context)
  789. _Context.AddError (excp);
  790. }
  791. }
  792. }
  793. internal void Startup (HttpContext context, HttpApplicationState state)
  794. {
  795. _Context = context;
  796. _appState = state;
  797. _state = new StateMachine (this);
  798. // Initialize all IHttpModule(s)
  799. InitModules ();
  800. HttpApplicationFactory.AttachEvents (this);
  801. InitCulture ();
  802. // Initialize custom application
  803. _InPreRequestResponseMode = true;
  804. try {
  805. Init ();
  806. } catch (Exception obj) {
  807. HandleError (obj);
  808. }
  809. _InPreRequestResponseMode = false;
  810. _state.Init ();
  811. }
  812. internal void Cleanup ()
  813. {
  814. try {
  815. Dispose ();
  816. } catch (Exception obj) {
  817. HandleError (obj);
  818. }
  819. if (null != _ModuleCollection) {
  820. int pos;
  821. int count = _ModuleCollection.Count;
  822. for (pos = 0; pos != count; pos++)
  823. ((IHttpModule) _ModuleCollection.Get (pos)).Dispose ();
  824. _ModuleCollection = null;
  825. }
  826. _state = null;
  827. }
  828. public void CompleteRequest ()
  829. {
  830. _CompleteRequest = true;
  831. }
  832. public virtual void Dispose ()
  833. {
  834. _Site = null;
  835. EventHandler disposed = (EventHandler) Events [HttpApplication.DisposedId];
  836. if (null != disposed)
  837. disposed (this, EventArgs.Empty);
  838. }
  839. public virtual void Init ()
  840. {
  841. }
  842. public virtual string GetVaryByCustomString (HttpContext context, string custom)
  843. {
  844. if (custom.ToLower () == "browser")
  845. return context.Request.Browser.Type;
  846. return null;
  847. }
  848. IAsyncResult IHttpAsyncHandler.BeginProcessRequest (HttpContext context,
  849. AsyncCallback cb,
  850. object extraData)
  851. {
  852. _Context = context;
  853. _Context.ApplicationInstance = this;
  854. _CompleteRequest = false;
  855. _asyncWebResult = new HttpAsyncResult (cb, extraData);
  856. _state.Start ();
  857. return _asyncWebResult;
  858. }
  859. void IHttpAsyncHandler.EndProcessRequest (IAsyncResult result)
  860. {
  861. HttpAsyncResult ar = (HttpAsyncResult) result;
  862. if (null != ar.Error)
  863. throw ar.Error;
  864. }
  865. void IHttpHandler.ProcessRequest (HttpContext context)
  866. {
  867. throw new NotSupportedException (HttpRuntime.FormatResourceString("sync_not_supported"));
  868. }
  869. bool IHttpHandler.IsReusable {
  870. get { return true; }
  871. }
  872. #endregion
  873. #region Properties
  874. internal string AssemblyLocation {
  875. get { return assemblyLocation; }
  876. }
  877. [Browsable (false)]
  878. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  879. public HttpApplicationState Application {
  880. get { return _appState; }
  881. }
  882. [Browsable (false)]
  883. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  884. public HttpContext Context {
  885. get { return _Context; }
  886. }
  887. protected EventHandlerList Events {
  888. get {
  889. if (null == _Events)
  890. _Events = new EventHandlerList ();
  891. return _Events;
  892. }
  893. }
  894. [Browsable (false)]
  895. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  896. public HttpModuleCollection Modules {
  897. get {
  898. if (null == _ModuleCollection)
  899. _ModuleCollection = new HttpModuleCollection ();
  900. return _ModuleCollection;
  901. }
  902. }
  903. [Browsable (false)]
  904. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  905. public HttpRequest Request {
  906. get {
  907. if (null != _Context && (!_InPreRequestResponseMode))
  908. return _Context.Request;
  909. throw new HttpException ("Can't get request object");
  910. }
  911. }
  912. [Browsable (false)]
  913. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  914. public HttpResponse Response {
  915. get {
  916. if (null != _Context && (!_InPreRequestResponseMode))
  917. return _Context.Response;
  918. throw new HttpException ("Can't get response object");
  919. }
  920. }
  921. [Browsable (false)]
  922. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  923. public HttpServerUtility Server {
  924. get {
  925. if (null != _Context)
  926. return _Context.Server;
  927. return new HttpServerUtility (this);
  928. }
  929. }
  930. [Browsable (false)]
  931. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  932. public HttpSessionState Session {
  933. get {
  934. if (null != _Session)
  935. return _Session;
  936. if (null != _Context && null != _Context.Session)
  937. return _Context.Session;
  938. throw new HttpException ("Failed to get session object");
  939. }
  940. }
  941. [Browsable (false)]
  942. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  943. public IPrincipal User {
  944. get { return _Context.User; }
  945. }
  946. [Browsable (false)]
  947. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  948. public ISite Site {
  949. get { return _Site; }
  950. set { _Site = value; }
  951. }
  952. #endregion Properties
  953. }
  954. // Used in HttpResponse.End ()
  955. class StepCompleteRequest
  956. {
  957. }
  958. }