RequestContext.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 = true;
  50. if (handler.CompressResponse)
  51. {
  52. Response.AddHeader("Content-Encoding", "deflate");
  53. }
  54. if (cacheDuration.Ticks > 0)
  55. {
  56. CacheResponse(Response, cacheDuration, handler.LastDateModified);
  57. }
  58. handler.WriteStream(Response.OutputStream);
  59. }
  60. else
  61. {
  62. Response.SendChunked = false;
  63. Response.OutputStream.Dispose();
  64. }
  65. }
  66. private void CacheResponse(HttpListenerResponse response, TimeSpan duration, DateTime? dateModified)
  67. {
  68. DateTime lastModified = dateModified ?? DateTime.Now;
  69. response.Headers[HttpResponseHeader.CacheControl] = "public, max-age=" + Convert.ToInt32(duration.TotalSeconds);
  70. response.Headers[HttpResponseHeader.Expires] = DateTime.Now.Add(duration).ToString("r");
  71. response.Headers[HttpResponseHeader.LastModified] = lastModified.ToString("r");
  72. }
  73. private bool IsCacheValid(DateTime ifModifiedSince, TimeSpan cacheDuration, DateTime? dateModified)
  74. {
  75. if (dateModified.HasValue)
  76. {
  77. DateTime lastModified = NormalizeDateForComparison(dateModified.Value);
  78. ifModifiedSince = NormalizeDateForComparison(ifModifiedSince);
  79. return lastModified <= ifModifiedSince;
  80. }
  81. DateTime cacheExpirationDate = ifModifiedSince.Add(cacheDuration);
  82. if (DateTime.Now < cacheExpirationDate)
  83. {
  84. return true;
  85. }
  86. return false;
  87. }
  88. /// <summary>
  89. /// When the browser sends the IfModifiedDate, it's precision is limited to seconds, so this will account for that
  90. /// </summary>
  91. private DateTime NormalizeDateForComparison(DateTime date)
  92. {
  93. return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second);
  94. }
  95. }
  96. }