SessionStateServerHandler.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.Runtime.Remoting;
  34. namespace System.Web.SessionState {
  35. internal class SessionStateServerHandler : ISessionHandler
  36. {
  37. const string CookieName = "ASPSESSION";
  38. const int DefTimeout = 600;
  39. private RemoteStateServer state_server;
  40. private SessionConfig config;
  41. public void Dispose ()
  42. {
  43. }
  44. public void Init (SessionStateModule module, HttpApplication context, SessionConfig config)
  45. {
  46. this.config = config;
  47. RemotingConfiguration.Configure (null);
  48. string cons, proto, server, port;
  49. GetConData (config.StateConnectionString, out proto, out server, out port);
  50. cons = String.Format ("{0}://{1}:{2}/StateServer", proto, server, port);
  51. state_server = (RemoteStateServer) Activator.GetObject (typeof (RemoteStateServer), cons);
  52. }
  53. public void UpdateHandler (HttpContext context, SessionStateModule module)
  54. {
  55. if (context.Session == null || context.Session.IsReadOnly)
  56. return;
  57. string id = context.Session.SessionID;
  58. SessionDictionary dict = context.Session.SessionDictionary;
  59. HttpStaticObjectsCollection sobjs = context.Session.StaticObjects;
  60. state_server.Update (id, dict.ToByteArray (), sobjs.ToByteArray ());
  61. }
  62. public HttpSessionState UpdateContext (HttpContext context, SessionStateModule module,
  63. bool required, bool read_only, ref bool isNew)
  64. {
  65. if (!required)
  66. return null;
  67. StateServerItem item = null;
  68. HttpSessionState session = null;
  69. SessionDictionary dict = null;
  70. HttpStaticObjectsCollection sobjs = null;
  71. string id = GetId (context);
  72. if (id != null) {
  73. item = state_server.Get (id);
  74. if (item != null) {
  75. dict = SessionDictionary.FromByteArray (item.DictionaryData);
  76. sobjs = HttpStaticObjectsCollection.FromByteArray (item.StaticObjectsData);
  77. session = new HttpSessionState (id, dict,
  78. HttpApplicationFactory.ApplicationState.SessionObjects,
  79. config.Timeout, false, config.CookieLess,
  80. SessionStateMode.StateServer, read_only);
  81. return session;
  82. }
  83. }
  84. id = SessionId.Create (module.Rng);
  85. dict = new SessionDictionary ();
  86. sobjs = HttpApplicationFactory.ApplicationState.SessionObjects;
  87. item = new StateServerItem (dict.ToByteArray (), sobjs.ToByteArray (), config.Timeout);
  88. state_server.Insert (id, item);
  89. session = new HttpSessionState (id, dict, sobjs, config.Timeout, true,
  90. config.CookieLess, SessionStateMode.StateServer,
  91. read_only);
  92. isNew = true;
  93. return session;
  94. }
  95. private string GetId (HttpContext context)
  96. {
  97. if (!config.CookieLess &&
  98. context.Request.Cookies [CookieName] != null)
  99. return context.Request.Cookies [CookieName].Value;
  100. return null;
  101. }
  102. private byte[] GetDictData (SessionDictionary dict)
  103. {
  104. MemoryStream stream = null;
  105. try {
  106. stream = new MemoryStream ();
  107. dict.Serialize (new BinaryWriter (stream));
  108. return stream.GetBuffer ();
  109. } catch {
  110. throw;
  111. } finally {
  112. if (stream != null)
  113. stream.Close ();
  114. }
  115. }
  116. private void GetConData (string cons, out string proto, out string server, out string port)
  117. {
  118. int ei = cons.IndexOf ('=');
  119. int ci = cons.IndexOf (':');
  120. if (ei < 0 || ci < 0)
  121. throw new Exception ("Invalid StateConnectionString");
  122. proto = cons.Substring (0, ei);
  123. server = cons.Substring (ei+1, ci - ei - 1);
  124. port = cons.Substring (ci+1, cons.Length - ci - 1);
  125. if (proto == "tcpip")
  126. proto = "tcp";
  127. }
  128. }
  129. }