GETHandler.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 OpenSim 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.Threading;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.IO;
  32. using System.Net;
  33. using System.Reflection;
  34. using System.Text.RegularExpressions;
  35. using System.Timers;
  36. using System.Xml;
  37. using System.Xml.Serialization;
  38. using OpenMetaverse;
  39. using Mono.Addins;
  40. using Nwc.XmlRpc;
  41. using Nini.Config;
  42. using OpenSim.Framework;
  43. using OpenSim.Framework.Console;
  44. using OpenSim.Framework.Servers;
  45. using OpenSim.Framework.Communications;
  46. using OpenSim.Region.Environment.Interfaces;
  47. using OpenSim.Region.Environment.Scenes;
  48. using OpenSim.ApplicationPlugins.Rest;
  49. namespace OpenSim.ApplicationPlugins.Rest.Regions
  50. {
  51. public partial class RestRegionPlugin : RestPlugin
  52. {
  53. #region GET methods
  54. public string GetHandler(string request, string path, string param,
  55. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  56. {
  57. // foreach (string h in httpRequest.Headers.AllKeys)
  58. // foreach (string v in httpRequest.Headers.GetValues(h))
  59. // m_log.DebugFormat("{0} IsGod: {1} -> {2}", MsgID, h, v);
  60. MsgID = RequestID;
  61. m_log.DebugFormat("{0} GET path {1} param {2}", MsgID, path, param);
  62. try
  63. {
  64. // param empty: regions list
  65. if (String.IsNullOrEmpty(param)) return GetHandlerRegions(httpResponse);
  66. // param not empty: specific region
  67. return GetHandlerRegion(httpResponse, param);
  68. }
  69. catch (Exception e)
  70. {
  71. return Failure(httpResponse, OSHttpStatusCode.ServerErrorInternalError, "GET", e);
  72. }
  73. }
  74. public string GetHandlerRegions(OSHttpResponse httpResponse)
  75. {
  76. XmlWriter.WriteStartElement(String.Empty, "regions", String.Empty);
  77. foreach (Scene s in App.SceneManager.Scenes)
  78. {
  79. XmlWriter.WriteStartElement(String.Empty, "uuid", String.Empty);
  80. XmlWriter.WriteString(s.RegionInfo.RegionID.ToString());
  81. XmlWriter.WriteEndElement();
  82. }
  83. XmlWriter.WriteEndElement();
  84. return XmlWriterResult;
  85. }
  86. protected string ShortRegionInfo(string key, string value)
  87. {
  88. if (String.IsNullOrEmpty(value) ||
  89. String.IsNullOrEmpty(key)) return null;
  90. XmlWriter.WriteStartElement(String.Empty, "region", String.Empty);
  91. XmlWriter.WriteStartElement(String.Empty, key, String.Empty);
  92. XmlWriter.WriteString(value);
  93. XmlWriter.WriteEndDocument();
  94. return XmlWriterResult;
  95. }
  96. public string GetHandlerRegion(OSHttpResponse httpResponse, string param)
  97. {
  98. // be resilient and don't get confused by a terminating '/'
  99. param = param.TrimEnd(new char[]{'/'});
  100. string[] comps = param.Split('/');
  101. UUID regionID = (UUID)comps[0];
  102. m_log.DebugFormat("{0} GET region UUID {1}", MsgID, regionID.ToString());
  103. if (UUID.Zero == regionID) throw new Exception("missing region ID");
  104. Scene scene = null;
  105. App.SceneManager.TryGetScene(regionID, out scene);
  106. if (null == scene) return Failure(httpResponse, OSHttpStatusCode.ClientErrorNotFound,
  107. "GET", "cannot find region {0}", regionID.ToString());
  108. RegionDetails details = new RegionDetails(scene.RegionInfo);
  109. // m_log.DebugFormat("{0} GET comps {1}", MsgID, comps.Length);
  110. // for (int i = 0; i < comps.Length; i++) m_log.DebugFormat("{0} GET comps[{1}] >{2}<", MsgID, i, comps[i]);
  111. if (1 == comps.Length)
  112. {
  113. // complete region details requested
  114. XmlSerializer xs = new XmlSerializer(typeof(RegionDetails));
  115. xs.Serialize(XmlWriter, details, _xmlNs);
  116. return XmlWriterResult;
  117. }
  118. if (2 == comps.Length)
  119. {
  120. string resp = ShortRegionInfo(comps[1], details[comps[1]]);
  121. if (null != resp) return resp;
  122. // m_log.DebugFormat("{0} GET comps advanced: >{1}<", MsgID, comps[1]);
  123. // check for {terrain,stats,prims}
  124. switch (comps[1].ToLower())
  125. {
  126. case "terrain":
  127. return RegionTerrain(httpResponse, scene);
  128. case "stats":
  129. return RegionStats(httpResponse, scene);
  130. case "prims":
  131. return RegionPrims(httpResponse, scene, Vector3.Zero, Vector3.Zero);
  132. }
  133. }
  134. if (3 == comps.Length)
  135. {
  136. switch (comps[1].ToLower())
  137. {
  138. case "prims":
  139. string[] subregion = comps[2].Split(',');
  140. if (subregion.Length == 6)
  141. {
  142. Vector3 min, max;
  143. try
  144. {
  145. min = new Vector3((float)Double.Parse(subregion[0]), (float)Double.Parse(subregion[1]), (float)Double.Parse(subregion[2]));
  146. max = new Vector3((float)Double.Parse(subregion[3]), (float)Double.Parse(subregion[4]), (float)Double.Parse(subregion[5]));
  147. }
  148. catch (Exception)
  149. {
  150. return Failure(httpResponse, OSHttpStatusCode.ClientErrorBadRequest,
  151. "GET", "invalid subregion parameter");
  152. }
  153. return RegionPrims(httpResponse, scene, min, max);
  154. }
  155. else
  156. {
  157. return Failure(httpResponse, OSHttpStatusCode.ClientErrorBadRequest,
  158. "GET", "invalid subregion parameter");
  159. }
  160. }
  161. }
  162. return Failure(httpResponse, OSHttpStatusCode.ClientErrorBadRequest,
  163. "GET", "too many parameters {0}", param);
  164. }
  165. #endregion GET methods
  166. protected string RegionTerrain(OSHttpResponse httpResponse, Scene scene)
  167. {
  168. return Failure(httpResponse, OSHttpStatusCode.ServerErrorNotImplemented,
  169. "GET", "terrain not implemented");
  170. }
  171. protected string RegionStats(OSHttpResponse httpResponse, Scene scene)
  172. {
  173. int users = scene.GetAvatars().Count;
  174. int objects = scene.Entities.Count - users;
  175. XmlWriter.WriteStartElement(String.Empty, "region", String.Empty);
  176. XmlWriter.WriteStartElement(String.Empty, "stats", String.Empty);
  177. XmlWriter.WriteStartElement(String.Empty, "users", String.Empty);
  178. XmlWriter.WriteString(users.ToString());
  179. XmlWriter.WriteEndElement();
  180. XmlWriter.WriteStartElement(String.Empty, "objects", String.Empty);
  181. XmlWriter.WriteString(objects.ToString());
  182. XmlWriter.WriteEndElement();
  183. XmlWriter.WriteEndDocument();
  184. return XmlWriterResult;
  185. }
  186. protected string RegionPrims(OSHttpResponse httpResponse, Scene scene, Vector3 min, Vector3 max)
  187. {
  188. httpResponse.SendChunked = true;
  189. httpResponse.ContentType = "text/xml";
  190. IRegionSerialiserModule serialiser = scene.RequestModuleInterface<IRegionSerialiserModule>();
  191. if (serialiser != null)
  192. serialiser.SavePrimsToXml2(scene, new StreamWriter(httpResponse.OutputStream), min, max);
  193. return "";
  194. }
  195. }
  196. }