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