SessionStateServerHandler.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // System.Web.SessionState.SessionStateServerHandler
  3. //
  4. // Author(s):
  5. // Jackson Harper ([email protected])
  6. //
  7. // (C) 2003 Novell, Inc, (http://www.novell.com)
  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. using System;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Configuration;
  33. using System.Web.Configuration;
  34. using System.Runtime.Remoting;
  35. namespace System.Web.SessionState {
  36. internal class SessionStateServerHandler : ISessionHandler
  37. {
  38. const string CookieName = "ASPSESSION";
  39. private RemoteStateServer state_server;
  40. #if NET_2_0
  41. private SessionStateSection config;
  42. #else
  43. private SessionConfig config;
  44. #endif
  45. public void Dispose ()
  46. {
  47. }
  48. public void Init (SessionStateModule module, HttpApplication context,
  49. #if NET_2_0
  50. SessionStateSection config
  51. #else
  52. SessionConfig config
  53. #endif
  54. )
  55. {
  56. this.config = config;
  57. RemotingConfiguration.Configure (null);
  58. string cons, proto, server, port;
  59. GetConData (config.StateConnectionString, out proto, out server, out port);
  60. cons = String.Format ("{0}://{1}:{2}/StateServer", proto, server, port);
  61. state_server = (RemoteStateServer) Activator.GetObject (typeof (RemoteStateServer), cons);
  62. }
  63. public void UpdateHandler (HttpContext context, SessionStateModule module)
  64. {
  65. HttpSessionState session = context.Session;
  66. if (session == null || session.IsReadOnly)
  67. return;
  68. string id = session.SessionID;
  69. if (!session._abandoned) {
  70. SessionDictionary dict = session.SessionDictionary;
  71. HttpStaticObjectsCollection sobjs = session.StaticObjects;
  72. state_server.Update (id, dict.ToByteArray (), sobjs.ToByteArray ());
  73. } else {
  74. state_server.Remove (id);
  75. }
  76. }
  77. public HttpSessionState UpdateContext (HttpContext context, SessionStateModule module,
  78. bool required, bool read_only, ref bool isNew)
  79. {
  80. if (!required)
  81. return null;
  82. StateServerItem item = null;
  83. HttpSessionState session = null;
  84. SessionDictionary dict = null;
  85. HttpStaticObjectsCollection sobjs = null;
  86. string id = GetId (context);
  87. if (id != null) {
  88. item = state_server.Get (id);
  89. if (item != null) {
  90. dict = SessionDictionary.FromByteArray (item.DictionaryData);
  91. sobjs = HttpStaticObjectsCollection.FromByteArray (item.StaticObjectsData);
  92. session = new HttpSessionState (id, dict,
  93. HttpApplicationFactory.ApplicationState.SessionObjects,
  94. #if NET_2_0
  95. (int)config.Timeout.TotalMinutes, // XXX is this right? we lose some precision here, but since the timeout is in minutes *anyway*...
  96. #else
  97. config.Timeout,
  98. #endif
  99. false, config.CookieLess,
  100. SessionStateMode.StateServer, read_only);
  101. return session;
  102. }
  103. }
  104. id = SessionId.Create (module.Rng);
  105. dict = new SessionDictionary ();
  106. sobjs = HttpApplicationFactory.ApplicationState.SessionObjects;
  107. item = new StateServerItem (dict.ToByteArray (), sobjs.ToByteArray (),
  108. #if NET_2_0
  109. (int)config.Timeout.TotalMinutes
  110. #else
  111. config.Timeout
  112. #endif
  113. );
  114. state_server.Insert (id, item);
  115. session = new HttpSessionState (id, dict, sobjs,
  116. #if NET_2_0
  117. (int)config.Timeout.TotalMinutes,
  118. #else
  119. config.Timeout,
  120. #endif
  121. true,
  122. config.CookieLess, SessionStateMode.StateServer,
  123. read_only);
  124. isNew = true;
  125. return session;
  126. }
  127. private string GetId (HttpContext context)
  128. {
  129. if (!config.CookieLess &&
  130. context.Request.Cookies [CookieName] != null)
  131. return context.Request.Cookies [CookieName].Value;
  132. return null;
  133. }
  134. private void GetConData (string cons, out string proto, out string server, out string port)
  135. {
  136. int ei = cons.IndexOf ('=');
  137. int ci = cons.IndexOf (':');
  138. if (ei < 0 || ci < 0)
  139. throw new Exception ("Invalid StateConnectionString");
  140. proto = cons.Substring (0, ei);
  141. server = cons.Substring (ei+1, ci - ei - 1);
  142. port = cons.Substring (ci+1, cons.Length - ci - 1);
  143. if (proto == "tcpip")
  144. proto = "tcp";
  145. }
  146. }
  147. }