CapsHandlers.cs 7.6 KB

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