AgentHandlers.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 System;
  28. using System.IO;
  29. using System.Reflection;
  30. using System.Net;
  31. using System.Text;
  32. using OpenSim.Server.Base;
  33. using OpenSim.Server.Handlers.Base;
  34. using OpenSim.Services.Interfaces;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenMetaverse;
  38. using OpenMetaverse.StructuredData;
  39. using Nini.Config;
  40. using log4net;
  41. namespace OpenSim.Server.Handlers.Simulation
  42. {
  43. public class AgentGetHandler : BaseStreamHandler
  44. {
  45. // TODO: unused: private ISimulationService m_SimulationService;
  46. // TODO: unused: private IAuthenticationService m_AuthenticationService;
  47. public AgentGetHandler(ISimulationService service, IAuthenticationService authentication) :
  48. base("GET", "/agent")
  49. {
  50. // TODO: unused: m_SimulationService = service;
  51. // TODO: unused: m_AuthenticationService = authentication;
  52. }
  53. public override byte[] Handle(string path, Stream request,
  54. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  55. {
  56. // Not implemented yet
  57. httpResponse.StatusCode = (int)HttpStatusCode.NotImplemented;
  58. return new byte[] { };
  59. }
  60. }
  61. public class AgentPostHandler : BaseStreamHandler
  62. {
  63. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  64. private ISimulationService m_SimulationService;
  65. private IAuthenticationService m_AuthenticationService;
  66. // TODO: unused: private bool m_AllowForeignGuests;
  67. public AgentPostHandler(ISimulationService service, IAuthenticationService authentication, bool foreignGuests) :
  68. base("POST", "/agent")
  69. {
  70. m_SimulationService = service;
  71. m_AuthenticationService = authentication;
  72. // TODO: unused: m_AllowForeignGuests = foreignGuests;
  73. }
  74. public override byte[] Handle(string path, Stream request,
  75. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  76. {
  77. byte[] result = new byte[0];
  78. UUID agentID;
  79. string action;
  80. ulong regionHandle;
  81. if (!RestHandlerUtils.GetParams(path, out agentID, out regionHandle, out action))
  82. {
  83. m_log.InfoFormat("[AgentPostHandler]: Invalid parameters for agent message {0}", path);
  84. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  85. httpResponse.StatusDescription = "Invalid parameters for agent message " + path;
  86. return result;
  87. }
  88. if (m_AuthenticationService != null)
  89. {
  90. // Authentication
  91. string authority = string.Empty;
  92. string authToken = string.Empty;
  93. if (!RestHandlerUtils.GetAuthentication(httpRequest, out authority, out authToken))
  94. {
  95. m_log.InfoFormat("[AgentPostHandler]: Authentication failed for agent message {0}", path);
  96. httpResponse.StatusCode = (int)HttpStatusCode.Unauthorized;
  97. return result;
  98. }
  99. if (!m_AuthenticationService.VerifyKey(agentID, authToken))
  100. {
  101. m_log.InfoFormat("[AgentPostHandler]: Authentication failed for agent message {0}", path);
  102. httpResponse.StatusCode = (int)HttpStatusCode.Forbidden;
  103. return result;
  104. }
  105. m_log.DebugFormat("[AgentPostHandler]: Authentication succeeded for {0}", agentID);
  106. }
  107. OSDMap args = Util.GetOSDMap(request, (int)httpRequest.ContentLength);
  108. if (args == null)
  109. {
  110. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  111. httpResponse.StatusDescription = "Unable to retrieve data";
  112. m_log.DebugFormat("[AgentPostHandler]: Unable to retrieve data for post {0}", path);
  113. return result;
  114. }
  115. // retrieve the regionhandle
  116. ulong regionhandle = 0;
  117. if (args["destination_handle"] != null)
  118. UInt64.TryParse(args["destination_handle"].AsString(), out regionhandle);
  119. AgentCircuitData aCircuit = new AgentCircuitData();
  120. try
  121. {
  122. aCircuit.UnpackAgentCircuitData(args);
  123. }
  124. catch (Exception ex)
  125. {
  126. m_log.InfoFormat("[AgentPostHandler]: exception on unpacking CreateAgent message {0}", ex.Message);
  127. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  128. httpResponse.StatusDescription = "Problems with data deserialization";
  129. return result;
  130. }
  131. string reason = string.Empty;
  132. // We need to clean up a few things in the user service before I can do this
  133. //if (m_AllowForeignGuests)
  134. // m_regionClient.AdjustUserInformation(aCircuit);
  135. // Finally!
  136. bool success = m_SimulationService.CreateAgent(regionhandle, aCircuit, out reason);
  137. OSDMap resp = new OSDMap(1);
  138. resp["success"] = OSD.FromBoolean(success);
  139. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  140. return Encoding.UTF8.GetBytes(OSDParser.SerializeJsonString(resp));
  141. }
  142. }
  143. public class AgentPutHandler : BaseStreamHandler
  144. {
  145. // TODO: unused: private ISimulationService m_SimulationService;
  146. // TODO: unused: private IAuthenticationService m_AuthenticationService;
  147. public AgentPutHandler(ISimulationService service, IAuthenticationService authentication) :
  148. base("PUT", "/agent")
  149. {
  150. // TODO: unused: m_SimulationService = service;
  151. // TODO: unused: m_AuthenticationService = authentication;
  152. }
  153. public override byte[] Handle(string path, Stream request,
  154. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  155. {
  156. // Not implemented yet
  157. httpResponse.StatusCode = (int)HttpStatusCode.NotImplemented;
  158. return new byte[] { };
  159. }
  160. }
  161. public class AgentDeleteHandler : BaseStreamHandler
  162. {
  163. // TODO: unused: private ISimulationService m_SimulationService;
  164. // TODO: unused: private IAuthenticationService m_AuthenticationService;
  165. public AgentDeleteHandler(ISimulationService service, IAuthenticationService authentication) :
  166. base("DELETE", "/agent")
  167. {
  168. // TODO: unused: m_SimulationService = service;
  169. // TODO: unused: m_AuthenticationService = authentication;
  170. }
  171. public override byte[] Handle(string path, Stream request,
  172. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  173. {
  174. // Not implemented yet
  175. httpResponse.StatusCode = (int)HttpStatusCode.NotImplemented;
  176. return new byte[] { };
  177. }
  178. }
  179. }