RequestHelper.cs 1.5 KB

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