diff options
Diffstat (limited to '')
-rw-r--r-- | OpenGridServices.GridServer/GridHttp.cs | 307 |
1 files changed, 0 insertions, 307 deletions
diff --git a/OpenGridServices.GridServer/GridHttp.cs b/OpenGridServices.GridServer/GridHttp.cs deleted file mode 100644 index 9e9246d..0000000 --- a/OpenGridServices.GridServer/GridHttp.cs +++ /dev/null | |||
@@ -1,307 +0,0 @@ | |||
1 | /* | ||
2 | Copyright (c) OpenGrid 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.Xml; | ||
37 | using System.IO; | ||
38 | using System.Collections; | ||
39 | using System.Collections.Generic; | ||
40 | using libsecondlife; | ||
41 | using OpenSim.Framework.Sims; | ||
42 | using OpenSim.Framework.Console; | ||
43 | using OpenSim.Servers; | ||
44 | |||
45 | namespace OpenGridServices.GridServer | ||
46 | { | ||
47 | public class GridHTTPServer : BaseHttpServer { | ||
48 | public Thread HTTPD; | ||
49 | public HttpListener Listener; | ||
50 | |||
51 | public GridHTTPServer() : base( 8001 ) { | ||
52 | Start(); | ||
53 | } | ||
54 | |||
55 | public void Start() | ||
56 | { | ||
57 | MainConsole.Instance.WriteLine("Starting up HTTP Server"); | ||
58 | HTTPD = new Thread(new ThreadStart(StartHTTP)); | ||
59 | HTTPD.Start(); | ||
60 | } | ||
61 | |||
62 | public void StartHTTP() { | ||
63 | MainConsole.Instance.WriteLine("GridHttp.cs:StartHTTP() - Spawned main thread OK"); | ||
64 | Listener = new HttpListener(); | ||
65 | |||
66 | Listener.Prefixes.Add("http://+:8001/"); | ||
67 | Listener.Prefixes.Add("http://+:8001/sims/"); | ||
68 | Listener.Prefixes.Add("http://+:8001/gods/"); | ||
69 | Listener.Prefixes.Add("http://+:8001/highestuuid/"); | ||
70 | Listener.Prefixes.Add("http://+:8001/uuidblocks/"); | ||
71 | Listener.Start(); | ||
72 | |||
73 | MainConsole.Instance.WriteLine("GridHttp.cs:StartHTTP() - Successfully bound to port 8001"); | ||
74 | |||
75 | HttpListenerContext context; | ||
76 | while(true) { | ||
77 | context = Listener.GetContext(); | ||
78 | ThreadPool.QueueUserWorkItem(new WaitCallback(HandleRequest), context); | ||
79 | } | ||
80 | } | ||
81 | |||
82 | static string ParseXMLRPC(string requestBody, string referrer) { | ||
83 | try{ | ||
84 | XmlRpcRequest request = (XmlRpcRequest)(new XmlRpcRequestDeserializer()).Deserialize(requestBody); | ||
85 | |||
86 | Hashtable requestData = (Hashtable)request.Params[0]; | ||
87 | switch(request.MethodName) { | ||
88 | case "simulator_login": | ||
89 | |||
90 | /*if(!((string)requestData["authkey"]==OpenGrid_Main.thegrid.SimRecvKey)) { | ||
91 | XmlRpcResponse ErrorResp = new XmlRpcResponse(); | ||
92 | Hashtable ErrorRespData = new Hashtable(); | ||
93 | ErrorRespData["error"]="invalid key"; | ||
94 | ErrorResp.Value=ErrorRespData; | ||
95 | return(Regex.Replace(XmlRpcResponseSerializer.Singleton.Serialize(ErrorResp),"utf-16","utf-8")); | ||
96 | }*/ | ||
97 | SimProfileBase TheSim = null; | ||
98 | |||
99 | if(requestData.ContainsKey("UUID")) { | ||
100 | TheSim = OpenGrid_Main.thegrid._regionmanager.GetProfileByLLUUID(new LLUUID((string)requestData["UUID"])); | ||
101 | } else if (requestData.ContainsKey("region_handle")){ | ||
102 | TheSim = OpenGrid_Main.thegrid._regionmanager.GetProfileByHandle((ulong)Convert.ToUInt64(requestData["region_handle"])); | ||
103 | } | ||
104 | |||
105 | if(TheSim==null) { | ||
106 | XmlRpcResponse ErrorResp = new XmlRpcResponse(); | ||
107 | Hashtable ErrorRespData = new Hashtable(); | ||
108 | ErrorRespData["error"]="sim not found"; | ||
109 | ErrorResp.Value=ErrorRespData; | ||
110 | return(Regex.Replace(XmlRpcResponseSerializer.Singleton.Serialize(ErrorResp),"utf-16","utf-8")); | ||
111 | } | ||
112 | |||
113 | XmlRpcResponse SimLoginResp = new XmlRpcResponse(); | ||
114 | Hashtable SimLoginRespData = new Hashtable(); | ||
115 | |||
116 | ArrayList SimNeighboursData = new ArrayList(); | ||
117 | |||
118 | SimProfileBase neighbour; | ||
119 | Hashtable NeighbourBlock; | ||
120 | for(int x=-1; x<2; x++) for(int y=-1; y<2; y++) { | ||
121 | if(OpenGrid_Main.thegrid._regionmanager.GetProfileByHandle(Helpers.UIntsToLong((uint)((TheSim.RegionLocX+x)*256), (uint)(TheSim.RegionLocY+y)*256))!=null) { | ||
122 | neighbour=OpenGrid_Main.thegrid._regionmanager.GetProfileByHandle(Helpers.UIntsToLong((uint)((TheSim.RegionLocX+x)*256), (uint)(TheSim.RegionLocY+y)*256)); | ||
123 | NeighbourBlock = new Hashtable(); | ||
124 | NeighbourBlock["sim_ip"] = neighbour.sim_ip; | ||
125 | NeighbourBlock["sim_port"] = neighbour.sim_port.ToString(); | ||
126 | NeighbourBlock["region_locx"] = neighbour.RegionLocX.ToString(); | ||
127 | NeighbourBlock["region_locy"] = neighbour.RegionLocY.ToString(); | ||
128 | NeighbourBlock["UUID"] = neighbour.UUID.ToString(); | ||
129 | SimNeighboursData.Add(NeighbourBlock); | ||
130 | } | ||
131 | } | ||
132 | |||
133 | SimLoginRespData["UUID"]=TheSim.UUID; | ||
134 | SimLoginRespData["region_locx"]=TheSim.RegionLocX.ToString(); | ||
135 | SimLoginRespData["region_locy"]=TheSim.RegionLocY.ToString(); | ||
136 | SimLoginRespData["regionname"]=TheSim.regionname; | ||
137 | SimLoginRespData["estate_id"]="1"; | ||
138 | SimLoginRespData["neighbours"]=SimNeighboursData; | ||
139 | SimLoginRespData["asset_url"]=OpenGrid_Main.thegrid.DefaultAssetServer; | ||
140 | SimLoginRespData["asset_sendkey"]=OpenGrid_Main.thegrid.AssetSendKey; | ||
141 | SimLoginRespData["asset_recvkey"]=OpenGrid_Main.thegrid.AssetRecvKey; | ||
142 | SimLoginRespData["user_url"]=OpenGrid_Main.thegrid.DefaultUserServer; | ||
143 | SimLoginRespData["user_sendkey"]=OpenGrid_Main.thegrid.UserSendKey; | ||
144 | SimLoginRespData["user_recvkey"]=OpenGrid_Main.thegrid.UserRecvKey; | ||
145 | SimLoginRespData["authkey"]=OpenGrid_Main.thegrid.SimSendKey; | ||
146 | SimLoginResp.Value=SimLoginRespData; | ||
147 | return(Regex.Replace(XmlRpcResponseSerializer.Singleton.Serialize(SimLoginResp),"utf-16","utf-8")); | ||
148 | break; | ||
149 | } | ||
150 | } catch(Exception e) { | ||
151 | Console.WriteLine(e.ToString()); | ||
152 | } | ||
153 | return ""; | ||
154 | } | ||
155 | |||
156 | static string ParseREST(string requestURL, string requestBody, string HTTPmethod) { | ||
157 | char[] splitter = {'/'}; | ||
158 | string[] rest_params = requestURL.Split(splitter); | ||
159 | string req_type = rest_params[0]; // First part of the URL is the type of request - | ||
160 | string respstring=""; | ||
161 | SimProfileBase TheSim; | ||
162 | Console.WriteLine(req_type); | ||
163 | switch(req_type) { | ||
164 | case "regions": | ||
165 | // DIRTY HACK ALERT | ||
166 | Console.WriteLine("/regions/ accessed"); | ||
167 | TheSim=OpenGrid_Main.thegrid._regionmanager.GetProfileByHandle((ulong)Convert.ToUInt64(rest_params[1])); | ||
168 | respstring=ParseREST("/regions/" + rest_params[1], requestBody, HTTPmethod); | ||
169 | break; | ||
170 | |||
171 | case "sims": | ||
172 | LLUUID UUID = new LLUUID((string)rest_params[1]); | ||
173 | TheSim = OpenGrid_Main.thegrid._regionmanager.GetProfileByLLUUID(UUID); | ||
174 | if(!(TheSim==null)) { | ||
175 | switch(HTTPmethod) { | ||
176 | case "GET": | ||
177 | respstring="<authkey>" + OpenGrid_Main.thegrid.SimSendKey + "</authkey>"; | ||
178 | respstring+="<sim>"; | ||
179 | respstring+="<uuid>" + TheSim.UUID.ToString() + "</uuid>"; | ||
180 | respstring+="<regionname>" + TheSim.regionname + "</regionname>"; | ||
181 | respstring+="<sim_ip>" + TheSim.sim_ip + "</sim_ip>"; | ||
182 | respstring+="<sim_port>" + TheSim.sim_port.ToString() + "</sim_port>"; | ||
183 | respstring+="<region_locx>" + TheSim.RegionLocX.ToString() + "</region_locx>"; | ||
184 | respstring+="<region_locy>" + TheSim.RegionLocY.ToString() + "</region_locy>"; | ||
185 | respstring+="<estate_id>1</estate_id>"; | ||
186 | respstring+="</sim>"; | ||
187 | break; | ||
188 | case "POST": | ||
189 | Console.WriteLine("Updating sim details....."); | ||
190 | XmlDocument doc = new XmlDocument(); | ||
191 | doc.LoadXml(requestBody); | ||
192 | XmlNode authkeynode = doc.FirstChild; | ||
193 | if (authkeynode.Name != "authkey") { | ||
194 | respstring = "ERROR! bad XML - expected authkey tag"; | ||
195 | return respstring; | ||
196 | } | ||
197 | XmlNode simnode = doc.ChildNodes[1]; | ||
198 | if (simnode.Name != "sim") { | ||
199 | respstring = "ERROR! bad XML - expected sim tag"; | ||
200 | return respstring; | ||
201 | } | ||
202 | if (authkeynode.Name != OpenGrid_Main.thegrid.SimRecvKey) { | ||
203 | respstring = "ERROR! invalid key"; | ||
204 | return respstring; | ||
205 | } | ||
206 | |||
207 | if (TheSim==null) { | ||
208 | respstring="ERROR! sim not found"; | ||
209 | return respstring; | ||
210 | } else { | ||
211 | for(int i=0; i<= simnode.ChildNodes.Count; i++) { | ||
212 | switch(simnode.ChildNodes[i].Name) { | ||
213 | case "uuid": | ||
214 | // should a sim be able to update it's own UUID? To be decided | ||
215 | // watch next week for the exciting conclusion in "the adventures of OpenGridServices.GridServer/GridHttp.cs:ParseREST() at line 190! | ||
216 | break; // and line 190's arch-enemy - THE BREAK STATEMENT! OH NOES!!!!! (this code written at 6:57AM, no sleep, lots of caffeine) | ||
217 | |||
218 | case "regionname": | ||
219 | TheSim.regionname=simnode.ChildNodes[i].InnerText; | ||
220 | break; | ||
221 | |||
222 | case "sim_ip": | ||
223 | TheSim.sim_ip=simnode.ChildNodes[i].InnerText; | ||
224 | break; | ||
225 | |||
226 | case "sim_port": | ||
227 | TheSim.sim_port=Convert.ToUInt32(simnode.ChildNodes[i].InnerText); | ||
228 | break; | ||
229 | |||
230 | case "region_locx": | ||
231 | TheSim.RegionLocX=Convert.ToUInt32((string)simnode.ChildNodes[i].InnerText); | ||
232 | TheSim.regionhandle=Helpers.UIntsToLong((TheSim.RegionLocX * 256), (TheSim.RegionLocY * 256)); | ||
233 | break; | ||
234 | |||
235 | case "region_locy": | ||
236 | TheSim.RegionLocY=Convert.ToUInt32((string)simnode.ChildNodes[i].InnerText); | ||
237 | TheSim.regionhandle=Helpers.UIntsToLong((TheSim.RegionLocX * 256), (TheSim.RegionLocY * 256)); | ||
238 | break; | ||
239 | } | ||
240 | } | ||
241 | respstring="OK"; | ||
242 | } | ||
243 | |||
244 | |||
245 | |||
246 | break; | ||
247 | } | ||
248 | } | ||
249 | return respstring; | ||
250 | break; | ||
251 | } | ||
252 | return ""; | ||
253 | |||
254 | } | ||
255 | |||
256 | |||
257 | static void HandleRequest(Object stateinfo) { | ||
258 | HttpListenerContext context=(HttpListenerContext)stateinfo; | ||
259 | |||
260 | HttpListenerRequest request = context.Request; | ||
261 | HttpListenerResponse response = context.Response; | ||
262 | |||
263 | response.KeepAlive=false; | ||
264 | response.SendChunked=false; | ||
265 | |||
266 | System.IO.Stream body = request.InputStream; | ||
267 | System.Text.Encoding encoding = System.Text.Encoding.UTF8; | ||
268 | System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding); | ||
269 | |||
270 | string requestBody = reader.ReadToEnd(); | ||
271 | body.Close(); | ||
272 | reader.Close(); | ||
273 | |||
274 | // TODO: AUTHENTICATION!!!!!!!!! MUST ADD!!!!!!!!!! SCRIPT KIDDIES LOVE LACK OF IT!!!!!!!!!! | ||
275 | |||
276 | string responseString=""; | ||
277 | switch(request.ContentType) { | ||
278 | case "text/xml": | ||
279 | // must be XML-RPC, so pass to the XML-RPC parser | ||
280 | |||
281 | responseString=ParseXMLRPC(requestBody,request.Headers["Referer"]); | ||
282 | response.AddHeader("Content-type","text/xml"); | ||
283 | break; | ||
284 | |||
285 | case "text/plaintext": | ||
286 | // must be REST | ||
287 | responseString=ParseREST(request.RawUrl,requestBody,request.HttpMethod); | ||
288 | break; | ||
289 | |||
290 | case null: | ||
291 | // must be REST or invalid crap, so pass to the REST parser | ||
292 | responseString=ParseREST(request.RawUrl,requestBody,request.HttpMethod); | ||
293 | break; | ||
294 | } | ||
295 | |||
296 | |||
297 | byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); | ||
298 | System.IO.Stream output = response.OutputStream; | ||
299 | response.SendChunked=false; | ||
300 | response.ContentLength64=buffer.Length; | ||
301 | output.Write(buffer,0,buffer.Length); | ||
302 | output.Close(); | ||
303 | } | ||
304 | } | ||
305 | |||
306 | |||
307 | } | ||