From 6a2588454a1ac4bb484ad0b9ee648e9ac156f8db Mon Sep 17 00:00:00 2001 From: lbsa71 Date: Wed, 4 Jul 2007 14:12:32 +0000 Subject: * Removed AssetHttpServer, using BaseHttpServer instead * Removed legacy REST handling * Created two custom IStreamHandlers for asset up/download * Removed quite a lot of double and triple encodings, trying to work towards binary only and direct write into storage. * Introduced BaseStreamHandler with GetParam() and some other goodies --- OpenSim/Framework/Servers/BaseHttpServer.cs | 163 +++++++++++---------- OpenSim/Framework/Servers/BaseStreamHandler.cs | 40 +++++ OpenSim/Framework/Servers/IStreamHandler.cs | 3 + .../Servers/OpenSim.Framework.Servers.csproj | 3 + .../Servers/OpenSim.Framework.Servers.dll.build | 1 + OpenSim/Framework/Servers/RestStreamHandler.cs | 30 +--- 6 files changed, 143 insertions(+), 97 deletions(-) create mode 100644 OpenSim/Framework/Servers/BaseStreamHandler.cs (limited to 'OpenSim/Framework') diff --git a/OpenSim/Framework/Servers/BaseHttpServer.cs b/OpenSim/Framework/Servers/BaseHttpServer.cs index 9831108..aed538b 100644 --- a/OpenSim/Framework/Servers/BaseHttpServer.cs +++ b/OpenSim/Framework/Servers/BaseHttpServer.cs @@ -42,48 +42,56 @@ namespace OpenSim.Framework.Servers { protected Thread m_workerThread; protected HttpListener m_httpListener; - protected Dictionary m_restHandlers = new Dictionary(); + //protected Dictionary m_restHandlers = new Dictionary(); protected Dictionary m_rpcHandlers = new Dictionary(); protected Dictionary m_streamHandlers = new Dictionary(); protected int m_port; - protected bool firstcaps = true; + protected bool m_firstcaps = true; public BaseHttpServer(int port) { m_port = port; } - public void AddStreamHandler( string path, IStreamHandler handler) + public void AddStreamHandler( IStreamHandler handler) { - string handlerKey = handler.HttpMethod + ":" + path; + string httpMethod = handler.HttpMethod; + string path = handler.Path; + + string handlerKey = GetHandlerKey(httpMethod, path); m_streamHandlers.Add(handlerKey, handler); } - public bool AddRestHandler(string method, string path, RestMethod handler) + private static string GetHandlerKey(string httpMethod, string path) { - //Console.WriteLine("adding new REST handler for path " + path); - string methodKey = String.Format("{0}: {1}", method, path); - - if (!this.m_restHandlers.ContainsKey(methodKey)) - { - this.m_restHandlers.Add(methodKey, new RestMethodEntry(path, handler)); - return true; - } - - //must already have a handler for that path so return false - return false; + return httpMethod + ":" + path; } - public bool RemoveRestHandler(string method, string path) - { - string methodKey = String.Format("{0}: {1}", method, path); - if (this.m_restHandlers.ContainsKey(methodKey)) - { - this.m_restHandlers.Remove(methodKey); - return true; - } - return false; - } + //public bool AddRestHandler(string method, string path, RestMethod handler) + //{ + // //Console.WriteLine("adding new REST handler for path " + path); + // string methodKey = String.Format("{0}: {1}", method, path); + + // if (!this.m_restHandlers.ContainsKey(methodKey)) + // { + // this.m_restHandlers.Add(methodKey, new RestMethodEntry(path, handler)); + // return true; + // } + + // //must already have a handler for that path so return false + // return false; + //} + + //public bool RemoveRestHandler(string method, string path) + //{ + // string methodKey = String.Format("{0}: {1}", method, path); + // if (this.m_restHandlers.ContainsKey(methodKey)) + // { + // this.m_restHandlers.Remove(methodKey); + // return true; + // } + // return false; + //} public bool AddXmlRPCHandler(string method, XmlRpcMethod handler) { @@ -119,40 +127,40 @@ namespace OpenSim.Framework.Servers return XmlRpcResponseSerializer.Singleton.Serialize(response); } - protected virtual string ParseREST(string request, string path, string method) - { - string response; + //protected virtual string ParseREST(string request, string path, string method) + //{ + // string response; - string requestKey = String.Format("{0}: {1}", method, path); + // string requestKey = String.Format("{0}: {1}", method, path); - string bestMatch = String.Empty; - foreach (string currentKey in m_restHandlers.Keys) - { - if (requestKey.StartsWith(currentKey)) - { - if (currentKey.Length > bestMatch.Length) - { - bestMatch = currentKey; - } - } - } + // string bestMatch = String.Empty; + // foreach (string currentKey in m_restHandlers.Keys) + // { + // if (requestKey.StartsWith(currentKey)) + // { + // if (currentKey.Length > bestMatch.Length) + // { + // bestMatch = currentKey; + // } + // } + // } - RestMethodEntry restMethodEntry; - if (m_restHandlers.TryGetValue(bestMatch, out restMethodEntry)) - { - RestMethod restMethod = restMethodEntry.RestMethod; + // RestMethodEntry restMethodEntry; + // if (m_restHandlers.TryGetValue(bestMatch, out restMethodEntry)) + // { + // RestMethod restMethod = restMethodEntry.RestMethod; - string param = path.Substring(restMethodEntry.Path.Length); - response = restMethod(request, path, param); + // string param = path.Substring(restMethodEntry.Path.Length); + // response = restMethod(request, path, param); - } - else - { - response = String.Empty; - } + // } + // else + // { + // response = String.Empty; + // } - return response; - } + // return response; + //} protected virtual string ParseXMLRPC(string requestBody) @@ -179,13 +187,13 @@ namespace OpenSim.Framework.Servers response.SendChunked = false; string path = request.RawUrl; - string handlerKey = request.HttpMethod + ":" + path; + string handlerKey = GetHandlerKey( request.HttpMethod, path ); IStreamHandler streamHandler; if (TryGetStreamHandler( handlerKey, out streamHandler)) { - byte[] buffer = streamHandler.Handle(path, request.InputStream ); + byte[] buffer = streamHandler.Handle(path, request.InputStream); request.InputStream.Close(); response.ContentType = streamHandler.ContentType; @@ -253,25 +261,25 @@ namespace OpenSim.Framework.Servers response.AddHeader("Content-type", "text/xml"); break; - case "application/xml": - case "application/octet-stream": - // probably LLSD we hope, otherwise it should be ignored by the parser - // responseString = ParseLLSDXML(requestBody); - responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); - 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; + //case "application/xml": + //case "application/octet-stream": + // // probably LLSD we hope, otherwise it should be ignored by the parser + // // responseString = ParseLLSDXML(requestBody); + // responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); + // 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; } @@ -318,5 +326,10 @@ namespace OpenSim.Framework.Servers } } + + public void RemoveStreamHandler(string httpMethod, string path) + { + m_streamHandlers.Remove(GetHandlerKey(httpMethod, path)); + } } } diff --git a/OpenSim/Framework/Servers/BaseStreamHandler.cs b/OpenSim/Framework/Servers/BaseStreamHandler.cs new file mode 100644 index 0000000..95e9707 --- /dev/null +++ b/OpenSim/Framework/Servers/BaseStreamHandler.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.IO; + +namespace OpenSim.Framework.Servers +{ + public abstract class BaseStreamHandler : IStreamHandler + { + public string ContentType + { + get { return "application/xml"; } + } + + private string m_httpMethod; + public string HttpMethod + { + get { return m_httpMethod; } + } + + private string m_path; + public string Path + { + get { return m_path; } + } + + protected string GetParam( string path ) + { + return path.Substring( m_path.Length ); + } + + public abstract byte[] Handle(string path, Stream request); + + protected BaseStreamHandler(string path, string httpMethod ) + { + m_httpMethod = httpMethod; + m_path = path; + } + } +} diff --git a/OpenSim/Framework/Servers/IStreamHandler.cs b/OpenSim/Framework/Servers/IStreamHandler.cs index bc76e9c..6cab40d 100644 --- a/OpenSim/Framework/Servers/IStreamHandler.cs +++ b/OpenSim/Framework/Servers/IStreamHandler.cs @@ -15,5 +15,8 @@ namespace OpenSim.Framework.Servers // Return required http method string HttpMethod { get;} + + // Return path + string Path { get; } } } diff --git a/OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj b/OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj index 555bd5d..4eb9844 100644 --- a/OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj +++ b/OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj @@ -93,6 +93,9 @@ Code + + Code + Code diff --git a/OpenSim/Framework/Servers/OpenSim.Framework.Servers.dll.build b/OpenSim/Framework/Servers/OpenSim.Framework.Servers.dll.build index a3d140f..5e96ef1 100644 --- a/OpenSim/Framework/Servers/OpenSim.Framework.Servers.dll.build +++ b/OpenSim/Framework/Servers/OpenSim.Framework.Servers.dll.build @@ -12,6 +12,7 @@ + diff --git a/OpenSim/Framework/Servers/RestStreamHandler.cs b/OpenSim/Framework/Servers/RestStreamHandler.cs index 64d6ea3..7ca369d 100644 --- a/OpenSim/Framework/Servers/RestStreamHandler.cs +++ b/OpenSim/Framework/Servers/RestStreamHandler.cs @@ -5,41 +5,27 @@ using System.IO; namespace OpenSim.Framework.Servers { - public class RestStreamHandler : IStreamHandler + public class RestStreamHandler : BaseStreamHandler { RestMethod m_restMethod; - private string m_contentType; - public string ContentType - { - get { return m_contentType; } - } - - private string m_httpMethod; - public string HttpMethod - { - get { return m_httpMethod; } - } - - - public byte[] Handle(string path, Stream request ) + override public byte[] Handle(string path, Stream request ) { Encoding encoding = Encoding.UTF8; - StreamReader reader = new StreamReader(request, encoding); + StreamReader streamReader = new StreamReader(request, encoding); - string requestBody = reader.ReadToEnd(); - reader.Close(); + string requestBody = streamReader.ReadToEnd(); + streamReader.Close(); - string responseString = m_restMethod(requestBody, path, m_httpMethod); + string param = GetParam(path); + string responseString = m_restMethod(requestBody, path, param ); return Encoding.UTF8.GetBytes(responseString); } - public RestStreamHandler(RestMethod restMethod, string httpMethod, string contentType) + public RestStreamHandler(string httpMethod, string path, RestMethod restMethod) : base( path, httpMethod ) { m_restMethod = restMethod; - m_httpMethod = httpMethod; - m_contentType = contentType; } } } -- cgit v1.1