NeighbourHandlers.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 GridRegion = OpenSim.Services.Interfaces.GridRegion;
  38. using OpenMetaverse;
  39. using OpenMetaverse.StructuredData;
  40. using log4net;
  41. namespace OpenSim.Server.Handlers.Neighbour
  42. {
  43. public class NeighbourSimpleHandler : SimpleStreamHandler
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private INeighbourService m_NeighbourService;
  47. private IAuthenticationService m_AuthenticationService;
  48. public NeighbourSimpleHandler(INeighbourService service, IAuthenticationService authentication) :
  49. base("/region")
  50. {
  51. m_NeighbourService = service;
  52. m_AuthenticationService = authentication;
  53. }
  54. protected override void ProcessRequest(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  55. {
  56. httpResponse.KeepAlive = false;
  57. if (m_NeighbourService == null)
  58. {
  59. httpResponse.StatusCode = (int)HttpStatusCode.InternalServerError;
  60. return;
  61. }
  62. switch (httpRequest.HttpMethod)
  63. {
  64. case "POST":
  65. {
  66. OSDMap args = RestHandlerUtils.DeserializeOSMap(httpRequest);
  67. if (args == null)
  68. {
  69. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  70. httpResponse.RawBuffer = Util.UTF8.GetBytes("false");
  71. return;
  72. }
  73. if (RestHandlerUtils.GetParams(httpRequest.UriPath, out UUID regionID, out ulong regionHandle, out string action)
  74. || regionID == UUID.Zero)
  75. {
  76. m_log.InfoFormat("[RegionPostHandler]: Invalid parameters for neighbour message {0}", httpRequest.UriPath);
  77. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  78. return;
  79. }
  80. ProcessPostRequest(args, httpRequest, httpResponse, regionID);
  81. break;
  82. }
  83. case "GET":
  84. case "PUT":
  85. case "DELETE":
  86. httpResponse.StatusCode = (int)HttpStatusCode.NotImplemented;
  87. return;
  88. default:
  89. {
  90. httpResponse.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
  91. return;
  92. }
  93. }
  94. }
  95. // TODO: unused: private bool m_AllowForeignGuests;
  96. protected void ProcessPostRequest(OSDMap args, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, UUID regionID)
  97. {
  98. byte[] result = new byte[0];
  99. if (m_AuthenticationService != null)
  100. {
  101. // Authentication
  102. string authority = string.Empty;
  103. string authToken = string.Empty;
  104. if (!RestHandlerUtils.GetAuthentication(httpRequest, out authority, out authToken))
  105. {
  106. m_log.InfoFormat("[RegionPostHandler]: Authentication failed for neighbour message");
  107. httpResponse.StatusCode = (int)HttpStatusCode.Unauthorized;
  108. return;
  109. }
  110. // TODO: Rethink this
  111. //if (!m_AuthenticationService.VerifyKey(regionID, authToken))
  112. //{
  113. // m_log.InfoFormat("[RegionPostHandler]: Authentication failed for neighbour message {0}", path);
  114. // httpResponse.StatusCode = (int)HttpStatusCode.Forbidden;
  115. // return result;
  116. //}
  117. m_log.DebugFormat("[RegionPostHandler]: Authentication succeeded for {0}", regionID);
  118. }
  119. // retrieve the regionhandle
  120. ulong regionhandle = 0;
  121. if (args["destination_handle"] != null)
  122. UInt64.TryParse(args["destination_handle"].AsString(), out regionhandle);
  123. RegionInfo aRegion = new RegionInfo();
  124. try
  125. {
  126. aRegion.UnpackRegionInfoData(args);
  127. }
  128. catch (Exception ex)
  129. {
  130. m_log.InfoFormat("[RegionPostHandler]: exception on unpacking region info {0}", ex.Message);
  131. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  132. return;
  133. }
  134. // Finally!
  135. GridRegion thisRegion = m_NeighbourService.HelloNeighbour(regionhandle, aRegion);
  136. OSDMap resp = new OSDMap(1);
  137. if (thisRegion != null)
  138. resp["success"] = OSD.FromBoolean(true);
  139. else
  140. resp["success"] = OSD.FromBoolean(false);
  141. httpResponse.RawBuffer = Util.UTF8.GetBytes(OSDParser.SerializeJsonString(resp));
  142. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  143. }
  144. }
  145. }