2006-02-01 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.SessionState / SessionStateModule.cs
1 //
2 // System.Web.SessionState.SesionStateModule
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //      Stefan Görling (stefan@gorling.se)
7 //      Jackson Harper (jackson@ximian.com)
8 //
9 // Copyright (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
32
33 using System.Web.Configuration;
34 using System.Web.Caching;
35 using System.Web.Util;
36 using System.Security.Cryptography;
37 using System.Security.Permissions;
38
39 namespace System.Web.SessionState
40 {
41         // CAS - no InheritanceDemand here as the class is sealed
42         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         public sealed class SessionStateModule : IHttpModule
44         {
45                 internal static readonly string CookieName = "ASPSESSION";
46                 internal static readonly string HeaderName = "AspFilterSessionId";
47                 static object locker = new object ();
48                 
49 #if TARGET_J2EE         
50                 static private SessionConfig config {
51                         get {
52                                 return (SessionConfig)AppDomain.CurrentDomain.GetData("SessionStateModule.config");
53                         }
54                         set {
55                                 AppDomain.CurrentDomain.SetData("SessionStateModule.config", value);
56                         }
57                 }
58                 static private Type handlerType {
59                         get {
60                                 return (Type)AppDomain.CurrentDomain.GetData("SessionStateModule.handlerType");
61                         }
62                         set {
63                                 AppDomain.CurrentDomain.SetData("SessionStateModule.handlerType", value);
64                         }
65                 }
66 #else
67 #if NET_2_0
68                 static SessionStateSection config;
69 #else
70                 static SessionConfig config;
71 #endif
72                 static Type handlerType;
73 #endif          
74                 ISessionHandler handler;
75                 bool sessionForStaticFiles;
76                 
77                 static RandomNumberGenerator rng = RandomNumberGenerator.Create ();
78                 
79                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
80                 public SessionStateModule ()
81                 {
82                 }
83
84                 internal RandomNumberGenerator Rng {
85                         get { return rng; }
86                 }
87
88                 public void Dispose ()
89                 {
90                     if (handler!=null)
91                         handler.Dispose();
92                 }
93
94 #if NET_2_0
95                 SessionStateSection GetConfig ()
96 #else
97                 SessionConfig GetConfig ()
98 #endif
99                 {
100                         lock (locker) {
101                                 if (config != null)
102                                         return config;
103
104 #if NET_2_0
105                                 config = (SessionStateSection) WebConfigurationManager.GetSection ("system.web/sessionState");
106 #else
107                                 config = (SessionConfig) HttpContext.GetAppConfig ("system.web/sessionState");
108                                 if (config ==  null)
109                                         config = new SessionConfig (null);
110 #endif
111
112 #if TARGET_J2EE
113                                 if (config.Mode == SessionStateMode.SQLServer || config.Mode == SessionStateMode.StateServer)
114                                         throw new NotImplementedException("You must use web.xml to specify session state handling");
115 #else
116                                 if (config.Mode == SessionStateMode.StateServer)
117                                         handlerType = typeof (SessionStateServerHandler);
118
119                                 if (config.Mode == SessionStateMode.SQLServer)
120                                         handlerType = typeof (SessionSQLServerHandler);
121 #endif
122                                 if (config.Mode == SessionStateMode.InProc)
123                                         handlerType = typeof (SessionInProcHandler);
124
125                                 return config;
126                         }
127                 }
128
129                 [EnvironmentPermission (SecurityAction.Assert, Read = "MONO_XSP_STATIC_SESSION")]
130                 public void Init (HttpApplication app)
131                 {
132                         sessionForStaticFiles = (Environment.GetEnvironmentVariable ("MONO_XSP_STATIC_SESSION") != null);
133 #if NET_2_0
134                         SessionStateSection cfg = GetConfig ();
135 #else
136                         SessionConfig cfg = GetConfig ();
137 #endif
138                         if (handlerType == null)
139                                 return;
140
141                         if (cfg.CookieLess)
142                                 app.BeginRequest += new EventHandler (OnBeginRequest);
143
144                         app.AcquireRequestState += new EventHandler (OnAcquireState);
145                         app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
146                         app.EndRequest += new EventHandler (OnEndRequest);
147                         
148                         if (handlerType != null && handler == null) {
149                                 handler = (ISessionHandler) Activator.CreateInstance (handlerType);
150                                 handler.Init (this, app, cfg); //initialize
151                         }
152                 }
153
154                 void OnBeginRequest (object o, EventArgs args)
155                 {
156                         HttpApplication application = (HttpApplication) o;
157                         HttpContext context = application.Context;
158                         string base_path = context.Request.BaseVirtualDir;
159                         string id = UrlUtils.GetSessionId (base_path);
160
161                         if (id == null)
162                                 return;
163                         
164                         string new_path = UrlUtils.RemoveSessionId (base_path, context.Request.FilePath);
165                         context.Request.SetFilePath (new_path);
166                         context.Request.SetHeader (HeaderName, id);
167                         context.Response.SetAppPathModifier (String.Concat ("(", id, ")"));
168                 }
169                 
170                 void OnReleaseRequestState (object o, EventArgs args)
171                 {
172                         if (handler == null)
173                                 return;
174
175                         HttpApplication application = (HttpApplication) o;
176                         HttpContext context = application.Context;
177                         handler.UpdateHandler (context, this);
178                 }
179
180                 void OnEndRequest (object o, EventArgs args)
181                 {
182                 }
183
184                 void OnAcquireState (object o, EventArgs args)
185                 {
186                         HttpApplication application = (HttpApplication) o;
187                         HttpContext context = application.Context;
188
189                         bool required = (context.Handler is IRequiresSessionState);
190                         
191                         // This is a hack. Sites that use Session in global.asax event handling code
192                         // are not supposed to get a Session object for static files, but seems that
193                         // IIS handles those files before getting there and thus they are served without
194                         // error.
195                         // As a workaround, setting MONO_XSP_STATIC_SESSION variable make this work
196                         // on mono, but you lose performance when serving static files.
197                         if (sessionForStaticFiles && context.Handler is StaticFileHandler)
198                                 required = true;
199                         // hack end
200
201                         bool read_only = (context.Handler is IReadOnlySessionState);
202                         
203                         bool isNew = false;
204                         HttpSessionState session = null;
205                         if (handler != null)
206                                 session = handler.UpdateContext (context, this, required, read_only, ref isNew);
207
208                         if (session != null) {
209                                 if (isNew)
210                                         session.SetNewSession (true);
211
212                                 if (read_only)
213                                         session = session.Clone ();
214                                         
215                                 context.SetSession (session);
216
217                                 HttpRequest request = context.Request;
218                                 HttpResponse response = context.Response;
219                                 string id = context.Session.SessionID;
220                                 if (isNew && config.CookieLess) {
221                                         request.SetHeader (HeaderName, id);
222                                         response.Redirect (UrlUtils.InsertSessionId (id, request.FilePath));
223                                 } else if (isNew) {
224                                         HttpCookie cookie = new HttpCookie (CookieName, id);
225                                         cookie.Path = request.ApplicationPath;
226                                         context.Response.AppendCookie (cookie);
227                                 }
228
229                                 if (isNew)
230                                         OnSessionStart ();
231                         }
232                 }
233
234                 void OnSessionStart ()
235                 {
236                         if (Start != null)
237                                 Start (this, EventArgs.Empty);
238                 }
239
240                 internal void OnSessionRemoved (string key, object value, CacheItemRemovedReason reason)
241                 {
242 #if NET_2_0
243                         SessionStateSection cfg = GetConfig ();
244 #else
245                         SessionConfig cfg = GetConfig ();
246 #endif
247
248                         // Only invoked for InProc (see msdn2 docs on SessionStateModule.End)
249                         if (cfg.Mode == SessionStateMode.InProc)
250                                 HttpApplicationFactory.InvokeSessionEnd (value);
251                 }
252                 
253                 public event EventHandler Start;
254
255                 // This event is public, but only Session_[On]End in global.asax will be invoked if present.
256                 public event EventHandler End;
257         }
258 }
259