GridHttp.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. Copyright (c) OpenGrid project, http://osgrid.org/
  3. * All rights reserved.
  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 <organization> 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 <copyright holder> ``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 <copyright holder> 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. using System;
  28. using System.Text;
  29. using Nwc.XmlRpc;
  30. using System.Threading;
  31. using System.Text.RegularExpressions;
  32. using System.Net;
  33. using System.Xml;
  34. using System.IO;
  35. using System.Collections;
  36. using System.Collections.Generic;
  37. using libsecondlife;
  38. using OpenSim.Framework.Sims;
  39. using OpenSim.Framework.Console;
  40. namespace OpenGridServices.GridServer
  41. {
  42. public class GridHTTPServer {
  43. public Thread HTTPD;
  44. public HttpListener Listener;
  45. public GridHTTPServer() {
  46. MainConsole.Instance.WriteLine("Starting up HTTP Server");
  47. HTTPD = new Thread(new ThreadStart(StartHTTP));
  48. HTTPD.Start();
  49. }
  50. public void StartHTTP() {
  51. MainConsole.Instance.WriteLine("GridHttp.cs:StartHTTP() - Spawned main thread OK");
  52. Listener = new HttpListener();
  53. Listener.Prefixes.Add("http://+:8001/sims/");
  54. Listener.Prefixes.Add("http://+:8001/gods/");
  55. Listener.Prefixes.Add("http://+:8001/highestuuid/");
  56. Listener.Prefixes.Add("http://+:8001/uuidblocks/");
  57. Listener.Start();
  58. MainConsole.Instance.WriteLine("GridHttp.cs:StartHTTP() - Successfully bound to port 8001");
  59. HttpListenerContext context;
  60. while(true) {
  61. context = Listener.GetContext();
  62. ThreadPool.QueueUserWorkItem(new WaitCallback(HandleRequest), context);
  63. }
  64. }
  65. static string ParseXMLRPC(string requestBody, string referrer) {
  66. try{
  67. XmlRpcRequest request = (XmlRpcRequest)(new XmlRpcRequestDeserializer()).Deserialize(requestBody);
  68. Hashtable requestData = (Hashtable)request.Params[0];
  69. switch(request.MethodName) {
  70. case "simulator_login":
  71. if(!(referrer=="simulator")) {
  72. XmlRpcResponse ErrorResp = new XmlRpcResponse();
  73. Hashtable ErrorRespData = new Hashtable();
  74. ErrorRespData["error"]="Only simulators can login with this method";
  75. ErrorResp.Value=ErrorRespData;
  76. return(Regex.Replace(XmlRpcResponseSerializer.Singleton.Serialize(ErrorResp),"utf-16","utf-8"));
  77. }
  78. if(!((string)requestData["authkey"]==OpenGrid_Main.thegrid.SimRecvKey)) {
  79. XmlRpcResponse ErrorResp = new XmlRpcResponse();
  80. Hashtable ErrorRespData = new Hashtable();
  81. ErrorRespData["error"]="invalid key";
  82. ErrorResp.Value=ErrorRespData;
  83. return(Regex.Replace(XmlRpcResponseSerializer.Singleton.Serialize(ErrorResp),"utf-16","utf-8"));
  84. }
  85. SimProfileBase TheSim = OpenGrid_Main.thegrid._regionmanager.GetProfileByLLUUID(new LLUUID((string)requestData["UUID"]));
  86. XmlRpcResponse SimLoginResp = new XmlRpcResponse();
  87. Hashtable SimLoginRespData = new Hashtable();
  88. ArrayList SimNeighboursData = new ArrayList();
  89. SimProfileBase neighbour;
  90. Hashtable NeighbourBlock;
  91. for(int x=-1; x<2; x++) for(int y=-1; y<2; y++) {
  92. if(OpenGrid_Main.thegrid._regionmanager.GetProfileByHandle(Helpers.UIntsToLong((uint)((TheSim.RegionLocX+x)*256), (uint)(TheSim.RegionLocY+y)*256))!=null) {
  93. neighbour=OpenGrid_Main.thegrid._regionmanager.GetProfileByHandle(Helpers.UIntsToLong((uint)((TheSim.RegionLocX+x)*256), (uint)(TheSim.RegionLocY+y)*256));
  94. NeighbourBlock = new Hashtable();
  95. NeighbourBlock["sim_ip"] = neighbour.sim_ip;
  96. NeighbourBlock["sim_port"] = neighbour.sim_port.ToString();
  97. NeighbourBlock["region_locx"] = neighbour.RegionLocX.ToString();
  98. NeighbourBlock["region_locy"] = neighbour.RegionLocY.ToString();
  99. NeighbourBlock["UUID"] = neighbour.UUID.ToString();
  100. SimNeighboursData.Add(NeighbourBlock);
  101. }
  102. }
  103. SimLoginRespData["region_locx"]=TheSim.RegionLocX.ToString();
  104. SimLoginRespData["region_locy"]=TheSim.RegionLocY.ToString();
  105. SimLoginRespData["regionname"]=TheSim.regionname;
  106. SimLoginRespData["estate_id"]="1";
  107. SimLoginRespData["neighbours"]=SimNeighboursData;
  108. SimLoginRespData["asset_url"]=OpenGrid_Main.thegrid.DefaultAssetServer;
  109. SimLoginRespData["asset_sendkey"]=OpenGrid_Main.thegrid.AssetSendKey;
  110. SimLoginRespData["asset_recvkey"]=OpenGrid_Main.thegrid.AssetRecvKey;
  111. SimLoginRespData["user_url"]=OpenGrid_Main.thegrid.DefaultUserServer;
  112. SimLoginRespData["user_sendkey"]=OpenGrid_Main.thegrid.UserSendKey;
  113. SimLoginRespData["user_recvkey"]=OpenGrid_Main.thegrid.UserRecvKey;
  114. SimLoginRespData["authkey"]=OpenGrid_Main.thegrid.SimSendKey;
  115. SimLoginResp.Value=SimLoginRespData;
  116. return(Regex.Replace(XmlRpcResponseSerializer.Singleton.Serialize(SimLoginResp),"utf-16","utf-8"));
  117. break;
  118. }
  119. } catch(Exception e) {
  120. Console.WriteLine(e.ToString());
  121. }
  122. return "";
  123. }
  124. static string ParseREST(string requestBody, string requestURL, string HTTPmethod) {
  125. char[] splitter = {'/'};
  126. string[] rest_params = requestURL.Split(splitter);
  127. string req_type = rest_params[0]; // First part of the URL is the type of request -
  128. string respstring;
  129. switch(req_type) {
  130. case "sims":
  131. LLUUID UUID = new LLUUID((string)rest_params[1]);
  132. SimProfileBase TheSim = OpenGrid_Main.thegrid._regionmanager.GetProfileByLLUUID(UUID);
  133. if(!(TheSim==null)) {
  134. switch(HTTPmethod) {
  135. case "GET":
  136. respstring="<authkey>" + OpenGrid_Main.thegrid.SimSendKey + "</authkey>";
  137. respstring+="<sim>";
  138. respstring+="<uuid>" + TheSim.UUID.ToString() + "</uuid>";
  139. respstring+="<regionname>" + TheSim.regionname + "</regionname>";
  140. respstring+="<sim_ip>" + TheSim.sim_ip + "</sim_ip>";
  141. respstring+="<sim_port>" + TheSim.sim_port.ToString() + "</sim_port>";
  142. respstring+="<region_locx>" + TheSim.RegionLocX.ToString() + "</region_locx>";
  143. respstring+="<region_locy>" + TheSim.RegionLocY.ToString() + "</region_locy>";
  144. respstring+="<estate_id>1</estate_id>";
  145. respstring+="</sim>";
  146. break;
  147. case "POST":
  148. XmlDocument doc = new XmlDocument();
  149. doc.LoadXml(requestBody);
  150. XmlNode authkeynode = doc.FirstChild;
  151. if (authkeynode.Name != "authkey")
  152. respstring = "<error>bad XML - expected authkey tag</error>";
  153. XmlNode simnode = doc.ChildNodes[1];
  154. if (simnode.Name != "sim")
  155. respstring = "<error>bad XML - expected sim tag</error>";
  156. break;
  157. }
  158. }
  159. break;
  160. }
  161. return "";
  162. }
  163. static void HandleRequest(Object stateinfo) {
  164. HttpListenerContext context=(HttpListenerContext)stateinfo;
  165. HttpListenerRequest request = context.Request;
  166. HttpListenerResponse response = context.Response;
  167. response.KeepAlive=false;
  168. response.SendChunked=false;
  169. System.IO.Stream body = request.InputStream;
  170. System.Text.Encoding encoding = System.Text.Encoding.UTF8;
  171. System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
  172. string requestBody = reader.ReadToEnd();
  173. body.Close();
  174. reader.Close();
  175. // TODO: AUTHENTICATION!!!!!!!!! MUST ADD!!!!!!!!!! SCRIPT KIDDIES LOVE LACK OF IT!!!!!!!!!!
  176. string responseString="";
  177. switch(request.ContentType) {
  178. case "text/xml":
  179. // must be XML-RPC, so pass to the XML-RPC parser
  180. responseString=ParseXMLRPC(requestBody,request.Headers["Referer"]);
  181. response.AddHeader("Content-type","text/xml");
  182. break;
  183. case null:
  184. // must be REST or invalid crap, so pass to the REST parser
  185. responseString=ParseREST(request.Url.OriginalString,requestBody,request.HttpMethod);
  186. break;
  187. }
  188. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
  189. System.IO.Stream output = response.OutputStream;
  190. response.SendChunked=false;
  191. response.ContentLength64=buffer.Length;
  192. output.Write(buffer,0,buffer.Length);
  193. output.Close();
  194. }
  195. }
  196. }