CapsHandlers.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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("PUT", m_capsHandlers[capsName].Path);
  89. m_httpListener.RemoveStreamHandler("GET", m_capsHandlers[capsName].Path);
  90. m_capsHandlers.Remove(capsName);
  91. }
  92. }
  93. public bool ContainsCap(string cap)
  94. {
  95. lock (m_capsHandlers)
  96. return m_capsHandlers.ContainsKey(cap);
  97. }
  98. /// <summary>
  99. /// The indexer allows us to treat the CapsHandlers object
  100. /// in an intuitive dictionary like way.
  101. /// </summary>
  102. /// <remarks>
  103. /// The indexer will throw an exception when you try to
  104. /// retrieve a cap handler for a cap that is not contained in
  105. /// CapsHandlers.
  106. /// </remarks>
  107. public IRequestHandler this[string idx]
  108. {
  109. get
  110. {
  111. lock (m_capsHandlers)
  112. return m_capsHandlers[idx];
  113. }
  114. set
  115. {
  116. lock (m_capsHandlers)
  117. {
  118. if (m_capsHandlers.ContainsKey(idx))
  119. {
  120. m_httpListener.RemoveStreamHandler("POST", m_capsHandlers[idx].Path);
  121. m_capsHandlers.Remove(idx);
  122. }
  123. if (null == value) return;
  124. m_capsHandlers[idx] = value;
  125. m_httpListener.AddStreamHandler(value);
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// Return the list of cap names for which this CapsHandlers
  131. /// object contains cap handlers.
  132. /// </summary>
  133. public string[] Caps
  134. {
  135. get
  136. {
  137. lock (m_capsHandlers)
  138. {
  139. string[] __keys = new string[m_capsHandlers.Keys.Count];
  140. m_capsHandlers.Keys.CopyTo(__keys, 0);
  141. return __keys;
  142. }
  143. }
  144. }
  145. /// <summary>
  146. /// Return an LLSD-serializable Hashtable describing the
  147. /// capabilities and their handler details.
  148. /// </summary>
  149. /// <param name="excludeSeed">If true, then exclude the seed cap.</param>
  150. public Hashtable GetCapsDetails(bool excludeSeed, List<string> requestedCaps)
  151. {
  152. Hashtable caps = new Hashtable();
  153. string protocol = "http://";
  154. if (m_useSSL)
  155. protocol = "https://";
  156. string baseUrl = protocol + m_httpListenerHostName + ":" + m_httpListenerPort.ToString();
  157. lock (m_capsHandlers)
  158. {
  159. foreach (string capsName in m_capsHandlers.Keys)
  160. {
  161. if (excludeSeed && "SEED" == capsName)
  162. continue;
  163. if (requestedCaps != null && !requestedCaps.Contains(capsName))
  164. continue;
  165. caps[capsName] = baseUrl + m_capsHandlers[capsName].Path;
  166. }
  167. }
  168. return caps;
  169. }
  170. /// <summary>
  171. /// Returns a copy of the dictionary of all the HTTP cap handlers
  172. /// </summary>
  173. /// <returns>
  174. /// The dictionary copy. The key is the capability name, the value is the HTTP handler.
  175. /// </returns>
  176. public Dictionary<string, IRequestHandler> GetCapsHandlers()
  177. {
  178. lock (m_capsHandlers)
  179. return new Dictionary<string, IRequestHandler>(m_capsHandlers);
  180. }
  181. }
  182. }