2 // System.Web.SessionState.SessionInProcHandler
5 // Marek Habersack <grendello@gmail.com>
7 // (C) 2006 Marek Habersack
8 // (C) 2010 Novell, Inc (http://novell.com/)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Configuration.Provider;
37 using System.Web.Caching;
38 using System.Web.Configuration;
39 using System.Threading;
41 namespace System.Web.SessionState
43 internal sealed class InProcSessionItem
46 public bool cookieless;
47 public ISessionStateItemCollection items;
48 public DateTime lockedTime;
49 public DateTime expiresAt;
50 public ReaderWriterLockSlim rwlock;
53 public bool resettingTimeout;
54 public HttpStaticObjectsCollection staticItems;
56 internal InProcSessionItem ()
59 this.cookieless = false;
61 this.staticItems = null;
62 this.lockedTime = DateTime.MinValue;
63 this.expiresAt = DateTime.MinValue;
64 this.rwlock = new ReaderWriterLockSlim ();
65 this.lockId = Int32.MinValue;
67 this.resettingTimeout = false;
70 public void Dispose ()
86 internal class SessionInProcHandler : SessionStateStoreProviderBase
88 const string CachePrefix = "@@@InProc@";
89 const int CachePrefixLength = 10;
91 const Int32 lockAcquireTimeout = 30000;
93 CacheItemRemovedCallback removedCB;
94 //NameValueCollection privateConfig;
95 SessionStateItemExpireCallback expireCallback;
96 HttpStaticObjectsCollection staticObjects;
98 public override SessionStateStoreData CreateNewStoreData (HttpContext context, int timeout)
100 return new SessionStateStoreData (new SessionStateItemCollection (),
101 staticObjects, timeout);
104 void InsertSessionItem (InProcSessionItem item, int timeout, string id)
106 if (item == null || String.IsNullOrEmpty (id))
109 HttpRuntime.InternalCache.Insert (id,
112 Cache.NoAbsoluteExpiration,
113 TimeSpan.FromMinutes (timeout),
114 CacheItemPriority.AboveNormal,
118 public override void CreateUninitializedItem (HttpContext context, string id, int timeout)
120 EnsureGoodId (id, true);
121 InProcSessionItem item = new InProcSessionItem ();
122 item.expiresAt = DateTime.UtcNow.AddMinutes (timeout);
123 item.timeout = timeout;
124 InsertSessionItem (item, timeout, CachePrefix + id);
127 public override void Dispose ()
131 public override void EndRequest (HttpContext context)
133 if (staticObjects != null) {
134 staticObjects.GetObjects ().Clear ();
135 staticObjects = null;
139 SessionStateStoreData GetItemInternal (HttpContext context,
142 out TimeSpan lockAge,
144 out SessionStateActions actions,
148 lockAge = TimeSpan.MinValue;
149 lockId = Int32.MinValue;
150 actions = SessionStateActions.None;
155 Cache cache = HttpRuntime.InternalCache;
156 string CacheId = CachePrefix + id;
157 InProcSessionItem item = cache [CacheId] as InProcSessionItem;
162 bool readLocked = false, writeLocked = false;
164 if (item.rwlock.TryEnterUpgradeableReadLock (lockAcquireTimeout))
167 throw new ApplicationException ("Failed to acquire lock");
171 lockAge = DateTime.UtcNow.Subtract (item.lockedTime);
172 lockId = item.lockId;
177 if (item.rwlock.TryEnterWriteLock (lockAcquireTimeout))
180 throw new ApplicationException ("Failed to acquire lock");
182 item.lockedTime = DateTime.UtcNow;
184 lockId = item.lockId;
186 if (item.items == null) {
187 actions = SessionStateActions.InitializeItem;
188 item.items = new SessionStateItemCollection ();
190 if (item.staticItems == null)
191 item.staticItems = staticObjects;
193 return new SessionStateStoreData (item.items,
197 // we want such errors to be passed to the application.
201 item.rwlock.ExitWriteLock ();
203 item.rwlock.ExitUpgradeableReadLock ();
207 public override SessionStateStoreData GetItem (HttpContext context,
210 out TimeSpan lockAge,
212 out SessionStateActions actions)
214 EnsureGoodId (id, false);
215 return GetItemInternal (context, id, out locked, out lockAge, out lockId, out actions, false);
218 public override SessionStateStoreData GetItemExclusive (HttpContext context,
221 out TimeSpan lockAge,
223 out SessionStateActions actions)
225 EnsureGoodId (id, false);
226 return GetItemInternal (context, id, out locked, out lockAge, out lockId, out actions, true);
229 public override void Initialize (string name, NameValueCollection config)
231 if (String.IsNullOrEmpty (name))
232 name = "Session InProc handler";
233 removedCB = new CacheItemRemovedCallback (OnSessionRemoved);
234 //privateConfig = config;
235 base.Initialize (name, config);
238 public override void InitializeRequest (HttpContext context)
240 staticObjects = HttpApplicationFactory.ApplicationState.SessionObjects.Clone ();
243 public override void ReleaseItemExclusive (HttpContext context,
247 EnsureGoodId (id, true);
248 string CacheId = CachePrefix + id;
249 InProcSessionItem item = HttpRuntime.InternalCache [CacheId] as InProcSessionItem;
251 if (item == null || lockId == null || lockId.GetType() != typeof(Int32) || item.lockId != (Int32)lockId)
256 if (item.rwlock.TryEnterWriteLock (lockAcquireTimeout))
259 throw new ApplicationException ("Failed to acquire lock");
265 item.rwlock.ExitWriteLock ();
269 public override void RemoveItem (HttpContext context,
272 SessionStateStoreData item)
274 EnsureGoodId (id, true);
275 string CacheId = CachePrefix + id;
276 Cache cache = HttpRuntime.InternalCache;
277 InProcSessionItem inProcItem = cache [CacheId] as InProcSessionItem;
279 if (inProcItem == null || lockId == null || lockId.GetType() != typeof(Int32) || inProcItem.lockId != (Int32)lockId)
284 if (inProcItem.rwlock.TryEnterWriteLock (lockAcquireTimeout))
287 throw new ApplicationException ("Failed to acquire lock after");
288 cache.Remove (CacheId);
293 inProcItem.rwlock.ExitWriteLock ();
297 public override void ResetItemTimeout (HttpContext context, string id)
299 EnsureGoodId (id, true);
300 string CacheId = CachePrefix + id;
301 Cache cache = HttpRuntime.InternalCache;
302 InProcSessionItem item = cache [CacheId] as InProcSessionItem;
309 if (item.rwlock.TryEnterWriteLock (lockAcquireTimeout))
312 throw new ApplicationException ("Failed to acquire lock after");
313 item.resettingTimeout = true;
314 cache.Remove (CacheId);
315 InsertSessionItem (item, item.timeout, CacheId);
320 item.rwlock.ExitWriteLock ();
324 /* In certain situations the 'item' parameter passed to SetAndReleaseItemExclusive
325 may be null. The issue was reported in bug #333898, but the reporter cannot
326 provide a test case that triggers the issue. Added work around the problem
327 in the way that should have the least impact on the rest of the code. If 'item'
328 is null, then the new session item is created without the items and staticItems
329 collections - they will be initialized to defaults when retrieving the session
330 item. This is not a correct fix, but since there is no test case this is the best
331 what can be done right now.
333 public override void SetAndReleaseItemExclusive (HttpContext context,
335 SessionStateStoreData item,
339 EnsureGoodId (id, true);
340 string CacheId = CachePrefix + id;
341 Cache cache = HttpRuntime.InternalCache;
342 InProcSessionItem inProcItem = cache [CacheId] as InProcSessionItem;
343 ISessionStateItemCollection itemItems = null;
344 int itemTimeout = 20;
345 HttpStaticObjectsCollection itemStaticItems = null;
348 itemItems = item.Items;
349 itemTimeout = item.Timeout;
350 itemStaticItems = item.StaticObjects;
353 if (newItem || inProcItem == null) {
354 inProcItem = new InProcSessionItem ();
355 inProcItem.timeout = itemTimeout;
356 inProcItem.expiresAt = DateTime.UtcNow.AddMinutes (itemTimeout);
357 if (lockId.GetType() == typeof(Int32))
358 inProcItem.lockId = (Int32)lockId;
360 if (lockId == null || lockId.GetType() != typeof(Int32) || inProcItem.lockId != (Int32)lockId)
362 inProcItem.resettingTimeout = true;
363 cache.Remove (CacheId);
368 if (inProcItem.rwlock.TryEnterWriteLock (lockAcquireTimeout))
371 throw new ApplicationException ("Failed to acquire lock");
372 inProcItem.locked = false;
373 inProcItem.items = itemItems;
374 inProcItem.staticItems = itemStaticItems;
375 InsertSessionItem (inProcItem, itemTimeout, CacheId);
380 inProcItem.rwlock.ExitWriteLock ();
384 public override bool SetItemExpireCallback (SessionStateItemExpireCallback expireCallback)
386 this.expireCallback = expireCallback;
390 void EnsureGoodId (string id, bool throwOnNull)
394 throw new HttpException ("Session ID is invalid");
398 if (id.Length > SessionIDManager.SessionIDMaxLength)
399 throw new HttpException ("Session ID too long");
402 void OnSessionRemoved (string key, object value, CacheItemRemovedReason reason)
404 if (expireCallback != null) {
405 if (key.StartsWith (CachePrefix, StringComparison.OrdinalIgnoreCase))
406 key = key.Substring (CachePrefixLength);
408 if (value is SessionStateStoreData)
409 expireCallback (key, (SessionStateStoreData)value);
410 else if (value is InProcSessionItem) {
411 InProcSessionItem item = (InProcSessionItem)value;
412 if (item.resettingTimeout) {
413 item.resettingTimeout = false;
418 new SessionStateStoreData (
424 expireCallback (key, null);
425 } else if (value is InProcSessionItem)
426 ((InProcSessionItem)value).Dispose ();