2
0

ResponseFilter.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using MediaBrowser.Model.Logging;
  2. using System;
  3. using System.Globalization;
  4. using System.Text;
  5. using Emby.Server.Implementations.HttpServer.SocketSharp;
  6. using MediaBrowser.Model.Services;
  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("X-UA-Compatible", "IE=Edge");
  27. res.AddHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Range, X-MediaBrowser-Token, X-Emby-Authorization");
  28. res.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS");
  29. res.AddHeader("Access-Control-Allow-Origin", "*");
  30. var exception = dto as Exception;
  31. if (exception != null)
  32. {
  33. _logger.ErrorException("Error processing request for {0}", exception, req.RawUrl);
  34. if (!string.IsNullOrEmpty(exception.Message))
  35. {
  36. var error = exception.Message.Replace(Environment.NewLine, " ");
  37. error = RemoveControlCharacters(error);
  38. res.AddHeader("X-Application-Error-Code", error);
  39. }
  40. }
  41. var vary = "Accept-Encoding";
  42. var hasHeaders = dto as IHasHeaders;
  43. var sharpResponse = res as WebSocketSharpResponse;
  44. if (hasHeaders != null)
  45. {
  46. if (!hasHeaders.Headers.ContainsKey("Server"))
  47. {
  48. hasHeaders.Headers["Server"] = "Mono-HTTPAPI/1.1, UPnP/1.0 DLNADOC/1.50";
  49. //hasHeaders.Headers["Server"] = "Mono-HTTPAPI/1.1";
  50. }
  51. // Content length has to be explicitly set on on HttpListenerResponse or it won't be happy
  52. string contentLength;
  53. if (hasHeaders.Headers.TryGetValue("Content-Length", out contentLength) && !string.IsNullOrEmpty(contentLength))
  54. {
  55. var length = long.Parse(contentLength, UsCulture);
  56. if (length > 0)
  57. {
  58. res.SetContentLength(length);
  59. //var listenerResponse = res.OriginalResponse as HttpListenerResponse;
  60. //if (listenerResponse != null)
  61. //{
  62. // // Disable chunked encoding. Technically this is only needed when using Content-Range, but
  63. // // anytime we know the content length there's no need for it
  64. // listenerResponse.SendChunked = false;
  65. // return;
  66. //}
  67. if (sharpResponse != null)
  68. {
  69. sharpResponse.SendChunked = false;
  70. }
  71. }
  72. }
  73. string hasHeadersVary;
  74. if (hasHeaders.Headers.TryGetValue("Vary", out hasHeadersVary))
  75. {
  76. vary = hasHeadersVary;
  77. }
  78. hasHeaders.Headers["Vary"] = vary;
  79. }
  80. //res.KeepAlive = false;
  81. // Per Google PageSpeed
  82. // This instructs the proxies to cache two versions of the resource: one compressed, and one uncompressed.
  83. // The correct version of the resource is delivered based on the client request header.
  84. // This is a good choice for applications that are singly homed and depend on public proxies for user locality.
  85. res.AddHeader("Vary", vary);
  86. }
  87. /// <summary>
  88. /// Removes the control characters.
  89. /// </summary>
  90. /// <param name="inString">The in string.</param>
  91. /// <returns>System.String.</returns>
  92. public static string RemoveControlCharacters(string inString)
  93. {
  94. if (inString == null) return null;
  95. var newString = new StringBuilder();
  96. foreach (var ch in inString)
  97. {
  98. if (!char.IsControl(ch))
  99. {
  100. newString.Append(ch);
  101. }
  102. }
  103. return newString.ToString();
  104. }
  105. }
  106. }