SessionInProcHandler.cs 10 KB

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