aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Capabilities/LLSDStreamHandler.cs
blob: ff63353cd852f420374fe9f317c5e506b3557174 (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
using System;
using System.Collections.Generic;
using System.Text;
using OpenSim.Framework.Servers;
using System.IO;
using System.Collections;
using libsecondlife;

namespace OpenSim.Region.Capabilities
{
    public class LLSDStreamhandler<TRequest, TResponse> : BaseStreamHandler
        where TRequest : new()
    {
        private LLSDMethod<TRequest, TResponse> m_method;

        public LLSDStreamhandler(string httpMethod, string path, LLSDMethod<TRequest, TResponse> method)
            : base(httpMethod, path)
        {
            m_method = method;
        }
        
        public override byte[] Handle(string path, Stream request)
        {
            Encoding encoding = Encoding.UTF8;
            StreamReader streamReader = new StreamReader(request, encoding);

            string requestBody = streamReader.ReadToEnd();
            streamReader.Close();

            Hashtable hash = (Hashtable)LLSD.LLSDDeserialize(encoding.GetBytes(requestBody));
            TRequest llsdRequest = new TRequest();
            LLSDHelpers.DeserialiseLLSDMap(hash, llsdRequest);

            TResponse response = m_method(llsdRequest);

            return encoding.GetBytes( LLSDHelpers.SerialiseLLSDReply(response) );

        }
    }
}