RequestHelper.cs 1.5 KB

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