BaseHandler.cs 3.9 KB

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