Caps.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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.Collections.Generic;
  30. using System.Collections.Concurrent;
  31. using System.Reflection;
  32. using System.Threading;
  33. using log4net;
  34. using OpenMetaverse;
  35. using OpenMetaverse.StructuredData;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. // using OpenSim.Region.Framework.Interfaces;
  39. namespace OpenSim.Framework.Capabilities
  40. {
  41. /// <summary>
  42. /// XXX Probably not a particularly nice way of allow us to get the scene presence from the scene (chiefly so that
  43. /// we can popup a message on the user's client if the inventory service has permanently failed). But I didn't want
  44. /// to just pass the whole Scene into CAPS.
  45. /// </summary>
  46. public delegate IClientAPI GetClientDelegate(UUID agentID);
  47. public class Caps : IDisposable
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. private readonly string m_httpListenerHostName;
  51. private readonly uint m_httpListenPort;
  52. /// <summary>
  53. /// This is the uuid portion of every CAPS path. It is used to make capability urls private to the requester.
  54. /// </summary>
  55. private readonly string m_capsObjectPath;
  56. public string CapsObjectPath { get { return m_capsObjectPath; } }
  57. private readonly CapsHandlers m_capsHandlers;
  58. private readonly ConcurrentDictionary<string, PollServiceEventArgs> m_pollServiceHandlers = new ();
  59. private readonly Dictionary<string, string> m_externalCapsHandlers = new ();
  60. private readonly IHttpServer m_httpListener;
  61. private readonly UUID m_agentID;
  62. private readonly string m_regionName;
  63. private ManualResetEvent m_capsActive = new(false);
  64. private readonly string m_baseCapsURL;
  65. public UUID AgentID
  66. {
  67. get { return m_agentID; }
  68. }
  69. public string RegionName
  70. {
  71. get { return m_regionName; }
  72. }
  73. public string HostName
  74. {
  75. get { return m_httpListenerHostName; }
  76. }
  77. public uint Port
  78. {
  79. get { return m_httpListenPort; }
  80. }
  81. public IHttpServer HttpListener
  82. {
  83. get { return m_httpListener; }
  84. }
  85. public bool SSLCaps
  86. {
  87. get { return m_httpListener.UseSSL; }
  88. }
  89. public string SSLCommonName
  90. {
  91. get { return m_httpListener.SSLCommonName; }
  92. }
  93. public CapsHandlers CapsHandlers
  94. {
  95. get { return m_capsHandlers; }
  96. }
  97. public Dictionary<string, string> ExternalCapsHandlers
  98. {
  99. get { return m_externalCapsHandlers; }
  100. }
  101. [Flags]
  102. public enum CapsFlags: uint
  103. {
  104. None = 0,
  105. SentSeeds = 1,
  106. ObjectAnim = 0x100,
  107. WLEnv = 0x200,
  108. AdvEnv = 0x400,
  109. PBR = 0x800,
  110. ViewerBenefits = 0x1000,
  111. TPBR = 0x2000
  112. }
  113. public CapsFlags Flags { get; set;}
  114. public Caps(IHttpServer httpServer, string httpListen, uint httpPort, string capsPath,
  115. UUID agent, string regionName)
  116. {
  117. m_capsObjectPath = capsPath;
  118. m_httpListener = httpServer;
  119. m_httpListenerHostName = httpListen;
  120. m_httpListenPort = httpPort;
  121. if (httpServer is not null && httpServer.UseSSL)
  122. {
  123. m_httpListenPort = httpServer.SSLPort;
  124. httpListen = httpServer.SSLCommonName;
  125. httpPort = httpServer.SSLPort;
  126. }
  127. m_agentID = agent;
  128. m_capsHandlers = new CapsHandlers(httpServer, httpListen, httpPort);
  129. m_regionName = regionName;
  130. Flags = CapsFlags.None;
  131. m_capsActive.Reset();
  132. if (MainServer.Instance.UseSSL)
  133. m_baseCapsURL = $"https://{MainServer.Instance.SSLCommonName}:{MainServer.Instance.SSLPort}";
  134. else
  135. {
  136. if (MainServer.Instance is null)
  137. m_baseCapsURL = $"http://{m_httpListenerHostName}:0";
  138. else
  139. m_baseCapsURL = $"http://{m_httpListenerHostName}:{MainServer.Instance.Port}";
  140. }
  141. }
  142. ~Caps()
  143. {
  144. Dispose(false);
  145. }
  146. public void Dispose()
  147. {
  148. Dispose(true);
  149. GC.SuppressFinalize(this);
  150. }
  151. public void Dispose(bool disposing)
  152. {
  153. Flags = CapsFlags.None;
  154. if (m_capsActive != null)
  155. {
  156. DeregisterHandlers();
  157. m_capsActive.Dispose();
  158. m_capsActive = null;
  159. }
  160. }
  161. /// <summary>
  162. /// Register a handler. This allows modules to register handlers.
  163. /// </summary>
  164. /// <param name="capName"></param>
  165. /// <param name="handler"></param>
  166. public void RegisterHandler(string capName, IRequestHandler handler)
  167. {
  168. //m_log.DebugFormat("[CAPS]: Registering handler for \"{0}\": path {1}", capName, handler.Path);
  169. m_capsHandlers[capName] = handler;
  170. }
  171. public void RegisterSimpleHandler(string capName, ISimpleStreamHandler handler, bool addToListener = true)
  172. {
  173. //m_log.DebugFormat("[CAPS]: Registering handler for \"{0}\": path {1}", capName, handler.Path);
  174. m_capsHandlers.AddSimpleHandler(capName, handler, addToListener);
  175. }
  176. public void RegisterPollHandler(string capName, PollServiceEventArgs pollServiceHandler)
  177. {
  178. //m_log.DebugFormat(
  179. // "[CAPS]: Registering handler with name {0}, url {1} for {2}",
  180. // capName, pollServiceHandler.Url, m_agentID, m_regionName);
  181. if(!m_pollServiceHandlers.TryAdd(capName, pollServiceHandler))
  182. {
  183. m_log.ErrorFormat(
  184. "[CAPS]: Handler with name {0} already registered (ulr {1}, agent {2}, region {3}",
  185. capName, pollServiceHandler.Url, m_agentID, m_regionName);
  186. return;
  187. }
  188. m_httpListener.AddPollServiceHTTPHandler(pollServiceHandler);
  189. //uint port = (MainServer.Instance == null) ? 0 : MainServer.Instance.Port;
  190. //string protocol = "http";
  191. //string hostName = m_httpListenerHostName;
  192. //if (MainServer.Instance.UseSSL)
  193. //{
  194. // hostName = MainServer.Instance.SSLCommonName;
  195. // port = MainServer.Instance.SSLPort;
  196. // protocol = "https";
  197. //}
  198. //RegisterHandler(
  199. // capName, String.Format("{0}://{1}:{2}{3}", protocol, hostName, port, pollServiceHandler.Url));
  200. }
  201. /// <summary>
  202. /// Register an external handler. The service for this capability is somewhere else
  203. /// given by the URL.
  204. /// </summary>
  205. /// <param name="capsName"></param>
  206. /// <param name="url"></param>
  207. public void RegisterHandler(string capsName, string url)
  208. {
  209. m_externalCapsHandlers.Add(capsName, url);
  210. }
  211. /// <summary>
  212. /// Remove all CAPS service handlers.
  213. /// </summary>
  214. public void DeregisterHandlers()
  215. {
  216. foreach (string capsName in m_capsHandlers.Caps)
  217. {
  218. m_capsHandlers.Remove(capsName);
  219. }
  220. foreach (PollServiceEventArgs handler in m_pollServiceHandlers.Values)
  221. {
  222. m_httpListener.RemovePollServiceHTTPHandler(handler.Url);
  223. }
  224. m_pollServiceHandlers.Clear();
  225. }
  226. public bool TryGetPollHandler(string name, out PollServiceEventArgs pollHandler)
  227. {
  228. return m_pollServiceHandlers.TryGetValue(name, out pollHandler);
  229. }
  230. public Dictionary<string, PollServiceEventArgs> GetPollHandlers()
  231. {
  232. return new Dictionary<string, PollServiceEventArgs>(m_pollServiceHandlers);
  233. }
  234. /// <summary>
  235. /// Return an LLSD-serializable Hashtable describing the
  236. /// capabilities and their handler details.
  237. /// </summary>
  238. /// <param name="excludeSeed">If true, then exclude the seed cap.</param>
  239. public Hashtable GetCapsDetails(bool excludeSeed, List<string> requestedCaps)
  240. {
  241. Hashtable caps = CapsHandlers.GetCapsDetails(excludeSeed, requestedCaps);
  242. lock (m_pollServiceHandlers)
  243. {
  244. foreach (KeyValuePair<string, PollServiceEventArgs> kvp in m_pollServiceHandlers)
  245. {
  246. if (!requestedCaps.Contains(kvp.Key))
  247. continue;
  248. string hostName = m_httpListenerHostName;
  249. uint port = (MainServer.Instance is null) ? 0 : MainServer.Instance.Port;
  250. string protocol = "http";
  251. if (MainServer.Instance.UseSSL)
  252. {
  253. hostName = MainServer.Instance.SSLCommonName;
  254. port = MainServer.Instance.SSLPort;
  255. protocol = "https";
  256. }
  257. caps[kvp.Key] = string.Format("{0}://{1}:{2}{3}", protocol, hostName, port, kvp.Value.Url);
  258. }
  259. }
  260. // Add the external too
  261. foreach (KeyValuePair<string, string> kvp in ExternalCapsHandlers)
  262. {
  263. if (!requestedCaps.Contains(kvp.Key))
  264. continue;
  265. caps[kvp.Key] = kvp.Value;
  266. }
  267. return caps;
  268. }
  269. public void GetCapsDetailsLLSDxml(HashSet<string> requestedCaps, osUTF8 sb)
  270. {
  271. CapsHandlers.GetCapsDetailsLLSDxml(requestedCaps, sb);
  272. lock (m_pollServiceHandlers)
  273. {
  274. foreach (KeyValuePair<string, PollServiceEventArgs> kvp in m_pollServiceHandlers)
  275. {
  276. if (requestedCaps.Contains(kvp.Key))
  277. LLSDxmlEncode2.AddElem(kvp.Key, m_baseCapsURL + kvp.Value.Url, sb);
  278. }
  279. }
  280. // Add the external too
  281. foreach (KeyValuePair<string, string> kvp in ExternalCapsHandlers)
  282. {
  283. if (requestedCaps.Contains(kvp.Key))
  284. LLSDxmlEncode2.AddElem(kvp.Key, kvp.Value, sb);
  285. }
  286. }
  287. public void Activate()
  288. {
  289. m_capsActive.Set();
  290. }
  291. public bool WaitForActivation()
  292. {
  293. // Wait for 30s. If that elapses, return false and run without caps
  294. return m_capsActive.WaitOne(120000);
  295. }
  296. }
  297. }