SessionInProcHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. [MonoTODO ("Investigate why in certain situations 'item' is null. See bug #333898")]
  252. /* In certain situations the 'item' parameter passed to SetAndReleaseItemExclusive
  253. may be null. The issue was reported in bug #333898, but the reporter cannot
  254. provide a test case that triggers the issue. Added work around the problem
  255. in the way that should have the least impact on the rest of the code. If 'item'
  256. is null, then the new session item is created without the items and staticItems
  257. collections - they will be initialized to defaults when retrieving the session
  258. item. This is not a correct fix, but since there is no test case this is the best
  259. what can be done right now.
  260. */
  261. public override void SetAndReleaseItemExclusive (HttpContext context,
  262. string id,
  263. SessionStateStoreData item,
  264. object lockId,
  265. bool newItem)
  266. {
  267. EnsureGoodId (id, true);
  268. string CacheId = CachePrefix + id;
  269. Cache cache = HttpRuntime.InternalCache;
  270. InProcSessionItem inProcItem = cache [CacheId] as InProcSessionItem;
  271. if (newItem || inProcItem == null) {
  272. inProcItem = new InProcSessionItem ();
  273. inProcItem.timeout = item != null ? item.Timeout : 20;
  274. inProcItem.expiresAt = DateTime.UtcNow.AddMinutes (item != null ? item.Timeout : 20);
  275. if (lockId.GetType() == typeof(Int32))
  276. inProcItem.lockId = (Int32)lockId;
  277. } else {
  278. if (lockId == null || lockId.GetType() != typeof(Int32) || inProcItem.lockId != (Int32)lockId)
  279. return;
  280. inProcItem.resettingTimeout = true;
  281. cache.Remove (CacheId);
  282. }
  283. try {
  284. inProcItem.rwlock.AcquireWriterLock (lockAcquireTimeout);
  285. inProcItem.locked = false;
  286. inProcItem.items = item != null ? item.Items : null;
  287. inProcItem.staticItems = item != null ? item.StaticObjects : null;
  288. InsertSessionItem (inProcItem, item != null ? item.Timeout : 20, CacheId);
  289. } catch {
  290. throw;
  291. } finally {
  292. if (inProcItem.rwlock.IsWriterLockHeld)
  293. inProcItem.rwlock.ReleaseWriterLock ();
  294. }
  295. }
  296. public override bool SetItemExpireCallback (SessionStateItemExpireCallback expireCallback)
  297. {
  298. this.expireCallback = expireCallback;
  299. return true;
  300. }
  301. void EnsureGoodId (string id, bool throwOnNull)
  302. {
  303. if (id == null)
  304. if (throwOnNull)
  305. throw new HttpException ("Session ID is invalid");
  306. else
  307. return;
  308. if (id.Length > SessionIDManager.SessionIDMaxLength)
  309. throw new HttpException ("Session ID too long");
  310. }
  311. void OnSessionRemoved (string key, object value, CacheItemRemovedReason reason)
  312. {
  313. if (expireCallback != null) {
  314. if (value is SessionStateStoreData)
  315. expireCallback (key, (SessionStateStoreData)value);
  316. else if (value is InProcSessionItem) {
  317. InProcSessionItem item = (InProcSessionItem)value;
  318. if (item.resettingTimeout) {
  319. item.resettingTimeout = false;
  320. return;
  321. }
  322. expireCallback (key,
  323. new SessionStateStoreData (
  324. item.items,
  325. item.staticItems,
  326. item.timeout));
  327. } else
  328. expireCallback (key, null);
  329. }
  330. }
  331. }
  332. }
  333. #endif