ResponseFilter.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4. using MediaBrowser.Model.Services;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.Net.Http.Headers;
  8. namespace Emby.Server.Implementations.HttpServer
  9. {
  10. /// <summary>
  11. /// Class ResponseFilter.
  12. /// </summary>
  13. public class ResponseFilter
  14. {
  15. private readonly ILogger _logger;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="ResponseFilter"/> class.
  18. /// </summary>
  19. /// <param name="logger">The logger.</param>
  20. public ResponseFilter(ILogger logger)
  21. {
  22. _logger = logger;
  23. }
  24. /// <summary>
  25. /// Filters the response.
  26. /// </summary>
  27. /// <param name="req">The req.</param>
  28. /// <param name="res">The res.</param>
  29. /// <param name="dto">The dto.</param>
  30. public void FilterResponse(IRequest req, HttpResponse res, object dto)
  31. {
  32. // Try to prevent compatibility view
  33. res.Headers.Add("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");
  34. res.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS");
  35. res.Headers.Add("Access-Control-Allow-Origin", "*");
  36. if (dto is Exception exception)
  37. {
  38. _logger.LogError(exception, "Error processing request for {RawUrl}", req.RawUrl);
  39. if (!string.IsNullOrEmpty(exception.Message))
  40. {
  41. var error = exception.Message.Replace(Environment.NewLine, " ", StringComparison.Ordinal);
  42. error = RemoveControlCharacters(error);
  43. res.Headers.Add("X-Application-Error-Code", error);
  44. }
  45. }
  46. if (dto is IHasHeaders hasHeaders)
  47. {
  48. if (!hasHeaders.Headers.ContainsKey(HeaderNames.Server))
  49. {
  50. hasHeaders.Headers[HeaderNames.Server] = "Microsoft-NetCore/2.0, UPnP/1.0 DLNADOC/1.50";
  51. }
  52. // Content length has to be explicitly set on on HttpListenerResponse or it won't be happy
  53. if (hasHeaders.Headers.TryGetValue(HeaderNames.ContentLength, out string contentLength)
  54. && !string.IsNullOrEmpty(contentLength))
  55. {
  56. var length = long.Parse(contentLength, CultureInfo.InvariantCulture);
  57. if (length > 0)
  58. {
  59. res.ContentLength = length;
  60. }
  61. }
  62. }
  63. }
  64. /// <summary>
  65. /// Removes the control characters.
  66. /// </summary>
  67. /// <param name="inString">The in string.</param>
  68. /// <returns>System.String.</returns>
  69. public static string RemoveControlCharacters(string inString)
  70. {
  71. if (inString == null)
  72. {
  73. return null;
  74. }
  75. var newString = new StringBuilder(inString.Length);
  76. foreach (var ch in inString)
  77. {
  78. if (!char.IsControl(ch))
  79. {
  80. newString.Append(ch);
  81. }
  82. }
  83. return newString.ToString();
  84. }
  85. }
  86. }