RequestContext.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using MediaBrowser.Common.Logging;
  5. using MediaBrowser.Common.Net.Handlers;
  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. handler.WriteStream(Response.OutputStream);
  53. }
  54. else
  55. {
  56. Response.SendChunked = false;
  57. Response.OutputStream.Dispose();
  58. }
  59. }
  60. private bool IsCacheValid(DateTime ifModifiedSince, TimeSpan cacheDuration, DateTime? dateModified)
  61. {
  62. if (dateModified.HasValue)
  63. {
  64. DateTime lastModified = NormalizeDateForComparison(dateModified.Value);
  65. ifModifiedSince = NormalizeDateForComparison(ifModifiedSince);
  66. return lastModified <= ifModifiedSince;
  67. }
  68. DateTime cacheExpirationDate = ifModifiedSince.Add(cacheDuration);
  69. if (DateTime.Now < cacheExpirationDate)
  70. {
  71. return true;
  72. }
  73. return false;
  74. }
  75. /// <summary>
  76. /// When the browser sends the IfModifiedDate, it's precision is limited to seconds, so this will account for that
  77. /// </summary>
  78. private DateTime NormalizeDateForComparison(DateTime date)
  79. {
  80. return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second);
  81. }
  82. }
  83. }