SessionStateServerHandler.cs 9.0 KB

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