* 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 Dictionary<string, RestMethodEntry> m_restHandlers = new Dictionary<string, RestMethodEntry>();
|
||||
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 bool firstcaps = true;
|
||||
|
||||
|
@ -73,9 +74,14 @@ namespace OpenSim.Framework.Servers
|
|||
m_port = port;
|
||||
}
|
||||
|
||||
private void AddStreamHandler(string path, IStreamHandler handler)
|
||||
{
|
||||
m_streamHandlers.Add(path, 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);
|
||||
|
||||
if (!this.m_restHandlers.ContainsKey(methodKey))
|
||||
|
@ -190,73 +196,113 @@ namespace OpenSim.Framework.Servers
|
|||
|
||||
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;
|
||||
|
||||
HttpListenerRequest request = context.Request;
|
||||
HttpListenerResponse response = context.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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
catch (Exception e)
|
||||
if( String.IsNullOrEmpty( bestMatch ) )
|
||||
{
|
||||
//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()
|
||||
|
@ -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>
|
||||
<ProjectType>Local</ProjectType>
|
||||
<ProductVersion>8.0.50727</ProductVersion>
|
||||
|
@ -6,7 +6,8 @@
|
|||
<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>
|
||||
|
@ -15,9 +16,11 @@
|
|||
<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>
|
||||
|
@ -28,7 +31,8 @@
|
|||
<ConfigurationOverrideFile>
|
||||
</ConfigurationOverrideFile>
|
||||
<DefineConstants>TRACE;DEBUG</DefineConstants>
|
||||
<DocumentationFile></DocumentationFile>
|
||||
<DocumentationFile>
|
||||
</DocumentationFile>
|
||||
<DebugSymbols>True</DebugSymbols>
|
||||
<FileAlignment>4096</FileAlignment>
|
||||
<Optimize>False</Optimize>
|
||||
|
@ -37,7 +41,8 @@
|
|||
<RemoveIntegerChecks>False</RemoveIntegerChecks>
|
||||
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<NoWarn></NoWarn>
|
||||
<NoWarn>
|
||||
</NoWarn>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
|
||||
|
@ -46,7 +51,8 @@
|
|||
<ConfigurationOverrideFile>
|
||||
</ConfigurationOverrideFile>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<DocumentationFile></DocumentationFile>
|
||||
<DocumentationFile>
|
||||
</DocumentationFile>
|
||||
<DebugSymbols>False</DebugSymbols>
|
||||
<FileAlignment>4096</FileAlignment>
|
||||
<Optimize>True</Optimize>
|
||||
|
@ -55,22 +61,24 @@
|
|||
<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.Xml" >
|
||||
<Reference Include="System.Data" />
|
||||
<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>
|
||||
|
@ -80,13 +88,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>
|
||||
|
@ -99,12 +107,14 @@
|
|||
<Compile Include="ILlsdMethodHandler.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="IStreamHandler.cs" />
|
||||
<Compile Include="LlsdMethod.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="RestMethod.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="RestStreamHandler.cs" />
|
||||
<Compile Include="UDPServerBase.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
|
@ -119,4 +129,4 @@
|
|||
<PostBuildEvent>
|
||||
</PostBuildEvent>
|
||||
</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