BaseHandler.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.IO;
  5. using System.IO.Compression;
  6. namespace MediaBrowser.Common.Net.Handlers
  7. {
  8. public abstract class BaseHandler
  9. {
  10. /// <summary>
  11. /// Response headers
  12. /// </summary>
  13. public IDictionary<string, string> Headers = new Dictionary<string, string>();
  14. /// <summary>
  15. /// Returns true or false indicating if the handler writes to the stream asynchronously.
  16. /// If so the subclass will be responsible for disposing the stream when complete.
  17. /// </summary>
  18. protected virtual bool IsAsyncHandler
  19. {
  20. get
  21. {
  22. return false;
  23. }
  24. }
  25. /// <summary>
  26. /// The action to write the response to the output stream
  27. /// </summary>
  28. public virtual Action<Stream> WriteStream
  29. {
  30. get
  31. {
  32. return s =>
  33. {
  34. WriteReponse(s);
  35. if (!IsAsyncHandler)
  36. {
  37. s.Dispose();
  38. }
  39. };
  40. }
  41. }
  42. /// <summary>
  43. /// The original RequestContext
  44. /// </summary>
  45. public RequestContext RequestContext { get; set; }
  46. /// <summary>
  47. /// The original QueryString
  48. /// </summary>
  49. protected NameValueCollection QueryString
  50. {
  51. get
  52. {
  53. return RequestContext.Request.QueryString;
  54. }
  55. }
  56. /// <summary>
  57. /// Gets the MIME type to include in the response headers
  58. /// </summary>
  59. public abstract string ContentType { get; }
  60. /// <summary>
  61. /// Gets the status code to include in the response headers
  62. /// </summary>
  63. public virtual int StatusCode
  64. {
  65. get
  66. {
  67. return 200;
  68. }
  69. }
  70. /// <summary>
  71. /// Gets the cache duration to include in the response headers
  72. /// </summary>
  73. public virtual TimeSpan CacheDuration
  74. {
  75. get
  76. {
  77. return TimeSpan.FromTicks(0);
  78. }
  79. }
  80. /// <summary>
  81. /// Gets the last date modified of the content being returned, if this can be determined.
  82. /// This will be used to invalidate the cache, so it's not needed if CacheDuration is 0.
  83. /// </summary>
  84. public virtual DateTime? LastDateModified
  85. {
  86. get
  87. {
  88. return null;
  89. }
  90. }
  91. public virtual bool CompressResponse
  92. {
  93. get
  94. {
  95. return true;
  96. }
  97. }
  98. private void WriteReponse(Stream stream)
  99. {
  100. if (CompressResponse)
  101. {
  102. using (DeflateStream compressedStream = new DeflateStream(stream, CompressionLevel.Fastest, false))
  103. {
  104. WriteResponseToOutputStream(compressedStream);
  105. }
  106. }
  107. else
  108. {
  109. WriteResponseToOutputStream(stream);
  110. }
  111. }
  112. protected abstract void WriteResponseToOutputStream(Stream stream);
  113. }
  114. }