aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Services/RequestHelper.cs
blob: 24e9cbfa4d7eb00dde0770517811bb34523bc3c5 (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
using System;
using System.IO;
using System.Threading.Tasks;
using Emby.Server.Implementations.HttpServer;

namespace Emby.Server.Implementations.Services
{
    public class RequestHelper
    {
        public static Func<Type, Stream, Task<object>> GetRequestReader(HttpListenerHost host, string contentType)
        {
            switch (GetContentTypeWithoutEncoding(contentType))
            {
                case "application/xml":
                case "text/xml":
                case "text/xml; charset=utf-8": //"text/xml; charset=utf-8" also matches xml
                    return host.DeserializeXml;

                case "application/json":
                case "text/json":
                    return host.DeserializeJson;
            }

            return null;
        }

        public static Action<object, Stream> GetResponseWriter(HttpListenerHost host, string contentType)
        {
            switch (GetContentTypeWithoutEncoding(contentType))
            {
                case "application/xml":
                case "text/xml":
                case "text/xml; charset=utf-8": //"text/xml; charset=utf-8" also matches xml
                    return host.SerializeToXml;

                case "application/json":
                case "text/json":
                    return host.SerializeToJson;
            }

            return null;
        }

        private static string GetContentTypeWithoutEncoding(string contentType)
        {
            return contentType == null
                       ? null
                       : contentType.Split(';')[0].ToLower().Trim();
        }

    }
}