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