diff options
author | lbsa71 | 2007-07-04 04:29:23 +0000 |
---|---|---|
committer | lbsa71 | 2007-07-04 04:29:23 +0000 |
commit | 8b3cb93b49b21b7729adc56a9c06658fb94b1e8f (patch) | |
tree | e279d472557a0966438e0eadedeb0b2579c34d3d /OpenSim | |
parent | Today's work on Building support/tools. Think I am slowly getting there. (diff) | |
download | opensim-SC-8b3cb93b49b21b7729adc56a9c06658fb94b1e8f.zip opensim-SC-8b3cb93b49b21b7729adc56a9c06658fb94b1e8f.tar.gz opensim-SC-8b3cb93b49b21b7729adc56a9c06658fb94b1e8f.tar.bz2 opensim-SC-8b3cb93b49b21b7729adc56a9c06658fb94b1e8f.tar.xz |
* Started work on converting BaseHttpServer to a stream dispatcher
Diffstat (limited to 'OpenSim')
-rw-r--r-- | OpenSim/Framework/Servers/BaseHttpServer.cs | 164 | ||||
-rw-r--r-- | OpenSim/Framework/Servers/IStreamHandler.cs | 12 | ||||
-rw-r--r-- | OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj | 40 | ||||
-rw-r--r-- | OpenSim/Framework/Servers/RestStreamHandler.cs | 15 |
4 files changed, 155 insertions, 76 deletions
diff --git a/OpenSim/Framework/Servers/BaseHttpServer.cs b/OpenSim/Framework/Servers/BaseHttpServer.cs index 713793c..81028b0 100644 --- a/OpenSim/Framework/Servers/BaseHttpServer.cs +++ b/OpenSim/Framework/Servers/BaseHttpServer.cs | |||
@@ -65,6 +65,7 @@ namespace OpenSim.Framework.Servers | |||
65 | protected HttpListener m_httpListener; | 65 | protected HttpListener m_httpListener; |
66 | protected Dictionary<string, RestMethodEntry> m_restHandlers = new Dictionary<string, RestMethodEntry>(); | 66 | protected Dictionary<string, RestMethodEntry> m_restHandlers = new Dictionary<string, RestMethodEntry>(); |
67 | protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>(); | 67 | protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>(); |
68 | protected Dictionary<string, IStreamHandler> m_streamHandlers = new Dictionary<string, IStreamHandler>(); | ||
68 | protected int m_port; | 69 | protected int m_port; |
69 | protected bool firstcaps = true; | 70 | protected bool firstcaps = true; |
70 | 71 | ||
@@ -73,9 +74,14 @@ namespace OpenSim.Framework.Servers | |||
73 | m_port = port; | 74 | m_port = port; |
74 | } | 75 | } |
75 | 76 | ||
77 | private void AddStreamHandler(string path, IStreamHandler handler) | ||
78 | { | ||
79 | m_streamHandlers.Add(path, handler); | ||
80 | } | ||
81 | |||
76 | public bool AddRestHandler(string method, string path, RestMethod handler) | 82 | public bool AddRestHandler(string method, string path, RestMethod handler) |
77 | { | 83 | { |
78 | //Console.WriteLine("adding new REST handler for path " + path); | 84 | //Console.WriteLine("adding new REST handler for path " + path); |
79 | string methodKey = String.Format("{0}: {1}", method, path); | 85 | string methodKey = String.Format("{0}: {1}", method, path); |
80 | 86 | ||
81 | if (!this.m_restHandlers.ContainsKey(methodKey)) | 87 | if (!this.m_restHandlers.ContainsKey(methodKey)) |
@@ -190,75 +196,115 @@ namespace OpenSim.Framework.Servers | |||
190 | 196 | ||
191 | public virtual void HandleRequest(Object stateinfo) | 197 | public virtual void HandleRequest(Object stateinfo) |
192 | { | 198 | { |
193 | try | 199 | HttpListenerContext context = (HttpListenerContext)stateinfo; |
194 | { | ||
195 | HttpListenerContext context = (HttpListenerContext)stateinfo; | ||
196 | 200 | ||
197 | HttpListenerRequest request = context.Request; | 201 | HttpListenerRequest request = context.Request; |
198 | HttpListenerResponse response = context.Response; | 202 | HttpListenerResponse response = context.Response; |
199 | 203 | ||
200 | response.KeepAlive = false; | 204 | response.KeepAlive = false; |
201 | response.SendChunked = false; | 205 | response.SendChunked = false; |
202 | 206 | ||
203 | Stream body = request.InputStream; | 207 | string path = request.RawUrl; |
204 | Encoding encoding = Encoding.UTF8; | ||
205 | StreamReader reader = new StreamReader(body, encoding); | ||
206 | 208 | ||
207 | string requestBody = reader.ReadToEnd(); | 209 | IStreamHandler streamHandler; |
208 | body.Close(); | ||
209 | reader.Close(); | ||
210 | 210 | ||
211 | //Console.WriteLine(request.HttpMethod + " " + request.RawUrl + " Http/" + request.ProtocolVersion.ToString() + " content type: " + request.ContentType); | 211 | if(TryGetStreamHandler(path, out streamHandler)) |
212 | //Console.WriteLine(requestBody); | 212 | { |
213 | 213 | streamHandler.Handle(path, request.InputStream, response.OutputStream ); | |
214 | string responseString = ""; | 214 | } |
215 | // Console.WriteLine("new request " + request.ContentType +" at "+ request.RawUrl); | 215 | else |
216 | switch (request.ContentType) | 216 | { |
217 | { | 217 | HandleLegacyRequests(request, response); |
218 | case "text/xml": | 218 | } |
219 | // must be XML-RPC, so pass to the XML-RPC parser | 219 | } |
220 | |||
221 | responseString = ParseXMLRPC(requestBody); | ||
222 | responseString = Regex.Replace(responseString, "utf-16", "utf-8"); | ||
223 | |||
224 | response.AddHeader("Content-type", "text/xml"); | ||
225 | break; | ||
226 | |||
227 | case "application/xml": | ||
228 | case "application/octet-stream": | ||
229 | // probably LLSD we hope, otherwise it should be ignored by the parser | ||
230 | // responseString = ParseLLSDXML(requestBody); | ||
231 | responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); | ||
232 | response.AddHeader("Content-type", "application/xml"); | ||
233 | break; | ||
234 | |||
235 | case "application/x-www-form-urlencoded": | ||
236 | // a form data POST so send to the REST parser | ||
237 | responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); | ||
238 | response.AddHeader("Content-type", "text/html"); | ||
239 | break; | ||
240 | |||
241 | case null: | ||
242 | // must be REST or invalid crap, so pass to the REST parser | ||
243 | responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); | ||
244 | response.AddHeader("Content-type", "text/html"); | ||
245 | break; | ||
246 | 220 | ||
221 | private bool TryGetStreamHandler(string path, out IStreamHandler streamHandler ) | ||
222 | { | ||
223 | string bestMatch = null; | ||
224 | |||
225 | foreach (string pattern in m_streamHandlers.Keys) | ||
226 | { | ||
227 | if (path.StartsWith(pattern)) | ||
228 | { | ||
229 | if (String.IsNullOrEmpty( bestMatch ) || pattern.Length > bestMatch.Length) | ||
230 | { | ||
231 | bestMatch = pattern; | ||
232 | } | ||
247 | } | 233 | } |
234 | } | ||
248 | 235 | ||
249 | byte[] buffer = Encoding.UTF8.GetBytes(responseString); | 236 | if( String.IsNullOrEmpty( bestMatch ) ) |
250 | Stream output = response.OutputStream; | 237 | { |
251 | response.SendChunked = false; | 238 | streamHandler = null; |
252 | response.ContentLength64 = buffer.Length; | 239 | return false; |
253 | output.Write(buffer, 0, buffer.Length); | ||
254 | output.Close(); | ||
255 | } | 240 | } |
256 | catch (Exception e) | 241 | else |
257 | { | 242 | { |
258 | //Console.WriteLine(e.ToString()); | 243 | streamHandler = m_streamHandlers[bestMatch]; |
244 | return true; | ||
259 | } | 245 | } |
260 | } | 246 | } |
261 | 247 | ||
248 | private void HandleLegacyRequests(HttpListenerRequest request, HttpListenerResponse response) | ||
249 | { | ||
250 | Stream body = request.InputStream; | ||
251 | |||
252 | Encoding encoding = Encoding.UTF8; | ||
253 | StreamReader reader = new StreamReader(body, encoding); | ||
254 | |||
255 | string requestBody = reader.ReadToEnd(); | ||
256 | body.Close(); | ||
257 | reader.Close(); | ||
258 | |||
259 | //Console.WriteLine(request.HttpMethod + " " + request.RawUrl + " Http/" + request.ProtocolVersion.ToString() + " content type: " + request.ContentType); | ||
260 | //Console.WriteLine(requestBody); | ||
261 | |||
262 | string responseString = ""; | ||
263 | // Console.WriteLine("new request " + request.ContentType +" at "+ request.RawUrl); | ||
264 | switch (request.ContentType) | ||
265 | { | ||
266 | case "text/xml": | ||
267 | // must be XML-RPC, so pass to the XML-RPC parser | ||
268 | |||
269 | responseString = ParseXMLRPC(requestBody); | ||
270 | responseString = Regex.Replace(responseString, "utf-16", "utf-8"); | ||
271 | |||
272 | response.AddHeader("Content-type", "text/xml"); | ||
273 | break; | ||
274 | |||
275 | case "application/xml": | ||
276 | case "application/octet-stream": | ||
277 | // probably LLSD we hope, otherwise it should be ignored by the parser | ||
278 | // responseString = ParseLLSDXML(requestBody); | ||
279 | responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); | ||
280 | response.AddHeader("Content-type", "application/xml"); | ||
281 | break; | ||
282 | |||
283 | case "application/x-www-form-urlencoded": | ||
284 | // a form data POST so send to the REST parser | ||
285 | responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); | ||
286 | response.AddHeader("Content-type", "text/html"); | ||
287 | break; | ||
288 | |||
289 | case null: | ||
290 | // must be REST or invalid crap, so pass to the REST parser | ||
291 | responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); | ||
292 | response.AddHeader("Content-type", "text/html"); | ||
293 | break; | ||
294 | |||
295 | } | ||
296 | |||
297 | byte[] buffer = Encoding.UTF8.GetBytes(responseString); | ||
298 | Stream output = response.OutputStream; | ||
299 | response.SendChunked = false; | ||
300 | response.ContentLength64 = buffer.Length; | ||
301 | |||
302 | |||
303 | |||
304 | output.Write(buffer, 0, buffer.Length); | ||
305 | output.Close(); | ||
306 | } | ||
307 | |||
262 | public void Start() | 308 | public void Start() |
263 | { | 309 | { |
264 | MainLog.Instance.WriteLine(LogPriority.LOW, "BaseHttpServer.cs: Starting up HTTP Server"); | 310 | MainLog.Instance.WriteLine(LogPriority.LOW, "BaseHttpServer.cs: Starting up HTTP Server"); |
@@ -291,9 +337,5 @@ namespace OpenSim.Framework.Servers | |||
291 | } | 337 | } |
292 | } | 338 | } |
293 | 339 | ||
294 | public void AddLlsdMethod<TResponse, TRequest>(string path, LlsdMethod<TResponse, TRequest> handler ) | ||
295 | { | ||
296 | throw new Exception("The method or operation is not implemented."); | ||
297 | } | ||
298 | } | 340 | } |
299 | } | 341 | } |
diff --git a/OpenSim/Framework/Servers/IStreamHandler.cs b/OpenSim/Framework/Servers/IStreamHandler.cs new file mode 100644 index 0000000..88ae641 --- /dev/null +++ b/OpenSim/Framework/Servers/IStreamHandler.cs | |||
@@ -0,0 +1,12 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Text; | ||
4 | using System.IO; | ||
5 | |||
6 | namespace OpenSim.Framework.Servers | ||
7 | { | ||
8 | public interface IStreamHandler | ||
9 | { | ||
10 | void Handle(string path, Stream request, Stream response); | ||
11 | } | ||
12 | } | ||
diff --git a/OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj b/OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj index 909bd62..956a9bc 100644 --- a/OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj +++ b/OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj | |||
@@ -1,4 +1,4 @@ | |||
1 | <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | 1 | <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
2 | <PropertyGroup> | 2 | <PropertyGroup> |
3 | <ProjectType>Local</ProjectType> | 3 | <ProjectType>Local</ProjectType> |
4 | <ProductVersion>8.0.50727</ProductVersion> | 4 | <ProductVersion>8.0.50727</ProductVersion> |
@@ -6,7 +6,8 @@ | |||
6 | <ProjectGuid>{2CC71860-0000-0000-0000-000000000000}</ProjectGuid> | 6 | <ProjectGuid>{2CC71860-0000-0000-0000-000000000000}</ProjectGuid> |
7 | <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | 7 | <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
8 | <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | 8 | <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> |
9 | <ApplicationIcon></ApplicationIcon> | 9 | <ApplicationIcon> |
10 | </ApplicationIcon> | ||
10 | <AssemblyKeyContainerName> | 11 | <AssemblyKeyContainerName> |
11 | </AssemblyKeyContainerName> | 12 | </AssemblyKeyContainerName> |
12 | <AssemblyName>OpenSim.Framework.Servers</AssemblyName> | 13 | <AssemblyName>OpenSim.Framework.Servers</AssemblyName> |
@@ -15,9 +16,11 @@ | |||
15 | <DefaultTargetSchema>IE50</DefaultTargetSchema> | 16 | <DefaultTargetSchema>IE50</DefaultTargetSchema> |
16 | <DelaySign>false</DelaySign> | 17 | <DelaySign>false</DelaySign> |
17 | <OutputType>Library</OutputType> | 18 | <OutputType>Library</OutputType> |
18 | <AppDesignerFolder></AppDesignerFolder> | 19 | <AppDesignerFolder> |
20 | </AppDesignerFolder> | ||
19 | <RootNamespace>OpenSim.Framework.Servers</RootNamespace> | 21 | <RootNamespace>OpenSim.Framework.Servers</RootNamespace> |
20 | <StartupObject></StartupObject> | 22 | <StartupObject> |
23 | </StartupObject> | ||
21 | <FileUpgradeFlags> | 24 | <FileUpgradeFlags> |
22 | </FileUpgradeFlags> | 25 | </FileUpgradeFlags> |
23 | </PropertyGroup> | 26 | </PropertyGroup> |
@@ -28,7 +31,8 @@ | |||
28 | <ConfigurationOverrideFile> | 31 | <ConfigurationOverrideFile> |
29 | </ConfigurationOverrideFile> | 32 | </ConfigurationOverrideFile> |
30 | <DefineConstants>TRACE;DEBUG</DefineConstants> | 33 | <DefineConstants>TRACE;DEBUG</DefineConstants> |
31 | <DocumentationFile></DocumentationFile> | 34 | <DocumentationFile> |
35 | </DocumentationFile> | ||
32 | <DebugSymbols>True</DebugSymbols> | 36 | <DebugSymbols>True</DebugSymbols> |
33 | <FileAlignment>4096</FileAlignment> | 37 | <FileAlignment>4096</FileAlignment> |
34 | <Optimize>False</Optimize> | 38 | <Optimize>False</Optimize> |
@@ -37,7 +41,8 @@ | |||
37 | <RemoveIntegerChecks>False</RemoveIntegerChecks> | 41 | <RemoveIntegerChecks>False</RemoveIntegerChecks> |
38 | <TreatWarningsAsErrors>False</TreatWarningsAsErrors> | 42 | <TreatWarningsAsErrors>False</TreatWarningsAsErrors> |
39 | <WarningLevel>4</WarningLevel> | 43 | <WarningLevel>4</WarningLevel> |
40 | <NoWarn></NoWarn> | 44 | <NoWarn> |
45 | </NoWarn> | ||
41 | </PropertyGroup> | 46 | </PropertyGroup> |
42 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | 47 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
43 | <AllowUnsafeBlocks>False</AllowUnsafeBlocks> | 48 | <AllowUnsafeBlocks>False</AllowUnsafeBlocks> |
@@ -46,7 +51,8 @@ | |||
46 | <ConfigurationOverrideFile> | 51 | <ConfigurationOverrideFile> |
47 | </ConfigurationOverrideFile> | 52 | </ConfigurationOverrideFile> |
48 | <DefineConstants>TRACE</DefineConstants> | 53 | <DefineConstants>TRACE</DefineConstants> |
49 | <DocumentationFile></DocumentationFile> | 54 | <DocumentationFile> |
55 | </DocumentationFile> | ||
50 | <DebugSymbols>False</DebugSymbols> | 56 | <DebugSymbols>False</DebugSymbols> |
51 | <FileAlignment>4096</FileAlignment> | 57 | <FileAlignment>4096</FileAlignment> |
52 | <Optimize>True</Optimize> | 58 | <Optimize>True</Optimize> |
@@ -55,22 +61,24 @@ | |||
55 | <RemoveIntegerChecks>False</RemoveIntegerChecks> | 61 | <RemoveIntegerChecks>False</RemoveIntegerChecks> |
56 | <TreatWarningsAsErrors>False</TreatWarningsAsErrors> | 62 | <TreatWarningsAsErrors>False</TreatWarningsAsErrors> |
57 | <WarningLevel>4</WarningLevel> | 63 | <WarningLevel>4</WarningLevel> |
58 | <NoWarn></NoWarn> | 64 | <NoWarn> |
65 | </NoWarn> | ||
59 | </PropertyGroup> | 66 | </PropertyGroup> |
60 | <ItemGroup> | 67 | <ItemGroup> |
61 | <Reference Include="libsecondlife.dll" > | 68 | <Reference Include="libsecondlife.dll"> |
62 | <HintPath>..\..\..\bin\libsecondlife.dll</HintPath> | 69 | <HintPath>..\..\..\bin\libsecondlife.dll</HintPath> |
63 | <Private>False</Private> | 70 | <Private>False</Private> |
64 | </Reference> | 71 | </Reference> |
65 | <Reference Include="System" > | 72 | <Reference Include="System"> |
66 | <HintPath>System.dll</HintPath> | 73 | <HintPath>System.dll</HintPath> |
67 | <Private>False</Private> | 74 | <Private>False</Private> |
68 | </Reference> | 75 | </Reference> |
69 | <Reference Include="System.Xml" > | 76 | <Reference Include="System.Data" /> |
77 | <Reference Include="System.Xml"> | ||
70 | <HintPath>System.Xml.dll</HintPath> | 78 | <HintPath>System.Xml.dll</HintPath> |
71 | <Private>False</Private> | 79 | <Private>False</Private> |
72 | </Reference> | 80 | </Reference> |
73 | <Reference Include="XMLRPC.dll" > | 81 | <Reference Include="XMLRPC.dll"> |
74 | <HintPath>..\..\..\bin\XMLRPC.dll</HintPath> | 82 | <HintPath>..\..\..\bin\XMLRPC.dll</HintPath> |
75 | <Private>False</Private> | 83 | <Private>False</Private> |
76 | </Reference> | 84 | </Reference> |
@@ -80,13 +88,13 @@ | |||
80 | <Name>OpenSim.Framework</Name> | 88 | <Name>OpenSim.Framework</Name> |
81 | <Project>{8ACA2445-0000-0000-0000-000000000000}</Project> | 89 | <Project>{8ACA2445-0000-0000-0000-000000000000}</Project> |
82 | <Package>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</Package> | 90 | <Package>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</Package> |
83 | <Private>False</Private> | 91 | <Private>False</Private> |
84 | </ProjectReference> | 92 | </ProjectReference> |
85 | <ProjectReference Include="..\Console\OpenSim.Framework.Console.csproj"> | 93 | <ProjectReference Include="..\Console\OpenSim.Framework.Console.csproj"> |
86 | <Name>OpenSim.Framework.Console</Name> | 94 | <Name>OpenSim.Framework.Console</Name> |
87 | <Project>{A7CD0630-0000-0000-0000-000000000000}</Project> | 95 | <Project>{A7CD0630-0000-0000-0000-000000000000}</Project> |
88 | <Package>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</Package> | 96 | <Package>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</Package> |
89 | <Private>False</Private> | 97 | <Private>False</Private> |
90 | </ProjectReference> | 98 | </ProjectReference> |
91 | </ItemGroup> | 99 | </ItemGroup> |
92 | <ItemGroup> | 100 | <ItemGroup> |
@@ -99,12 +107,14 @@ | |||
99 | <Compile Include="ILlsdMethodHandler.cs"> | 107 | <Compile Include="ILlsdMethodHandler.cs"> |
100 | <SubType>Code</SubType> | 108 | <SubType>Code</SubType> |
101 | </Compile> | 109 | </Compile> |
110 | <Compile Include="IStreamHandler.cs" /> | ||
102 | <Compile Include="LlsdMethod.cs"> | 111 | <Compile Include="LlsdMethod.cs"> |
103 | <SubType>Code</SubType> | 112 | <SubType>Code</SubType> |
104 | </Compile> | 113 | </Compile> |
105 | <Compile Include="RestMethod.cs"> | 114 | <Compile Include="RestMethod.cs"> |
106 | <SubType>Code</SubType> | 115 | <SubType>Code</SubType> |
107 | </Compile> | 116 | </Compile> |
117 | <Compile Include="RestStreamHandler.cs" /> | ||
108 | <Compile Include="UDPServerBase.cs"> | 118 | <Compile Include="UDPServerBase.cs"> |
109 | <SubType>Code</SubType> | 119 | <SubType>Code</SubType> |
110 | </Compile> | 120 | </Compile> |
@@ -119,4 +129,4 @@ | |||
119 | <PostBuildEvent> | 129 | <PostBuildEvent> |
120 | </PostBuildEvent> | 130 | </PostBuildEvent> |
121 | </PropertyGroup> | 131 | </PropertyGroup> |
122 | </Project> | 132 | </Project> \ No newline at end of file |
diff --git a/OpenSim/Framework/Servers/RestStreamHandler.cs b/OpenSim/Framework/Servers/RestStreamHandler.cs new file mode 100644 index 0000000..145a184 --- /dev/null +++ b/OpenSim/Framework/Servers/RestStreamHandler.cs | |||
@@ -0,0 +1,15 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Text; | ||
4 | using System.IO; | ||
5 | |||
6 | namespace OpenSim.Framework.Servers | ||
7 | { | ||
8 | public class RestStreamHandler : IStreamHandler | ||
9 | { | ||
10 | public void Handle( string path, Stream request, Stream response ) | ||
11 | { | ||
12 | |||
13 | } | ||
14 | } | ||
15 | } | ||