2
0

RequestHelper.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using Emby.Server.Implementations.HttpServer;
  5. namespace Emby.Server.Implementations.Services
  6. {
  7. public class RequestHelper
  8. {
  9. public static Func<Type, Stream, Task<object>> GetRequestReader(HttpListenerHost host, string contentType)
  10. {
  11. switch (GetContentTypeWithoutEncoding(contentType))
  12. {
  13. case "application/xml":
  14. case "text/xml":
  15. case "text/xml; charset=utf-8": //"text/xml; charset=utf-8" also matches xml
  16. return host.DeserializeXml;
  17. case "application/json":
  18. case "text/json":
  19. return host.DeserializeJson;
  20. }
  21. return null;
  22. }
  23. public static Action<object, Stream> GetResponseWriter(HttpListenerHost host, string contentType)
  24. {
  25. switch (GetContentTypeWithoutEncoding(contentType))
  26. {
  27. case "application/xml":
  28. case "text/xml":
  29. case "text/xml; charset=utf-8": //"text/xml; charset=utf-8" also matches xml
  30. return host.SerializeToXml;
  31. case "application/json":
  32. case "text/json":
  33. return host.SerializeToJson;
  34. }
  35. return null;
  36. }
  37. private static string GetContentTypeWithoutEncoding(string contentType)
  38. {
  39. return contentType == null
  40. ? null
  41. : contentType.Split(';')[0].ToLowerInvariant().Trim();
  42. }
  43. }
  44. }