2016-11-11 19:55:12 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
2017-02-13 01:07:48 +00:00
|
|
|
using ServiceStack;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2017-02-13 01:07:48 +00:00
|
|
|
namespace Emby.Server.Implementations.Services
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2017-02-13 01:07:48 +00:00
|
|
|
public class RequestHelper
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2017-02-13 01:07:48 +00:00
|
|
|
public static Func<Type, Stream, object> GetRequestReader(string contentType)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2017-02-13 01:07:48 +00:00
|
|
|
switch (GetContentTypeWithoutEncoding(contentType))
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
case "application/xml":
|
|
|
|
case "text/xml":
|
|
|
|
case "text/xml; charset=utf-8": //"text/xml; charset=utf-8" also matches xml
|
2017-02-13 01:07:48 +00:00
|
|
|
return ServiceStackHost.Instance.DeserializeXml;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
case "application/json":
|
|
|
|
case "text/json":
|
2017-02-13 01:07:48 +00:00
|
|
|
return ServiceStackHost.Instance.DeserializeJson;
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-02-13 01:07:48 +00:00
|
|
|
public static Action<object, Stream> GetResponseWriter(string contentType)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
2017-02-13 01:07:48 +00:00
|
|
|
switch (GetContentTypeWithoutEncoding(contentType))
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
case "application/xml":
|
|
|
|
case "text/xml":
|
|
|
|
case "text/xml; charset=utf-8": //"text/xml; charset=utf-8" also matches xml
|
2017-02-13 01:07:48 +00:00
|
|
|
return (o, s) => ServiceStackHost.Instance.SerializeToXml(o, s);
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
case "application/json":
|
|
|
|
case "text/json":
|
2017-02-13 01:07:48 +00:00
|
|
|
return (o, s) => ServiceStackHost.Instance.SerializeToJson(o, s);
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-02-13 01:07:48 +00:00
|
|
|
private static string GetContentTypeWithoutEncoding(string contentType)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
return contentType == null
|
|
|
|
? null
|
|
|
|
: contentType.Split(';')[0].ToLower().Trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|