PresenceServerPostHandler.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 OpenMetaverse;
  43. namespace OpenSim.Server.Handlers.Presence
  44. {
  45. public class PresenceServerPostHandler : BaseStreamHandler
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private IPresenceService m_PresenceService;
  49. public PresenceServerPostHandler(IPresenceService service) :
  50. base("POST", "/presence")
  51. {
  52. m_PresenceService = service;
  53. }
  54. public override byte[] Handle(string path, Stream requestData,
  55. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  56. {
  57. StreamReader sr = new StreamReader(requestData);
  58. string body = sr.ReadToEnd();
  59. sr.Close();
  60. body = body.Trim();
  61. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  62. string method = string.Empty;
  63. try
  64. {
  65. Dictionary<string, object> request =
  66. ServerUtils.ParseQueryString(body);
  67. if (!request.ContainsKey("METHOD"))
  68. return FailureResult();
  69. method = request["METHOD"].ToString();
  70. switch (method)
  71. {
  72. case "login":
  73. return LoginAgent(request);
  74. case "logout":
  75. return LogoutAgent(request);
  76. case "logoutregion":
  77. return LogoutRegionAgents(request);
  78. case "report":
  79. return Report(request);
  80. case "getagent":
  81. return GetAgent(request);
  82. case "getagents":
  83. return GetAgents(request);
  84. }
  85. m_log.DebugFormat("[PRESENCE HANDLER]: unknown method request: {0}", method);
  86. }
  87. catch (Exception e)
  88. {
  89. m_log.DebugFormat("[PRESENCE HANDLER]: Exception in method {0}: {1}", method, e);
  90. }
  91. return FailureResult();
  92. }
  93. byte[] LoginAgent(Dictionary<string, object> request)
  94. {
  95. string user = String.Empty;
  96. UUID session = UUID.Zero;
  97. UUID ssession = UUID.Zero;
  98. if (!request.ContainsKey("UserID") || !request.ContainsKey("SessionID"))
  99. return FailureResult();
  100. user = request["UserID"].ToString();
  101. if (!UUID.TryParse(request["SessionID"].ToString(), out session))
  102. return FailureResult();
  103. if (request.ContainsKey("SecureSessionID"))
  104. // If it's malformed, we go on with a Zero on it
  105. UUID.TryParse(request["SecureSessionID"].ToString(), out ssession);
  106. if (m_PresenceService.LoginAgent(user, session, ssession))
  107. return SuccessResult();
  108. return FailureResult();
  109. }
  110. byte[] LogoutAgent(Dictionary<string, object> request)
  111. {
  112. UUID session = UUID.Zero;
  113. Vector3 position = Vector3.Zero;
  114. Vector3 lookat = Vector3.Zero;
  115. if (!request.ContainsKey("SessionID"))
  116. return FailureResult();
  117. if (!UUID.TryParse(request["SessionID"].ToString(), out session))
  118. return FailureResult();
  119. if (m_PresenceService.LogoutAgent(session))
  120. return SuccessResult();
  121. return FailureResult();
  122. }
  123. byte[] LogoutRegionAgents(Dictionary<string, object> request)
  124. {
  125. UUID region = UUID.Zero;
  126. if (!request.ContainsKey("RegionID"))
  127. return FailureResult();
  128. if (!UUID.TryParse(request["RegionID"].ToString(), out region))
  129. return FailureResult();
  130. if (m_PresenceService.LogoutRegionAgents(region))
  131. return SuccessResult();
  132. return FailureResult();
  133. }
  134. byte[] Report(Dictionary<string, object> request)
  135. {
  136. UUID session = UUID.Zero;
  137. UUID region = UUID.Zero;
  138. if (!request.ContainsKey("SessionID") || !request.ContainsKey("RegionID"))
  139. return FailureResult();
  140. if (!UUID.TryParse(request["SessionID"].ToString(), out session))
  141. return FailureResult();
  142. if (!UUID.TryParse(request["RegionID"].ToString(), out region))
  143. return FailureResult();
  144. if (m_PresenceService.ReportAgent(session, region))
  145. {
  146. return SuccessResult();
  147. }
  148. return FailureResult();
  149. }
  150. byte[] GetAgent(Dictionary<string, object> request)
  151. {
  152. UUID session = UUID.Zero;
  153. if (!request.ContainsKey("SessionID"))
  154. return FailureResult();
  155. if (!UUID.TryParse(request["SessionID"].ToString(), out session))
  156. return FailureResult();
  157. PresenceInfo pinfo = m_PresenceService.GetAgent(session);
  158. Dictionary<string, object> result = new Dictionary<string, object>();
  159. if (pinfo == null)
  160. result["result"] = "null";
  161. else
  162. result["result"] = pinfo.ToKeyValuePairs();
  163. string xmlString = ServerUtils.BuildXmlResponse(result);
  164. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  165. UTF8Encoding encoding = new UTF8Encoding();
  166. return encoding.GetBytes(xmlString);
  167. }
  168. byte[] GetAgents(Dictionary<string, object> request)
  169. {
  170. string[] userIDs;
  171. if (!request.ContainsKey("uuids"))
  172. {
  173. m_log.DebugFormat("[PRESENCE HANDLER]: GetAgents called without required uuids argument");
  174. return FailureResult();
  175. }
  176. if (!(request["uuids"] is List<string>))
  177. {
  178. m_log.DebugFormat("[PRESENCE HANDLER]: GetAgents input argument was of unexpected type {0}", request["uuids"].GetType().ToString());
  179. return FailureResult();
  180. }
  181. userIDs = ((List<string>)request["uuids"]).ToArray();
  182. PresenceInfo[] pinfos = m_PresenceService.GetAgents(userIDs);
  183. Dictionary<string, object> result = new Dictionary<string, object>();
  184. if ((pinfos == null) || ((pinfos != null) && (pinfos.Length == 0)))
  185. result["result"] = "null";
  186. else
  187. {
  188. int i = 0;
  189. foreach (PresenceInfo pinfo in pinfos)
  190. {
  191. Dictionary<string, object> rinfoDict = pinfo.ToKeyValuePairs();
  192. result["presence" + i] = rinfoDict;
  193. i++;
  194. }
  195. }
  196. string xmlString = ServerUtils.BuildXmlResponse(result);
  197. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  198. UTF8Encoding encoding = new UTF8Encoding();
  199. return encoding.GetBytes(xmlString);
  200. }
  201. private byte[] SuccessResult()
  202. {
  203. XmlDocument doc = new XmlDocument();
  204. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  205. "", "");
  206. doc.AppendChild(xmlnode);
  207. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  208. "");
  209. doc.AppendChild(rootElement);
  210. XmlElement result = doc.CreateElement("", "result", "");
  211. result.AppendChild(doc.CreateTextNode("Success"));
  212. rootElement.AppendChild(result);
  213. return DocToBytes(doc);
  214. }
  215. private byte[] FailureResult()
  216. {
  217. XmlDocument doc = new XmlDocument();
  218. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  219. "", "");
  220. doc.AppendChild(xmlnode);
  221. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  222. "");
  223. doc.AppendChild(rootElement);
  224. XmlElement result = doc.CreateElement("", "result", "");
  225. result.AppendChild(doc.CreateTextNode("Failure"));
  226. rootElement.AppendChild(result);
  227. return DocToBytes(doc);
  228. }
  229. private byte[] DocToBytes(XmlDocument doc)
  230. {
  231. MemoryStream ms = new MemoryStream();
  232. XmlTextWriter xw = new XmlTextWriter(ms, null);
  233. xw.Formatting = Formatting.Indented;
  234. doc.WriteTo(xw);
  235. xw.Flush();
  236. return ms.ToArray();
  237. }
  238. }
  239. }