SessionStateServerHandler.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // System.Web.SessionStateServerHandler
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2006 Marek Habersack
  8. // (C) 2007-2010 Novell, Inc (http://novell.com/)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Collections.Specialized;
  31. using System.IO;
  32. using System.IO.Compression;
  33. using System.Web;
  34. using System.Web.Configuration;
  35. using System.Runtime.Remoting;
  36. using System.Diagnostics;
  37. namespace System.Web.SessionState
  38. {
  39. internal class SessionStateServerHandler : SessionStateStoreProviderBase
  40. {
  41. const Int32 lockAcquireTimeout = 30000;
  42. SessionStateSection config;
  43. RemoteStateServer stateServer;
  44. public override SessionStateStoreData CreateNewStoreData (HttpContext context, int timeout)
  45. {
  46. return new SessionStateStoreData (new SessionStateItemCollection (),
  47. HttpApplicationFactory.ApplicationState.SessionObjects,
  48. timeout);
  49. }
  50. public override void CreateUninitializedItem (HttpContext context, string id, int timeout)
  51. {
  52. EnsureGoodId (id, true);
  53. stateServer.CreateUninitializedItem (id, timeout);
  54. }
  55. public override void Dispose ()
  56. {
  57. }
  58. public override void EndRequest (HttpContext context)
  59. {
  60. }
  61. SessionStateStoreData GetItemInternal (HttpContext context,
  62. string id,
  63. out bool locked,
  64. out TimeSpan lockAge,
  65. out object lockId,
  66. out SessionStateActions actions,
  67. bool exclusive)
  68. {
  69. locked = false;
  70. lockAge = TimeSpan.MinValue;
  71. lockId = Int32.MinValue;
  72. actions = SessionStateActions.None;
  73. if (id == null)
  74. return null;
  75. StateServerItem item = stateServer.GetItem (id,
  76. out locked,
  77. out lockAge,
  78. out lockId,
  79. out actions,
  80. exclusive);
  81. if (item == null)
  82. return null;
  83. if (actions == SessionStateActions.InitializeItem)
  84. return CreateNewStoreData (context, item.Timeout);
  85. SessionStateItemCollection items = null;
  86. HttpStaticObjectsCollection sobjs = null;
  87. MemoryStream stream = null;
  88. BinaryReader reader = null;
  89. Stream input = null;
  90. GZipStream gzip = null;
  91. try {
  92. if (item.CollectionData != null && item.CollectionData.Length > 0) {
  93. stream = new MemoryStream (item.CollectionData);
  94. if (config.CompressionEnabled)
  95. input = gzip = new GZipStream (stream, CompressionMode.Decompress, true);
  96. else
  97. input = stream;
  98. reader = new BinaryReader (input);
  99. items = SessionStateItemCollection.Deserialize (reader);
  100. if (gzip != null)
  101. gzip.Close ();
  102. reader.Close ();
  103. } else
  104. items = new SessionStateItemCollection ();
  105. if (item.StaticObjectsData != null && item.StaticObjectsData.Length > 0)
  106. sobjs = HttpStaticObjectsCollection.FromByteArray (item.StaticObjectsData);
  107. else
  108. sobjs = new HttpStaticObjectsCollection ();
  109. } catch (Exception ex) {
  110. throw new HttpException ("Failed to retrieve session state.", ex);
  111. } finally {
  112. if (stream != null)
  113. stream.Dispose ();
  114. if (reader != null)
  115. reader.Dispose ();
  116. if (gzip != null)
  117. gzip.Dispose ();
  118. }
  119. return new SessionStateStoreData (items,
  120. sobjs,
  121. item.Timeout);
  122. }
  123. public override SessionStateStoreData GetItem (HttpContext context,
  124. string id,
  125. out bool locked,
  126. out TimeSpan lockAge,
  127. out object lockId,
  128. out SessionStateActions actions)
  129. {
  130. EnsureGoodId (id, false);
  131. return GetItemInternal (context, id, out locked, out lockAge, out lockId, out actions, false);
  132. }
  133. public override SessionStateStoreData GetItemExclusive (HttpContext context,
  134. string id,
  135. out bool locked,
  136. out TimeSpan lockAge,
  137. out object lockId,
  138. out SessionStateActions actions)
  139. {
  140. EnsureGoodId (id, false);
  141. return GetItemInternal (context, id, out locked, out lockAge, out lockId, out actions, true);
  142. }
  143. public override void Initialize (string name, NameValueCollection config)
  144. {
  145. this.config = (SessionStateSection) WebConfigurationManager.GetSection ("system.web/sessionState");
  146. if (String.IsNullOrEmpty (name))
  147. name = "Session Server handler";
  148. RemotingConfiguration.Configure (null);
  149. string cons = null, proto = null, server = null, port = null;
  150. GetConData (out proto, out server, out port);
  151. cons = String.Format ("{0}://{1}:{2}/StateServer", proto, server, port);
  152. stateServer = Activator.GetObject (typeof (RemoteStateServer), cons) as RemoteStateServer;
  153. base.Initialize (name, config);
  154. }
  155. public override void InitializeRequest (HttpContext context)
  156. {
  157. // nothing to do here
  158. }
  159. public override void ReleaseItemExclusive (HttpContext context,
  160. string id,
  161. object lockId)
  162. {
  163. EnsureGoodId (id, true);
  164. stateServer.ReleaseItemExclusive (id, lockId);
  165. }
  166. public override void RemoveItem (HttpContext context,
  167. string id,
  168. object lockId,
  169. SessionStateStoreData item)
  170. {
  171. EnsureGoodId (id, true);
  172. stateServer.Remove (id, lockId);
  173. }
  174. public override void ResetItemTimeout (HttpContext context, string id)
  175. {
  176. EnsureGoodId (id, true);
  177. stateServer.ResetItemTimeout (id);
  178. }
  179. public override void SetAndReleaseItemExclusive (HttpContext context,
  180. string id,
  181. SessionStateStoreData item,
  182. object lockId,
  183. bool newItem)
  184. {
  185. if (item == null)
  186. return;
  187. EnsureGoodId (id, true);
  188. byte[] collection_data = null;
  189. byte[] sobjs_data = null;
  190. MemoryStream stream = null;
  191. BinaryWriter writer = null;
  192. Stream output = null;
  193. GZipStream gzip = null;
  194. try {
  195. SessionStateItemCollection items = item.Items as SessionStateItemCollection;
  196. if (items != null && items.Count > 0) {
  197. stream = new MemoryStream ();
  198. if (config.CompressionEnabled)
  199. output = gzip = new GZipStream (stream, CompressionMode.Compress, true);
  200. else
  201. output = stream;
  202. writer = new BinaryWriter (output);
  203. items.Serialize (writer);
  204. if (gzip != null)
  205. gzip.Close ();
  206. writer.Close ();
  207. collection_data = stream.ToArray ();
  208. }
  209. HttpStaticObjectsCollection sobjs = item.StaticObjects;
  210. if (sobjs != null && sobjs.Count > 0)
  211. sobjs_data = sobjs.ToByteArray ();
  212. } catch (Exception ex) {
  213. throw new HttpException ("Failed to store session data.", ex);
  214. } finally {
  215. if (writer != null)
  216. writer.Dispose ();
  217. if (gzip != null)
  218. gzip.Dispose ();
  219. if (stream != null)
  220. stream.Dispose ();
  221. }
  222. stateServer.SetAndReleaseItemExclusive (id, collection_data, sobjs_data, lockId, item.Timeout, newItem);
  223. }
  224. public override bool SetItemExpireCallback (SessionStateItemExpireCallback expireCallback)
  225. {
  226. return false;
  227. }
  228. void EnsureGoodId (string id, bool throwOnNull)
  229. {
  230. if (id == null)
  231. if (throwOnNull)
  232. throw new HttpException ("Session ID is invalid");
  233. else
  234. return;
  235. if (id.Length > SessionIDManager.SessionIDMaxLength)
  236. throw new HttpException ("Session ID too long");
  237. }
  238. void GetConData (out string proto, out string server, out string port)
  239. {
  240. string cons = config.StateConnectionString;
  241. int ei = cons.IndexOf ('=');
  242. int ci = cons.IndexOf (':');
  243. if (ei < 0 || ci < 0)
  244. throw new HttpException ("Invalid StateConnectionString");
  245. proto = cons.Substring (0, ei);
  246. server = cons.Substring (ei+1, ci - ei - 1);
  247. port = cons.Substring (ci+1, cons.Length - ci - 1);
  248. if (proto == "tcpip")
  249. proto = "tcp";
  250. }
  251. }
  252. }