GridInfoHandlers.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.Collections;
  29. using System.Net;
  30. using System.Reflection;
  31. using System.Security;
  32. using log4net;
  33. using Nini.Config;
  34. using Nwc.XmlRpc;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenMetaverse;
  38. using OpenMetaverse.StructuredData;
  39. namespace OpenSim.Server.Handlers.Grid
  40. {
  41. public class GridInfoHandlers
  42. {
  43. private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private IConfigSource m_Config;
  45. private Hashtable _info = new Hashtable();
  46. /// <summary>
  47. /// Instantiate a GridInfoService object.
  48. /// </summary>
  49. /// <param name="configPath">path to config path containing
  50. /// grid information</param>
  51. /// <remarks>
  52. /// GridInfoService uses the [GridInfo] section of the
  53. /// standard OpenSim.ini file --- which is not optimal, but
  54. /// anything else requires a general redesign of the config
  55. /// system.
  56. /// </remarks>
  57. public GridInfoHandlers(IConfigSource configSource)
  58. {
  59. m_Config = configSource;
  60. loadGridInfo(configSource);
  61. }
  62. private void loadGridInfo(IConfigSource configSource)
  63. {
  64. _info["platform"] = "OpenSim";
  65. try
  66. {
  67. IConfig gridCfg = configSource.Configs["GridInfoService"];
  68. IConfig netCfg = configSource.Configs["Network"];
  69. if (null != gridCfg)
  70. {
  71. foreach (string k in gridCfg.GetKeys())
  72. {
  73. _info[k] = gridCfg.GetString(k);
  74. }
  75. }
  76. else if (null != netCfg)
  77. {
  78. _info["login"] = string.Format("http://127.0.0.1:{0}/",
  79. netCfg.GetString("http_listener_port", ConfigSettings.DefaultRegionHttpPort.ToString()));
  80. IssueWarning();
  81. }
  82. else
  83. {
  84. _info["login"] = "http://127.0.0.1:9000/";
  85. IssueWarning();
  86. }
  87. }
  88. catch (Exception)
  89. {
  90. _log.Warn("[GRID INFO SERVICE]: Cannot get grid info from config source, using minimal defaults");
  91. }
  92. _log.DebugFormat("[GRID INFO SERVICE]: Grid info service initialized with {0} keys", _info.Count);
  93. }
  94. private void IssueWarning()
  95. {
  96. _log.Warn("[GRID INFO SERVICE]: found no [GridInfo] section in your configuration files");
  97. _log.Warn("[GRID INFO SERVICE]: trying to guess sensible defaults, you might want to provide better ones:");
  98. foreach (string k in _info.Keys)
  99. {
  100. _log.WarnFormat("[GRID INFO SERVICE]: {0}: {1}", k, _info[k]);
  101. }
  102. }
  103. public XmlRpcResponse XmlRpcGridInfoMethod(XmlRpcRequest request, IPEndPoint remoteClient)
  104. {
  105. XmlRpcResponse response = new XmlRpcResponse();
  106. Hashtable responseData = new Hashtable();
  107. _log.Debug("[GRID INFO SERVICE]: Request for grid info");
  108. foreach (string k in _info.Keys)
  109. {
  110. responseData[k] = _info[k];
  111. }
  112. response.Value = responseData;
  113. return response;
  114. }
  115. public void RestGetGridInfoMethod(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  116. {
  117. httpResponse.KeepAlive = false;
  118. if (httpRequest.HttpMethod != "GET")
  119. {
  120. httpResponse.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
  121. return;
  122. }
  123. osUTF8 osb = OSUTF8Cached.Acquire();
  124. osb.AppendASCII("<gridinfo>");
  125. foreach (string k in _info.Keys)
  126. {
  127. osb.AppendASCII('<');
  128. osb.AppendASCII(k);
  129. osb.AppendASCII('>');
  130. osb.AppendASCII(SecurityElement.Escape(_info[k].ToString()));
  131. osb.AppendASCII("</");
  132. osb.AppendASCII(k);
  133. osb.AppendASCII('>');
  134. }
  135. osb.AppendASCII("</gridinfo>");
  136. httpResponse.RawBuffer = OSUTF8Cached.GetArrayAndRelease(osb);
  137. }
  138. /// <summary>
  139. /// Get GridInfo in json format: Used by the OSSL osGetGrid*
  140. /// Adding the SRV_HomeIRI to the kvp returned for use in scripts
  141. /// </summary>
  142. /// <returns>
  143. /// json string
  144. /// </returns>
  145. /// </param>
  146. /// <param name='httpRequest'>
  147. /// Http request.
  148. /// </param>
  149. /// <param name='httpResponse'>
  150. /// Http response.
  151. /// </param>
  152. public void JsonGetGridInfoMethod(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  153. {
  154. httpResponse.KeepAlive = false;
  155. if (httpRequest.HttpMethod != "GET")
  156. {
  157. httpResponse.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
  158. return;
  159. }
  160. OSDMap map = new OSDMap();
  161. foreach (string k in _info.Keys)
  162. {
  163. map[k] = OSD.FromString(_info[k].ToString());
  164. }
  165. string HomeURI = Util.GetConfigVarFromSections<string>(m_Config, "HomeURI",
  166. new string[] { "Startup", "Hypergrid" }, String.Empty);
  167. if (!string.IsNullOrEmpty(HomeURI))
  168. map["home"] = OSD.FromString(HomeURI);
  169. else // Legacy. Remove soon!
  170. {
  171. IConfig cfg = m_Config.Configs["LoginService"];
  172. if (null != cfg)
  173. HomeURI = cfg.GetString("SRV_HomeURI", HomeURI);
  174. if (!string.IsNullOrEmpty(HomeURI))
  175. map["home"] = OSD.FromString(HomeURI);
  176. }
  177. httpResponse.RawBuffer = OSDParser.SerializeJsonToBytes(map);
  178. }
  179. }
  180. }