PresenceServerPostHandler.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using Nini.Config;
  28. using log4net;
  29. using System;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Net;
  33. using System.Text;
  34. using System.Text.RegularExpressions;
  35. using System.Xml;
  36. using System.Xml.Serialization;
  37. using System.Collections.Generic;
  38. using OpenSim.Server.Base;
  39. using OpenSim.Services.Interfaces;
  40. using OpenSim.Framework;
  41. using OpenSim.Framework.Servers.HttpServer;
  42. using OpenSim.Framework.ServiceAuth;
  43. using OpenMetaverse;
  44. namespace OpenSim.Server.Handlers.Presence
  45. {
  46. public class PresenceServerPostHandler : BaseStreamHandler
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private IPresenceService m_PresenceService;
  50. public PresenceServerPostHandler(IPresenceService service, IServiceAuth auth) :
  51. base("POST", "/presence", auth)
  52. {
  53. m_PresenceService = service;
  54. }
  55. protected override byte[] ProcessRequest(string path, Stream requestData,
  56. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  57. {
  58. StreamReader sr = new StreamReader(requestData);
  59. string body = sr.ReadToEnd();
  60. sr.Close();
  61. body = body.Trim();
  62. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  63. string method = string.Empty;
  64. try
  65. {
  66. Dictionary<string, object> request =
  67. ServerUtils.ParseQueryString(body);
  68. if (!request.ContainsKey("METHOD"))
  69. return FailureResult();
  70. method = request["METHOD"].ToString();
  71. switch (method)
  72. {
  73. case "login":
  74. return LoginAgent(request);
  75. case "logout":
  76. return LogoutAgent(request);
  77. case "logoutregion":
  78. return LogoutRegionAgents(request);
  79. case "report":
  80. return Report(request);
  81. case "getagent":
  82. return GetAgent(request);
  83. case "getagents":
  84. return GetAgents(request);
  85. }
  86. m_log.DebugFormat("[PRESENCE HANDLER]: unknown method request: {0}", method);
  87. }
  88. catch (Exception e)
  89. {
  90. m_log.DebugFormat("[PRESENCE HANDLER]: Exception in method {0}: {1}", method, e);
  91. }
  92. return FailureResult();
  93. }
  94. byte[] LoginAgent(Dictionary<string, object> request)
  95. {
  96. string user = String.Empty;
  97. UUID session = UUID.Zero;
  98. UUID ssession = UUID.Zero;
  99. if (!request.ContainsKey("UserID") || !request.ContainsKey("SessionID"))
  100. return FailureResult();
  101. user = request["UserID"].ToString();
  102. if (!UUID.TryParse(request["SessionID"].ToString(), out session))
  103. return FailureResult();
  104. if (request.ContainsKey("SecureSessionID"))
  105. // If it's malformed, we go on with a Zero on it
  106. UUID.TryParse(request["SecureSessionID"].ToString(), out ssession);
  107. if (m_PresenceService.LoginAgent(user, session, ssession))
  108. return SuccessResult();
  109. return FailureResult();
  110. }
  111. byte[] LogoutAgent(Dictionary<string, object> request)
  112. {
  113. UUID session = UUID.Zero;
  114. if (!request.ContainsKey("SessionID"))
  115. return FailureResult();
  116. if (!UUID.TryParse(request["SessionID"].ToString(), out session))
  117. return FailureResult();
  118. if (m_PresenceService.LogoutAgent(session))
  119. return SuccessResult();
  120. return FailureResult();
  121. }
  122. byte[] LogoutRegionAgents(Dictionary<string, object> request)
  123. {
  124. UUID region = UUID.Zero;
  125. if (!request.ContainsKey("RegionID"))
  126. return FailureResult();
  127. if (!UUID.TryParse(request["RegionID"].ToString(), out region))
  128. return FailureResult();
  129. if (m_PresenceService.LogoutRegionAgents(region))
  130. return SuccessResult();
  131. return FailureResult();
  132. }
  133. byte[] Report(Dictionary<string, object> request)
  134. {
  135. UUID session = UUID.Zero;
  136. UUID region = UUID.Zero;
  137. if (!request.ContainsKey("SessionID") || !request.ContainsKey("RegionID"))
  138. return FailureResult();
  139. if (!UUID.TryParse(request["SessionID"].ToString(), out session))
  140. return FailureResult();
  141. if (!UUID.TryParse(request["RegionID"].ToString(), out region))
  142. return FailureResult();
  143. if (m_PresenceService.ReportAgent(session, region))
  144. {
  145. return SuccessResult();
  146. }
  147. return FailureResult();
  148. }
  149. byte[] GetAgent(Dictionary<string, object> request)
  150. {
  151. UUID session = UUID.Zero;
  152. if (!request.ContainsKey("SessionID"))
  153. return FailureResult();
  154. if (!UUID.TryParse(request["SessionID"].ToString(), out session))
  155. return FailureResult();
  156. PresenceInfo pinfo = m_PresenceService.GetAgent(session);
  157. Dictionary<string, object> result = new Dictionary<string, object>();
  158. if (pinfo == null)
  159. result["result"] = "null";
  160. else
  161. result["result"] = pinfo.ToKeyValuePairs();
  162. string xmlString = ServerUtils.BuildXmlResponse(result);
  163. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  164. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  165. }
  166. byte[] GetAgents(Dictionary<string, object> request)
  167. {
  168. string[] userIDs;
  169. if (!request.ContainsKey("uuids"))
  170. {
  171. m_log.DebugFormat("[PRESENCE HANDLER]: GetAgents called without required uuids argument");
  172. return FailureResult();
  173. }
  174. if (!(request["uuids"] is List<string>))
  175. {
  176. m_log.DebugFormat("[PRESENCE HANDLER]: GetAgents input argument was of unexpected type {0}", request["uuids"].GetType().ToString());
  177. return FailureResult();
  178. }
  179. userIDs = ((List<string>)request["uuids"]).ToArray();
  180. PresenceInfo[] pinfos = m_PresenceService.GetAgents(userIDs);
  181. Dictionary<string, object> result = new Dictionary<string, object>();
  182. if ((pinfos == null) || ((pinfos != null) && (pinfos.Length == 0)))
  183. result["result"] = "null";
  184. else
  185. {
  186. int i = 0;
  187. foreach (PresenceInfo pinfo in pinfos)
  188. {
  189. Dictionary<string, object> rinfoDict = pinfo.ToKeyValuePairs();
  190. result["presence" + i] = rinfoDict;
  191. i++;
  192. }
  193. }
  194. string xmlString = ServerUtils.BuildXmlResponse(result);
  195. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  196. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  197. }
  198. private byte[] SuccessResult()
  199. {
  200. XmlDocument doc = new XmlDocument();
  201. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  202. "", "");
  203. doc.AppendChild(xmlnode);
  204. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  205. "");
  206. doc.AppendChild(rootElement);
  207. XmlElement result = doc.CreateElement("", "result", "");
  208. result.AppendChild(doc.CreateTextNode("Success"));
  209. rootElement.AppendChild(result);
  210. return Util.DocToBytes(doc);
  211. }
  212. private byte[] FailureResult()
  213. {
  214. XmlDocument doc = new XmlDocument();
  215. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  216. "", "");
  217. doc.AppendChild(xmlnode);
  218. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  219. "");
  220. doc.AppendChild(rootElement);
  221. XmlElement result = doc.CreateElement("", "result", "");
  222. result.AppendChild(doc.CreateTextNode("Failure"));
  223. rootElement.AppendChild(result);
  224. return Util.DocToBytes(doc);
  225. }
  226. }
  227. }