2
0

SessionStateServerHandler.cs 4.4 KB

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