1
0

CapsHandlers.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.Collections;
  28. using System.Collections.Generic;
  29. using System.Collections.Concurrent;
  30. using OpenSim.Framework.Servers;
  31. using OpenSim.Framework.Servers.HttpServer;
  32. using OpenMetaverse;
  33. using OpenMetaverse.StructuredData;
  34. namespace OpenSim.Framework.Capabilities
  35. {
  36. /// <summary>
  37. /// CapsHandlers is a cap handler container but also takes
  38. /// care of adding and removing cap handlers to and from the
  39. /// supplied BaseHttpServer.
  40. /// </summary>
  41. public class CapsHandlers
  42. {
  43. private readonly Dictionary<string, IRequestHandler> m_capsHandlers = new Dictionary<string, IRequestHandler>();
  44. private readonly ConcurrentDictionary<string, ISimpleStreamHandler> m_capsSimpleHandlers = new ConcurrentDictionary<string, ISimpleStreamHandler>();
  45. private IHttpServer m_httpListener;
  46. private string m_httpListenerHostName;
  47. private uint m_httpListenerPort;
  48. public readonly string BaseURL;
  49. /// <summary></summary>
  50. /// CapsHandlers is a cap handler container but also takes
  51. /// care of adding and removing cap handlers to and from the
  52. /// supplied BaseHttpServer.
  53. /// </summary>
  54. /// <param name="httpListener">base HTTP server</param>
  55. /// <param name="httpListenerHostname">host name of the HTTP server</param>
  56. /// <param name="httpListenerPort">HTTP port</param>
  57. public CapsHandlers(IHttpServer httpListener, string httpListenerHostname, uint httpListenerPort)
  58. {
  59. m_httpListener = httpListener;
  60. m_httpListenerHostName = httpListenerHostname;
  61. m_httpListenerPort = httpListenerPort;
  62. if (httpListener != null && httpListener.UseSSL)
  63. BaseURL = $"https://{m_httpListenerHostName}:{m_httpListenerPort}";
  64. else
  65. BaseURL = $"http://{m_httpListenerHostName}:{m_httpListenerPort}";
  66. }
  67. /// <summary>
  68. /// Remove the cap handler for a capability.
  69. /// </summary>
  70. /// <param name="capsName">name of the capability of the cap
  71. /// handler to be removed</param>
  72. public void Remove(string capsName)
  73. {
  74. lock (m_capsHandlers)
  75. {
  76. if(m_capsHandlers.ContainsKey(capsName))
  77. {
  78. m_httpListener.RemoveStreamHandler("POST", m_capsHandlers[capsName].Path);
  79. m_httpListener.RemoveStreamHandler("PUT", m_capsHandlers[capsName].Path);
  80. m_httpListener.RemoveStreamHandler("GET", m_capsHandlers[capsName].Path);
  81. m_httpListener.RemoveStreamHandler("DELETE", m_capsHandlers[capsName].Path);
  82. m_capsHandlers.Remove(capsName);
  83. }
  84. }
  85. if(m_capsSimpleHandlers.TryRemove(capsName, out ISimpleStreamHandler hdr))
  86. {
  87. m_httpListener.RemoveSimpleStreamHandler(hdr.Path);
  88. }
  89. }
  90. public void AddSimpleHandler(string capName, ISimpleStreamHandler handler, bool addToListener = true)
  91. {
  92. if(ContainsCap(capName))
  93. Remove(capName);
  94. if(m_capsSimpleHandlers.TryAdd(capName, handler) && addToListener)
  95. m_httpListener.AddSimpleStreamHandler(handler);
  96. }
  97. public bool ContainsCap(string cap)
  98. {
  99. lock (m_capsHandlers)
  100. if (m_capsHandlers.ContainsKey(cap))
  101. return true;
  102. return m_capsSimpleHandlers.ContainsKey(cap);
  103. }
  104. /// <summary>
  105. /// The indexer allows us to treat the CapsHandlers object
  106. /// in an intuitive dictionary like way.
  107. /// </summary>
  108. /// <remarks>
  109. /// The indexer will throw an exception when you try to
  110. /// retrieve a cap handler for a cap that is not contained in
  111. /// CapsHandlers.
  112. /// </remarks>
  113. public IRequestHandler this[string idx]
  114. {
  115. get
  116. {
  117. lock (m_capsHandlers)
  118. return m_capsHandlers[idx];
  119. }
  120. set
  121. {
  122. lock (m_capsHandlers)
  123. {
  124. if (m_capsHandlers.ContainsKey(idx))
  125. {
  126. m_httpListener.RemoveStreamHandler("POST", m_capsHandlers[idx].Path);
  127. m_httpListener.RemoveStreamHandler("PUT", m_capsHandlers[idx].Path);
  128. m_httpListener.RemoveStreamHandler("GET", m_capsHandlers[idx].Path);
  129. m_httpListener.RemoveStreamHandler("DELETE", m_capsHandlers[idx].Path);
  130. m_capsHandlers.Remove(idx);
  131. }
  132. if (null == value) return;
  133. m_capsHandlers[idx] = value;
  134. m_httpListener.AddStreamHandler(value);
  135. }
  136. }
  137. }
  138. /// <summary>
  139. /// Return the list of cap names for which this CapsHandlers
  140. /// object contains cap handlers.
  141. /// </summary>
  142. public string[] Caps
  143. {
  144. get
  145. {
  146. lock (m_capsHandlers)
  147. {
  148. string[] __keys = new string[m_capsHandlers.Keys.Count + m_capsSimpleHandlers.Keys.Count];
  149. m_capsHandlers.Keys.CopyTo(__keys, 0);
  150. m_capsSimpleHandlers.Keys.CopyTo(__keys, m_capsHandlers.Keys.Count);
  151. return __keys;
  152. }
  153. }
  154. }
  155. /// <summary>
  156. /// Return an LLSD-serializable Hashtable describing the
  157. /// capabilities and their handler details.
  158. /// </summary>
  159. /// <param name="excludeSeed">If true, then exclude the seed cap.</param>
  160. public Hashtable GetCapsDetails(bool excludeSeed, List<string> requestedCaps)
  161. {
  162. Hashtable caps = new Hashtable();
  163. if (requestedCaps == null)
  164. {
  165. lock (m_capsHandlers)
  166. {
  167. foreach (KeyValuePair<string, ISimpleStreamHandler> kvp in m_capsSimpleHandlers)
  168. caps[kvp.Key] = BaseURL + kvp.Value.Path;
  169. foreach (KeyValuePair<string, IRequestHandler> kvp in m_capsHandlers)
  170. caps[kvp.Key] = BaseURL + kvp.Value.Path;
  171. }
  172. return caps;
  173. }
  174. lock (m_capsHandlers)
  175. {
  176. for (int i = 0; i < requestedCaps.Count; ++i)
  177. {
  178. string capsName = requestedCaps[i];
  179. if (excludeSeed && "SEED" == capsName)
  180. continue;
  181. if (m_capsSimpleHandlers.TryGetValue(capsName, out ISimpleStreamHandler shdr))
  182. {
  183. caps[capsName] = BaseURL + shdr.Path;
  184. continue;
  185. }
  186. if (m_capsHandlers.TryGetValue(capsName, out IRequestHandler chdr))
  187. {
  188. caps[capsName] = BaseURL + chdr.Path;
  189. }
  190. }
  191. }
  192. return caps;
  193. }
  194. public Dictionary<string, string> GetCapsLocalPaths()
  195. {
  196. Dictionary<string, string> caps = new();
  197. lock (m_capsHandlers)
  198. {
  199. foreach (KeyValuePair<string, ISimpleStreamHandler> kvp in m_capsSimpleHandlers)
  200. caps[kvp.Key] = kvp.Value.Path;
  201. foreach (KeyValuePair<string, IRequestHandler> kvp in m_capsHandlers)
  202. caps[kvp.Key] = kvp.Value.Path;
  203. }
  204. return caps;
  205. }
  206. public void GetCapsDetailsLLSDxml(HashSet<string> requestedCaps, osUTF8 sb)
  207. {
  208. lock (m_capsHandlers)
  209. {
  210. if (requestedCaps is null)
  211. return;
  212. foreach (string capsName in requestedCaps)
  213. {
  214. if (m_capsSimpleHandlers.TryGetValue(capsName, out ISimpleStreamHandler shdr))
  215. LLSDxmlEncode2.AddElem(capsName, BaseURL + shdr.Path, sb);
  216. else if (m_capsHandlers.TryGetValue(capsName, out IRequestHandler chdr))
  217. LLSDxmlEncode2.AddElem(capsName, BaseURL + chdr.Path, sb);
  218. }
  219. }
  220. }
  221. /// <summary>
  222. /// Returns a copy of the dictionary of all the HTTP cap handlers
  223. /// </summary>
  224. /// <returns>
  225. /// The dictionary copy. The key is the capability name, the value is the HTTP handler.
  226. /// </returns>
  227. public Dictionary<string, IRequestHandler> GetCapsHandlers()
  228. {
  229. lock (m_capsHandlers)
  230. return new Dictionary<string, IRequestHandler>(m_capsHandlers);
  231. }
  232. }
  233. }