SessionStateServerHandler.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // System.Web.Compilation.SessionStateItemCollection
  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.Collections.Specialized;
  31. using System.IO;
  32. using System.Web;
  33. using System.Web.Configuration;
  34. using System.Runtime.Remoting;
  35. namespace System.Web.SessionState
  36. {
  37. internal class SessionStateServerHandler : SessionStateStoreProviderBase
  38. {
  39. private const Int32 lockAcquireTimeout = 30000;
  40. SessionStateSection config;
  41. RemoteStateServer stateServer;
  42. public override SessionStateStoreData CreateNewStoreData (HttpContext context, int timeout)
  43. {
  44. return new SessionStateStoreData (new SessionStateItemCollection (),
  45. HttpApplicationFactory.ApplicationState.SessionObjects,
  46. timeout);
  47. }
  48. public override void CreateUninitializedItem (HttpContext context, string id, int timeout)
  49. {
  50. EnsureGoodId (id, true);
  51. stateServer.CreateUninitializedItem (id, timeout);
  52. }
  53. public override void Dispose ()
  54. {
  55. }
  56. public override void EndRequest (HttpContext context)
  57. {
  58. }
  59. SessionStateStoreData GetItemInternal (HttpContext context,
  60. string id,
  61. out bool locked,
  62. out TimeSpan lockAge,
  63. out object lockId,
  64. out SessionStateActions actions,
  65. bool exclusive)
  66. {
  67. #if TRACE
  68. Console.WriteLine ("SessionStateServerHandler.GetItemInternal");
  69. Console.WriteLine ("\tid == {0}", id);
  70. Console.WriteLine ("\tpath == {0}", context.Request.FilePath);
  71. #endif
  72. locked = false;
  73. lockAge = TimeSpan.MinValue;
  74. lockId = Int32.MinValue;
  75. actions = SessionStateActions.None;
  76. if (id == null)
  77. return null;
  78. StateServerItem item = stateServer.GetItem (id,
  79. out locked,
  80. out lockAge,
  81. out lockId,
  82. out actions,
  83. exclusive);
  84. if (item == null) {
  85. #if TRACE
  86. Console.WriteLine ("\titem is null (locked == {0}, actions == {1})", locked, actions);
  87. #endif
  88. return null;
  89. }
  90. if (actions == SessionStateActions.InitializeItem) {
  91. #if TRACE
  92. Console.WriteLine ("\titem needs initialization");
  93. #endif
  94. return CreateNewStoreData (context, item.Timeout);
  95. }
  96. SessionStateItemCollection items = null;
  97. HttpStaticObjectsCollection sobjs = null;
  98. MemoryStream stream = null;
  99. BinaryReader reader = null;
  100. try {
  101. if (item.CollectionData != null && item.CollectionData.Length > 0) {
  102. stream = new MemoryStream (item.CollectionData);
  103. reader = new BinaryReader (stream);
  104. items = SessionStateItemCollection.Deserialize (reader);
  105. } else
  106. items = new SessionStateItemCollection ();
  107. if (item.StaticObjectsData != null && item.StaticObjectsData.Length > 0)
  108. sobjs = HttpStaticObjectsCollection.FromByteArray (item.StaticObjectsData);
  109. else
  110. sobjs = new HttpStaticObjectsCollection ();
  111. } catch (Exception ex) {
  112. throw new HttpException ("Failed to retrieve session state.", ex);
  113. } finally {
  114. if (reader != null)
  115. reader.Close ();
  116. }
  117. return new SessionStateStoreData (items,
  118. sobjs,
  119. item.Timeout);
  120. }
  121. public override SessionStateStoreData GetItem (HttpContext context,
  122. string id,
  123. out bool locked,
  124. out TimeSpan lockAge,
  125. out object lockId,
  126. out SessionStateActions actions)
  127. {
  128. EnsureGoodId (id, false);
  129. return GetItemInternal (context, id, out locked, out lockAge, out lockId, out actions, false);
  130. }
  131. public override SessionStateStoreData GetItemExclusive (HttpContext context,
  132. string id,
  133. out bool locked,
  134. out TimeSpan lockAge,
  135. out object lockId,
  136. out SessionStateActions actions)
  137. {
  138. EnsureGoodId (id, false);
  139. return GetItemInternal (context, id, out locked, out lockAge, out lockId, out actions, true);
  140. }
  141. public override void Initialize (string name, NameValueCollection config)
  142. {
  143. #if TRACE
  144. Console.WriteLine ("SessionStateServerHandler.Initialize");
  145. #endif
  146. this.config = (SessionStateSection) WebConfigurationManager.GetSection ("system.web/sessionState");
  147. if (String.IsNullOrEmpty (name))
  148. name = "Session Server handler";
  149. RemotingConfiguration.Configure (null);
  150. string cons = null, proto = null, server = null, port = null;
  151. GetConData (out proto, out server, out port);
  152. cons = String.Format ("{0}://{1}:{2}/StateServer", proto, server, port);
  153. stateServer = Activator.GetObject (typeof (RemoteStateServer), cons) as RemoteStateServer;
  154. base.Initialize (name, config);
  155. }
  156. public override void InitializeRequest (HttpContext context)
  157. {
  158. // nothing to do here
  159. }
  160. public override void ReleaseItemExclusive (HttpContext context,
  161. string id,
  162. object lockId)
  163. {
  164. EnsureGoodId (id, true);
  165. stateServer.ReleaseItemExclusive (id, lockId);
  166. }
  167. public override void RemoveItem (HttpContext context,
  168. string id,
  169. object lockId,
  170. SessionStateStoreData item)
  171. {
  172. EnsureGoodId (id, true);
  173. stateServer.Remove (id, lockId);
  174. }
  175. public override void ResetItemTimeout (HttpContext context, string id)
  176. {
  177. EnsureGoodId (id, true);
  178. stateServer.ResetItemTimeout (id);
  179. }
  180. public override void SetAndReleaseItemExclusive (HttpContext context,
  181. string id,
  182. SessionStateStoreData item,
  183. object lockId,
  184. bool newItem)
  185. {
  186. EnsureGoodId (id, true);
  187. byte[] collection_data = null;
  188. byte[] sobjs_data = null;
  189. MemoryStream stream = null;
  190. BinaryWriter writer = null;
  191. try {
  192. SessionStateItemCollection items = item.Items as SessionStateItemCollection;
  193. if (items != null && items.Count > 0) {
  194. stream = new MemoryStream ();
  195. writer = new BinaryWriter (stream);
  196. items.Serialize (writer);
  197. collection_data = stream.GetBuffer ();
  198. }
  199. HttpStaticObjectsCollection sobjs = item.StaticObjects;
  200. if (sobjs != null && sobjs.Count > 0)
  201. sobjs_data = sobjs.ToByteArray ();
  202. } catch (Exception ex) {
  203. throw new HttpException ("Failed to store session data.", ex);
  204. } finally {
  205. if (writer != null)
  206. writer.Close ();
  207. }
  208. stateServer.SetAndReleaseItemExclusive (id, collection_data, sobjs_data, lockId, item.Timeout, newItem);
  209. }
  210. public override bool SetItemExpireCallback (SessionStateItemExpireCallback expireCallback)
  211. {
  212. return false;
  213. }
  214. void EnsureGoodId (string id, bool throwOnNull)
  215. {
  216. if (id == null)
  217. if (throwOnNull)
  218. throw new HttpException ("Session ID is invalid");
  219. else
  220. return;
  221. if (id.Length > SessionIDManager.SessionIDMaxLength)
  222. throw new HttpException ("Session ID too long");
  223. }
  224. void GetConData (out string proto, out string server, out string port)
  225. {
  226. string cons = config.StateConnectionString;
  227. int ei = cons.IndexOf ('=');
  228. int ci = cons.IndexOf (':');
  229. if (ei < 0 || ci < 0)
  230. throw new HttpException ("Invalid StateConnectionString");
  231. proto = cons.Substring (0, ei);
  232. server = cons.Substring (ei+1, ci - ei - 1);
  233. port = cons.Substring (ci+1, cons.Length - ci - 1);
  234. if (proto == "tcpip")
  235. proto = "tcp";
  236. }
  237. }
  238. }
  239. #endif