diff options
Diffstat (limited to 'Servers/CapsHttpServer.cs')
-rw-r--r-- | Servers/CapsHttpServer.cs | 332 |
1 files changed, 332 insertions, 0 deletions
diff --git a/Servers/CapsHttpServer.cs b/Servers/CapsHttpServer.cs new file mode 100644 index 0000000..1e62c81 --- /dev/null +++ b/Servers/CapsHttpServer.cs | |||
@@ -0,0 +1,332 @@ | |||
1 | /* | ||
2 | Copyright (c) OpenSimCAPS project, http://osgrid.org/ | ||
3 | |||
4 | |||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions are met: | ||
9 | * * Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * * Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * * Neither the name of the <organization> nor the | ||
15 | * names of its contributors may be used to endorse or promote products | ||
16 | * derived from this software without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY | ||
19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
21 | * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY | ||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | using System; | ||
31 | using System.Text; | ||
32 | using Nwc.XmlRpc; | ||
33 | using System.Threading; | ||
34 | using System.Text.RegularExpressions; | ||
35 | using System.Net; | ||
36 | using System.IO; | ||
37 | using System.Collections; | ||
38 | using System.Collections.Generic; | ||
39 | using libsecondlife; | ||
40 | using OpenSim.Framework.Console; | ||
41 | using OpenSim.Framework.Interfaces; | ||
42 | |||
43 | namespace OpenSim.Servers | ||
44 | { | ||
45 | // Dummy HTTP server, does nothing useful for now | ||
46 | |||
47 | public class CapsHttpServer | ||
48 | { | ||
49 | public Thread HTTPD; | ||
50 | public HttpListener Listener; | ||
51 | private string AdminPage; | ||
52 | private string NewAccountForm; | ||
53 | private string LoginForm; | ||
54 | private string passWord = "Admin"; | ||
55 | |||
56 | public CapsHttpServer() | ||
57 | { | ||
58 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Starting up HTTP Server"); | ||
59 | HTTPD = new Thread(new ThreadStart(StartHTTP)); | ||
60 | HTTPD.Start(); | ||
61 | LoadAdminPage(); | ||
62 | } | ||
63 | |||
64 | public void StartHTTP() | ||
65 | { | ||
66 | try | ||
67 | { | ||
68 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine("SimHttp.cs:StartHTTP() - Spawned main thread OK"); | ||
69 | Listener = new HttpListener(); | ||
70 | |||
71 | Listener.Prefixes.Add("http://+:" + OpenSimRoot.Instance.Cfg.IPListenPort + "/"); | ||
72 | Listener.Start(); | ||
73 | |||
74 | HttpListenerContext context; | ||
75 | while (true) | ||
76 | { | ||
77 | context = Listener.GetContext(); | ||
78 | ThreadPool.QueueUserWorkItem(new WaitCallback(HandleRequest), context); | ||
79 | } | ||
80 | } | ||
81 | catch (Exception e) | ||
82 | { | ||
83 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine(e.Message); | ||
84 | } | ||
85 | } | ||
86 | |||
87 | private string ParseXMLRPC(string requestBody) | ||
88 | { | ||
89 | try | ||
90 | { | ||
91 | XmlRpcRequest request = (XmlRpcRequest)(new XmlRpcRequestDeserializer()).Deserialize(requestBody); | ||
92 | |||
93 | Hashtable requestData = (Hashtable)request.Params[0]; | ||
94 | switch (request.MethodName) | ||
95 | { | ||
96 | case "expect_user": | ||
97 | AgentCircuitData agent_data = new AgentCircuitData(); | ||
98 | agent_data.SessionID = new LLUUID((string)requestData["session_id"]); | ||
99 | agent_data.SecureSessionID = new LLUUID((string)requestData["secure_session_id"]); | ||
100 | agent_data.firstname = (string)requestData["firstname"]; | ||
101 | agent_data.lastname = (string)requestData["lastname"]; | ||
102 | agent_data.AgentID = new LLUUID((string)requestData["agent_id"]); | ||
103 | agent_data.circuitcode = Convert.ToUInt32(requestData["circuit_code"]); | ||
104 | if (OpenSimRoot.Instance.GridServers.GridServer.GetName() == "Remote") | ||
105 | { | ||
106 | ((RemoteGridBase)OpenSimRoot.Instance.GridServers.GridServer).agentcircuits.Add((uint)agent_data.circuitcode, agent_data); | ||
107 | } | ||
108 | return "<?xml version=\"1.0\"?><methodResponse><params /></methodResponse>"; | ||
109 | break; | ||
110 | } | ||
111 | } | ||
112 | catch (Exception e) | ||
113 | { | ||
114 | Console.WriteLine(e.ToString()); | ||
115 | } | ||
116 | return ""; | ||
117 | } | ||
118 | |||
119 | private string ParseREST(string requestBody, string requestURL, string requestMethod) | ||
120 | { | ||
121 | string responseString = ""; | ||
122 | try | ||
123 | { | ||
124 | switch (requestURL) | ||
125 | { | ||
126 | case "/Admin/Accounts": | ||
127 | if (requestMethod == "GET") | ||
128 | { | ||
129 | responseString = "<p> Account management </p>"; | ||
130 | responseString += "<br> "; | ||
131 | responseString += "<p> Create New Account </p>"; | ||
132 | responseString += NewAccountForm; | ||
133 | } | ||
134 | break; | ||
135 | case "/Admin/Clients": | ||
136 | if (requestMethod == "GET") | ||
137 | { | ||
138 | responseString = " <p> Listing connected Clients </p>"; | ||
139 | OpenSim.world.Avatar TempAv; | ||
140 | foreach (libsecondlife.LLUUID UUID in OpenSimRoot.Instance.LocalWorld.Entities.Keys) | ||
141 | { | ||
142 | if (OpenSimRoot.Instance.LocalWorld.Entities[UUID].ToString() == "OpenSim.world.Avatar") | ||
143 | { | ||
144 | TempAv = (OpenSim.world.Avatar)OpenSimRoot.Instance.LocalWorld.Entities[UUID]; | ||
145 | responseString += "<p>"; | ||
146 | responseString += String.Format("{0,-16}{1,-16}{2,-25}{3,-25}{4,-16},{5,-16}", TempAv.firstname, TempAv.lastname, UUID, TempAv.ControllingClient.SessionID, TempAv.ControllingClient.CircuitCode, TempAv.ControllingClient.userEP.ToString()); | ||
147 | responseString += "</p>"; | ||
148 | } | ||
149 | } | ||
150 | } | ||
151 | break; | ||
152 | case "/Admin/NewAccount": | ||
153 | if (requestMethod == "POST") | ||
154 | { | ||
155 | string[] comp = new string[10]; | ||
156 | string[] passw = new string[3]; | ||
157 | string delimStr = "&"; | ||
158 | char[] delimiter = delimStr.ToCharArray(); | ||
159 | string delimStr2 = "="; | ||
160 | char[] delimiter2 = delimStr2.ToCharArray(); | ||
161 | |||
162 | //Console.WriteLine(requestBody); | ||
163 | comp = requestBody.Split(delimiter); | ||
164 | passw = comp[3].Split(delimiter2); | ||
165 | if (passw[1] == passWord) | ||
166 | { | ||
167 | responseString = "<p> New Account created </p>"; | ||
168 | } | ||
169 | else | ||
170 | { | ||
171 | responseString = "<p> Admin password is incorrect, please login with the correct password</p>"; | ||
172 | responseString += "<br><br>" + LoginForm; | ||
173 | } | ||
174 | } | ||
175 | break; | ||
176 | case "/Admin/Login": | ||
177 | if (requestMethod == "POST") | ||
178 | { | ||
179 | // Console.WriteLine(requestBody); | ||
180 | if (requestBody == passWord) | ||
181 | { | ||
182 | responseString = "<p> Login Successful </p>"; | ||
183 | } | ||
184 | else | ||
185 | { | ||
186 | responseString = "<p> Password Error </p>"; | ||
187 | responseString += "<p> Please Login with the correct password </p>"; | ||
188 | responseString += "<br><br> " + LoginForm; | ||
189 | } | ||
190 | } | ||
191 | break; | ||
192 | case "/Admin/Welcome": | ||
193 | if (requestMethod == "GET") | ||
194 | { | ||
195 | responseString = "Welcome to the OpenSim Admin Page"; | ||
196 | responseString += "<br><br><br> " + LoginForm; | ||
197 | |||
198 | } | ||
199 | break; | ||
200 | } | ||
201 | } | ||
202 | catch (Exception e) | ||
203 | { | ||
204 | Console.WriteLine(e.ToString()); | ||
205 | } | ||
206 | |||
207 | return responseString; | ||
208 | } | ||
209 | |||
210 | private string ParseLLSDXML(string requestBody) | ||
211 | { | ||
212 | // dummy function for now - IMPLEMENT ME! | ||
213 | return ""; | ||
214 | } | ||
215 | |||
216 | public void HandleRequest(Object stateinfo) | ||
217 | { | ||
218 | // Console.WriteLine("new http incoming"); | ||
219 | HttpListenerContext context = (HttpListenerContext)stateinfo; | ||
220 | |||
221 | HttpListenerRequest request = context.Request; | ||
222 | HttpListenerResponse response = context.Response; | ||
223 | |||
224 | response.KeepAlive = false; | ||
225 | response.SendChunked = false; | ||
226 | |||
227 | System.IO.Stream body = request.InputStream; | ||
228 | System.Text.Encoding encoding = System.Text.Encoding.UTF8; | ||
229 | System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding); | ||
230 | |||
231 | string requestBody = reader.ReadToEnd(); | ||
232 | body.Close(); | ||
233 | reader.Close(); | ||
234 | |||
235 | //Console.WriteLine(request.HttpMethod + " " + request.RawUrl + " Http/" + request.ProtocolVersion.ToString() + " content type: " + request.ContentType); | ||
236 | //Console.WriteLine(requestBody); | ||
237 | |||
238 | string responseString = ""; | ||
239 | switch (request.ContentType) | ||
240 | { | ||
241 | case "text/xml": | ||
242 | // must be XML-RPC, so pass to the XML-RPC parser | ||
243 | |||
244 | responseString = ParseXMLRPC(requestBody); | ||
245 | response.AddHeader("Content-type", "text/xml"); | ||
246 | break; | ||
247 | |||
248 | case "application/xml": | ||
249 | // probably LLSD we hope, otherwise it should be ignored by the parser | ||
250 | responseString = ParseLLSDXML(requestBody); | ||
251 | response.AddHeader("Content-type", "application/xml"); | ||
252 | break; | ||
253 | |||
254 | case "application/x-www-form-urlencoded": | ||
255 | // a form data POST so send to the REST parser | ||
256 | responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); | ||
257 | response.AddHeader("Content-type", "text/html"); | ||
258 | break; | ||
259 | |||
260 | case null: | ||
261 | if ((request.HttpMethod == "GET") && (request.RawUrl == "/Admin")) | ||
262 | { | ||
263 | responseString = AdminPage; | ||
264 | response.AddHeader("Content-type", "text/html"); | ||
265 | } | ||
266 | else | ||
267 | { | ||
268 | // must be REST or invalid crap, so pass to the REST parser | ||
269 | responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); | ||
270 | response.AddHeader("Content-type", "text/html"); | ||
271 | } | ||
272 | break; | ||
273 | |||
274 | } | ||
275 | |||
276 | byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); | ||
277 | System.IO.Stream output = response.OutputStream; | ||
278 | response.SendChunked = false; | ||
279 | response.ContentLength64 = buffer.Length; | ||
280 | output.Write(buffer, 0, buffer.Length); | ||
281 | output.Close(); | ||
282 | } | ||
283 | |||
284 | private void LoadAdminPage() | ||
285 | { | ||
286 | try | ||
287 | { | ||
288 | StreamReader SR; | ||
289 | string lines; | ||
290 | AdminPage = ""; | ||
291 | NewAccountForm = ""; | ||
292 | LoginForm = ""; | ||
293 | SR = File.OpenText("testadmin.htm"); | ||
294 | |||
295 | while (!SR.EndOfStream) | ||
296 | { | ||
297 | lines = SR.ReadLine(); | ||
298 | AdminPage += lines + "\n"; | ||
299 | |||
300 | } | ||
301 | SR.Close(); | ||
302 | |||
303 | SR = File.OpenText("newaccountform.htm"); | ||
304 | |||
305 | while (!SR.EndOfStream) | ||
306 | { | ||
307 | lines = SR.ReadLine(); | ||
308 | NewAccountForm += lines + "\n"; | ||
309 | |||
310 | } | ||
311 | SR.Close(); | ||
312 | |||
313 | SR = File.OpenText("login.htm"); | ||
314 | |||
315 | while (!SR.EndOfStream) | ||
316 | { | ||
317 | lines = SR.ReadLine(); | ||
318 | LoginForm += lines + "\n"; | ||
319 | |||
320 | } | ||
321 | SR.Close(); | ||
322 | } | ||
323 | catch (Exception e) | ||
324 | { | ||
325 | Console.WriteLine(e.ToString()); | ||
326 | } | ||
327 | |||
328 | } | ||
329 | } | ||
330 | |||
331 | |||
332 | } | ||