From 9a51949cb4c833dcacf2a5803a8f2753273941c8 Mon Sep 17 00:00:00 2001 From: lbsa71 Date: Wed, 4 Jul 2007 11:47:32 +0000 Subject: * Added StreamHandler support * Implemented RestStreamHandler * Some caps functions now use it * Moved out RestMethodEntry from httpserver * The IStreamHandler interface now reports required method and Content-Type --- OpenSim/Framework/Servers/BaseHttpServer.cs | 63 ++++++++-------------- OpenSim/Framework/Servers/IStreamHandler.cs | 9 +++- .../Servers/OpenSim.Framework.Servers.csproj | 49 +++++++++-------- .../Servers/OpenSim.Framework.Servers.dll.build | 3 ++ OpenSim/Framework/Servers/RestMethodEntry.cs | 27 ++++++++++ OpenSim/Framework/Servers/RestStreamHandler.cs | 34 +++++++++++- 6 files changed, 116 insertions(+), 69 deletions(-) create mode 100644 OpenSim/Framework/Servers/RestMethodEntry.cs (limited to 'OpenSim/Framework/Servers') diff --git a/OpenSim/Framework/Servers/BaseHttpServer.cs b/OpenSim/Framework/Servers/BaseHttpServer.cs index 8fa577c..9831108 100644 --- a/OpenSim/Framework/Servers/BaseHttpServer.cs +++ b/OpenSim/Framework/Servers/BaseHttpServer.cs @@ -40,27 +40,6 @@ namespace OpenSim.Framework.Servers { public class BaseHttpServer { - protected class RestMethodEntry - { - private string m_path; - public string Path - { - get { return m_path; } - } - - private RestMethod m_restMethod; - public RestMethod RestMethod - { - get { return m_restMethod; } - } - - public RestMethodEntry(string path, RestMethod restMethod) - { - m_path = path; - m_restMethod = restMethod; - } - } - protected Thread m_workerThread; protected HttpListener m_httpListener; protected Dictionary m_restHandlers = new Dictionary(); @@ -74,9 +53,10 @@ namespace OpenSim.Framework.Servers m_port = port; } - private void AddStreamHandler(string path, IStreamHandler handler) + public void AddStreamHandler( string path, IStreamHandler handler) { - m_streamHandlers.Add(path, handler); + string handlerKey = handler.HttpMethod + ":" + path; + m_streamHandlers.Add(handlerKey, handler); } public bool AddRestHandler(string method, string path, RestMethod handler) @@ -179,18 +159,12 @@ namespace OpenSim.Framework.Servers { string responseString = String.Empty; - try - { - XmlRpcRequest request = (XmlRpcRequest)(new XmlRpcRequestDeserializer()).Deserialize(requestBody); + XmlRpcRequest request = (XmlRpcRequest)(new XmlRpcRequestDeserializer()).Deserialize(requestBody); - string methodName = request.MethodName; + string methodName = request.MethodName; + + responseString = ProcessXMLRPCMethod(methodName, request); - responseString = ProcessXMLRPCMethod(methodName, request); - } - catch - { - //Console.WriteLine(e.ToString()); - } return responseString; } @@ -205,12 +179,19 @@ namespace OpenSim.Framework.Servers response.SendChunked = false; string path = request.RawUrl; + string handlerKey = request.HttpMethod + ":" + path; IStreamHandler streamHandler; - if(TryGetStreamHandler(path, out streamHandler)) + if (TryGetStreamHandler( handlerKey, out streamHandler)) { - streamHandler.Handle(path, request.InputStream, response.OutputStream ); + byte[] buffer = streamHandler.Handle(path, request.InputStream ); + request.InputStream.Close(); + + response.ContentType = streamHandler.ContentType; + response.ContentLength64 = buffer.LongLength; + response.OutputStream.Write(buffer, 0, buffer.Length); + response.OutputStream.Close(); } else { @@ -218,22 +199,22 @@ namespace OpenSim.Framework.Servers } } - private bool TryGetStreamHandler(string path, out IStreamHandler streamHandler ) + private bool TryGetStreamHandler(string handlerKey, out IStreamHandler streamHandler) { string bestMatch = null; - + foreach (string pattern in m_streamHandlers.Keys) { - if (path.StartsWith(pattern)) - { - if (String.IsNullOrEmpty( bestMatch ) || pattern.Length > bestMatch.Length) + if (handlerKey.StartsWith(pattern)) + { + if (String.IsNullOrEmpty(bestMatch) || pattern.Length > bestMatch.Length) { bestMatch = pattern; } } } - if( String.IsNullOrEmpty( bestMatch ) ) + if (String.IsNullOrEmpty(bestMatch)) { streamHandler = null; return false; diff --git a/OpenSim/Framework/Servers/IStreamHandler.cs b/OpenSim/Framework/Servers/IStreamHandler.cs index 88ae641..bc76e9c 100644 --- a/OpenSim/Framework/Servers/IStreamHandler.cs +++ b/OpenSim/Framework/Servers/IStreamHandler.cs @@ -7,6 +7,13 @@ namespace OpenSim.Framework.Servers { public interface IStreamHandler { - void Handle(string path, Stream request, Stream response); + // Handle request stream, return byte array + byte[] Handle(string path, Stream request ); + + // Return response content type + string ContentType { get; } + + // Return required http method + string HttpMethod { get;} } } diff --git a/OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj b/OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj index 956a9bc..555bd5d 100644 --- a/OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj +++ b/OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj @@ -1,4 +1,4 @@ - + Local 8.0.50727 @@ -6,8 +6,7 @@ {2CC71860-0000-0000-0000-000000000000} Debug AnyCPU - - + OpenSim.Framework.Servers @@ -16,11 +15,9 @@ IE50 false Library - - + OpenSim.Framework.Servers - - + @@ -31,8 +28,7 @@ TRACE;DEBUG - - + True 4096 False @@ -41,8 +37,7 @@ False False 4 - - + False @@ -51,8 +46,7 @@ TRACE - - + False 4096 True @@ -61,24 +55,22 @@ False False 4 - - + - + ..\..\..\bin\libsecondlife.dll False - + System.dll False - - + System.Xml.dll False - + ..\..\..\bin\XMLRPC.dll False @@ -88,13 +80,13 @@ OpenSim.Framework {8ACA2445-0000-0000-0000-000000000000} {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - False + False OpenSim.Framework.Console {A7CD0630-0000-0000-0000-000000000000} {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - False + False @@ -107,14 +99,21 @@ Code - + + Code + Code Code - + + Code + + + Code + Code @@ -129,4 +128,4 @@ - \ No newline at end of file + diff --git a/OpenSim/Framework/Servers/OpenSim.Framework.Servers.dll.build b/OpenSim/Framework/Servers/OpenSim.Framework.Servers.dll.build index 22d98dc..a3d140f 100644 --- a/OpenSim/Framework/Servers/OpenSim.Framework.Servers.dll.build +++ b/OpenSim/Framework/Servers/OpenSim.Framework.Servers.dll.build @@ -14,8 +14,11 @@ + + + diff --git a/OpenSim/Framework/Servers/RestMethodEntry.cs b/OpenSim/Framework/Servers/RestMethodEntry.cs new file mode 100644 index 0000000..ab926e0 --- /dev/null +++ b/OpenSim/Framework/Servers/RestMethodEntry.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenSim.Framework.Servers +{ + public class RestMethodEntry + { + private string m_path; + public string Path + { + get { return m_path; } + } + + private RestMethod m_restMethod; + public RestMethod RestMethod + { + get { return m_restMethod; } + } + + public RestMethodEntry(string path, RestMethod restMethod) + { + m_path = path; + m_restMethod = restMethod; + } + } +} diff --git a/OpenSim/Framework/Servers/RestStreamHandler.cs b/OpenSim/Framework/Servers/RestStreamHandler.cs index 145a184..64d6ea3 100644 --- a/OpenSim/Framework/Servers/RestStreamHandler.cs +++ b/OpenSim/Framework/Servers/RestStreamHandler.cs @@ -7,9 +7,39 @@ namespace OpenSim.Framework.Servers { public class RestStreamHandler : IStreamHandler { - public void Handle( string path, Stream request, Stream response ) + 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 ) + { + Encoding encoding = Encoding.UTF8; + StreamReader reader = new StreamReader(request, encoding); + + string requestBody = reader.ReadToEnd(); + reader.Close(); + + string responseString = m_restMethod(requestBody, path, m_httpMethod); + + return Encoding.UTF8.GetBytes(responseString); + } + + public RestStreamHandler(RestMethod restMethod, string httpMethod, string contentType) { - + m_restMethod = restMethod; + m_httpMethod = httpMethod; + m_contentType = contentType; } } } -- cgit v1.1