Caps.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.IO;
  31. using System.Reflection;
  32. using log4net;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenSim.Framework.Servers;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Services.Interfaces;
  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
  48. {
  49. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. private string m_httpListenerHostName;
  51. private 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 string m_capsObjectPath;
  56. public string CapsObjectPath { get { return m_capsObjectPath; } }
  57. private CapsHandlers m_capsHandlers;
  58. private Dictionary<string, PollServiceEventArgs> m_pollServiceHandlers
  59. = new Dictionary<string, PollServiceEventArgs>();
  60. private Dictionary<string, string> m_externalCapsHandlers = new Dictionary<string, string>();
  61. private IHttpServer m_httpListener;
  62. private UUID m_agentID;
  63. private string m_regionName;
  64. public UUID AgentID
  65. {
  66. get { return m_agentID; }
  67. }
  68. public string RegionName
  69. {
  70. get { return m_regionName; }
  71. }
  72. public string HostName
  73. {
  74. get { return m_httpListenerHostName; }
  75. }
  76. public uint Port
  77. {
  78. get { return m_httpListenPort; }
  79. }
  80. public IHttpServer HttpListener
  81. {
  82. get { return m_httpListener; }
  83. }
  84. public bool SSLCaps
  85. {
  86. get { return m_httpListener.UseSSL; }
  87. }
  88. public string SSLCommonName
  89. {
  90. get { return m_httpListener.SSLCommonName; }
  91. }
  92. public CapsHandlers CapsHandlers
  93. {
  94. get { return m_capsHandlers; }
  95. }
  96. public Dictionary<string, string> ExternalCapsHandlers
  97. {
  98. get { return m_externalCapsHandlers; }
  99. }
  100. public Caps(IHttpServer httpServer, string httpListen, uint httpPort, string capsPath,
  101. UUID agent, string regionName)
  102. {
  103. m_capsObjectPath = capsPath;
  104. m_httpListener = httpServer;
  105. m_httpListenerHostName = httpListen;
  106. m_httpListenPort = httpPort;
  107. if (httpServer != null && httpServer.UseSSL)
  108. {
  109. m_httpListenPort = httpServer.SSLPort;
  110. httpListen = httpServer.SSLCommonName;
  111. httpPort = httpServer.SSLPort;
  112. }
  113. m_agentID = agent;
  114. m_capsHandlers = new CapsHandlers(httpServer, httpListen, httpPort, (httpServer == null) ? false : httpServer.UseSSL);
  115. m_regionName = regionName;
  116. }
  117. /// <summary>
  118. /// Register a handler. This allows modules to register handlers.
  119. /// </summary>
  120. /// <param name="capName"></param>
  121. /// <param name="handler"></param>
  122. public void RegisterHandler(string capName, IRequestHandler handler)
  123. {
  124. //m_log.DebugFormat("[CAPS]: Registering handler for \"{0}\": path {1}", capName, handler.Path);
  125. m_capsHandlers[capName] = handler;
  126. }
  127. public void RegisterPollHandler(string capName, PollServiceEventArgs pollServiceHandler)
  128. {
  129. // m_log.DebugFormat(
  130. // "[CAPS]: Registering handler with name {0}, url {1} for {2}",
  131. // capName, pollServiceHandler.Url, m_agentID, m_regionName);
  132. m_pollServiceHandlers.Add(capName, pollServiceHandler);
  133. m_httpListener.AddPollServiceHTTPHandler(pollServiceHandler.Url, pollServiceHandler);
  134. // uint port = (MainServer.Instance == null) ? 0 : MainServer.Instance.Port;
  135. // string protocol = "http";
  136. // string hostName = m_httpListenerHostName;
  137. //
  138. // if (MainServer.Instance.UseSSL)
  139. // {
  140. // hostName = MainServer.Instance.SSLCommonName;
  141. // port = MainServer.Instance.SSLPort;
  142. // protocol = "https";
  143. // }
  144. // RegisterHandler(
  145. // capName, String.Format("{0}://{1}:{2}{3}", protocol, hostName, port, pollServiceHandler.Url));
  146. }
  147. /// <summary>
  148. /// Register an external handler. The service for this capability is somewhere else
  149. /// given by the URL.
  150. /// </summary>
  151. /// <param name="capsName"></param>
  152. /// <param name="url"></param>
  153. public void RegisterHandler(string capsName, string url)
  154. {
  155. m_externalCapsHandlers.Add(capsName, url);
  156. }
  157. /// <summary>
  158. /// Remove all CAPS service handlers.
  159. /// </summary>
  160. public void DeregisterHandlers()
  161. {
  162. foreach (string capsName in m_capsHandlers.Caps)
  163. {
  164. m_capsHandlers.Remove(capsName);
  165. }
  166. foreach (PollServiceEventArgs handler in m_pollServiceHandlers.Values)
  167. {
  168. m_httpListener.RemovePollServiceHTTPHandler("", handler.Url);
  169. }
  170. }
  171. public bool TryGetPollHandler(string name, out PollServiceEventArgs pollHandler)
  172. {
  173. return m_pollServiceHandlers.TryGetValue(name, out pollHandler);
  174. }
  175. public Dictionary<string, PollServiceEventArgs> GetPollHandlers()
  176. {
  177. return new Dictionary<string, PollServiceEventArgs>(m_pollServiceHandlers);
  178. }
  179. /// <summary>
  180. /// Return an LLSD-serializable Hashtable describing the
  181. /// capabilities and their handler details.
  182. /// </summary>
  183. /// <param name="excludeSeed">If true, then exclude the seed cap.</param>
  184. public Hashtable GetCapsDetails(bool excludeSeed, List<string> requestedCaps)
  185. {
  186. Hashtable caps = CapsHandlers.GetCapsDetails(excludeSeed, requestedCaps);
  187. lock (m_pollServiceHandlers)
  188. {
  189. foreach (KeyValuePair <string, PollServiceEventArgs> kvp in m_pollServiceHandlers)
  190. {
  191. if (!requestedCaps.Contains(kvp.Key))
  192. continue;
  193. string hostName = m_httpListenerHostName;
  194. uint port = (MainServer.Instance == null) ? 0 : MainServer.Instance.Port;
  195. string protocol = "http";
  196. if (MainServer.Instance.UseSSL)
  197. {
  198. hostName = MainServer.Instance.SSLCommonName;
  199. port = MainServer.Instance.SSLPort;
  200. protocol = "https";
  201. }
  202. //
  203. // caps.RegisterHandler("FetchInventoryDescendents2", String.Format("{0}://{1}:{2}{3}", protocol, hostName, port, capUrl));
  204. caps[kvp.Key] = string.Format("{0}://{1}:{2}{3}", protocol, hostName, port, kvp.Value.Url);
  205. }
  206. }
  207. // Add the external too
  208. foreach (KeyValuePair<string, string> kvp in ExternalCapsHandlers)
  209. {
  210. if (!requestedCaps.Contains(kvp.Key))
  211. continue;
  212. caps[kvp.Key] = kvp.Value;
  213. }
  214. return caps;
  215. }
  216. }
  217. }