TARGET_J2EE ifdef reorder for readability
[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 // (C) 2002,2003,2004,2005 Novell, Inc (http://www.novell.com)
10 // (C) 2003 Stefan Görling (http://www.gorling.se)
11
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 using System.Web;
34 using System.Web.Caching;
35 using System.Web.Util;
36 using System.Security.Cryptography;
37
38 namespace System.Web.SessionState
39 {
40         public sealed class SessionStateModule : IHttpModule
41         {
42                 internal static readonly string CookieName = "ASPSESSION";
43                 internal static readonly string HeaderName = "AspFilterSessionId";
44                 
45 #if TARGET_J2EE         
46                 static private SessionConfig config {
47                         get {
48                                 return (SessionConfig)AppDomain.CurrentDomain.GetData("SessionStateModule.config");
49                         }
50                         set {
51                                 AppDomain.CurrentDomain.SetData("SessionStateModule.config", value);
52                         }
53                 }
54                 static private Type handlerType {
55                         get {
56                                 return (Type)AppDomain.CurrentDomain.GetData("SessionStateModule.handlerType");
57                         }
58                         set {
59                                 AppDomain.CurrentDomain.SetData("SessionStateModule.handlerType", value);
60                         }
61                 }
62 #else
63                 static SessionConfig config;
64                 static Type handlerType;
65 #endif          
66                 ISessionHandler handler;
67                 bool sessionForStaticFiles;
68                 
69                 static RandomNumberGenerator rng = new RNGCryptoServiceProvider ();
70                 
71                 public SessionStateModule ()
72                 {
73                 }
74
75                 internal RandomNumberGenerator Rng {
76                         get { return rng; }
77                 }
78
79                 public void Dispose ()
80                 {
81                     if (handler!=null)
82                         handler.Dispose();
83                 }
84
85                 public void Init (HttpApplication app)
86                 {
87                         sessionForStaticFiles = (Environment.GetEnvironmentVariable ("MONO_XSP_STATIC_SESSION") != null);
88                         if (config == null) {
89                                 config = (SessionConfig) HttpContext.GetAppConfig ("system.web/sessionState");
90                                 if (config ==  null)
91                                         config = new SessionConfig (null);
92
93 #if TARGET_J2EE
94                                 if (config.Mode == SessionStateMode.SQLServer || config.Mode == SessionStateMode.StateServer)
95                                         throw new NotImplementedException("You must use web.xml to specify session state handling");
96 #else
97                                 if (config.Mode == SessionStateMode.StateServer)
98                                         handlerType = typeof (SessionStateServerHandler);
99
100                                 if (config.Mode == SessionStateMode.SQLServer)
101                                         handlerType = typeof (SessionSQLServerHandler);
102 #endif
103                                 
104                                 if (config.Mode == SessionStateMode.InProc)
105                                         handlerType = typeof (SessionInProcHandler);
106
107                                 if (config.Mode == SessionStateMode.Off)
108                                         return;
109                         }
110
111                         if (config.CookieLess)
112                                 app.BeginRequest += new EventHandler (OnBeginRequest);
113                         
114                         app.AddOnAcquireRequestStateAsync (
115                                 new BeginEventHandler (OnBeginAcquireState),
116                                 new EndEventHandler (OnEndAcquireState));
117
118                         app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
119                         app.EndRequest += new EventHandler (OnEndRequest);
120                         
121                         if (handlerType != null && handler == null) {
122                                 handler = (ISessionHandler) Activator.CreateInstance (handlerType);
123                                 handler.Init (this, app, config); //initialize
124                         }
125                 }
126
127                 void OnBeginRequest (object o, EventArgs args)
128                 {
129                         HttpApplication application = (HttpApplication) o;
130                         HttpContext context = application.Context;
131                         string base_path = context.Request.BaseVirtualDir;
132                         string id = UrlUtils.GetSessionId (base_path);
133
134                         if (id == null)
135                                 return;
136                         
137                         context.Request.SetCurrentExePath (UrlUtils.RemoveSessionId (base_path,
138                                                                      context.Request.FilePath));
139                         context.Request.SetHeader (HeaderName, id);
140                         context.Response.SetAppPathModifier (String.Format ("({0})", id));
141                 }
142                 
143                 void OnReleaseRequestState (object o, EventArgs args)
144                 {
145                         if (handler == null)
146                                 return;
147
148                         HttpApplication application = (HttpApplication) o;
149                         HttpContext context = application.Context;
150                         handler.UpdateHandler (context, this);
151                 }
152
153                 void OnEndRequest (object o, EventArgs args)
154                 {
155                 }
156
157                 IAsyncResult OnBeginAcquireState (object o, EventArgs args, AsyncCallback cb, object data)
158                 {
159                         HttpApplication application = (HttpApplication) o;
160                         HttpContext context = application.Context;
161
162                         bool required = (context.Handler is IRequiresSessionState);
163                         
164                         // This is a hack. Sites that use Session in global.asax event handling code
165                         // are not supposed to get a Session object for static files, but seems that
166                         // IIS handles those files before getting there and thus they are served without
167                         // error.
168                         // As a workaround, setting MONO_XSP_STATIC_SESSION variable make this work
169                         // on mono, but you lose performance when serving static files.
170                         if (sessionForStaticFiles && context.Handler is StaticFileHandler)
171                                 required = true;
172                         // hack end
173
174                         bool read_only = (context.Handler is IReadOnlySessionState);
175                         
176                         bool isNew = false;
177                         HttpSessionState session = null;
178                         if (handler != null)
179                                 session = handler.UpdateContext (context, this, required, read_only, ref isNew);
180
181                         if (session != null) {
182                                 if (isNew)
183                                         session.SetNewSession (true);
184
185                                 if (read_only)
186                                         session = session.Clone ();
187                                         
188                                 context.SetSession (session);
189
190                                 if (isNew && config.CookieLess) {
191                                         string id = context.Session.SessionID;
192                                         context.Request.SetHeader (HeaderName, id);
193                                         context.Response.Redirect (UrlUtils.InsertSessionId (id,
194                                                                    context.Request.FilePath));
195                                 } else if (isNew) {
196                                         string id = context.Session.SessionID;
197                                         HttpCookie cookie = new HttpCookie (CookieName, id);
198                                         cookie.Path = UrlUtils.GetDirectory (context.Request.ApplicationPath);
199                                         context.Response.AppendCookie (cookie);
200                                 }
201                         }
202                         
203                         // In the future, we might want to move the Async stuff down to
204                         // the interface level, if we're going to support other than
205                         // InProc, we might actually want to do things async, now we
206                         // simply fake it.
207                         HttpAsyncResult result=new HttpAsyncResult (cb,this);
208                         result.Complete (true, o, null);
209                         if (isNew && Start != null)
210                                 Start (this, args);
211
212                         return result;
213                 }
214
215                 void OnEndAcquireState (IAsyncResult result) { }
216
217                 internal void OnSessionRemoved (string key, object value, CacheItemRemovedReason reason)
218                 {
219                         OnEnd ();
220                 }
221
222                 internal void OnEnd ()
223                 {
224 #if !TARGET_J2EE                        
225                         if (End != null)
226                                 End (this, EventArgs.Empty);
227 #endif                          
228                 }
229                 
230                 public event EventHandler Start;
231                 public event EventHandler End;
232         }
233 }
234