POSTHandler.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 OpenMetaverse;
  30. using OpenSim.Framework.Servers;
  31. using OpenSim.Framework.Servers.HttpServer;
  32. using OpenSim.Region.Framework.Interfaces;
  33. using OpenSim.Region.Framework.Scenes;
  34. namespace OpenSim.ApplicationPlugins.Rest.Regions
  35. {
  36. public partial class RestRegionPlugin : RestPlugin
  37. {
  38. #region POST methods
  39. public string PostHandler(string request, string path, string param,
  40. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  41. {
  42. // foreach (string h in httpRequest.Headers.AllKeys)
  43. // foreach (string v in httpRequest.Headers.GetValues(h))
  44. // m_log.DebugFormat("{0} IsGod: {1} -> {2}", MsgID, h, v);
  45. MsgID = RequestID;
  46. m_log.DebugFormat("{0} POST path {1} param {2}", MsgID, path, param);
  47. try
  48. {
  49. // param empty: new region post
  50. if (!IsGod(httpRequest))
  51. // XXX: this needs to be turned into a FailureUnauthorized(...)
  52. return Failure(httpResponse, OSHttpStatusCode.ClientErrorUnauthorized,
  53. "GET", "you are not god");
  54. if (String.IsNullOrEmpty(param)) return CreateRegion(httpRequest, httpResponse);
  55. // Parse region ID and other parameters
  56. param = param.TrimEnd(new char[] {'/'});
  57. string[] comps = param.Split('/');
  58. UUID regionID = (UUID) comps[0];
  59. m_log.DebugFormat("{0} POST region UUID {1}", MsgID, regionID.ToString());
  60. if (UUID.Zero == regionID) throw new Exception("missing region ID");
  61. Scene scene = null;
  62. App.SceneManager.TryGetScene(regionID, out scene);
  63. if (null == scene)
  64. return Failure(httpResponse, OSHttpStatusCode.ClientErrorNotFound,
  65. "POST", "cannot find region {0}", regionID.ToString());
  66. if (2 == comps.Length)
  67. {
  68. // check for {prims}
  69. switch (comps[1].ToLower())
  70. {
  71. case "prims":
  72. return LoadPrims(request, httpRequest, httpResponse, scene);
  73. }
  74. }
  75. return Failure(httpResponse, OSHttpStatusCode.ClientErrorNotFound,
  76. "POST", "url {0} not supported", param);
  77. }
  78. catch (Exception e)
  79. {
  80. return Failure(httpResponse, OSHttpStatusCode.ServerErrorInternalError, "POST", e);
  81. }
  82. }
  83. public string CreateRegion(OSHttpRequest request, OSHttpResponse response)
  84. {
  85. RestXmlWriter rxw = new RestXmlWriter(new StringWriter());
  86. rxw.WriteStartElement(String.Empty, "regions", String.Empty);
  87. foreach (Scene s in App.SceneManager.Scenes)
  88. {
  89. rxw.WriteStartElement(String.Empty, "uuid", String.Empty);
  90. rxw.WriteString(s.RegionInfo.RegionID.ToString());
  91. rxw.WriteEndElement();
  92. }
  93. rxw.WriteEndElement();
  94. return rxw.ToString();
  95. }
  96. public string LoadPrims(string requestBody, OSHttpRequest request, OSHttpResponse response, Scene scene)
  97. {
  98. IRegionSerialiserModule serialiser = scene.RequestModuleInterface<IRegionSerialiserModule>();
  99. if (serialiser != null)
  100. serialiser.LoadPrimsFromXml2(scene, new StringReader(requestBody), true);
  101. return "";
  102. }
  103. #endregion POST methods
  104. }
  105. }