CapsHandlers.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 OpenSim 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. namespace OpenSim.Framework.Communications.Capabilities
  31. {
  32. /// <summary>
  33. /// CapsHandlers is a cap handler container but also takes
  34. /// care of adding and removing cap handlers to and from the
  35. /// supplied BaseHttpServer.
  36. /// </summary>
  37. public class CapsHandlers
  38. {
  39. private Dictionary <string, IRequestHandler> m_capsHandlers = new Dictionary<string, IRequestHandler>();
  40. private BaseHttpServer m_httpListener;
  41. private string m_httpListenerHostName;
  42. private uint m_httpListenerPort;
  43. private bool m_useSSL = false;
  44. /// <summary></summary>
  45. /// CapsHandlers is a cap handler container but also takes
  46. /// care of adding and removing cap handlers to and from the
  47. /// supplied BaseHttpServer.
  48. /// </summary>
  49. /// <param name="httpListener">base HTTP server</param>
  50. /// <param name="httpListenerHostname">host name of the HTTP
  51. /// 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(BaseHttpServer 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 (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. // This line must be here, or caps will break!
  86. m_httpListener.RemoveStreamHandler("POST", m_capsHandlers[capsName].Path);
  87. m_capsHandlers.Remove(capsName);
  88. }
  89. public bool ContainsCap(string cap)
  90. {
  91. return m_capsHandlers.ContainsKey(cap);
  92. }
  93. /// <summary>
  94. /// The indexer allows us to treat the CapsHandlers object
  95. /// in an intuitive dictionary like way.
  96. /// </summary>
  97. /// <Remarks>
  98. /// The indexer will throw an exception when you try to
  99. /// retrieve a cap handler for a cap that is not contained in
  100. /// CapsHandlers.
  101. /// </Remarks>
  102. public IRequestHandler this[string idx]
  103. {
  104. get
  105. {
  106. return m_capsHandlers[idx];
  107. }
  108. set
  109. {
  110. if (m_capsHandlers.ContainsKey(idx))
  111. {
  112. m_httpListener.RemoveStreamHandler("POST", m_capsHandlers[idx].Path);
  113. m_capsHandlers.Remove(idx);
  114. }
  115. if (null == value) return;
  116. m_capsHandlers[idx] = value;
  117. m_httpListener.AddStreamHandler(value);
  118. }
  119. }
  120. /// <summary>
  121. /// Return the list of cap names for which this CapsHandlers
  122. /// object contains cap handlers.
  123. /// </summary>
  124. public string[] Caps
  125. {
  126. get
  127. {
  128. string[] __keys = new string[m_capsHandlers.Keys.Count];
  129. m_capsHandlers.Keys.CopyTo(__keys, 0);
  130. return __keys;
  131. }
  132. }
  133. /// <summary>
  134. /// Return an LLSD-serializable Hashtable describing the
  135. /// capabilities and their handler details.
  136. /// </summary>
  137. public Hashtable CapsDetails
  138. {
  139. get
  140. {
  141. Hashtable caps = new Hashtable();
  142. string protocol = "http://";
  143. if (m_useSSL)
  144. protocol = "https://";
  145. string baseUrl = protocol + m_httpListenerHostName + ":" + m_httpListenerPort.ToString();
  146. foreach (string capsName in m_capsHandlers.Keys)
  147. {
  148. // skip SEED cap
  149. if ("SEED" == capsName) continue;
  150. caps[capsName] = baseUrl + m_capsHandlers[capsName].Path;
  151. }
  152. return caps;
  153. }
  154. }
  155. }
  156. }