SessionStateModule.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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,2004,2005 Novell, Inc (http://www.novell.com)
  10. // (C) 2003 Stefan Görling (http://www.gorling.se)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Web;
  32. using System.Web.Caching;
  33. using System.Web.Util;
  34. using System.Security.Cryptography;
  35. namespace System.Web.SessionState
  36. {
  37. public sealed class SessionStateModule : IHttpModule
  38. {
  39. internal static readonly string CookieName = "ASPSESSION";
  40. internal static readonly string HeaderName = "AspFilterSessionId";
  41. static SessionConfig config;
  42. static Type handlerType;
  43. ISessionHandler handler;
  44. bool sessionForStaticFiles;
  45. private RandomNumberGenerator rng;
  46. public SessionStateModule ()
  47. {
  48. rng = new RNGCryptoServiceProvider ();
  49. }
  50. internal RandomNumberGenerator Rng {
  51. get { return rng; }
  52. }
  53. public void Dispose ()
  54. {
  55. if (handler!=null)
  56. handler.Dispose();
  57. }
  58. public void Init (HttpApplication app)
  59. {
  60. sessionForStaticFiles = (Environment.GetEnvironmentVariable ("MONO_XSP_STATIC_SESSION") != null);
  61. if (config == null) {
  62. config = (SessionConfig) HttpContext.GetAppConfig ("system.web/sessionState");
  63. if (config == null)
  64. config = new SessionConfig (null);
  65. if (config.Mode == SessionStateMode.StateServer)
  66. handlerType = typeof (SessionStateServerHandler);
  67. if (config.Mode == SessionStateMode.SQLServer)
  68. handlerType = typeof (SessionSQLServerHandler);
  69. if (config.Mode == SessionStateMode.InProc)
  70. handlerType = typeof (SessionInProcHandler);
  71. if (config.Mode == SessionStateMode.Off)
  72. return;
  73. }
  74. if (config.CookieLess)
  75. app.BeginRequest += new EventHandler (OnBeginRequest);
  76. app.AddOnAcquireRequestStateAsync (
  77. new BeginEventHandler (OnBeginAcquireState),
  78. new EndEventHandler (OnEndAcquireState));
  79. app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
  80. app.EndRequest += new EventHandler (OnEndRequest);
  81. if (handlerType != null && handler == null) {
  82. handler = (ISessionHandler) Activator.CreateInstance (handlerType);
  83. handler.Init (this, app, config); //initialize
  84. }
  85. }
  86. void OnBeginRequest (object o, EventArgs args)
  87. {
  88. HttpApplication application = (HttpApplication) o;
  89. HttpContext context = application.Context;
  90. string base_path = context.Request.BaseVirtualDir;
  91. string id = UrlUtils.GetSessionId (base_path);
  92. if (id == null)
  93. return;
  94. context.Request.SetCurrentExePath (UrlUtils.RemoveSessionId (base_path,
  95. context.Request.FilePath));
  96. context.Request.SetHeader (HeaderName, id);
  97. context.Response.SetAppPathModifier (String.Format ("({0})", id));
  98. }
  99. void OnReleaseRequestState (object o, EventArgs args)
  100. {
  101. if (handler == null)
  102. return;
  103. HttpApplication application = (HttpApplication) o;
  104. HttpContext context = application.Context;
  105. handler.UpdateHandler (context, this);
  106. }
  107. void OnEndRequest (object o, EventArgs args)
  108. {
  109. }
  110. IAsyncResult OnBeginAcquireState (object o, EventArgs args, AsyncCallback cb, object data)
  111. {
  112. HttpApplication application = (HttpApplication) o;
  113. HttpContext context = application.Context;
  114. bool required = (context.Handler is IRequiresSessionState);
  115. // This is a hack. Sites that use Session in global.asax event handling code
  116. // are not supposed to get a Session object for static files, but seems that
  117. // IIS handles those files before getting there and thus they are served without
  118. // error.
  119. // As a workaround, setting MONO_XSP_STATIC_SESSION variable make this work
  120. // on mono, but you lose performance when serving static files.
  121. if (sessionForStaticFiles && context.Handler is StaticFileHandler)
  122. required = true;
  123. // hack end
  124. bool read_only = (context.Handler is IReadOnlySessionState);
  125. bool isNew = false;
  126. HttpSessionState session = null;
  127. if (handler != null)
  128. session = handler.UpdateContext (context, this, required, read_only, ref isNew);
  129. if (session != null) {
  130. if (isNew)
  131. session.SetNewSession (true);
  132. if (read_only)
  133. session = session.Clone ();
  134. context.SetSession (session);
  135. if (isNew && config.CookieLess) {
  136. string id = context.Session.SessionID;
  137. context.Request.SetHeader (HeaderName, id);
  138. context.Response.Redirect (UrlUtils.InsertSessionId (id,
  139. context.Request.FilePath));
  140. } else if (isNew) {
  141. string id = context.Session.SessionID;
  142. HttpCookie cookie = new HttpCookie (CookieName, id);
  143. cookie.Path = UrlUtils.GetDirectory (context.Request.ApplicationPath);
  144. context.Response.AppendCookie (cookie);
  145. }
  146. }
  147. // In the future, we might want to move the Async stuff down to
  148. // the interface level, if we're going to support other than
  149. // InProc, we might actually want to do things async, now we
  150. // simply fake it.
  151. HttpAsyncResult result=new HttpAsyncResult (cb,this);
  152. result.Complete (true, o, null);
  153. if (isNew && Start != null)
  154. Start (this, args);
  155. return result;
  156. }
  157. void OnEndAcquireState (IAsyncResult result) { }
  158. internal void OnSessionRemoved (string key, object value, CacheItemRemovedReason reason)
  159. {
  160. OnEnd ();
  161. }
  162. internal void OnEnd ()
  163. {
  164. if (End != null)
  165. End (this, EventArgs.Empty);
  166. }
  167. public event EventHandler Start;
  168. public event EventHandler End;
  169. }
  170. }