diff options
Diffstat (limited to '')
-rw-r--r-- | OpenGridServices.AssetServer/AssetHttpServer.cs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/OpenGridServices.AssetServer/AssetHttpServer.cs b/OpenGridServices.AssetServer/AssetHttpServer.cs new file mode 100644 index 0000000..8439e92 --- /dev/null +++ b/OpenGridServices.AssetServer/AssetHttpServer.cs | |||
@@ -0,0 +1,92 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Net; | ||
4 | using System.Text; | ||
5 | using System.Text.RegularExpressions; | ||
6 | using System.Threading; | ||
7 | //using OpenSim.CAPS; | ||
8 | using Nwc.XmlRpc; | ||
9 | using System.Collections; | ||
10 | using OpenSim.Framework.Console; | ||
11 | using OpenSim.Servers; | ||
12 | |||
13 | namespace OpenGridServices.AssetServer | ||
14 | { | ||
15 | public class AssetHttpServer :BaseHttpServer | ||
16 | { | ||
17 | public AssetHttpServer(int port) | ||
18 | : base(port) | ||
19 | { | ||
20 | } | ||
21 | |||
22 | public override void HandleRequest(Object stateinfo) | ||
23 | { | ||
24 | try | ||
25 | { | ||
26 | HttpListenerContext context = (HttpListenerContext)stateinfo; | ||
27 | |||
28 | HttpListenerRequest request = context.Request; | ||
29 | HttpListenerResponse response = context.Response; | ||
30 | |||
31 | response.KeepAlive = false; | ||
32 | response.SendChunked = false; | ||
33 | |||
34 | System.IO.Stream body = request.InputStream; | ||
35 | System.Text.Encoding encoding = System.Text.Encoding.UTF8; | ||
36 | System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding); | ||
37 | |||
38 | string requestBody = reader.ReadToEnd(); | ||
39 | body.Close(); | ||
40 | reader.Close(); | ||
41 | |||
42 | //Console.WriteLine(request.HttpMethod + " " + request.RawUrl + " Http/" + request.ProtocolVersion.ToString() + " content type: " + request.ContentType); | ||
43 | //Console.WriteLine(requestBody); | ||
44 | |||
45 | string responseString = ""; | ||
46 | switch (request.ContentType) | ||
47 | { | ||
48 | case "text/xml": | ||
49 | // must be XML-RPC, so pass to the XML-RPC parser | ||
50 | |||
51 | responseString = ParseXMLRPC(requestBody); | ||
52 | responseString = Regex.Replace(responseString, "utf-16", "utf-8"); | ||
53 | |||
54 | response.AddHeader("Content-type", "text/xml"); | ||
55 | break; | ||
56 | |||
57 | case "application/xml": | ||
58 | // probably LLSD we hope, otherwise it should be ignored by the parser | ||
59 | responseString = ParseLLSDXML(requestBody); | ||
60 | response.AddHeader("Content-type", "application/xml"); | ||
61 | break; | ||
62 | |||
63 | case "application/x-www-form-urlencoded": | ||
64 | // a form data POST so send to the REST parser | ||
65 | responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); | ||
66 | response.AddHeader("Content-type", "text/plain"); | ||
67 | break; | ||
68 | |||
69 | case null: | ||
70 | // must be REST or invalid crap, so pass to the REST parser | ||
71 | responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); | ||
72 | response.AddHeader("Content-type", "text/plain"); | ||
73 | break; | ||
74 | |||
75 | } | ||
76 | |||
77 | Encoding Windows1252Encoding = Encoding.GetEncoding(1252); | ||
78 | byte[] buffer = Windows1252Encoding.GetBytes(responseString); | ||
79 | System.IO.Stream output = response.OutputStream; | ||
80 | response.SendChunked = false; | ||
81 | response.ContentLength64 = buffer.Length; | ||
82 | output.Write(buffer, 0, buffer.Length); | ||
83 | output.Close(); | ||
84 | } | ||
85 | catch (Exception e) | ||
86 | { | ||
87 | Console.WriteLine(e.ToString()); | ||
88 | } | ||
89 | } | ||
90 | |||
91 | } | ||
92 | } | ||