RequestContext.cs 4.1 KB

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