ResponseFilter.cs 3.3 KB

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