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 +++++++++++- OpenSim/Region/Capabilities/Caps.cs | 47 +++++++++------- 7 files changed, 143 insertions(+), 89 deletions(-) create mode 100644 OpenSim/Framework/Servers/RestMethodEntry.cs (limited to 'OpenSim') 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; } } } diff --git a/OpenSim/Region/Capabilities/Caps.cs b/OpenSim/Region/Capabilities/Caps.cs index 4a283b2..0f6c471 100644 --- a/OpenSim/Region/Capabilities/Caps.cs +++ b/OpenSim/Region/Capabilities/Caps.cs @@ -41,13 +41,13 @@ namespace OpenSim.Region.Capabilities public class Caps { - private string httpListenerHostName; - private int httpListenPort; - private string capsObjectPath = "00001-"; - private string requestPath = "0000/"; - private string mapLayerPath = "0001/"; - private string newInventory = "0002/"; - private string requestTexture = "0003/"; + private string m_httpListenerHostName; + private int m_httpListenPort; + private string m_capsObjectPath = "00001-"; + private string m_requestPath = "0000/"; + private string m_mapLayerPath = "0001/"; + private string m_newInventory = "0002/"; + private string m_requestTexture = "0003/"; private string eventQueue = "0100/"; private BaseHttpServer httpListener; private LLUUID agentID; @@ -58,10 +58,10 @@ namespace OpenSim.Region.Capabilities public Caps(AssetCache assetCach, BaseHttpServer httpServer, string httpListen, int httpPort, string capsPath, LLUUID agent) { assetCache = assetCach; - capsObjectPath = capsPath; + m_capsObjectPath = capsPath; httpListener = httpServer; - httpListenerHostName = httpListen; - httpListenPort = httpPort; + m_httpListenerHostName = httpListen; + m_httpListenPort = httpPort; agentID = agent; } @@ -71,13 +71,20 @@ namespace OpenSim.Region.Capabilities public void RegisterHandlers() { Console.WriteLine("registering CAPS handlers"); - httpListener.AddRestHandler("POST", "/CAPS/" + capsObjectPath + requestPath, CapsRequest); - httpListener.AddRestHandler("POST", "/CAPS/" + capsObjectPath + mapLayerPath, MapLayer); - httpListener.AddRestHandler("POST", "/CAPS/" + capsObjectPath + newInventory, NewAgentInventory); - httpListener.AddRestHandler("POST", "/CAPS/" + capsObjectPath + eventQueue, ProcessEventQueue); - httpListener.AddRestHandler("POST", "/CAPS/" + capsObjectPath + requestTexture, RequestTexture); + + AddCapsHandler( httpListener, m_requestPath, CapsRequest); + AddCapsHandler( httpListener, m_mapLayerPath, MapLayer); + AddCapsHandler( httpListener, m_newInventory, NewAgentInventory); + AddCapsHandler( httpListener, eventQueue, ProcessEventQueue); + AddCapsHandler( httpListener, m_requestTexture, RequestTexture); } + private void AddCapsHandler( BaseHttpServer httpListener, string path, RestMethod restMethod ) + { + string capsBase = "/CAPS/" + m_capsObjectPath; + httpListener.AddStreamHandler(capsBase + path, new RestStreamHandler(restMethod, "POST", "application/xml")); + } + /// /// /// @@ -100,10 +107,10 @@ namespace OpenSim.Region.Capabilities protected LLSDCapsDetails GetCapabilities() { LLSDCapsDetails caps = new LLSDCapsDetails(); - string capsBaseUrl = "http://" + httpListenerHostName + ":" + httpListenPort.ToString() + "/CAPS/" + capsObjectPath; + string capsBaseUrl = "http://" + m_httpListenerHostName + ":" + m_httpListenPort.ToString() + "/CAPS/" + m_capsObjectPath; - caps.MapLayer = capsBaseUrl + mapLayerPath; - caps.NewFileAgentInventory = capsBaseUrl + newInventory; + caps.MapLayer = capsBaseUrl + m_mapLayerPath; + caps.NewFileAgentInventory = capsBaseUrl + m_newInventory; return caps; } @@ -204,10 +211,10 @@ namespace OpenSim.Region.Capabilities string res = ""; LLUUID newAsset = LLUUID.Random(); LLUUID newInvItem = LLUUID.Random(); - string uploaderPath = capsObjectPath + Util.RandomClass.Next(5000, 8000).ToString("0000"); + string uploaderPath = m_capsObjectPath + Util.RandomClass.Next(5000, 8000).ToString("0000"); AssetUploader uploader = new AssetUploader(newAsset, newInvItem, uploaderPath, this.httpListener); httpListener.AddRestHandler("POST", "/CAPS/" + uploaderPath, uploader.uploaderCaps); - string uploaderURL = "http://" + httpListenerHostName + ":" + httpListenPort.ToString() + "/CAPS/" + uploaderPath; + string uploaderURL = "http://" + m_httpListenerHostName + ":" + m_httpListenPort.ToString() + "/CAPS/" + uploaderPath; //Console.WriteLine("uploader url is " + uploaderURL); res += ""; res += "uploader" + uploaderURL + ""; -- cgit v1.1