POSTHandler.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 POST methods
  54. public string PostHandler(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} POST path {1} param {2}", MsgID, path, param);
  62. try
  63. {
  64. // param empty: new region post
  65. if (!IsGod(httpRequest))
  66. // XXX: this needs to be turned into a FailureUnauthorized(...)
  67. return Failure(httpResponse, OSHttpStatusCode.ClientErrorUnauthorized,
  68. "GET", "you are not god");
  69. if (String.IsNullOrEmpty(param)) return CreateRegion(httpRequest, httpResponse);
  70. // Parse region ID and other parameters
  71. param = param.TrimEnd(new char[]{'/'});
  72. string[] comps = param.Split('/');
  73. UUID regionID = (UUID)comps[0];
  74. m_log.DebugFormat("{0} POST region UUID {1}", MsgID, regionID.ToString());
  75. if (UUID.Zero == regionID) throw new Exception("missing region ID");
  76. Scene scene = null;
  77. App.SceneManager.TryGetScene(regionID, out scene);
  78. if (null == scene) return Failure(httpResponse, OSHttpStatusCode.ClientErrorNotFound,
  79. "POST", "cannot find region {0}", regionID.ToString());
  80. if (2 == comps.Length) {
  81. // check for {prims}
  82. switch (comps[1].ToLower())
  83. {
  84. case "prims":
  85. return LoadPrims(request, httpRequest, httpResponse, scene);
  86. }
  87. }
  88. return Failure(httpResponse, OSHttpStatusCode.ClientErrorNotFound,
  89. "POST", "url {0} not supported", param);
  90. }
  91. catch (Exception e)
  92. {
  93. return Failure(httpResponse, OSHttpStatusCode.ServerErrorInternalError, "POST", e);
  94. }
  95. }
  96. public string CreateRegion(OSHttpRequest request, OSHttpResponse response)
  97. {
  98. XmlWriter.WriteStartElement(String.Empty, "regions", String.Empty);
  99. foreach (Scene s in App.SceneManager.Scenes)
  100. {
  101. XmlWriter.WriteStartElement(String.Empty, "uuid", String.Empty);
  102. XmlWriter.WriteString(s.RegionInfo.RegionID.ToString());
  103. XmlWriter.WriteEndElement();
  104. }
  105. XmlWriter.WriteEndElement();
  106. return XmlWriterResult;
  107. }
  108. public string LoadPrims(string requestBody, OSHttpRequest request, OSHttpResponse response, Scene scene)
  109. {
  110. IRegionSerialiserModule serialiser = scene.RequestModuleInterface<IRegionSerialiserModule>();
  111. if (serialiser != null)
  112. serialiser.LoadPrimsFromXml2(scene, new StringReader(requestBody), true);
  113. return "";
  114. }
  115. #endregion POST methods
  116. }
  117. }