2
0

ResponseFilter.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. 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. var vary = "Accept-Encoding";
  40. var hasOptions = dto as IHasOptions;
  41. var sharpResponse = res as WebSocketSharpResponse;
  42. if (hasOptions != null)
  43. {
  44. if (!hasOptions.Options.ContainsKey("Server"))
  45. {
  46. hasOptions.Options["Server"] = "Mono-HTTPAPI/1.1, UPnP/1.0 DLNADOC/1.50";
  47. //hasOptions.Options["Server"] = "Mono-HTTPAPI/1.1";
  48. }
  49. // Content length has to be explicitly set on on HttpListenerResponse or it won't be happy
  50. string contentLength;
  51. if (hasOptions.Options.TryGetValue("Content-Length", out contentLength) && !string.IsNullOrEmpty(contentLength))
  52. {
  53. var length = long.Parse(contentLength, UsCulture);
  54. if (length > 0)
  55. {
  56. res.SetContentLength(length);
  57. var listenerResponse = res.OriginalResponse as HttpListenerResponse;
  58. if (listenerResponse != null)
  59. {
  60. // Disable chunked encoding. Technically this is only needed when using Content-Range, but
  61. // anytime we know the content length there's no need for it
  62. listenerResponse.SendChunked = false;
  63. return;
  64. }
  65. if (sharpResponse != null)
  66. {
  67. sharpResponse.SendChunked = false;
  68. }
  69. }
  70. }
  71. string hasOptionsVary;
  72. if (hasOptions.Options.TryGetValue("Vary", out hasOptionsVary))
  73. {
  74. vary = hasOptionsVary;
  75. }
  76. hasOptions.Options["Vary"] = vary;
  77. }
  78. //res.KeepAlive = false;
  79. // Per Google PageSpeed
  80. // This instructs the proxies to cache two versions of the resource: one compressed, and one uncompressed.
  81. // The correct version of the resource is delivered based on the client request header.
  82. // This is a good choice for applications that are singly homed and depend on public proxies for user locality.
  83. res.AddHeader("Vary", vary);
  84. }
  85. /// <summary>
  86. /// Removes the control characters.
  87. /// </summary>
  88. /// <param name="inString">The in string.</param>
  89. /// <returns>System.String.</returns>
  90. public static string RemoveControlCharacters(string inString)
  91. {
  92. if (inString == null) return null;
  93. var newString = new StringBuilder();
  94. foreach (var ch in inString)
  95. {
  96. if (!char.IsControl(ch))
  97. {
  98. newString.Append(ch);
  99. }
  100. }
  101. return newString.ToString();
  102. }
  103. }
  104. }