SessionInProcHandler.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //
  2. // System.Web.SessionState.SessionInProcHandler
  3. //
  4. // Authors:
  5. // Marek Habersack <[email protected]
  6. //
  7. // (C) 2006 Marek Habersack
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.IO;
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Configuration.Provider;
  35. using System.Web.Caching;
  36. using System.Web.Configuration;
  37. using System.Threading;
  38. namespace System.Web.SessionState
  39. {
  40. internal sealed class InProcSessionItem
  41. {
  42. public bool locked;
  43. public bool cookieless;
  44. public ISessionStateItemCollection items;
  45. public DateTime lockedTime;
  46. public DateTime expiresAt;
  47. public ReaderWriterLock rwlock;
  48. public Int32 lockId;
  49. public int timeout;
  50. internal InProcSessionItem ()
  51. {
  52. this.locked = false;
  53. this.cookieless = false;
  54. this.items = null;
  55. this.lockedTime = DateTime.MinValue;
  56. this.expiresAt = DateTime.MinValue;
  57. this.rwlock = new ReaderWriterLock ();
  58. this.lockId = Int32.MinValue;
  59. this.timeout = 0;
  60. }
  61. }
  62. internal class SessionInProcHandler : SessionStateStoreProviderBase
  63. {
  64. private const string CachePrefix = "@@@InProc@";
  65. private const Int32 lockAcquireTimeout = 30000;
  66. CacheItemRemovedCallback removedCB;
  67. NameValueCollection privateConfig;
  68. SessionStateItemExpireCallback expireCallback;
  69. public override SessionStateStoreData CreateNewStoreData (HttpContext context, int timeout)
  70. {
  71. return new SessionStateStoreData (new SessionStateItemCollection (),
  72. SessionStateUtility.GetSessionStaticObjects(context),
  73. timeout);
  74. }
  75. void InsertSessionItem (InProcSessionItem item, int timeout, string id)
  76. {
  77. HttpRuntime.Cache.InsertPrivate (id,
  78. item,
  79. null,
  80. Cache.NoAbsoluteExpiration,
  81. TimeSpan.FromMinutes (timeout),
  82. CacheItemPriority.AboveNormal,
  83. removedCB);
  84. }
  85. public override void CreateUninitializedItem (HttpContext context, string id, int timeout)
  86. {
  87. EnsureGoodId (id, true);
  88. InProcSessionItem item = new InProcSessionItem ();
  89. item.expiresAt = DateTime.UtcNow.AddMinutes (timeout);
  90. item.timeout = timeout;
  91. InsertSessionItem (item, timeout, CachePrefix + id);
  92. }
  93. public override void Dispose ()
  94. {
  95. }
  96. public override void EndRequest (HttpContext context)
  97. {
  98. }
  99. SessionStateStoreData GetItemInternal (HttpContext context,
  100. string id,
  101. out bool locked,
  102. out TimeSpan lockAge,
  103. out object lockId,
  104. out SessionStateActions actions,
  105. bool exclusive)
  106. {
  107. locked = false;
  108. lockAge = TimeSpan.MinValue;
  109. lockId = Int32.MinValue;
  110. actions = SessionStateActions.None;
  111. if (id == null)
  112. return null;
  113. Cache cache = HttpRuntime.Cache;
  114. string CacheId = CachePrefix + id;
  115. InProcSessionItem item = cache [CacheId] as InProcSessionItem;
  116. if (item == null)
  117. return null;
  118. try {
  119. item.rwlock.AcquireReaderLock (lockAcquireTimeout);
  120. if (item.locked) {
  121. locked = true;
  122. lockAge = DateTime.UtcNow.Subtract (item.lockedTime);
  123. lockId = item.lockId;
  124. return null;
  125. }
  126. item.rwlock.ReleaseReaderLock ();
  127. if (exclusive) {
  128. item.rwlock.AcquireWriterLock (lockAcquireTimeout);
  129. item.locked = true;
  130. item.lockedTime = DateTime.UtcNow;
  131. item.lockId++;
  132. lockId = item.lockId;
  133. }
  134. if (item.items == null) {
  135. actions = SessionStateActions.InitializeItem;
  136. item.items = new SessionStateItemCollection ();
  137. }
  138. return new SessionStateStoreData (item.items,
  139. SessionStateUtility.GetSessionStaticObjects(context),
  140. item.timeout);
  141. } catch {
  142. // we want such errors to be passed to the application.
  143. throw;
  144. } finally {
  145. if (item.rwlock.IsReaderLockHeld)
  146. item.rwlock.ReleaseReaderLock ();
  147. if (item.rwlock.IsWriterLockHeld)
  148. item.rwlock.ReleaseWriterLock ();
  149. }
  150. }
  151. public override SessionStateStoreData GetItem (HttpContext context,
  152. string id,
  153. out bool locked,
  154. out TimeSpan lockAge,
  155. out object lockId,
  156. out SessionStateActions actions)
  157. {
  158. EnsureGoodId (id, false);
  159. return GetItemInternal (context, id, out locked, out lockAge, out lockId, out actions, false);
  160. }
  161. public override SessionStateStoreData GetItemExclusive (HttpContext context,
  162. string id,
  163. out bool locked,
  164. out TimeSpan lockAge,
  165. out object lockId,
  166. out SessionStateActions actions)
  167. {
  168. EnsureGoodId (id, false);
  169. return GetItemInternal (context, id, out locked, out lockAge, out lockId, out actions, true);
  170. }
  171. public override void Initialize (string name, NameValueCollection config)
  172. {
  173. if (String.IsNullOrEmpty (name))
  174. name = "Session InProc handler";
  175. removedCB = new CacheItemRemovedCallback (OnSessionRemoved);
  176. privateConfig = config;
  177. base.Initialize (name, config);
  178. }
  179. public override void InitializeRequest (HttpContext context)
  180. {
  181. // nothing to do here
  182. }
  183. public override void ReleaseItemExclusive (HttpContext context,
  184. string id,
  185. object lockId)
  186. {
  187. EnsureGoodId (id, true);
  188. string CacheId = CachePrefix + id;
  189. InProcSessionItem item = HttpRuntime.Cache [CacheId] as InProcSessionItem;
  190. if (item == null || lockId == null || lockId.GetType() != typeof(Int32) || item.lockId != (Int32)lockId)
  191. return;
  192. try {
  193. item.rwlock.AcquireWriterLock (lockAcquireTimeout);
  194. item.locked = false;
  195. } catch {
  196. throw;
  197. } finally {
  198. if (item.rwlock.IsWriterLockHeld)
  199. item.rwlock.ReleaseWriterLock ();
  200. }
  201. }
  202. public override void RemoveItem (HttpContext context,
  203. string id,
  204. object lockId,
  205. SessionStateStoreData item)
  206. {
  207. EnsureGoodId (id, true);
  208. string CacheId = CachePrefix + id;
  209. Cache cache = HttpRuntime.Cache;
  210. InProcSessionItem inProcItem = cache [CacheId] as InProcSessionItem;
  211. if (inProcItem == null || lockId == null || lockId.GetType() != typeof(Int32) || inProcItem.lockId != (Int32)lockId)
  212. return;
  213. try {
  214. inProcItem.rwlock.AcquireWriterLock (lockAcquireTimeout);
  215. cache.Remove (CacheId);
  216. } catch {
  217. throw;
  218. } finally {
  219. if (inProcItem.rwlock.IsWriterLockHeld)
  220. inProcItem.rwlock.ReleaseWriterLock ();
  221. }
  222. }
  223. public override void ResetItemTimeout (HttpContext context, string id)
  224. {
  225. EnsureGoodId (id, true);
  226. string CacheId = CachePrefix + id;
  227. Cache cache = HttpRuntime.Cache;
  228. InProcSessionItem item = cache [CacheId] as InProcSessionItem;
  229. if (item == null)
  230. return;
  231. try {
  232. item.rwlock.AcquireWriterLock (lockAcquireTimeout);
  233. cache.Remove (CacheId);
  234. InsertSessionItem (item, item.timeout, CacheId);
  235. } catch {
  236. throw;
  237. } finally {
  238. if (item.rwlock.IsWriterLockHeld)
  239. item.rwlock.ReleaseWriterLock ();
  240. }
  241. }
  242. public override void SetAndReleaseItemExclusive (HttpContext context,
  243. string id,
  244. SessionStateStoreData item,
  245. object lockId,
  246. bool newItem)
  247. {
  248. EnsureGoodId (id, true);
  249. string CacheId = CachePrefix + id;
  250. Cache cache = HttpRuntime.Cache;
  251. InProcSessionItem inProcItem = cache [CacheId] as InProcSessionItem;
  252. if (newItem || inProcItem == null) {
  253. inProcItem = new InProcSessionItem ();
  254. inProcItem.timeout = item.Timeout;
  255. inProcItem.expiresAt = DateTime.UtcNow.AddMinutes (item.Timeout);
  256. if (lockId.GetType() == typeof(Int32))
  257. inProcItem.lockId = (Int32)lockId;
  258. } else {
  259. if (lockId == null || lockId.GetType() != typeof(Int32) || inProcItem.lockId != (Int32)lockId)
  260. return;
  261. cache.Remove (CacheId);
  262. }
  263. try {
  264. inProcItem.rwlock.AcquireWriterLock (lockAcquireTimeout);
  265. inProcItem.locked = false;
  266. inProcItem.items = item.Items;
  267. InsertSessionItem (inProcItem, item.Timeout, CacheId);
  268. } catch {
  269. throw;
  270. } finally {
  271. if (inProcItem.rwlock.IsWriterLockHeld)
  272. inProcItem.rwlock.ReleaseWriterLock ();
  273. }
  274. }
  275. public override bool SetItemExpireCallback (SessionStateItemExpireCallback expireCallback)
  276. {
  277. this.expireCallback = expireCallback;
  278. return true;
  279. }
  280. void EnsureGoodId (string id, bool throwOnNull)
  281. {
  282. if (id == null)
  283. if (throwOnNull)
  284. throw new HttpException ("Session ID is invalid");
  285. else
  286. return;
  287. if (id.Length > SessionIDManager.SessionIDMaxLength)
  288. throw new HttpException ("Session ID too long");
  289. }
  290. void OnSessionRemoved (string key, object value, CacheItemRemovedReason reason)
  291. {
  292. if (expireCallback != null)
  293. expireCallback (key, value as SessionStateStoreData);
  294. }
  295. }
  296. }
  297. #endif