SessionStateServerHandler.cs 8.3 KB

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