RequestContext.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using MediaBrowser.Common.Net.Handlers;
  5. using MediaBrowser.Common.Logging;
  6. namespace MediaBrowser.Common.Net
  7. {
  8. public class RequestContext
  9. {
  10. public HttpListenerRequest Request { get; private set; }
  11. public HttpListenerResponse Response { get; private set; }
  12. public string LocalPath
  13. {
  14. get
  15. {
  16. return Request.Url.LocalPath;
  17. }
  18. }
  19. public RequestContext(HttpListenerContext context)
  20. {
  21. Response = context.Response;
  22. Request = context.Request;
  23. }
  24. public void Respond(BaseHandler handler)
  25. {
  26. Logger.LogInfo("Http Server received request at: " + Request.Url.ToString());
  27. Logger.LogInfo("Http Headers: " + string.Join(",", Request.Headers.AllKeys.Select(k => k + "=" + Request.Headers[k])));
  28. Response.AddHeader("Access-Control-Allow-Origin", "*");
  29. Response.KeepAlive = true;
  30. foreach (var header in handler.Headers)
  31. {
  32. Response.AddHeader(header.Key, header.Value);
  33. }
  34. int statusCode = handler.StatusCode;
  35. Response.ContentType = handler.ContentType;
  36. TimeSpan cacheDuration = handler.CacheDuration;
  37. if (Request.Headers.AllKeys.Contains("If-Modified-Since"))
  38. {
  39. DateTime ifModifiedSince;
  40. if (DateTime.TryParse(Request.Headers["If-Modified-Since"].Replace(" GMT", string.Empty), out ifModifiedSince))
  41. {
  42. // If the cache hasn't expired yet just return a 304
  43. if (IsCacheValid(ifModifiedSince, cacheDuration, handler.LastDateModified))
  44. {
  45. statusCode = 304;
  46. }
  47. }
  48. }
  49. Response.StatusCode = statusCode;
  50. if (statusCode == 200 || statusCode == 206)
  51. {
  52. // Don't force this to true. HttpListener will default it to true if supported by the client.
  53. if (!handler.UseChunkedEncoding)
  54. {
  55. Response.SendChunked = false;
  56. }
  57. if (handler.ContentLength.HasValue)
  58. {
  59. Response.ContentLength64 = handler.ContentLength.Value;
  60. }
  61. if (handler.CompressResponse)
  62. {
  63. Response.AddHeader("Content-Encoding", "deflate");
  64. }
  65. if (cacheDuration.Ticks > 0)
  66. {
  67. CacheResponse(Response, cacheDuration, handler.LastDateModified);
  68. }
  69. handler.WriteStream(Response.OutputStream);
  70. }
  71. else
  72. {
  73. Response.SendChunked = false;
  74. Response.OutputStream.Dispose();
  75. }
  76. }
  77. private void CacheResponse(HttpListenerResponse response, TimeSpan duration, DateTime? dateModified)
  78. {
  79. DateTime lastModified = dateModified ?? DateTime.Now;
  80. response.Headers[HttpResponseHeader.CacheControl] = "public, max-age=" + Convert.ToInt32(duration.TotalSeconds);
  81. response.Headers[HttpResponseHeader.Expires] = DateTime.Now.Add(duration).ToString("r");
  82. response.Headers[HttpResponseHeader.LastModified] = lastModified.ToString("r");
  83. }
  84. private bool IsCacheValid(DateTime ifModifiedSince, TimeSpan cacheDuration, DateTime? dateModified)
  85. {
  86. if (dateModified.HasValue)
  87. {
  88. DateTime lastModified = NormalizeDateForComparison(dateModified.Value);
  89. ifModifiedSince = NormalizeDateForComparison(ifModifiedSince);
  90. return lastModified <= ifModifiedSince;
  91. }
  92. DateTime cacheExpirationDate = ifModifiedSince.Add(cacheDuration);
  93. if (DateTime.Now < cacheExpirationDate)
  94. {
  95. return true;
  96. }
  97. return false;
  98. }
  99. /// <summary>
  100. /// When the browser sends the IfModifiedDate, it's precision is limited to seconds, so this will account for that
  101. /// </summary>
  102. private DateTime NormalizeDateForComparison(DateTime date)
  103. {
  104. return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second);
  105. }
  106. }
  107. }