2 // System.Web.SessionState.SesionStateModule
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Stefan Görling (stefan@gorling.se)
7 // Jackson Harper (jackson@ximian.com)
9 // (C) 2002,2003,2004,2005 Novell, Inc (http://www.novell.com)
10 // (C) 2003 Stefan Görling (http://www.gorling.se)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
34 using System.Web.Caching;
35 using System.Web.Util;
36 using System.Security.Cryptography;
38 namespace System.Web.SessionState
40 public sealed class SessionStateModule : IHttpModule
42 internal static readonly string CookieName = "ASPSESSION";
43 internal static readonly string HeaderName = "AspFilterSessionId";
44 static object locker = new object ();
47 static private SessionConfig config {
49 return (SessionConfig)AppDomain.CurrentDomain.GetData("SessionStateModule.config");
52 AppDomain.CurrentDomain.SetData("SessionStateModule.config", value);
55 static private Type handlerType {
57 return (Type)AppDomain.CurrentDomain.GetData("SessionStateModule.handlerType");
60 AppDomain.CurrentDomain.SetData("SessionStateModule.handlerType", value);
64 static SessionConfig config;
65 static Type handlerType;
67 ISessionHandler handler;
68 bool sessionForStaticFiles;
70 static RandomNumberGenerator rng = RandomNumberGenerator.Create ();
72 public SessionStateModule ()
76 internal RandomNumberGenerator Rng {
80 public void Dispose ()
86 SessionConfig GetConfig ()
92 config = (SessionConfig) HttpContext.GetAppConfig ("system.web/sessionState");
94 config = new SessionConfig (null);
97 if (config.Mode == SessionStateMode.SQLServer || config.Mode == SessionStateMode.StateServer)
98 throw new NotImplementedException("You must use web.xml to specify session state handling");
100 if (config.Mode == SessionStateMode.StateServer)
101 handlerType = typeof (SessionStateServerHandler);
103 if (config.Mode == SessionStateMode.SQLServer)
104 handlerType = typeof (SessionSQLServerHandler);
106 if (config.Mode == SessionStateMode.InProc)
107 handlerType = typeof (SessionInProcHandler);
113 public void Init (HttpApplication app)
115 sessionForStaticFiles = (Environment.GetEnvironmentVariable ("MONO_XSP_STATIC_SESSION") != null);
116 SessionConfig cfg = GetConfig ();
117 if (handlerType == null)
121 app.BeginRequest += new EventHandler (OnBeginRequest);
123 app.AcquireRequestState += new EventHandler (OnAcquireState);
124 app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
125 app.EndRequest += new EventHandler (OnEndRequest);
127 if (handlerType != null && handler == null) {
128 handler = (ISessionHandler) Activator.CreateInstance (handlerType);
129 handler.Init (this, app, cfg); //initialize
133 void OnBeginRequest (object o, EventArgs args)
135 HttpApplication application = (HttpApplication) o;
136 HttpContext context = application.Context;
137 string base_path = context.Request.BaseVirtualDir;
138 string id = UrlUtils.GetSessionId (base_path);
143 context.Request.SetCurrentExePath (UrlUtils.RemoveSessionId (base_path,
144 context.Request.FilePath));
145 context.Request.SetHeader (HeaderName, id);
146 context.Response.SetAppPathModifier (String.Format ("({0})", id));
149 void OnReleaseRequestState (object o, EventArgs args)
154 HttpApplication application = (HttpApplication) o;
155 HttpContext context = application.Context;
156 handler.UpdateHandler (context, this);
159 void OnEndRequest (object o, EventArgs args)
163 void OnAcquireState (object o, EventArgs args)
165 HttpApplication application = (HttpApplication) o;
166 HttpContext context = application.Context;
168 bool required = (context.Handler is IRequiresSessionState);
170 // This is a hack. Sites that use Session in global.asax event handling code
171 // are not supposed to get a Session object for static files, but seems that
172 // IIS handles those files before getting there and thus they are served without
174 // As a workaround, setting MONO_XSP_STATIC_SESSION variable make this work
175 // on mono, but you lose performance when serving static files.
176 if (sessionForStaticFiles && context.Handler is StaticFileHandler)
180 bool read_only = (context.Handler is IReadOnlySessionState);
183 HttpSessionState session = null;
185 session = handler.UpdateContext (context, this, required, read_only, ref isNew);
187 if (session != null) {
189 session.SetNewSession (true);
192 session = session.Clone ();
194 context.SetSession (session);
196 HttpRequest request = context.Request;
197 HttpResponse response = context.Response;
198 string id = context.Session.SessionID;
199 if (isNew && config.CookieLess) {
200 request.SetHeader (HeaderName, id);
201 response.Redirect (UrlUtils.InsertSessionId (id, request.FilePath));
203 HttpCookie cookie = new HttpCookie (CookieName, id);
204 cookie.Path = request.ApplicationPath;
205 context.Response.AppendCookie (cookie);
213 void OnSessionStart ()
216 Start (this, EventArgs.Empty);
219 internal void OnSessionRemoved (string key, object value, CacheItemRemovedReason reason)
221 // Only invoked for InProc (see msdn2 docs on SessionStateModule.End)
222 if (GetConfig ().Mode == SessionStateMode.InProc)
223 HttpApplicationFactory.InvokeSessionEnd (value);
226 public event EventHandler Start;
228 // This event is public, but only Session_[On]End in global.asax will be invoked if present.
229 public event EventHandler End;