CapsHandlers.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. namespace OpenSim.Framework.Capabilities
  33. {
  34. /// <summary>
  35. /// CapsHandlers is a cap handler container but also takes
  36. /// care of adding and removing cap handlers to and from the
  37. /// supplied BaseHttpServer.
  38. /// </summary>
  39. public class CapsHandlers
  40. {
  41. private Dictionary<string, IRequestHandler> m_capsHandlers = new Dictionary<string, IRequestHandler>();
  42. private ConcurrentDictionary<string, ISimpleStreamHandler> m_capsSimpleHandlers = new ConcurrentDictionary<string, ISimpleStreamHandler>();
  43. private IHttpServer m_httpListener;
  44. private string m_httpListenerHostName;
  45. private uint m_httpListenerPort;
  46. private bool m_useSSL = false;
  47. /// <summary></summary>
  48. /// CapsHandlers is a cap handler container but also takes
  49. /// care of adding and removing cap handlers to and from the
  50. /// supplied BaseHttpServer.
  51. /// </summary>
  52. /// <param name="httpListener">base HTTP server</param>
  53. /// <param name="httpListenerHostname">host name of the HTTP server</param>
  54. /// <param name="httpListenerPort">HTTP port</param>
  55. public CapsHandlers(IHttpServer httpListener, string httpListenerHostname, uint httpListenerPort)
  56. {
  57. m_httpListener = httpListener;
  58. m_httpListenerHostName = httpListenerHostname;
  59. m_httpListenerPort = httpListenerPort;
  60. if (httpListener != null && httpListener.UseSSL)
  61. m_useSSL = true;
  62. else
  63. m_useSSL = false;
  64. }
  65. /// <summary>
  66. /// Remove the cap handler for a capability.
  67. /// </summary>
  68. /// <param name="capsName">name of the capability of the cap
  69. /// handler to be removed</param>
  70. public void Remove(string capsName)
  71. {
  72. lock (m_capsHandlers)
  73. {
  74. if(m_capsHandlers.ContainsKey(capsName))
  75. {
  76. m_httpListener.RemoveStreamHandler("POST", m_capsHandlers[capsName].Path);
  77. m_httpListener.RemoveStreamHandler("PUT", m_capsHandlers[capsName].Path);
  78. m_httpListener.RemoveStreamHandler("GET", m_capsHandlers[capsName].Path);
  79. m_httpListener.RemoveStreamHandler("DELETE", m_capsHandlers[capsName].Path);
  80. m_capsHandlers.Remove(capsName);
  81. }
  82. }
  83. if(m_capsSimpleHandlers.TryRemove(capsName, out ISimpleStreamHandler hdr))
  84. {
  85. m_httpListener.RemoveSimpleStreamHandler(hdr.Path);
  86. }
  87. }
  88. public void AddSimpleHandler(string capName, ISimpleStreamHandler handler, bool addToListener = true)
  89. {
  90. if(ContainsCap(capName))
  91. Remove(capName);
  92. if(m_capsSimpleHandlers.TryAdd(capName, handler) && addToListener)
  93. m_httpListener.AddSimpleStreamHandler(handler);
  94. }
  95. public bool ContainsCap(string cap)
  96. {
  97. lock (m_capsHandlers)
  98. if (m_capsHandlers.ContainsKey(cap))
  99. return true;
  100. return m_capsSimpleHandlers.ContainsKey(cap);
  101. }
  102. /// <summary>
  103. /// The indexer allows us to treat the CapsHandlers object
  104. /// in an intuitive dictionary like way.
  105. /// </summary>
  106. /// <remarks>
  107. /// The indexer will throw an exception when you try to
  108. /// retrieve a cap handler for a cap that is not contained in
  109. /// CapsHandlers.
  110. /// </remarks>
  111. public IRequestHandler this[string idx]
  112. {
  113. get
  114. {
  115. lock (m_capsHandlers)
  116. return m_capsHandlers[idx];
  117. }
  118. set
  119. {
  120. lock (m_capsHandlers)
  121. {
  122. if (m_capsHandlers.ContainsKey(idx))
  123. {
  124. m_httpListener.RemoveStreamHandler("POST", m_capsHandlers[idx].Path);
  125. m_httpListener.RemoveStreamHandler("PUT", m_capsHandlers[idx].Path);
  126. m_httpListener.RemoveStreamHandler("GET", m_capsHandlers[idx].Path);
  127. m_httpListener.RemoveStreamHandler("DELETE", m_capsHandlers[idx].Path);
  128. m_capsHandlers.Remove(idx);
  129. }
  130. if (null == value) return;
  131. m_capsHandlers[idx] = value;
  132. m_httpListener.AddStreamHandler(value);
  133. }
  134. }
  135. }
  136. /// <summary>
  137. /// Return the list of cap names for which this CapsHandlers
  138. /// object contains cap handlers.
  139. /// </summary>
  140. public string[] Caps
  141. {
  142. get
  143. {
  144. lock (m_capsHandlers)
  145. {
  146. string[] __keys = new string[m_capsHandlers.Keys.Count + m_capsSimpleHandlers.Keys.Count];
  147. m_capsHandlers.Keys.CopyTo(__keys, 0);
  148. m_capsSimpleHandlers.Keys.CopyTo(__keys, m_capsHandlers.Keys.Count);
  149. return __keys;
  150. }
  151. }
  152. }
  153. /// <summary>
  154. /// Return an LLSD-serializable Hashtable describing the
  155. /// capabilities and their handler details.
  156. /// </summary>
  157. /// <param name="excludeSeed">If true, then exclude the seed cap.</param>
  158. public Hashtable GetCapsDetails(bool excludeSeed, List<string> requestedCaps)
  159. {
  160. Hashtable caps = new Hashtable();
  161. string protocol = m_useSSL ? "https://" : "http://";
  162. string baseUrl = protocol + m_httpListenerHostName + ":" + m_httpListenerPort.ToString();
  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. /// <summary>
  195. /// Returns a copy of the dictionary of all the HTTP cap handlers
  196. /// </summary>
  197. /// <returns>
  198. /// The dictionary copy. The key is the capability name, the value is the HTTP handler.
  199. /// </returns>
  200. public Dictionary<string, IRequestHandler> GetCapsHandlers()
  201. {
  202. lock (m_capsHandlers)
  203. return new Dictionary<string, IRequestHandler>(m_capsHandlers);
  204. }
  205. }
  206. }