SessionStateModule.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // System.Web.SessionState.SesionStateModule
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Stefan Görling ([email protected])
  7. // Jackson Harper ([email protected])
  8. //
  9. // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
  10. // (C) 2003 Stefan Görling (http://www.gorling.se)
  11. using System.Web;
  12. using System.Web.Util;
  13. using System.Security.Cryptography;
  14. namespace System.Web.SessionState
  15. {
  16. [MonoTODO]
  17. public sealed class SessionStateModule : IHttpModule
  18. {
  19. internal static readonly string CookieName = "ASPSESSION";
  20. internal static readonly string HeaderName = "AspFilterSessionId";
  21. static SessionConfig config;
  22. static Type handlerType;
  23. ISessionHandler handler;
  24. bool read_only;
  25. private RandomNumberGenerator rng;
  26. public SessionStateModule ()
  27. {
  28. rng = new RNGCryptoServiceProvider ();
  29. }
  30. internal RandomNumberGenerator Rng {
  31. get { return rng; }
  32. }
  33. internal bool IsReadOnly
  34. {
  35. get { return read_only; }
  36. }
  37. public void Dispose ()
  38. {
  39. if (handler!=null)
  40. handler.Dispose();
  41. }
  42. [MonoTODO]
  43. public void Init (HttpApplication app)
  44. {
  45. if (config == null) {
  46. config = (SessionConfig) HttpContext.GetAppConfig ("system.web/sessionState");
  47. if (config == null)
  48. config = new SessionConfig (null);
  49. if (config.Mode == SessionStateMode.StateServer)
  50. handlerType = typeof (SessionStateServerHandler);
  51. if (config.Mode == SessionStateMode.SQLServer)
  52. handlerType = typeof (SessionSQLServerHandler);
  53. if (config.Mode == SessionStateMode.InProc)
  54. handlerType = typeof (SessionInProcHandler);
  55. if (config.Mode == SessionStateMode.Off)
  56. return;
  57. }
  58. if (config.CookieLess)
  59. app.BeginRequest += new EventHandler (OnBeginRequest);
  60. app.AddOnAcquireRequestStateAsync (
  61. new BeginEventHandler (OnBeginAcquireState),
  62. new EndEventHandler (OnEndAcquireState));
  63. app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
  64. app.EndRequest += new EventHandler (OnEndRequest);
  65. if (handlerType != null && handler == null) {
  66. handler = (ISessionHandler) Activator.CreateInstance (handlerType);
  67. handler.Init(app, config); //initialize
  68. }
  69. }
  70. void OnBeginRequest (object o, EventArgs args)
  71. {
  72. HttpApplication application = (HttpApplication) o;
  73. HttpContext context = application.Context;
  74. string base_path = context.Request.BaseVirtualDir;
  75. string id = UrlUtils.GetSessionId (base_path);
  76. if (id == null)
  77. return;
  78. context.Request.SetFilePath (UrlUtils.RemoveSessionId (base_path,
  79. context.Request.FilePath));
  80. context.Request.SetHeader (HeaderName, id);
  81. }
  82. void OnReleaseRequestState (object o, EventArgs args)
  83. {
  84. if (handler == null)
  85. return;
  86. HttpApplication application = (HttpApplication) o;
  87. HttpContext context = application.Context;
  88. handler.UpdateHandler (context, this);
  89. }
  90. void OnEndRequest (object o, EventArgs args)
  91. {
  92. }
  93. IAsyncResult OnBeginAcquireState (object o, EventArgs args, AsyncCallback cb, object data)
  94. {
  95. HttpApplication application = (HttpApplication) o;
  96. HttpContext context = application.Context;
  97. read_only = (context.Handler is IReadOnlySessionState);
  98. bool isNew = false;
  99. if (handler != null)
  100. isNew = handler.UpdateContext (context, this);
  101. if (isNew && config.CookieLess) {
  102. string id = context.Session.SessionID;
  103. context.Request.SetHeader (HeaderName, id);
  104. context.Response.Redirect (UrlUtils.InsertSessionId (id,
  105. context.Request.FilePath));
  106. } else if (isNew) {
  107. string id = context.Session.SessionID;
  108. HttpCookie cookie = new HttpCookie (CookieName, id);
  109. cookie.Path = UrlUtils.GetDirectory (context.Request.Path);
  110. context.Response.AppendCookie (cookie);
  111. }
  112. // In the future, we might want to move the Async stuff down to
  113. // the interface level, if we're going to support other than
  114. // InProc, we might actually want to do things async, now we
  115. // simply fake it.
  116. HttpAsyncResult result=new HttpAsyncResult (cb,this);
  117. result.Complete (true, o, null);
  118. if (isNew && Start != null)
  119. Start (this, args);
  120. return result;
  121. }
  122. void OnEndAcquireState (IAsyncResult result)
  123. {
  124. }
  125. internal void OnEnd ()
  126. {
  127. if (End != null)
  128. End (this, EventArgs.Empty);
  129. }
  130. public event EventHandler Start;
  131. public event EventHandler End;
  132. }
  133. }