* Started work on converting BaseHttpServer to a stream dispatcher

Sugilite
lbsa71 2007-07-04 04:29:23 +00:00
parent bd8018fa1c
commit 8b3cb93b49
4 changed files with 160 additions and 81 deletions

View File

@ -65,6 +65,7 @@ namespace OpenSim.Framework.Servers
protected HttpListener m_httpListener; protected HttpListener m_httpListener;
protected Dictionary<string, RestMethodEntry> m_restHandlers = new Dictionary<string, RestMethodEntry>(); protected Dictionary<string, RestMethodEntry> m_restHandlers = new Dictionary<string, RestMethodEntry>();
protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>(); protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>();
protected Dictionary<string, IStreamHandler> m_streamHandlers = new Dictionary<string, IStreamHandler>();
protected int m_port; protected int m_port;
protected bool firstcaps = true; protected bool firstcaps = true;
@ -73,6 +74,11 @@ namespace OpenSim.Framework.Servers
m_port = port; m_port = port;
} }
private void AddStreamHandler(string path, IStreamHandler handler)
{
m_streamHandlers.Add(path, handler);
}
public bool AddRestHandler(string method, string path, RestMethod handler) public bool AddRestHandler(string method, string path, RestMethod handler)
{ {
//Console.WriteLine("adding new REST handler for path " + path); //Console.WriteLine("adding new REST handler for path " + path);
@ -189,8 +195,6 @@ namespace OpenSim.Framework.Servers
} }
public virtual void HandleRequest(Object stateinfo) public virtual void HandleRequest(Object stateinfo)
{
try
{ {
HttpListenerContext context = (HttpListenerContext)stateinfo; HttpListenerContext context = (HttpListenerContext)stateinfo;
@ -200,7 +204,51 @@ namespace OpenSim.Framework.Servers
response.KeepAlive = false; response.KeepAlive = false;
response.SendChunked = false; response.SendChunked = false;
string path = request.RawUrl;
IStreamHandler streamHandler;
if(TryGetStreamHandler(path, out streamHandler))
{
streamHandler.Handle(path, request.InputStream, response.OutputStream );
}
else
{
HandleLegacyRequests(request, response);
}
}
private bool TryGetStreamHandler(string path, 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)
{
bestMatch = pattern;
}
}
}
if( String.IsNullOrEmpty( bestMatch ) )
{
streamHandler = null;
return false;
}
else
{
streamHandler = m_streamHandlers[bestMatch];
return true;
}
}
private void HandleLegacyRequests(HttpListenerRequest request, HttpListenerResponse response)
{
Stream body = request.InputStream; Stream body = request.InputStream;
Encoding encoding = Encoding.UTF8; Encoding encoding = Encoding.UTF8;
StreamReader reader = new StreamReader(body, encoding); StreamReader reader = new StreamReader(body, encoding);
@ -250,14 +298,12 @@ namespace OpenSim.Framework.Servers
Stream output = response.OutputStream; Stream output = response.OutputStream;
response.SendChunked = false; response.SendChunked = false;
response.ContentLength64 = buffer.Length; response.ContentLength64 = buffer.Length;
output.Write(buffer, 0, buffer.Length); output.Write(buffer, 0, buffer.Length);
output.Close(); output.Close();
} }
catch (Exception e)
{
//Console.WriteLine(e.ToString());
}
}
public void Start() public void Start()
{ {
@ -291,9 +337,5 @@ namespace OpenSim.Framework.Servers
} }
} }
public void AddLlsdMethod<TResponse, TRequest>(string path, LlsdMethod<TResponse, TRequest> handler )
{
throw new Exception("The method or operation is not implemented.");
}
} }
} }

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace OpenSim.Framework.Servers
{
public interface IStreamHandler
{
void Handle(string path, Stream request, Stream response);
}
}

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> <PropertyGroup>
<ProjectType>Local</ProjectType> <ProjectType>Local</ProjectType>
<ProductVersion>8.0.50727</ProductVersion> <ProductVersion>8.0.50727</ProductVersion>
@ -6,7 +6,8 @@
<ProjectGuid>{2CC71860-0000-0000-0000-000000000000}</ProjectGuid> <ProjectGuid>{2CC71860-0000-0000-0000-000000000000}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ApplicationIcon></ApplicationIcon> <ApplicationIcon>
</ApplicationIcon>
<AssemblyKeyContainerName> <AssemblyKeyContainerName>
</AssemblyKeyContainerName> </AssemblyKeyContainerName>
<AssemblyName>OpenSim.Framework.Servers</AssemblyName> <AssemblyName>OpenSim.Framework.Servers</AssemblyName>
@ -15,9 +16,11 @@
<DefaultTargetSchema>IE50</DefaultTargetSchema> <DefaultTargetSchema>IE50</DefaultTargetSchema>
<DelaySign>false</DelaySign> <DelaySign>false</DelaySign>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<AppDesignerFolder></AppDesignerFolder> <AppDesignerFolder>
</AppDesignerFolder>
<RootNamespace>OpenSim.Framework.Servers</RootNamespace> <RootNamespace>OpenSim.Framework.Servers</RootNamespace>
<StartupObject></StartupObject> <StartupObject>
</StartupObject>
<FileUpgradeFlags> <FileUpgradeFlags>
</FileUpgradeFlags> </FileUpgradeFlags>
</PropertyGroup> </PropertyGroup>
@ -28,7 +31,8 @@
<ConfigurationOverrideFile> <ConfigurationOverrideFile>
</ConfigurationOverrideFile> </ConfigurationOverrideFile>
<DefineConstants>TRACE;DEBUG</DefineConstants> <DefineConstants>TRACE;DEBUG</DefineConstants>
<DocumentationFile></DocumentationFile> <DocumentationFile>
</DocumentationFile>
<DebugSymbols>True</DebugSymbols> <DebugSymbols>True</DebugSymbols>
<FileAlignment>4096</FileAlignment> <FileAlignment>4096</FileAlignment>
<Optimize>False</Optimize> <Optimize>False</Optimize>
@ -37,7 +41,8 @@
<RemoveIntegerChecks>False</RemoveIntegerChecks> <RemoveIntegerChecks>False</RemoveIntegerChecks>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors> <TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<NoWarn></NoWarn> <NoWarn>
</NoWarn>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<AllowUnsafeBlocks>False</AllowUnsafeBlocks> <AllowUnsafeBlocks>False</AllowUnsafeBlocks>
@ -46,7 +51,8 @@
<ConfigurationOverrideFile> <ConfigurationOverrideFile>
</ConfigurationOverrideFile> </ConfigurationOverrideFile>
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE</DefineConstants>
<DocumentationFile></DocumentationFile> <DocumentationFile>
</DocumentationFile>
<DebugSymbols>False</DebugSymbols> <DebugSymbols>False</DebugSymbols>
<FileAlignment>4096</FileAlignment> <FileAlignment>4096</FileAlignment>
<Optimize>True</Optimize> <Optimize>True</Optimize>
@ -55,22 +61,24 @@
<RemoveIntegerChecks>False</RemoveIntegerChecks> <RemoveIntegerChecks>False</RemoveIntegerChecks>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors> <TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<NoWarn></NoWarn> <NoWarn>
</NoWarn>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="libsecondlife.dll" > <Reference Include="libsecondlife.dll">
<HintPath>..\..\..\bin\libsecondlife.dll</HintPath> <HintPath>..\..\..\bin\libsecondlife.dll</HintPath>
<Private>False</Private> <Private>False</Private>
</Reference> </Reference>
<Reference Include="System" > <Reference Include="System">
<HintPath>System.dll</HintPath> <HintPath>System.dll</HintPath>
<Private>False</Private> <Private>False</Private>
</Reference> </Reference>
<Reference Include="System.Xml" > <Reference Include="System.Data" />
<Reference Include="System.Xml">
<HintPath>System.Xml.dll</HintPath> <HintPath>System.Xml.dll</HintPath>
<Private>False</Private> <Private>False</Private>
</Reference> </Reference>
<Reference Include="XMLRPC.dll" > <Reference Include="XMLRPC.dll">
<HintPath>..\..\..\bin\XMLRPC.dll</HintPath> <HintPath>..\..\..\bin\XMLRPC.dll</HintPath>
<Private>False</Private> <Private>False</Private>
</Reference> </Reference>
@ -99,12 +107,14 @@
<Compile Include="ILlsdMethodHandler.cs"> <Compile Include="ILlsdMethodHandler.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="IStreamHandler.cs" />
<Compile Include="LlsdMethod.cs"> <Compile Include="LlsdMethod.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="RestMethod.cs"> <Compile Include="RestMethod.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="RestStreamHandler.cs" />
<Compile Include="UDPServerBase.cs"> <Compile Include="UDPServerBase.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace OpenSim.Framework.Servers
{
public class RestStreamHandler : IStreamHandler
{
public void Handle( string path, Stream request, Stream response )
{
}
}
}