diff options
Diffstat (limited to 'OpenGridServices.GridServer/GridHttp.cs')
-rw-r--r-- | OpenGridServices.GridServer/GridHttp.cs | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/OpenGridServices.GridServer/GridHttp.cs b/OpenGridServices.GridServer/GridHttp.cs new file mode 100644 index 0000000..496a3bc --- /dev/null +++ b/OpenGridServices.GridServer/GridHttp.cs | |||
@@ -0,0 +1,166 @@ | |||
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.IO; | ||
37 | using System.Collections; | ||
38 | using System.Collections.Generic; | ||
39 | using libsecondlife; | ||
40 | using ServerConsole; | ||
41 | using OpenSim.Framework.Sims; | ||
42 | |||
43 | namespace OpenGridServices | ||
44 | { | ||
45 | public class GridHTTPServer { | ||
46 | public Thread HTTPD; | ||
47 | public HttpListener Listener; | ||
48 | |||
49 | public GridHTTPServer() { | ||
50 | ServerConsole.MainConsole.Instance.WriteLine("Starting up HTTP Server"); | ||
51 | HTTPD = new Thread(new ThreadStart(StartHTTP)); | ||
52 | HTTPD.Start(); | ||
53 | } | ||
54 | |||
55 | public void StartHTTP() { | ||
56 | ServerConsole.MainConsole.Instance.WriteLine("GridHttp.cs:StartHTTP() - Spawned main thread OK"); | ||
57 | Listener = new HttpListener(); | ||
58 | |||
59 | Listener.Prefixes.Add("http://+:8001/gridserver/"); | ||
60 | Listener.Start(); | ||
61 | |||
62 | HttpListenerContext context; | ||
63 | while(true) { | ||
64 | context = Listener.GetContext(); | ||
65 | ThreadPool.QueueUserWorkItem(new WaitCallback(HandleRequest), context); | ||
66 | } | ||
67 | } | ||
68 | |||
69 | static string ParseXMLRPC(string requestBody) { | ||
70 | try{ | ||
71 | XmlRpcRequest request = (XmlRpcRequest)(new XmlRpcRequestDeserializer()).Deserialize(requestBody); | ||
72 | |||
73 | Hashtable requestData = (Hashtable)request.Params[0]; | ||
74 | switch(request.MethodName) { | ||
75 | case "get_sim_info": | ||
76 | ulong req_handle=(ulong)Convert.ToInt64(requestData["region_handle"]); | ||
77 | SimProfileBase TheSim = OpenGrid_Main.thegrid._regionmanager.GetProfileByHandle(req_handle); | ||
78 | string RecvKey=""; | ||
79 | string caller=(string)requestData["caller"]; | ||
80 | switch(caller) { | ||
81 | case "userserver": | ||
82 | RecvKey=OpenGrid_Main.thegrid.UserRecvKey; | ||
83 | break; | ||
84 | case "assetserver": | ||
85 | RecvKey=OpenGrid_Main.thegrid.AssetRecvKey; | ||
86 | break; | ||
87 | } | ||
88 | if((TheSim!=null) && (string)requestData["authkey"]==RecvKey) { | ||
89 | XmlRpcResponse SimInfoResp = new XmlRpcResponse(); | ||
90 | Hashtable SimInfoData = new Hashtable(); | ||
91 | SimInfoData["UUID"]=TheSim.UUID.ToString(); | ||
92 | SimInfoData["regionhandle"]=TheSim.regionhandle.ToString(); | ||
93 | SimInfoData["regionname"]=TheSim.regionname; | ||
94 | SimInfoData["sim_ip"]=TheSim.sim_ip; | ||
95 | SimInfoData["sim_port"]=TheSim.sim_port.ToString(); | ||
96 | SimInfoData["caps_url"]=TheSim.caps_url; | ||
97 | SimInfoData["RegionLocX"]=TheSim.RegionLocX.ToString(); | ||
98 | SimInfoData["RegionLocY"]=TheSim.RegionLocY.ToString(); | ||
99 | SimInfoData["sendkey"]=TheSim.sendkey; | ||
100 | SimInfoData["recvkey"]=TheSim.recvkey; | ||
101 | SimInfoResp.Value=SimInfoData; | ||
102 | return(Regex.Replace(XmlRpcResponseSerializer.Singleton.Serialize(SimInfoResp),"utf-16","utf-8")); | ||
103 | } else { | ||
104 | XmlRpcResponse SimErrorResp = new XmlRpcResponse(); | ||
105 | Hashtable SimErrorData = new Hashtable(); | ||
106 | SimErrorData["error"]="sim not found"; | ||
107 | SimErrorResp.Value=SimErrorData; | ||
108 | return(XmlRpcResponseSerializer.Singleton.Serialize(SimErrorResp)); | ||
109 | } | ||
110 | break; | ||
111 | } | ||
112 | } catch(Exception e) { | ||
113 | Console.WriteLine(e.ToString()); | ||
114 | } | ||
115 | return ""; | ||
116 | } | ||
117 | |||
118 | static string ParseREST(string requestBody, string requestURL) { | ||
119 | return ""; | ||
120 | } | ||
121 | |||
122 | |||
123 | static void HandleRequest(Object stateinfo) { | ||
124 | HttpListenerContext context=(HttpListenerContext)stateinfo; | ||
125 | |||
126 | HttpListenerRequest request = context.Request; | ||
127 | HttpListenerResponse response = context.Response; | ||
128 | |||
129 | response.KeepAlive=false; | ||
130 | response.SendChunked=false; | ||
131 | |||
132 | System.IO.Stream body = request.InputStream; | ||
133 | System.Text.Encoding encoding = System.Text.Encoding.UTF8; | ||
134 | System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding); | ||
135 | |||
136 | string requestBody = reader.ReadToEnd(); | ||
137 | body.Close(); | ||
138 | reader.Close(); | ||
139 | |||
140 | string responseString=""; | ||
141 | switch(request.ContentType) { | ||
142 | case "text/xml": | ||
143 | // must be XML-RPC, so pass to the XML-RPC parser | ||
144 | |||
145 | responseString=ParseXMLRPC(requestBody); | ||
146 | response.AddHeader("Content-type","text/xml"); | ||
147 | break; | ||
148 | |||
149 | case null: | ||
150 | // must be REST or invalid crap, so pass to the REST parser | ||
151 | responseString=ParseREST(request.Url.OriginalString,requestBody); | ||
152 | break; | ||
153 | } | ||
154 | |||
155 | |||
156 | byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); | ||
157 | System.IO.Stream output = response.OutputStream; | ||
158 | response.SendChunked=false; | ||
159 | response.ContentLength64=buffer.Length; | ||
160 | output.Write(buffer,0,buffer.Length); | ||
161 | output.Close(); | ||
162 | } | ||
163 | } | ||
164 | |||
165 | |||
166 | } | ||