CapsHandlers.cs 6.6 KB

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