aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenGridServices.AssetServer/AssetHttpServer.cs
blob: 8439e92839596053d0d98ba4dda433d0cd2fd49b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
//using OpenSim.CAPS;
using Nwc.XmlRpc;
using System.Collections;
using OpenSim.Framework.Console;
using OpenSim.Servers;

namespace OpenGridServices.AssetServer
{
    public class AssetHttpServer :BaseHttpServer
    {
        public AssetHttpServer(int port)
            : base(port)
        {
        }

        public override void HandleRequest(Object stateinfo)
        {
            try
            {
                HttpListenerContext context = (HttpListenerContext)stateinfo;

                HttpListenerRequest request = context.Request;
                HttpListenerResponse response = context.Response;

                response.KeepAlive = false;
                response.SendChunked = false;

                System.IO.Stream body = request.InputStream;
                System.Text.Encoding encoding = System.Text.Encoding.UTF8;
                System.IO.StreamReader reader = new System.IO.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 = "";
                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":
                        // probably LLSD we hope, otherwise it should be ignored by the parser
                        responseString = ParseLLSDXML(requestBody);
                        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/plain");
                        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/plain");
                        break;

                }

                Encoding Windows1252Encoding = Encoding.GetEncoding(1252);
                byte[] buffer = Windows1252Encoding.GetBytes(responseString);
                System.IO.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());
            }
        }

    }
}