BaseHandler.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. public virtual bool UseChunkedEncoding
  15. {
  16. get
  17. {
  18. return true;
  19. }
  20. }
  21. public virtual long? ContentLength
  22. {
  23. get
  24. {
  25. return null;
  26. }
  27. }
  28. /// <summary>
  29. /// Returns true or false indicating if the handler writes to the stream asynchronously.
  30. /// If so the subclass will be responsible for disposing the stream when complete.
  31. /// </summary>
  32. protected virtual bool IsAsyncHandler
  33. {
  34. get
  35. {
  36. return false;
  37. }
  38. }
  39. /// <summary>
  40. /// The action to write the response to the output stream
  41. /// </summary>
  42. public virtual Action<Stream> WriteStream
  43. {
  44. get
  45. {
  46. return s =>
  47. {
  48. WriteReponse(s);
  49. if (!IsAsyncHandler)
  50. {
  51. s.Dispose();
  52. }
  53. };
  54. }
  55. }
  56. /// <summary>
  57. /// The original RequestContext
  58. /// </summary>
  59. public RequestContext RequestContext { get; set; }
  60. /// <summary>
  61. /// The original QueryString
  62. /// </summary>
  63. protected NameValueCollection QueryString
  64. {
  65. get
  66. {
  67. return RequestContext.Request.QueryString;
  68. }
  69. }
  70. /// <summary>
  71. /// Gets the MIME type to include in the response headers
  72. /// </summary>
  73. public abstract string ContentType { get; }
  74. /// <summary>
  75. /// Gets the status code to include in the response headers
  76. /// </summary>
  77. public virtual int StatusCode
  78. {
  79. get
  80. {
  81. return 200;
  82. }
  83. }
  84. /// <summary>
  85. /// Gets the cache duration to include in the response headers
  86. /// </summary>
  87. public virtual TimeSpan CacheDuration
  88. {
  89. get
  90. {
  91. return TimeSpan.FromTicks(0);
  92. }
  93. }
  94. /// <summary>
  95. /// Gets the last date modified of the content being returned, if this can be determined.
  96. /// This will be used to invalidate the cache, so it's not needed if CacheDuration is 0.
  97. /// </summary>
  98. public virtual DateTime? LastDateModified
  99. {
  100. get
  101. {
  102. return null;
  103. }
  104. }
  105. public virtual bool CompressResponse
  106. {
  107. get
  108. {
  109. return true;
  110. }
  111. }
  112. private void WriteReponse(Stream stream)
  113. {
  114. if (CompressResponse)
  115. {
  116. using (DeflateStream compressedStream = new DeflateStream(stream, CompressionLevel.Fastest, false))
  117. {
  118. WriteResponseToOutputStream(compressedStream);
  119. }
  120. }
  121. else
  122. {
  123. WriteResponseToOutputStream(stream);
  124. }
  125. }
  126. protected abstract void WriteResponseToOutputStream(Stream stream);
  127. }
  128. }