diff options
Renamed the new Directories. (removed the "-Source" from the end of them)
Diffstat (limited to 'Common/OpenSim.Servers/BaseHttpServer.cs')
-rw-r--r-- | Common/OpenSim.Servers/BaseHttpServer.cs | 256 |
1 files changed, 256 insertions, 0 deletions
diff --git a/Common/OpenSim.Servers/BaseHttpServer.cs b/Common/OpenSim.Servers/BaseHttpServer.cs new file mode 100644 index 0000000..38f4370 --- /dev/null +++ b/Common/OpenSim.Servers/BaseHttpServer.cs | |||
@@ -0,0 +1,256 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Net; | ||
4 | using System.Text; | ||
5 | using System.Text.RegularExpressions; | ||
6 | using System.Threading; | ||
7 | //using OpenSim.CAPS; | ||
8 | using Nwc.XmlRpc; | ||
9 | using System.Collections; | ||
10 | using OpenSim.Framework.Console; | ||
11 | |||
12 | namespace OpenSim.Servers | ||
13 | { | ||
14 | public class BaseHttpServer | ||
15 | { | ||
16 | protected class RestMethodEntry | ||
17 | { | ||
18 | private string m_path; | ||
19 | public string Path | ||
20 | { | ||
21 | get { return m_path; } | ||
22 | } | ||
23 | |||
24 | private RestMethod m_restMethod; | ||
25 | public RestMethod RestMethod | ||
26 | { | ||
27 | get { return m_restMethod; } | ||
28 | } | ||
29 | |||
30 | public RestMethodEntry(string path, RestMethod restMethod) | ||
31 | { | ||
32 | m_path = path; | ||
33 | m_restMethod = restMethod; | ||
34 | } | ||
35 | } | ||
36 | |||
37 | protected Thread m_workerThread; | ||
38 | protected HttpListener m_httpListener; | ||
39 | protected Dictionary<string, RestMethodEntry> m_restHandlers = new Dictionary<string, RestMethodEntry>(); | ||
40 | protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>(); | ||
41 | protected int m_port; | ||
42 | |||
43 | public BaseHttpServer(int port) | ||
44 | { | ||
45 | m_port = port; | ||
46 | } | ||
47 | |||
48 | public bool AddRestHandler(string method, string path, RestMethod handler) | ||
49 | { | ||
50 | string methodKey = String.Format("{0}: {1}", method, path); | ||
51 | |||
52 | if (!this.m_restHandlers.ContainsKey(methodKey)) | ||
53 | { | ||
54 | this.m_restHandlers.Add(methodKey, new RestMethodEntry(path, handler)); | ||
55 | return true; | ||
56 | } | ||
57 | |||
58 | //must already have a handler for that path so return false | ||
59 | return false; | ||
60 | } | ||
61 | |||
62 | public bool AddXmlRPCHandler(string method, XmlRpcMethod handler) | ||
63 | { | ||
64 | if (!this.m_rpcHandlers.ContainsKey(method)) | ||
65 | { | ||
66 | this.m_rpcHandlers.Add(method, handler); | ||
67 | return true; | ||
68 | } | ||
69 | |||
70 | //must already have a handler for that path so return false | ||
71 | return false; | ||
72 | } | ||
73 | |||
74 | protected virtual string ProcessXMLRPCMethod(string methodName, XmlRpcRequest request) | ||
75 | { | ||
76 | XmlRpcResponse response; | ||
77 | |||
78 | XmlRpcMethod method; | ||
79 | if (this.m_rpcHandlers.TryGetValue(methodName, out method)) | ||
80 | { | ||
81 | response = method(request); | ||
82 | } | ||
83 | else | ||
84 | { | ||
85 | response = new XmlRpcResponse(); | ||
86 | Hashtable unknownMethodError = new Hashtable(); | ||
87 | unknownMethodError["reason"] = "XmlRequest"; ; | ||
88 | unknownMethodError["message"] = "Unknown Rpc request"; | ||
89 | unknownMethodError["login"] = "false"; | ||
90 | response.Value = unknownMethodError; | ||
91 | } | ||
92 | |||
93 | return XmlRpcResponseSerializer.Singleton.Serialize(response); | ||
94 | } | ||
95 | |||
96 | protected virtual string ParseREST(string request, string path, string method) | ||
97 | { | ||
98 | string response; | ||
99 | |||
100 | string requestKey = String.Format("{0}: {1}", method, path); | ||
101 | |||
102 | string bestMatch = String.Empty; | ||
103 | foreach (string currentKey in m_restHandlers.Keys) | ||
104 | { | ||
105 | if (requestKey.StartsWith(currentKey)) | ||
106 | { | ||
107 | if (currentKey.Length > bestMatch.Length) | ||
108 | { | ||
109 | bestMatch = currentKey; | ||
110 | } | ||
111 | } | ||
112 | } | ||
113 | |||
114 | RestMethodEntry restMethodEntry; | ||
115 | if (m_restHandlers.TryGetValue(bestMatch, out restMethodEntry)) | ||
116 | { | ||
117 | RestMethod restMethod = restMethodEntry.RestMethod; | ||
118 | |||
119 | string param = path.Substring(restMethodEntry.Path.Length); | ||
120 | response = restMethod(request, path, param); | ||
121 | |||
122 | } | ||
123 | else | ||
124 | { | ||
125 | response = String.Empty; | ||
126 | } | ||
127 | |||
128 | return response; | ||
129 | } | ||
130 | |||
131 | protected virtual string ParseLLSDXML(string requestBody) | ||
132 | { | ||
133 | // dummy function for now - IMPLEMENT ME! | ||
134 | return ""; | ||
135 | } | ||
136 | |||
137 | protected virtual string ParseXMLRPC(string requestBody) | ||
138 | { | ||
139 | string responseString = String.Empty; | ||
140 | |||
141 | try | ||
142 | { | ||
143 | XmlRpcRequest request = (XmlRpcRequest)(new XmlRpcRequestDeserializer()).Deserialize(requestBody); | ||
144 | |||
145 | string methodName = request.MethodName; | ||
146 | |||
147 | responseString = ProcessXMLRPCMethod(methodName, request); | ||
148 | } | ||
149 | catch (Exception e) | ||
150 | { | ||
151 | Console.WriteLine(e.ToString()); | ||
152 | } | ||
153 | return responseString; | ||
154 | } | ||
155 | |||
156 | public virtual void HandleRequest(Object stateinfo) | ||
157 | { | ||
158 | try | ||
159 | { | ||
160 | HttpListenerContext context = (HttpListenerContext)stateinfo; | ||
161 | |||
162 | HttpListenerRequest request = context.Request; | ||
163 | HttpListenerResponse response = context.Response; | ||
164 | |||
165 | response.KeepAlive = false; | ||
166 | response.SendChunked = false; | ||
167 | |||
168 | System.IO.Stream body = request.InputStream; | ||
169 | System.Text.Encoding encoding = System.Text.Encoding.UTF8; | ||
170 | System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding); | ||
171 | |||
172 | string requestBody = reader.ReadToEnd(); | ||
173 | body.Close(); | ||
174 | reader.Close(); | ||
175 | |||
176 | //Console.WriteLine(request.HttpMethod + " " + request.RawUrl + " Http/" + request.ProtocolVersion.ToString() + " content type: " + request.ContentType); | ||
177 | //Console.WriteLine(requestBody); | ||
178 | |||
179 | string responseString = ""; | ||
180 | switch (request.ContentType) | ||
181 | { | ||
182 | case "text/xml": | ||
183 | // must be XML-RPC, so pass to the XML-RPC parser | ||
184 | |||
185 | responseString = ParseXMLRPC(requestBody); | ||
186 | responseString = Regex.Replace(responseString, "utf-16", "utf-8"); | ||
187 | |||
188 | response.AddHeader("Content-type", "text/xml"); | ||
189 | break; | ||
190 | |||
191 | case "application/xml": | ||
192 | // probably LLSD we hope, otherwise it should be ignored by the parser | ||
193 | responseString = ParseLLSDXML(requestBody); | ||
194 | response.AddHeader("Content-type", "application/xml"); | ||
195 | break; | ||
196 | |||
197 | case "application/x-www-form-urlencoded": | ||
198 | // a form data POST so send to the REST parser | ||
199 | responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); | ||
200 | response.AddHeader("Content-type", "text/html"); | ||
201 | break; | ||
202 | |||
203 | case null: | ||
204 | // must be REST or invalid crap, so pass to the REST parser | ||
205 | responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); | ||
206 | response.AddHeader("Content-type", "text/html"); | ||
207 | break; | ||
208 | |||
209 | } | ||
210 | |||
211 | byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); | ||
212 | System.IO.Stream output = response.OutputStream; | ||
213 | response.SendChunked = false; | ||
214 | response.ContentLength64 = buffer.Length; | ||
215 | output.Write(buffer, 0, buffer.Length); | ||
216 | output.Close(); | ||
217 | } | ||
218 | catch (Exception e) | ||
219 | { | ||
220 | Console.WriteLine(e.ToString()); | ||
221 | } | ||
222 | } | ||
223 | |||
224 | public void Start() | ||
225 | { | ||
226 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.LOW, "BaseHttpServer.cs: Starting up HTTP Server"); | ||
227 | |||
228 | m_workerThread = new Thread(new ThreadStart(StartHTTP)); | ||
229 | m_workerThread.IsBackground = true; | ||
230 | m_workerThread.Start(); | ||
231 | } | ||
232 | |||
233 | private void StartHTTP() | ||
234 | { | ||
235 | try | ||
236 | { | ||
237 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.LOW, "BaseHttpServer.cs: StartHTTP() - Spawned main thread OK"); | ||
238 | m_httpListener = new HttpListener(); | ||
239 | |||
240 | m_httpListener.Prefixes.Add("http://+:" + m_port + "/"); | ||
241 | m_httpListener.Start(); | ||
242 | |||
243 | HttpListenerContext context; | ||
244 | while (true) | ||
245 | { | ||
246 | context = m_httpListener.GetContext(); | ||
247 | ThreadPool.QueueUserWorkItem(new WaitCallback(HandleRequest), context); | ||
248 | } | ||
249 | } | ||
250 | catch (Exception e) | ||
251 | { | ||
252 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.MEDIUM, e.Message); | ||
253 | } | ||
254 | } | ||
255 | } | ||
256 | } | ||