SessionInProcHandler.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. public bool resettingTimeout;
  51. internal InProcSessionItem ()
  52. {
  53. this.locked = false;
  54. this.cookieless = false;
  55. this.items = null;
  56. this.lockedTime = DateTime.MinValue;
  57. this.expiresAt = DateTime.MinValue;
  58. this.rwlock = new ReaderWriterLock ();
  59. this.lockId = Int32.MinValue;
  60. this.timeout = 0;
  61. this.resettingTimeout = false;
  62. }
  63. }
  64. internal class SessionInProcHandler : SessionStateStoreProviderBase
  65. {
  66. private const string CachePrefix = "@@@InProc@";
  67. private const Int32 lockAcquireTimeout = 30000;
  68. CacheItemRemovedCallback removedCB;
  69. //NameValueCollection privateConfig;
  70. SessionStateItemExpireCallback expireCallback;
  71. public override SessionStateStoreData CreateNewStoreData (HttpContext context, int timeout)
  72. {
  73. return new SessionStateStoreData (new SessionStateItemCollection (),
  74. SessionStateUtility.GetSessionStaticObjects(context),
  75. timeout);
  76. }
  77. void InsertSessionItem (InProcSessionItem item, int timeout, string id)
  78. {
  79. if (item == null || String.IsNullOrEmpty (id))
  80. return;
  81. HttpRuntime.Cache.InsertPrivate (id,
  82. item,
  83. null,
  84. Cache.NoAbsoluteExpiration,
  85. TimeSpan.FromMinutes (timeout),
  86. CacheItemPriority.AboveNormal,
  87. removedCB);
  88. }
  89. public override void CreateUninitializedItem (HttpContext context, string id, int timeout)
  90. {
  91. EnsureGoodId (id, true);
  92. InProcSessionItem item = new InProcSessionItem ();
  93. item.expiresAt = DateTime.UtcNow.AddMinutes (timeout);
  94. item.timeout = timeout;
  95. InsertSessionItem (item, timeout, CachePrefix + id);
  96. }
  97. public override void Dispose ()
  98. {
  99. }
  100. public override void EndRequest (HttpContext context)
  101. {
  102. }
  103. SessionStateStoreData GetItemInternal (HttpContext context,
  104. string id,
  105. out bool locked,
  106. out TimeSpan lockAge,
  107. out object lockId,
  108. out SessionStateActions actions,
  109. bool exclusive)
  110. {
  111. locked = false;
  112. lockAge = TimeSpan.MinValue;
  113. lockId = Int32.MinValue;
  114. actions = SessionStateActions.None;
  115. if (id == null)
  116. return null;
  117. Cache cache = HttpRuntime.Cache;
  118. string CacheId = CachePrefix + id;
  119. InProcSessionItem item = cache [CacheId] as InProcSessionItem;
  120. if (item == null)
  121. return null;
  122. try {
  123. item.rwlock.AcquireReaderLock (lockAcquireTimeout);
  124. if (item.locked) {
  125. locked = true;
  126. lockAge = DateTime.UtcNow.Subtract (item.lockedTime);
  127. lockId = item.lockId;
  128. return null;
  129. }
  130. item.rwlock.ReleaseReaderLock ();
  131. if (exclusive) {
  132. item.rwlock.AcquireWriterLock (lockAcquireTimeout);
  133. item.locked = true;
  134. item.lockedTime = DateTime.UtcNow;
  135. item.lockId++;
  136. lockId = item.lockId;
  137. }
  138. if (item.items == null) {
  139. actions = SessionStateActions.InitializeItem;
  140. item.items = new SessionStateItemCollection ();
  141. }
  142. return new SessionStateStoreData (item.items,
  143. SessionStateUtility.GetSessionStaticObjects(context),
  144. item.timeout);
  145. } catch {
  146. // we want such errors to be passed to the application.
  147. throw;
  148. } finally {
  149. if (item.rwlock.IsReaderLockHeld)
  150. item.rwlock.ReleaseReaderLock ();
  151. if (item.rwlock.IsWriterLockHeld)
  152. item.rwlock.ReleaseWriterLock ();
  153. }
  154. }
  155. public override SessionStateStoreData GetItem (HttpContext context,
  156. string id,
  157. out bool locked,
  158. out TimeSpan lockAge,
  159. out object lockId,
  160. out SessionStateActions actions)
  161. {
  162. EnsureGoodId (id, false);
  163. return GetItemInternal (context, id, out locked, out lockAge, out lockId, out actions, false);
  164. }
  165. public override SessionStateStoreData GetItemExclusive (HttpContext context,
  166. string id,
  167. out bool locked,
  168. out TimeSpan lockAge,
  169. out object lockId,
  170. out SessionStateActions actions)
  171. {
  172. EnsureGoodId (id, false);
  173. return GetItemInternal (context, id, out locked, out lockAge, out lockId, out actions, true);
  174. }
  175. public override void Initialize (string name, NameValueCollection config)
  176. {
  177. if (String.IsNullOrEmpty (name))
  178. name = "Session InProc handler";
  179. removedCB = new CacheItemRemovedCallback (OnSessionRemoved);
  180. //privateConfig = config;
  181. base.Initialize (name, config);
  182. }
  183. public override void InitializeRequest (HttpContext context)
  184. {
  185. // nothing to do here
  186. }
  187. public override void ReleaseItemExclusive (HttpContext context,
  188. string id,
  189. object lockId)
  190. {
  191. EnsureGoodId (id, true);
  192. string CacheId = CachePrefix + id;
  193. InProcSessionItem item = HttpRuntime.Cache [CacheId] as InProcSessionItem;
  194. if (item == null || lockId == null || lockId.GetType() != typeof(Int32) || item.lockId != (Int32)lockId)
  195. return;
  196. try {
  197. item.rwlock.AcquireWriterLock (lockAcquireTimeout);
  198. item.locked = false;
  199. } catch {
  200. throw;
  201. } finally {
  202. if (item.rwlock.IsWriterLockHeld)
  203. item.rwlock.ReleaseWriterLock ();
  204. }
  205. }
  206. public override void RemoveItem (HttpContext context,
  207. string id,
  208. object lockId,
  209. SessionStateStoreData item)
  210. {
  211. EnsureGoodId (id, true);
  212. string CacheId = CachePrefix + id;
  213. Cache cache = HttpRuntime.Cache;
  214. InProcSessionItem inProcItem = cache [CacheId] as InProcSessionItem;
  215. if (inProcItem == null || lockId == null || lockId.GetType() != typeof(Int32) || inProcItem.lockId != (Int32)lockId)
  216. return;
  217. try {
  218. inProcItem.rwlock.AcquireWriterLock (lockAcquireTimeout);
  219. cache.Remove (CacheId);
  220. } catch {
  221. throw;
  222. } finally {
  223. if (inProcItem.rwlock.IsWriterLockHeld)
  224. inProcItem.rwlock.ReleaseWriterLock ();
  225. }
  226. }
  227. public override void ResetItemTimeout (HttpContext context, string id)
  228. {
  229. EnsureGoodId (id, true);
  230. string CacheId = CachePrefix + id;
  231. Cache cache = HttpRuntime.Cache;
  232. InProcSessionItem item = cache [CacheId] as InProcSessionItem;
  233. if (item == null)
  234. return;
  235. try {
  236. item.rwlock.AcquireWriterLock (lockAcquireTimeout);
  237. item.resettingTimeout = true;
  238. cache.Remove (CacheId);
  239. InsertSessionItem (item, item.timeout, CacheId);
  240. } catch {
  241. throw;
  242. } finally {
  243. if (item.rwlock.IsWriterLockHeld)
  244. item.rwlock.ReleaseWriterLock ();
  245. }
  246. }
  247. public override void SetAndReleaseItemExclusive (HttpContext context,
  248. string id,
  249. SessionStateStoreData item,
  250. object lockId,
  251. bool newItem)
  252. {
  253. EnsureGoodId (id, true);
  254. string CacheId = CachePrefix + id;
  255. Cache cache = HttpRuntime.Cache;
  256. InProcSessionItem inProcItem = cache [CacheId] as InProcSessionItem;
  257. if (newItem || inProcItem == null) {
  258. inProcItem = new InProcSessionItem ();
  259. inProcItem.timeout = item.Timeout;
  260. inProcItem.expiresAt = DateTime.UtcNow.AddMinutes (item.Timeout);
  261. if (lockId.GetType() == typeof(Int32))
  262. inProcItem.lockId = (Int32)lockId;
  263. } else {
  264. if (lockId == null || lockId.GetType() != typeof(Int32) || inProcItem.lockId != (Int32)lockId)
  265. return;
  266. inProcItem.resettingTimeout = true;
  267. cache.Remove (CacheId);
  268. }
  269. try {
  270. inProcItem.rwlock.AcquireWriterLock (lockAcquireTimeout);
  271. inProcItem.locked = false;
  272. inProcItem.items = item.Items;
  273. InsertSessionItem (inProcItem, item.Timeout, CacheId);
  274. } catch {
  275. throw;
  276. } finally {
  277. if (inProcItem.rwlock.IsWriterLockHeld)
  278. inProcItem.rwlock.ReleaseWriterLock ();
  279. }
  280. }
  281. public override bool SetItemExpireCallback (SessionStateItemExpireCallback expireCallback)
  282. {
  283. this.expireCallback = expireCallback;
  284. return true;
  285. }
  286. void EnsureGoodId (string id, bool throwOnNull)
  287. {
  288. if (id == null)
  289. if (throwOnNull)
  290. throw new HttpException ("Session ID is invalid");
  291. else
  292. return;
  293. if (id.Length > SessionIDManager.SessionIDMaxLength)
  294. throw new HttpException ("Session ID too long");
  295. }
  296. void OnSessionRemoved (string key, object value, CacheItemRemovedReason reason)
  297. {
  298. if (expireCallback != null) {
  299. if (value is SessionStateStoreData)
  300. expireCallback (key, (SessionStateStoreData)value);
  301. else if (value is InProcSessionItem) {
  302. InProcSessionItem item = (InProcSessionItem)value;
  303. if (item.resettingTimeout) {
  304. item.resettingTimeout = false;
  305. return;
  306. }
  307. expireCallback (key,
  308. new SessionStateStoreData (
  309. item.items,
  310. SessionStateUtility.GetSessionStaticObjects (HttpContext.Current),
  311. item.timeout));
  312. } else
  313. expireCallback (key, null);
  314. }
  315. }
  316. }
  317. }
  318. #endif