SessionStateModule.cs 7.0 KB

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