GridHttp.cs 6.1 KB

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