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