* Mono.Posix.dll.sources: Rename Mono.Posix to Mono.Unix.
[mono.git] / mcs / class / System.Web / System.Web.SessionState / SessionSQLServerHandler.cs
1 //
2 // System.Web.SessionState.SessionSQLServerHandler
3 //
4 // Author(s):
5 //  Jackson Harper (jackson@ximian.com)
6 //
7 // (C) 2003 Novell, Inc. (http://www.novell.com), All rights reserved
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.IO;
33 using System.Data;
34 using System.Reflection;
35 using System.Configuration;
36 using System.Collections.Specialized;
37
38 namespace System.Web.SessionState {
39
40         internal class SessionSQLServerHandler : ISessionHandler
41         {
42                 const int DefTimeout = 600;
43
44                 private Type cncType = null;
45                 private IDbConnection cnc = null;
46                 private SessionConfig config;
47
48                 public void Dispose ()
49                 {
50                         if (cnc != null) {
51                                 cnc.Close ();
52                                 cnc = null;
53                         }
54                 }
55
56                 public void Init (SessionStateModule module, HttpApplication context, SessionConfig config)
57                 {
58                         string connectionTypeName;
59                         string providerAssemblyName;
60                         string cncString;
61
62                         this.config = config;
63
64                         GetConnectionData (out providerAssemblyName, out connectionTypeName, out cncString);
65                         if (cncType == null) {
66                                 Assembly dbAssembly = Assembly.Load (providerAssemblyName);
67                                 cncType = dbAssembly.GetType (connectionTypeName, true);
68                                 if (!typeof (IDbConnection).IsAssignableFrom (cncType))
69                                         throw new ApplicationException ("The type '" + cncType +
70                                                         "' does not implement IDB Connection.\n" +
71                                                         "Check 'DbConnectionType' in server.exe.config.");
72                         }
73
74                         cnc = (IDbConnection) Activator.CreateInstance (cncType);
75                         cnc.ConnectionString = cncString;
76                         try {
77                                 cnc.Open ();
78                         } catch (Exception exc) {
79                                 cnc = null;
80                                 throw exc;
81                         }
82                 }
83
84                 public void UpdateHandler (HttpContext context, SessionStateModule module)
85                 {
86                         if (context.Session == null || context.Session.IsReadOnly)
87                                 return;
88
89                         string id = context.Session.SessionID;
90                         SessionDictionary dict = context.Session.SessionDictionary;
91
92                         UpdateSession (id, dict);
93                 }
94
95                 public HttpSessionState UpdateContext (HttpContext context, SessionStateModule module,
96                                                         bool required, bool read_only, ref bool isNew)
97                 {
98                         if (!required)
99                                 return null;
100
101                         HttpSessionState session = null;
102                         string id = SessionId.Lookup (context.Request, config.CookieLess);
103
104                         if (id != null) {
105                                 session = SelectSession (id, read_only);
106                                 if (session != null)
107                                         return session;
108                         }
109
110                         id = SessionId.Create (module.Rng);
111                         session = new HttpSessionState (id, new SessionDictionary (),
112                                         HttpApplicationFactory.ApplicationState.SessionObjects, config.Timeout,
113                                         true, config.CookieLess, SessionStateMode.SQLServer, read_only);
114
115                         InsertSession (session, config.Timeout);
116                         isNew = true;
117                         return session;
118                 }
119
120                 private void GetConnectionData (out string providerAssembly,
121                                 out string cncTypeName, out string cncString)
122                 {
123                         providerAssembly = null;
124                         cncTypeName = null;
125                         cncString = null;
126
127                         NameValueCollection config = ConfigurationSettings.AppSettings as NameValueCollection;
128                         if (config != null) {
129                                 foreach (string s in config.Keys) {
130                                         if (0 == String.Compare ("StateDBProviderAssembly", s, true)) {
131                                                 providerAssembly = config [s];
132                                         } else if (0 == String.Compare ("StateDBConnectionType", s, true)) {
133                                                 cncTypeName = config [s];
134                                         }
135                                 }
136                         }
137
138                         cncString = this.config.SqlConnectionString;
139
140                         if (providerAssembly == null || providerAssembly == String.Empty)
141                                 providerAssembly = "Npgsql.dll";
142
143                         if (cncTypeName == null || cncTypeName == String.Empty)
144                                 cncTypeName = "Npgsql.NpgsqlConnection";
145
146                         if (cncString == null || cncString == String.Empty)
147                                 cncString = "SERVER=127.0.0.1;USER ID=monostate;PASSWORD=monostate;dbname=monostate";
148                 }
149
150                 private HttpSessionState SelectSession (string id, bool read_only)
151                 {
152                         HttpSessionState session = null;
153                         IDbCommand command = cnc.CreateCommand();
154                         IDataReader reader;
155
156                         string select = "SELECT * from aspstatetempsessions WHERE SessionID = :SessionID";
157
158                         command.CommandText = select;
159
160                         command.Parameters.Add (CreateParam (command, DbType.String, ":SessionID", id));
161
162                         try {
163                                 reader = command.ExecuteReader ();
164
165                                 if (!reader.Read ())
166                                         return null;
167
168                                 SessionDictionary dict; 
169                                 HttpStaticObjectsCollection sobjs;
170                                 
171                                 dict = SessionDictionary.FromByteArray (ReadBytes (reader, reader.FieldCount-1));
172                                 sobjs = HttpStaticObjectsCollection.FromByteArray (ReadBytes (reader, reader.FieldCount-2));
173                                 
174                                 session = new HttpSessionState (id, dict, sobjs, 100, false, config.CookieLess,
175                                                 SessionStateMode.SQLServer, read_only);
176                                 return session;
177                         } catch {
178                                 throw;
179                         }
180                 }
181
182                 private void InsertSession (HttpSessionState session, int timeout)
183                 {
184                         IDbCommand command = cnc.CreateCommand ();
185                         IDataParameterCollection param;
186
187                         string insert = "INSERT INTO ASPStateTempSessions VALUES " +
188                         "(:SessionID, :Created, :Expires, :Timeout, :StaticObjectsData, :SessionData)";
189
190                         command.CommandText = insert;
191
192                         param = command.Parameters;
193                         param.Add (CreateParam (command, DbType.String, ":SessionID", session.SessionID));
194                         param.Add (CreateParam (command, DbType.DateTime, ":Created", DateTime.Now));
195                         param.Add (CreateParam (command, DbType.DateTime, ":Expires", Tommorow ()));
196                         param.Add (CreateParam (command, DbType.Int32, ":Timeout", timeout));
197                         param.Add (CreateParam (command, DbType.Binary, ":StaticObjectsData",
198                                                    session.StaticObjects.ToByteArray ()));
199                         param.Add (CreateParam (command, DbType.Binary, ":SessionData",
200                                                    session.SessionDictionary.ToByteArray ()));
201
202                         command.ExecuteNonQuery ();
203                 }
204
205                 private void UpdateSession (string id, SessionDictionary dict)
206                 {
207                         IDbCommand command = cnc.CreateCommand ();
208                         IDataParameterCollection param;
209
210                         string update = "UPDATE ASPStateTempSessions SET " +
211                         "SessionData = :SessionData WHERE SessionId = :SessionID";
212
213                         command.CommandText = update;
214
215                         param = command.Parameters;
216                         param.Add (CreateParam (command, DbType.String, ":SessionID", id));
217                         param.Add (CreateParam (command, DbType.Binary, ":SessionData",
218                                                                 dict.ToByteArray ()));
219
220                         command.ExecuteNonQuery ();
221                 }
222
223                 private IDataParameter CreateParam (IDbCommand command, DbType type,
224                                 string name, object value)
225                 {
226                         IDataParameter result = command.CreateParameter ();
227                         result.DbType = type;
228                         result.ParameterName = name;
229                         result.Value = value;
230                         return result;
231                 }
232
233                 private DateTime Tommorow ()
234                 {
235                         return DateTime.Now.AddDays (1);
236                 }
237
238                 private byte [] ReadBytes (IDataReader reader, int index)
239                 {
240                         int len = (int) reader.GetBytes (reader.FieldCount-1, 0, null, 0, 0);
241                         byte [] data = new byte [len];
242                         reader.GetBytes (index, 0, data, 0, len);
243                         return data;
244                 }
245         }
246 }
247