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 ++++++++++------------------- 1 file changed, 22 insertions(+), 41 deletions(-) (limited to 'OpenSim/Framework/Servers/BaseHttpServer.cs') 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; -- cgit v1.1