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