jellyfin/Emby.Server.Implementations/Services/RequestHelper.cs

53 lines
1.6 KiB
C#
Raw Normal View History

2016-11-11 20:55:12 +01:00
using System;
using System.IO;
2018-09-12 19:26:21 +02:00
using System.Threading.Tasks;
using Emby.Server.Implementations.HttpServer;
2016-11-11 20:55:12 +01:00
2017-02-13 02:07:48 +01:00
namespace Emby.Server.Implementations.Services
2016-11-11 20:55:12 +01:00
{
2017-02-13 02:07:48 +01:00
public class RequestHelper
2016-11-11 20:55:12 +01:00
{
2018-09-12 19:26:21 +02:00
public static Func<Type, Stream, Task<object>> GetRequestReader(HttpListenerHost host, string contentType)
2016-11-11 20:55:12 +01:00
{
2017-02-13 02:07:48 +01:00
switch (GetContentTypeWithoutEncoding(contentType))
2016-11-11 20:55:12 +01:00
{
case "application/xml":
case "text/xml":
case "text/xml; charset=utf-8": //"text/xml; charset=utf-8" also matches xml
2017-02-13 03:06:54 +01:00
return host.DeserializeXml;
2016-11-11 20:55:12 +01:00
case "application/json":
case "text/json":
2017-02-13 03:06:54 +01:00
return host.DeserializeJson;
2016-11-11 20:55:12 +01:00
}
return null;
}
2017-02-13 03:06:54 +01:00
public static Action<object, Stream> GetResponseWriter(HttpListenerHost host, string contentType)
2016-11-11 20:55:12 +01:00
{
2017-02-13 02:07:48 +01:00
switch (GetContentTypeWithoutEncoding(contentType))
2016-11-11 20:55:12 +01:00
{
case "application/xml":
case "text/xml":
case "text/xml; charset=utf-8": //"text/xml; charset=utf-8" also matches xml
2017-02-13 03:06:54 +01:00
return host.SerializeToXml;
2016-11-11 20:55:12 +01:00
case "application/json":
case "text/json":
2017-02-13 03:06:54 +01:00
return host.SerializeToJson;
2016-11-11 20:55:12 +01:00
}
return null;
}
2017-02-13 02:07:48 +01:00
private static string GetContentTypeWithoutEncoding(string contentType)
2016-11-11 20:55:12 +01:00
{
return contentType == null
? null
2019-01-27 12:03:43 +01:00
: contentType.Split(';')[0].ToLowerInvariant().Trim();
2016-11-11 20:55:12 +01:00
}
}
}