aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Servers/BaseHttpServer.cs
diff options
context:
space:
mode:
authorMW2007-06-27 15:28:52 +0000
committerMW2007-06-27 15:28:52 +0000
commit646bbbc84b8010e0dacbeed5342cdb045f46cc49 (patch)
tree770b34d19855363c3c113ab9a0af9a56d821d887 /OpenSim/Framework/Servers/BaseHttpServer.cs
downloadopensim-SC-646bbbc84b8010e0dacbeed5342cdb045f46cc49.zip
opensim-SC-646bbbc84b8010e0dacbeed5342cdb045f46cc49.tar.gz
opensim-SC-646bbbc84b8010e0dacbeed5342cdb045f46cc49.tar.bz2
opensim-SC-646bbbc84b8010e0dacbeed5342cdb045f46cc49.tar.xz
Some work on restructuring the namespaces / project names. Note this doesn't compile yet as not all the code has been changed to use the new namespaces. Am committing it now for feedback on the namespaces.
Diffstat (limited to 'OpenSim/Framework/Servers/BaseHttpServer.cs')
-rw-r--r--OpenSim/Framework/Servers/BaseHttpServer.cs312
1 files changed, 312 insertions, 0 deletions
diff --git a/OpenSim/Framework/Servers/BaseHttpServer.cs b/OpenSim/Framework/Servers/BaseHttpServer.cs
new file mode 100644
index 0000000..e55e33c
--- /dev/null
+++ b/OpenSim/Framework/Servers/BaseHttpServer.cs
@@ -0,0 +1,312 @@
1/*
2* Copyright (c) Contributors, http://www.openmetaverse.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*/
28using System;
29using System.Collections.Generic;
30using System.Net;
31using System.Text;
32using System.Text.RegularExpressions;
33using System.Threading;
34//using OpenSim.CAPS;
35using Nwc.XmlRpc;
36using System.Collections;
37using OpenSim.Framework.Console;
38
39namespace OpenSim.Servers
40{
41 public class BaseHttpServer
42 {
43 protected class RestMethodEntry
44 {
45 private string m_path;
46 public string Path
47 {
48 get { return m_path; }
49 }
50
51 private RestMethod m_restMethod;
52 public RestMethod RestMethod
53 {
54 get { return m_restMethod; }
55 }
56
57 public RestMethodEntry(string path, RestMethod restMethod)
58 {
59 m_path = path;
60 m_restMethod = restMethod;
61 }
62 }
63
64 protected Thread m_workerThread;
65 protected HttpListener m_httpListener;
66 protected Dictionary<string, RestMethodEntry> m_restHandlers = new Dictionary<string, RestMethodEntry>();
67 protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>();
68 protected int m_port;
69 protected bool firstcaps = true;
70
71 public BaseHttpServer(int port)
72 {
73 m_port = port;
74 }
75
76 public bool AddRestHandler(string method, string path, RestMethod handler)
77 {
78 //Console.WriteLine("adding new REST handler for path " + path);
79 string methodKey = String.Format("{0}: {1}", method, path);
80
81 if (!this.m_restHandlers.ContainsKey(methodKey))
82 {
83 this.m_restHandlers.Add(methodKey, new RestMethodEntry(path, handler));
84 return true;
85 }
86
87 //must already have a handler for that path so return false
88 return false;
89 }
90
91 public bool RemoveRestHandler(string method, string path)
92 {
93 string methodKey = String.Format("{0}: {1}", method, path);
94 if (this.m_restHandlers.ContainsKey(methodKey))
95 {
96 this.m_restHandlers.Remove(methodKey);
97 return true;
98 }
99 return false;
100 }
101
102 public bool AddXmlRPCHandler(string method, XmlRpcMethod handler)
103 {
104 if (!this.m_rpcHandlers.ContainsKey(method))
105 {
106 this.m_rpcHandlers.Add(method, handler);
107 return true;
108 }
109
110 //must already have a handler for that path so return false
111 return false;
112 }
113
114 protected virtual string ProcessXMLRPCMethod(string methodName, XmlRpcRequest request)
115 {
116 XmlRpcResponse response;
117
118 XmlRpcMethod method;
119 if (this.m_rpcHandlers.TryGetValue(methodName, out method))
120 {
121 response = method(request);
122 }
123 else
124 {
125 response = new XmlRpcResponse();
126 Hashtable unknownMethodError = new Hashtable();
127 unknownMethodError["reason"] = "XmlRequest"; ;
128 unknownMethodError["message"] = "Unknown Rpc request";
129 unknownMethodError["login"] = "false";
130 response.Value = unknownMethodError;
131 }
132
133 return XmlRpcResponseSerializer.Singleton.Serialize(response);
134 }
135
136 protected virtual string ParseREST(string request, string path, string method)
137 {
138 string response;
139
140 string requestKey = String.Format("{0}: {1}", method, path);
141
142 string bestMatch = String.Empty;
143 foreach (string currentKey in m_restHandlers.Keys)
144 {
145 if (requestKey.StartsWith(currentKey))
146 {
147 if (currentKey.Length > bestMatch.Length)
148 {
149 bestMatch = currentKey;
150 }
151 }
152 }
153
154 RestMethodEntry restMethodEntry;
155 if (m_restHandlers.TryGetValue(bestMatch, out restMethodEntry))
156 {
157 RestMethod restMethod = restMethodEntry.RestMethod;
158
159 string param = path.Substring(restMethodEntry.Path.Length);
160 response = restMethod(request, path, param);
161
162 }
163 else
164 {
165 response = String.Empty;
166 }
167
168 return response;
169 }
170
171 protected virtual string ParseLLSDXML(string requestBody)
172 {
173 // dummy function for now - IMPLEMENT ME!
174 Console.WriteLine("LLSD request "+requestBody);
175 string resp = "";
176 if (firstcaps)
177 {
178 resp = "<llsd><map><key>MapLayer</key><string>http://127.0.0.1:9000/CAPS/</string></map></llsd>";
179 firstcaps = false;
180 }
181 return resp;
182 }
183
184 protected virtual string ParseXMLRPC(string requestBody)
185 {
186 string responseString = String.Empty;
187
188 try
189 {
190 XmlRpcRequest request = (XmlRpcRequest)(new XmlRpcRequestDeserializer()).Deserialize(requestBody);
191
192 string methodName = request.MethodName;
193
194 responseString = ProcessXMLRPCMethod(methodName, request);
195 }
196 catch (Exception e)
197 {
198 Console.WriteLine(e.ToString());
199 }
200 return responseString;
201 }
202
203 public virtual void HandleRequest(Object stateinfo)
204 {
205 try
206 {
207 HttpListenerContext context = (HttpListenerContext)stateinfo;
208
209 HttpListenerRequest request = context.Request;
210 HttpListenerResponse response = context.Response;
211
212 response.KeepAlive = false;
213 response.SendChunked = false;
214
215 System.IO.Stream body = request.InputStream;
216 System.Text.Encoding encoding = System.Text.Encoding.UTF8;
217 System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
218
219 string requestBody = reader.ReadToEnd();
220 body.Close();
221 reader.Close();
222
223 //Console.WriteLine(request.HttpMethod + " " + request.RawUrl + " Http/" + request.ProtocolVersion.ToString() + " content type: " + request.ContentType);
224 //Console.WriteLine(requestBody);
225
226 string responseString = "";
227 // Console.WriteLine("new request " + request.ContentType +" at "+ request.RawUrl);
228 switch (request.ContentType)
229 {
230 case "text/xml":
231 // must be XML-RPC, so pass to the XML-RPC parser
232
233 responseString = ParseXMLRPC(requestBody);
234 responseString = Regex.Replace(responseString, "utf-16", "utf-8");
235
236 response.AddHeader("Content-type", "text/xml");
237 break;
238
239 case "application/xml":
240 // probably LLSD we hope, otherwise it should be ignored by the parser
241 // responseString = ParseLLSDXML(requestBody);
242 responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
243 response.AddHeader("Content-type", "application/xml");
244 break;
245
246 case "application/octet-stream":
247 // probably LLSD we hope, otherwise it should be ignored by the parser
248 // responseString = ParseLLSDXML(requestBody);
249 responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
250 response.AddHeader("Content-type", "application/xml");
251 break;
252
253 case "application/x-www-form-urlencoded":
254 // a form data POST so send to the REST parser
255 responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
256 response.AddHeader("Content-type", "text/html");
257 break;
258
259 case null:
260 // must be REST or invalid crap, so pass to the REST parser
261 responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
262 response.AddHeader("Content-type", "text/html");
263 break;
264
265 }
266
267 byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
268 System.IO.Stream output = response.OutputStream;
269 response.SendChunked = false;
270 response.ContentLength64 = buffer.Length;
271 output.Write(buffer, 0, buffer.Length);
272 output.Close();
273 }
274 catch (Exception e)
275 {
276 Console.WriteLine(e.ToString());
277 }
278 }
279
280 public void Start()
281 {
282 OpenSim.Framework.Console.MainLog.Instance.WriteLine(LogPriority.LOW, "BaseHttpServer.cs: Starting up HTTP Server");
283
284 m_workerThread = new Thread(new ThreadStart(StartHTTP));
285 m_workerThread.IsBackground = true;
286 m_workerThread.Start();
287 }
288
289 private void StartHTTP()
290 {
291 try
292 {
293 OpenSim.Framework.Console.MainLog.Instance.WriteLine(LogPriority.LOW, "BaseHttpServer.cs: StartHTTP() - Spawned main thread OK");
294 m_httpListener = new HttpListener();
295
296 m_httpListener.Prefixes.Add("http://+:" + m_port + "/");
297 m_httpListener.Start();
298
299 HttpListenerContext context;
300 while (true)
301 {
302 context = m_httpListener.GetContext();
303 ThreadPool.QueueUserWorkItem(new WaitCallback(HandleRequest), context);
304 }
305 }
306 catch (Exception e)
307 {
308 OpenSim.Framework.Console.MainLog.Instance.WriteLine(LogPriority.MEDIUM, e.Message);
309 }
310 }
311 }
312}