ResponseFilter.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using MediaBrowser.Model.Logging;
  2. using MediaBrowser.Server.Implementations.HttpServer.SocketSharp;
  3. using ServiceStack.Web;
  4. using System;
  5. using System.Globalization;
  6. using System.Net;
  7. using System.Text;
  8. namespace MediaBrowser.Server.Implementations.HttpServer
  9. {
  10. public class ResponseFilter
  11. {
  12. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  13. private readonly ILogger _logger;
  14. private readonly Func<bool> _denyIframeEmbedding;
  15. public ResponseFilter(ILogger logger, Func<bool> denyIframeEmbedding)
  16. {
  17. _logger = logger;
  18. _denyIframeEmbedding = denyIframeEmbedding;
  19. }
  20. /// <summary>
  21. /// Filters the response.
  22. /// </summary>
  23. /// <param name="req">The req.</param>
  24. /// <param name="res">The res.</param>
  25. /// <param name="dto">The dto.</param>
  26. public void FilterResponse(IRequest req, IResponse res, object dto)
  27. {
  28. // Try to prevent compatibility view
  29. res.AddHeader("X-UA-Compatible", "IE=Edge");
  30. if (_denyIframeEmbedding())
  31. {
  32. res.AddHeader("X-Frame-Options", "SAMEORIGIN");
  33. }
  34. var exception = dto as Exception;
  35. if (exception != null)
  36. {
  37. _logger.ErrorException("Error processing request for {0}", exception, req.RawUrl);
  38. if (!string.IsNullOrEmpty(exception.Message))
  39. {
  40. var error = exception.Message.Replace(Environment.NewLine, " ");
  41. error = RemoveControlCharacters(error);
  42. res.AddHeader("X-Application-Error-Code", error);
  43. }
  44. }
  45. var vary = "Accept-Encoding";
  46. var hasOptions = dto as IHasOptions;
  47. var sharpResponse = res as WebSocketSharpResponse;
  48. if (hasOptions != null)
  49. {
  50. if (!hasOptions.Options.ContainsKey("Server"))
  51. {
  52. hasOptions.Options["Server"] = "Mono-HTTPAPI/1.1, UPnP/1.0 DLNADOC/1.50";
  53. //hasOptions.Options["Server"] = "Mono-HTTPAPI/1.1";
  54. }
  55. // Content length has to be explicitly set on on HttpListenerResponse or it won't be happy
  56. string contentLength;
  57. if (hasOptions.Options.TryGetValue("Content-Length", out contentLength) && !string.IsNullOrEmpty(contentLength))
  58. {
  59. var length = long.Parse(contentLength, UsCulture);
  60. if (length > 0)
  61. {
  62. res.SetContentLength(length);
  63. var listenerResponse = res.OriginalResponse as HttpListenerResponse;
  64. if (listenerResponse != null)
  65. {
  66. // Disable chunked encoding. Technically this is only needed when using Content-Range, but
  67. // anytime we know the content length there's no need for it
  68. listenerResponse.SendChunked = false;
  69. return;
  70. }
  71. if (sharpResponse != null)
  72. {
  73. sharpResponse.SendChunked = false;
  74. }
  75. }
  76. }
  77. string hasOptionsVary;
  78. if (hasOptions.Options.TryGetValue("Vary", out hasOptionsVary))
  79. {
  80. vary = hasOptionsVary;
  81. }
  82. hasOptions.Options["Vary"] = vary;
  83. }
  84. //res.KeepAlive = false;
  85. // Per Google PageSpeed
  86. // This instructs the proxies to cache two versions of the resource: one compressed, and one uncompressed.
  87. // The correct version of the resource is delivered based on the client request header.
  88. // This is a good choice for applications that are singly homed and depend on public proxies for user locality.
  89. res.AddHeader("Vary", vary);
  90. }
  91. /// <summary>
  92. /// Removes the control characters.
  93. /// </summary>
  94. /// <param name="inString">The in string.</param>
  95. /// <returns>System.String.</returns>
  96. public static string RemoveControlCharacters(string inString)
  97. {
  98. if (inString == null) return null;
  99. var newString = new StringBuilder();
  100. foreach (var ch in inString)
  101. {
  102. if (!char.IsControl(ch))
  103. {
  104. newString.Append(ch);
  105. }
  106. }
  107. return newString.ToString();
  108. }
  109. }
  110. }