ResponseFilter.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4. using MediaBrowser.Model.Services;
  5. using Microsoft.Extensions.Logging;
  6. namespace Emby.Server.Implementations.HttpServer
  7. {
  8. public class ResponseFilter
  9. {
  10. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  11. private readonly ILogger _logger;
  12. public ResponseFilter(ILogger logger)
  13. {
  14. _logger = logger;
  15. }
  16. /// <summary>
  17. /// Filters the response.
  18. /// </summary>
  19. /// <param name="req">The req.</param>
  20. /// <param name="res">The res.</param>
  21. /// <param name="dto">The dto.</param>
  22. public void FilterResponse(IRequest req, IResponse res, object dto)
  23. {
  24. // Try to prevent compatibility view
  25. res.AddHeader("Access-Control-Allow-Headers", "Accept, Accept-Language, Authorization, Cache-Control, Content-Disposition, Content-Encoding, Content-Language, Content-Length, Content-MD5, Content-Range, Content-Type, Date, Host, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, Origin, OriginToken, Pragma, Range, Slug, Transfer-Encoding, Want-Digest, X-MediaBrowser-Token, X-Emby-Authorization");
  26. res.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS");
  27. res.AddHeader("Access-Control-Allow-Origin", "*");
  28. if (dto is Exception exception)
  29. {
  30. _logger.LogError(exception, "Error processing request for {RawUrl}", req.RawUrl);
  31. if (!string.IsNullOrEmpty(exception.Message))
  32. {
  33. var error = exception.Message.Replace(Environment.NewLine, " ");
  34. error = RemoveControlCharacters(error);
  35. res.AddHeader("X-Application-Error-Code", error);
  36. }
  37. }
  38. if (dto is IHasHeaders hasHeaders)
  39. {
  40. if (!hasHeaders.Headers.ContainsKey("Server"))
  41. {
  42. hasHeaders.Headers["Server"] = "Microsoft-NetCore/2.0, UPnP/1.0 DLNADOC/1.50";
  43. }
  44. // Content length has to be explicitly set on on HttpListenerResponse or it won't be happy
  45. if (hasHeaders.Headers.TryGetValue("Content-Length", out string contentLength)
  46. && !string.IsNullOrEmpty(contentLength))
  47. {
  48. var length = long.Parse(contentLength, UsCulture);
  49. if (length > 0)
  50. {
  51. res.SetContentLength(length);
  52. res.SendChunked = false;
  53. }
  54. }
  55. }
  56. }
  57. /// <summary>
  58. /// Removes the control characters.
  59. /// </summary>
  60. /// <param name="inString">The in string.</param>
  61. /// <returns>System.String.</returns>
  62. public static string RemoveControlCharacters(string inString)
  63. {
  64. if (inString == null) return null;
  65. var newString = new StringBuilder();
  66. foreach (var ch in inString)
  67. {
  68. if (!char.IsControl(ch))
  69. {
  70. newString.Append(ch);
  71. }
  72. }
  73. return newString.ToString();
  74. }
  75. }
  76. }