* 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
This commit is contained in:
lbsa71
2007-07-04 11:47:32 +00:00
parent 827cccb99c
commit 9a51949cb4
9 changed files with 408 additions and 260 deletions

View File

@@ -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<string, RestMethodEntry> m_restHandlers = new Dictionary<string, RestMethodEntry>();
@@ -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;

View File

@@ -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;}
}
}

View File

@@ -1,4 +1,4 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectType>Local</ProjectType>
<ProductVersion>8.0.50727</ProductVersion>
@@ -6,8 +6,7 @@
<ProjectGuid>{2CC71860-0000-0000-0000-000000000000}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ApplicationIcon>
</ApplicationIcon>
<ApplicationIcon></ApplicationIcon>
<AssemblyKeyContainerName>
</AssemblyKeyContainerName>
<AssemblyName>OpenSim.Framework.Servers</AssemblyName>
@@ -16,11 +15,9 @@
<DefaultTargetSchema>IE50</DefaultTargetSchema>
<DelaySign>false</DelaySign>
<OutputType>Library</OutputType>
<AppDesignerFolder>
</AppDesignerFolder>
<AppDesignerFolder></AppDesignerFolder>
<RootNamespace>OpenSim.Framework.Servers</RootNamespace>
<StartupObject>
</StartupObject>
<StartupObject></StartupObject>
<FileUpgradeFlags>
</FileUpgradeFlags>
</PropertyGroup>
@@ -31,8 +28,7 @@
<ConfigurationOverrideFile>
</ConfigurationOverrideFile>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<DocumentationFile>
</DocumentationFile>
<DocumentationFile></DocumentationFile>
<DebugSymbols>True</DebugSymbols>
<FileAlignment>4096</FileAlignment>
<Optimize>False</Optimize>
@@ -41,8 +37,7 @@
<RemoveIntegerChecks>False</RemoveIntegerChecks>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<WarningLevel>4</WarningLevel>
<NoWarn>
</NoWarn>
<NoWarn></NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
@@ -51,8 +46,7 @@
<ConfigurationOverrideFile>
</ConfigurationOverrideFile>
<DefineConstants>TRACE</DefineConstants>
<DocumentationFile>
</DocumentationFile>
<DocumentationFile></DocumentationFile>
<DebugSymbols>False</DebugSymbols>
<FileAlignment>4096</FileAlignment>
<Optimize>True</Optimize>
@@ -61,24 +55,22 @@
<RemoveIntegerChecks>False</RemoveIntegerChecks>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<WarningLevel>4</WarningLevel>
<NoWarn>
</NoWarn>
<NoWarn></NoWarn>
</PropertyGroup>
<ItemGroup>
<Reference Include="libsecondlife.dll">
<Reference Include="libsecondlife.dll" >
<HintPath>..\..\..\bin\libsecondlife.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System">
<Reference Include="System" >
<HintPath>System.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml">
<Reference Include="System.Xml" >
<HintPath>System.Xml.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="XMLRPC.dll">
<Reference Include="XMLRPC.dll" >
<HintPath>..\..\..\bin\XMLRPC.dll</HintPath>
<Private>False</Private>
</Reference>
@@ -88,13 +80,13 @@
<Name>OpenSim.Framework</Name>
<Project>{8ACA2445-0000-0000-0000-000000000000}</Project>
<Package>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</Package>
<Private>False</Private>
<Private>False</Private>
</ProjectReference>
<ProjectReference Include="..\Console\OpenSim.Framework.Console.csproj">
<Name>OpenSim.Framework.Console</Name>
<Project>{A7CD0630-0000-0000-0000-000000000000}</Project>
<Package>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</Package>
<Private>False</Private>
<Private>False</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
@@ -107,14 +99,21 @@
<Compile Include="ILlsdMethodHandler.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="IStreamHandler.cs" />
<Compile Include="IStreamHandler.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="LlsdMethod.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="RestMethod.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="RestStreamHandler.cs" />
<Compile Include="RestMethodEntry.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="RestStreamHandler.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="UDPServerBase.cs">
<SubType>Code</SubType>
</Compile>
@@ -129,4 +128,4 @@
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
</Project>
</Project>

View File

@@ -14,8 +14,11 @@
<include name="BaseHttpServer.cs" />
<include name="CheckSumServer.cs" />
<include name="ILlsdMethodHandler.cs" />
<include name="IStreamHandler.cs" />
<include name="LlsdMethod.cs" />
<include name="RestMethod.cs" />
<include name="RestMethodEntry.cs" />
<include name="RestStreamHandler.cs" />
<include name="UDPServerBase.cs" />
<include name="XmlRpcMethod.cs" />
</sources>

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}