SessionStateModule.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 object locker = new object ();
  42. #if TARGET_J2EE
  43. static private SessionConfig config {
  44. get {
  45. return (SessionConfig)AppDomain.CurrentDomain.GetData("SessionStateModule.config");
  46. }
  47. set {
  48. AppDomain.CurrentDomain.SetData("SessionStateModule.config", value);
  49. }
  50. }
  51. static private Type handlerType {
  52. get {
  53. return (Type)AppDomain.CurrentDomain.GetData("SessionStateModule.handlerType");
  54. }
  55. set {
  56. AppDomain.CurrentDomain.SetData("SessionStateModule.handlerType", value);
  57. }
  58. }
  59. #else
  60. static SessionConfig config;
  61. static Type handlerType;
  62. #endif
  63. ISessionHandler handler;
  64. bool sessionForStaticFiles;
  65. static RandomNumberGenerator rng = new RNGCryptoServiceProvider ();
  66. public SessionStateModule ()
  67. {
  68. }
  69. internal RandomNumberGenerator Rng {
  70. get { return rng; }
  71. }
  72. public void Dispose ()
  73. {
  74. if (handler!=null)
  75. handler.Dispose();
  76. }
  77. SessionConfig GetConfig ()
  78. {
  79. lock (locker) {
  80. if (config != null)
  81. return config;
  82. config = (SessionConfig) HttpContext.GetAppConfig ("system.web/sessionState");
  83. if (config == null)
  84. config = new SessionConfig (null);
  85. #if TARGET_J2EE
  86. if (config.Mode == SessionStateMode.SQLServer || config.Mode == SessionStateMode.StateServer)
  87. throw new NotImplementedException("You must use web.xml to specify session state handling");
  88. #else
  89. if (config.Mode == SessionStateMode.StateServer)
  90. handlerType = typeof (SessionStateServerHandler);
  91. if (config.Mode == SessionStateMode.SQLServer)
  92. handlerType = typeof (SessionSQLServerHandler);
  93. #endif
  94. if (config.Mode == SessionStateMode.InProc)
  95. handlerType = typeof (SessionInProcHandler);
  96. return config;
  97. }
  98. }
  99. public void Init (HttpApplication app)
  100. {
  101. sessionForStaticFiles = (Environment.GetEnvironmentVariable ("MONO_XSP_STATIC_SESSION") != null);
  102. SessionConfig cfg = GetConfig ();
  103. if (handlerType == null)
  104. return;
  105. if (config.CookieLess)
  106. app.BeginRequest += new EventHandler (OnBeginRequest);
  107. app.AcquireRequestState += new EventHandler (OnAcquireState);
  108. app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
  109. app.EndRequest += new EventHandler (OnEndRequest);
  110. if (handlerType != null && handler == null) {
  111. handler = (ISessionHandler) Activator.CreateInstance (handlerType);
  112. handler.Init (this, app, config); //initialize
  113. }
  114. }
  115. void OnBeginRequest (object o, EventArgs args)
  116. {
  117. HttpApplication application = (HttpApplication) o;
  118. HttpContext context = application.Context;
  119. string base_path = context.Request.BaseVirtualDir;
  120. string id = UrlUtils.GetSessionId (base_path);
  121. if (id == null)
  122. return;
  123. context.Request.SetCurrentExePath (UrlUtils.RemoveSessionId (base_path,
  124. context.Request.FilePath));
  125. context.Request.SetHeader (HeaderName, id);
  126. context.Response.SetAppPathModifier (String.Format ("({0})", id));
  127. }
  128. void OnReleaseRequestState (object o, EventArgs args)
  129. {
  130. if (handler == null)
  131. return;
  132. HttpApplication application = (HttpApplication) o;
  133. HttpContext context = application.Context;
  134. handler.UpdateHandler (context, this);
  135. }
  136. void OnEndRequest (object o, EventArgs args)
  137. {
  138. }
  139. void OnAcquireState (object o, EventArgs args)
  140. {
  141. HttpApplication application = (HttpApplication) o;
  142. HttpContext context = application.Context;
  143. bool required = (context.Handler is IRequiresSessionState);
  144. // This is a hack. Sites that use Session in global.asax event handling code
  145. // are not supposed to get a Session object for static files, but seems that
  146. // IIS handles those files before getting there and thus they are served without
  147. // error.
  148. // As a workaround, setting MONO_XSP_STATIC_SESSION variable make this work
  149. // on mono, but you lose performance when serving static files.
  150. if (sessionForStaticFiles && context.Handler is StaticFileHandler)
  151. required = true;
  152. // hack end
  153. bool read_only = (context.Handler is IReadOnlySessionState);
  154. bool isNew = false;
  155. HttpSessionState session = null;
  156. if (handler != null)
  157. session = handler.UpdateContext (context, this, required, read_only, ref isNew);
  158. if (session != null) {
  159. if (isNew)
  160. session.SetNewSession (true);
  161. if (read_only)
  162. session = session.Clone ();
  163. context.SetSession (session);
  164. if (isNew && config.CookieLess) {
  165. string id = context.Session.SessionID;
  166. context.Request.SetHeader (HeaderName, id);
  167. context.Response.Redirect (UrlUtils.InsertSessionId (id,
  168. context.Request.FilePath));
  169. } else if (isNew) {
  170. string id = context.Session.SessionID;
  171. HttpCookie cookie = new HttpCookie (CookieName, id);
  172. cookie.Path = UrlUtils.GetDirectory (context.Request.ApplicationPath);
  173. context.Response.AppendCookie (cookie);
  174. }
  175. if (isNew)
  176. OnSessionStart ();
  177. }
  178. }
  179. void OnSessionStart ()
  180. {
  181. if (Start != null)
  182. Start (this, EventArgs.Empty);
  183. }
  184. internal void OnSessionRemoved (string key, object value, CacheItemRemovedReason reason)
  185. {
  186. // Only invoked for InProc (see msdn2 docs on SessionStateModule.End)
  187. if (GetConfig ().Mode == SessionStateMode.InProc)
  188. HttpApplicationFactory.InvokeSessionEnd (value);
  189. }
  190. public event EventHandler Start;
  191. // This event is public, but only Session_[On]End in global.asax will be invoked if present.
  192. public event EventHandler End;
  193. }
  194. }