1
0

CapsHandlers.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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(IHttpServer httpListener, string httpListenerHostname, uint httpListenerPort)
  54. {
  55. m_httpListener = httpListener;
  56. m_httpListenerHostName = httpListenerHostname;
  57. m_httpListenerPort = httpListenerPort;
  58. if (httpListener != null && httpListener.UseSSL)
  59. m_useSSL = true;
  60. else
  61. m_useSSL = false;
  62. }
  63. /// <summary>
  64. /// Remove the cap handler for a capability.
  65. /// </summary>
  66. /// <param name="capsName">name of the capability of the cap
  67. /// handler to be removed</param>
  68. public void Remove(string capsName)
  69. {
  70. lock (m_capsHandlers)
  71. {
  72. m_httpListener.RemoveStreamHandler("POST", m_capsHandlers[capsName].Path);
  73. m_httpListener.RemoveStreamHandler("PUT", m_capsHandlers[capsName].Path);
  74. m_httpListener.RemoveStreamHandler("GET", m_capsHandlers[capsName].Path);
  75. m_capsHandlers.Remove(capsName);
  76. }
  77. }
  78. public bool ContainsCap(string cap)
  79. {
  80. lock (m_capsHandlers)
  81. return m_capsHandlers.ContainsKey(cap);
  82. }
  83. /// <summary>
  84. /// The indexer allows us to treat the CapsHandlers object
  85. /// in an intuitive dictionary like way.
  86. /// </summary>
  87. /// <remarks>
  88. /// The indexer will throw an exception when you try to
  89. /// retrieve a cap handler for a cap that is not contained in
  90. /// CapsHandlers.
  91. /// </remarks>
  92. public IRequestHandler this[string idx]
  93. {
  94. get
  95. {
  96. lock (m_capsHandlers)
  97. return m_capsHandlers[idx];
  98. }
  99. set
  100. {
  101. lock (m_capsHandlers)
  102. {
  103. if (m_capsHandlers.ContainsKey(idx))
  104. {
  105. m_httpListener.RemoveStreamHandler("POST", m_capsHandlers[idx].Path);
  106. m_capsHandlers.Remove(idx);
  107. }
  108. if (null == value) return;
  109. m_capsHandlers[idx] = value;
  110. m_httpListener.AddStreamHandler(value);
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// Return the list of cap names for which this CapsHandlers
  116. /// object contains cap handlers.
  117. /// </summary>
  118. public string[] Caps
  119. {
  120. get
  121. {
  122. lock (m_capsHandlers)
  123. {
  124. string[] __keys = new string[m_capsHandlers.Keys.Count];
  125. m_capsHandlers.Keys.CopyTo(__keys, 0);
  126. return __keys;
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// Return an LLSD-serializable Hashtable describing the
  132. /// capabilities and their handler details.
  133. /// </summary>
  134. /// <param name="excludeSeed">If true, then exclude the seed cap.</param>
  135. public Hashtable GetCapsDetails(bool excludeSeed, List<string> requestedCaps)
  136. {
  137. Hashtable caps = new Hashtable();
  138. string protocol = "http://";
  139. if (m_useSSL)
  140. protocol = "https://";
  141. string baseUrl = protocol + m_httpListenerHostName + ":" + m_httpListenerPort.ToString();
  142. lock (m_capsHandlers)
  143. {
  144. foreach (string capsName in m_capsHandlers.Keys)
  145. {
  146. if (excludeSeed && "SEED" == capsName)
  147. continue;
  148. if (requestedCaps != null && !requestedCaps.Contains(capsName))
  149. continue;
  150. caps[capsName] = baseUrl + m_capsHandlers[capsName].Path;
  151. }
  152. }
  153. return caps;
  154. }
  155. /// <summary>
  156. /// Returns a copy of the dictionary of all the HTTP cap handlers
  157. /// </summary>
  158. /// <returns>
  159. /// The dictionary copy. The key is the capability name, the value is the HTTP handler.
  160. /// </returns>
  161. public Dictionary<string, IRequestHandler> GetCapsHandlers()
  162. {
  163. lock (m_capsHandlers)
  164. return new Dictionary<string, IRequestHandler>(m_capsHandlers);
  165. }
  166. }
  167. }