ResponseFilter.cs 3.4 KB

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