* Started work on converting BaseHttpServer to a stream dispatcher
parent
bd8018fa1c
commit
8b3cb93b49
|
@ -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,9 +74,14 @@ 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);
|
||||||
string methodKey = String.Format("{0}: {1}", method, path);
|
string methodKey = String.Format("{0}: {1}", method, path);
|
||||||
|
|
||||||
if (!this.m_restHandlers.ContainsKey(methodKey))
|
if (!this.m_restHandlers.ContainsKey(methodKey))
|
||||||
|
@ -190,73 +196,113 @@ namespace OpenSim.Framework.Servers
|
||||||
|
|
||||||
public virtual void HandleRequest(Object stateinfo)
|
public virtual void HandleRequest(Object stateinfo)
|
||||||
{
|
{
|
||||||
try
|
HttpListenerContext context = (HttpListenerContext)stateinfo;
|
||||||
|
|
||||||
|
HttpListenerRequest request = context.Request;
|
||||||
|
HttpListenerResponse response = context.Response;
|
||||||
|
|
||||||
|
response.KeepAlive = false;
|
||||||
|
response.SendChunked = false;
|
||||||
|
|
||||||
|
string path = request.RawUrl;
|
||||||
|
|
||||||
|
IStreamHandler streamHandler;
|
||||||
|
|
||||||
|
if(TryGetStreamHandler(path, out streamHandler))
|
||||||
{
|
{
|
||||||
HttpListenerContext context = (HttpListenerContext)stateinfo;
|
streamHandler.Handle(path, request.InputStream, response.OutputStream );
|
||||||
|
}
|
||||||
HttpListenerRequest request = context.Request;
|
else
|
||||||
HttpListenerResponse response = context.Response;
|
{
|
||||||
|
HandleLegacyRequests(request, response);
|
||||||
response.KeepAlive = false;
|
}
|
||||||
response.SendChunked = false;
|
}
|
||||||
|
|
||||||
Stream body = request.InputStream;
|
|
||||||
Encoding encoding = Encoding.UTF8;
|
|
||||||
StreamReader reader = new StreamReader(body, encoding);
|
|
||||||
|
|
||||||
string requestBody = reader.ReadToEnd();
|
|
||||||
body.Close();
|
|
||||||
reader.Close();
|
|
||||||
|
|
||||||
//Console.WriteLine(request.HttpMethod + " " + request.RawUrl + " Http/" + request.ProtocolVersion.ToString() + " content type: " + request.ContentType);
|
|
||||||
//Console.WriteLine(requestBody);
|
|
||||||
|
|
||||||
string responseString = "";
|
|
||||||
// Console.WriteLine("new request " + request.ContentType +" at "+ request.RawUrl);
|
|
||||||
switch (request.ContentType)
|
|
||||||
{
|
|
||||||
case "text/xml":
|
|
||||||
// must be XML-RPC, so pass to the XML-RPC parser
|
|
||||||
|
|
||||||
responseString = ParseXMLRPC(requestBody);
|
|
||||||
responseString = Regex.Replace(responseString, "utf-16", "utf-8");
|
|
||||||
|
|
||||||
response.AddHeader("Content-type", "text/xml");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "application/xml":
|
|
||||||
case "application/octet-stream":
|
|
||||||
// probably LLSD we hope, otherwise it should be ignored by the parser
|
|
||||||
// responseString = ParseLLSDXML(requestBody);
|
|
||||||
responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
|
|
||||||
response.AddHeader("Content-type", "application/xml");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "application/x-www-form-urlencoded":
|
|
||||||
// a form data POST so send to the REST parser
|
|
||||||
responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
|
|
||||||
response.AddHeader("Content-type", "text/html");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case null:
|
|
||||||
// must be REST or invalid crap, so pass to the REST parser
|
|
||||||
responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
|
|
||||||
response.AddHeader("Content-type", "text/html");
|
|
||||||
break;
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
byte[] buffer = Encoding.UTF8.GetBytes(responseString);
|
if( String.IsNullOrEmpty( bestMatch ) )
|
||||||
Stream output = response.OutputStream;
|
|
||||||
response.SendChunked = false;
|
|
||||||
response.ContentLength64 = buffer.Length;
|
|
||||||
output.Write(buffer, 0, buffer.Length);
|
|
||||||
output.Close();
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
{
|
||||||
//Console.WriteLine(e.ToString());
|
streamHandler = null;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
streamHandler = m_streamHandlers[bestMatch];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleLegacyRequests(HttpListenerRequest request, HttpListenerResponse response)
|
||||||
|
{
|
||||||
|
Stream body = request.InputStream;
|
||||||
|
|
||||||
|
Encoding encoding = Encoding.UTF8;
|
||||||
|
StreamReader reader = new StreamReader(body, encoding);
|
||||||
|
|
||||||
|
string requestBody = reader.ReadToEnd();
|
||||||
|
body.Close();
|
||||||
|
reader.Close();
|
||||||
|
|
||||||
|
//Console.WriteLine(request.HttpMethod + " " + request.RawUrl + " Http/" + request.ProtocolVersion.ToString() + " content type: " + request.ContentType);
|
||||||
|
//Console.WriteLine(requestBody);
|
||||||
|
|
||||||
|
string responseString = "";
|
||||||
|
// Console.WriteLine("new request " + request.ContentType +" at "+ request.RawUrl);
|
||||||
|
switch (request.ContentType)
|
||||||
|
{
|
||||||
|
case "text/xml":
|
||||||
|
// must be XML-RPC, so pass to the XML-RPC parser
|
||||||
|
|
||||||
|
responseString = ParseXMLRPC(requestBody);
|
||||||
|
responseString = Regex.Replace(responseString, "utf-16", "utf-8");
|
||||||
|
|
||||||
|
response.AddHeader("Content-type", "text/xml");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "application/xml":
|
||||||
|
case "application/octet-stream":
|
||||||
|
// probably LLSD we hope, otherwise it should be ignored by the parser
|
||||||
|
// responseString = ParseLLSDXML(requestBody);
|
||||||
|
responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
|
||||||
|
response.AddHeader("Content-type", "application/xml");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "application/x-www-form-urlencoded":
|
||||||
|
// a form data POST so send to the REST parser
|
||||||
|
responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
|
||||||
|
response.AddHeader("Content-type", "text/html");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case null:
|
||||||
|
// must be REST or invalid crap, so pass to the REST parser
|
||||||
|
responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
|
||||||
|
response.AddHeader("Content-type", "text/html");
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] buffer = Encoding.UTF8.GetBytes(responseString);
|
||||||
|
Stream output = response.OutputStream;
|
||||||
|
response.SendChunked = false;
|
||||||
|
response.ContentLength64 = buffer.Length;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
output.Write(buffer, 0, buffer.Length);
|
||||||
|
output.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
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.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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>
|
||||||
|
@ -80,13 +88,13 @@
|
||||||
<Name>OpenSim.Framework</Name>
|
<Name>OpenSim.Framework</Name>
|
||||||
<Project>{8ACA2445-0000-0000-0000-000000000000}</Project>
|
<Project>{8ACA2445-0000-0000-0000-000000000000}</Project>
|
||||||
<Package>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</Package>
|
<Package>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</Package>
|
||||||
<Private>False</Private>
|
<Private>False</Private>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
<ProjectReference Include="..\Console\OpenSim.Framework.Console.csproj">
|
<ProjectReference Include="..\Console\OpenSim.Framework.Console.csproj">
|
||||||
<Name>OpenSim.Framework.Console</Name>
|
<Name>OpenSim.Framework.Console</Name>
|
||||||
<Project>{A7CD0630-0000-0000-0000-000000000000}</Project>
|
<Project>{A7CD0630-0000-0000-0000-000000000000}</Project>
|
||||||
<Package>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</Package>
|
<Package>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</Package>
|
||||||
<Private>False</Private>
|
<Private>False</Private>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -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>
|
||||||
|
@ -119,4 +129,4 @@
|
||||||
<PostBuildEvent>
|
<PostBuildEvent>
|
||||||
</PostBuildEvent>
|
</PostBuildEvent>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
</Project>
|
</Project>
|
|
@ -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 )
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue