ResponseFilter.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using MediaBrowser.Model.Logging;
  2. using ServiceStack;
  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. 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, IResponse res, object dto)
  25. {
  26. // Try to prevent compatibility view
  27. res.AddHeader("X-UA-Compatible", "IE=Edge");
  28. var exception = dto as Exception;
  29. if (exception != null)
  30. {
  31. _logger.ErrorException("Error processing request for {0}", exception, 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 CompressedResult)
  40. {
  41. // Per Google PageSpeed
  42. // This instructs the proxies to cache two versions of the resource: one compressed, and one uncompressed.
  43. // The correct version of the resource is delivered based on the client request header.
  44. // This is a good choice for applications that are singly homed and depend on public proxies for user locality.
  45. res.AddHeader("Vary", "Accept-Encoding");
  46. }
  47. var hasOptions = dto as IHasOptions;
  48. if (hasOptions != null)
  49. {
  50. // Content length has to be explicitly set on on HttpListenerResponse or it won't be happy
  51. string contentLength;
  52. if (hasOptions.Options.TryGetValue("Content-Length", out contentLength) && !string.IsNullOrEmpty(contentLength))
  53. {
  54. var length = long.Parse(contentLength, UsCulture);
  55. if (length > 0)
  56. {
  57. var response = (HttpListenerResponse)res.OriginalResponse;
  58. response.ContentLength64 = length;
  59. // Disable chunked encoding. Technically this is only needed when using Content-Range, but
  60. // anytime we know the content length there's no need for it
  61. response.SendChunked = false;
  62. }
  63. }
  64. }
  65. }
  66. /// <summary>
  67. /// Removes the control characters.
  68. /// </summary>
  69. /// <param name="inString">The in string.</param>
  70. /// <returns>System.String.</returns>
  71. private static string RemoveControlCharacters(string inString)
  72. {
  73. if (inString == null) return null;
  74. var newString = new StringBuilder();
  75. foreach (var ch in inString)
  76. {
  77. if (!char.IsControl(ch))
  78. {
  79. newString.Append(ch);
  80. }
  81. }
  82. return newString.ToString();
  83. }
  84. }
  85. }