2
0

SessionStateServerHandler.cs 5.1 KB

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