From 17b00785ee10a71d290cbcddca9b0152fdf60c8f Mon Sep 17 00:00:00 2001 From: MW Date: Thu, 29 Mar 2007 11:08:42 +0000 Subject: Added lbsa71's http server changes. So it now has a single http listener that passes incoming requests to the correct handler. This means that logins (in sandbox mode) now go through this listener, which for now is listening on the port you set in startup (default 9000). (This needs changing so that the http listener and udp server are not using the same port) --- Servers/BaseHttpServer.cs | 201 ++++++++++++++++++++++++++++++++++++++ Servers/IRestHandler.cs | 11 +++ Servers/OpenSim.Servers.csproj | 2 + Servers/OpenSim.Servers.dll.build | 2 + Servers/XmlRpcMethod.cs | 7 ++ 5 files changed, 223 insertions(+) create mode 100644 Servers/IRestHandler.cs create mode 100644 Servers/XmlRpcMethod.cs (limited to 'Servers') diff --git a/Servers/BaseHttpServer.cs b/Servers/BaseHttpServer.cs index 28849dc..bac7e86 100644 --- a/Servers/BaseHttpServer.cs +++ b/Servers/BaseHttpServer.cs @@ -1,10 +1,211 @@ using System; using System.Collections.Generic; +using System.Net; using System.Text; +using System.Text.RegularExpressions; +using System.Threading; +using OpenSim.CAPS; +using Nwc.XmlRpc; +using System.Collections; namespace OpenSim.Servers { public class BaseHttpServer { + protected Thread m_workerThread; + protected HttpListener m_httpListener; + protected Dictionary m_restHandlers = new Dictionary(); + protected Dictionary m_rpcHandlers = new Dictionary(); + protected int m_port; + + public BaseHttpServer(int port) + { + m_port = port; + } + + public bool AddRestHandler(string path, IRestHandler handler) + { + if (!this.m_restHandlers.ContainsKey(path)) + { + this.m_restHandlers.Add(path, handler); + return true; + } + + //must already have a handler for that path so return false + return false; + } + + public bool AddXmlRPCHandler(string method, XmlRpcMethod handler) + { + if (!this.m_rpcHandlers.ContainsKey(method)) + { + this.m_rpcHandlers.Add(method, handler); + return true; + } + + //must already have a handler for that path so return false + return false; + } + + protected virtual string ProcessXMLRPCMethod(string methodName, XmlRpcRequest request) + { + XmlRpcResponse response; + + XmlRpcMethod method; + if( this.m_rpcHandlers.TryGetValue( methodName, out method ) ) + { + response = method(request); + } + else + { + response = new XmlRpcResponse(); + Hashtable unknownMethodError = new Hashtable(); + unknownMethodError["reason"] = "XmlRequest"; ; + unknownMethodError["message"] = "Unknown Rpc request"; + unknownMethodError["login"] = "false"; + response.Value = unknownMethodError; + } + + return XmlRpcResponseSerializer.Singleton.Serialize(response); + } + + protected virtual string ParseREST(string requestBody, string requestURL, string requestMethod) + { + string[] path; + string pathDelimStr = "/"; + char[] pathDelimiter = pathDelimStr.ToCharArray(); + path = requestURL.Split(pathDelimiter); + + string responseString = ""; + + //path[0] should be empty so we are interested in path[1] + if (path.Length > 1) + { + if ((path[1] != "") && (this.m_restHandlers.ContainsKey(path[1]))) + { + responseString = this.m_restHandlers[path[1]].HandleREST(requestBody, requestURL, requestMethod); + } + } + + return responseString; + } + + protected virtual string ParseLLSDXML(string requestBody) + { + // dummy function for now - IMPLEMENT ME! + return ""; + } + + protected virtual string ParseXMLRPC(string requestBody) + { + string responseString = String.Empty; + + try + { + XmlRpcRequest request = (XmlRpcRequest)(new XmlRpcRequestDeserializer()).Deserialize(requestBody); + + string methodName = request.MethodName; + + responseString = ProcessXMLRPCMethod(methodName, request ); + } + catch (Exception e) + { + Console.WriteLine(e.ToString()); + } + return responseString; + } + + public virtual void HandleRequest(Object stateinfo) + { + HttpListenerContext context = (HttpListenerContext)stateinfo; + + HttpListenerRequest request = context.Request; + HttpListenerResponse response = context.Response; + + response.KeepAlive = false; + response.SendChunked = false; + + System.IO.Stream body = request.InputStream; + System.Text.Encoding encoding = System.Text.Encoding.UTF8; + System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding); + + string requestBody = reader.ReadToEnd(); + body.Close(); + reader.Close(); + + //Console.WriteLine(request.HttpMethod + " " + request.RawUrl + " Http/" + request.ProtocolVersion.ToString() + " content type: " + request.ContentType); + //Console.WriteLine(requestBody); + + string responseString = ""; + switch (request.ContentType) + { + case "text/xml": + // must be XML-RPC, so pass to the XML-RPC parser + + responseString = ParseXMLRPC(requestBody); + responseString = Regex.Replace(responseString, "utf-16", "utf-8"); + + response.AddHeader("Content-type", "text/xml"); + break; + + case "application/xml": + // probably LLSD we hope, otherwise it should be ignored by the parser + responseString = ParseLLSDXML(requestBody); + response.AddHeader("Content-type", "application/xml"); + break; + + case "application/x-www-form-urlencoded": + // a form data POST so send to the REST parser + responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); + response.AddHeader("Content-type", "text/html"); + break; + + case null: + // must be REST or invalid crap, so pass to the REST parser + responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); + response.AddHeader("Content-type", "text/html"); + break; + + } + + byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); + System.IO.Stream output = response.OutputStream; + response.SendChunked = false; + response.ContentLength64 = buffer.Length; + output.Write(buffer, 0, buffer.Length); + output.Close(); + } + + public void Start() + { + OpenSim.Framework.Console.MainConsole.Instance.WriteLine("BaseHttpServer.cs: Starting up HTTP Server"); + + m_workerThread = new Thread(new ThreadStart(StartHTTP)); + m_workerThread.IsBackground = true; + m_workerThread.Start(); + } + + private void StartHTTP() + { + try + { + OpenSim.Framework.Console.MainConsole.Instance.WriteLine("BaseHttpServer.cs: StartHTTP() - Spawned main thread OK"); + m_httpListener = new HttpListener(); + + m_httpListener.Prefixes.Add("http://+:" + m_port + "/"); + m_httpListener.Start(); + + HttpListenerContext context; + while (true) + { + context = m_httpListener.GetContext(); + ThreadPool.QueueUserWorkItem(new WaitCallback(HandleRequest), context); + } + } + catch (Exception e) + { + OpenSim.Framework.Console.MainConsole.Instance.WriteLine(e.Message); + } + } } } diff --git a/Servers/IRestHandler.cs b/Servers/IRestHandler.cs new file mode 100644 index 0000000..f269600 --- /dev/null +++ b/Servers/IRestHandler.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenSim.CAPS +{ + public interface IRestHandler + { + string HandleREST(string requestBody, string requestURL, string requestMethod); + } +} diff --git a/Servers/OpenSim.Servers.csproj b/Servers/OpenSim.Servers.csproj index c983fca..9710f47 100644 --- a/Servers/OpenSim.Servers.csproj +++ b/Servers/OpenSim.Servers.csproj @@ -96,6 +96,8 @@ Code + + diff --git a/Servers/OpenSim.Servers.dll.build b/Servers/OpenSim.Servers.dll.build index 8600ab5..48e8dec 100644 --- a/Servers/OpenSim.Servers.dll.build +++ b/Servers/OpenSim.Servers.dll.build @@ -12,6 +12,8 @@ + + diff --git a/Servers/XmlRpcMethod.cs b/Servers/XmlRpcMethod.cs new file mode 100644 index 0000000..2295405 --- /dev/null +++ b/Servers/XmlRpcMethod.cs @@ -0,0 +1,7 @@ +using System; +using Nwc.XmlRpc; + +namespace OpenSim.Servers +{ + public delegate XmlRpcResponse XmlRpcMethod( XmlRpcRequest request ); +} -- cgit v1.1