SessionStateServerHandler.cs 8.4 KB

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