ResponseFilter.cs 4.5 KB

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