IHttpResponse.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. namespace OSHttpServer
  6. {
  7. /// <summary>
  8. /// Response that is sent back to the web browser / client.
  9. ///
  10. /// A response can be sent if different ways. The easiest one is
  11. /// to just fill the Body stream with content, everything else
  12. /// will then be taken care of by the framework. The default content-type
  13. /// is text/html, you should change it if you send anything else.
  14. ///
  15. /// The second and slighty more complex way is to send the response
  16. /// as parts. Start with sending the header using the SendHeaders method and
  17. /// then you can send the body using SendBody method, but do not forget
  18. /// to set ContentType and ContentLength before doing so.
  19. /// </summary>
  20. /// <example>
  21. /// public void MyHandler(IHttpRequest request, IHttpResponse response)
  22. /// {
  23. ///
  24. /// }
  25. /// </example>
  26. public interface IHttpResponse
  27. {
  28. /// <summary>
  29. /// The body stream is used to cache the body contents
  30. /// before sending everything to the client. It's the simplest
  31. /// way to serve documents.
  32. /// </summary>
  33. Stream Body { get; set; }
  34. byte[] RawBuffer { get; set; }
  35. int RawBufferStart { get; set; }
  36. int RawBufferLen { get; set; }
  37. uint requestID { get; }
  38. /// <summary>
  39. /// Defines the version of the HTTP Response for applications where it's required
  40. /// for this to be forced.
  41. /// </summary>
  42. string ProtocolVersion { get; set; }
  43. int Priority { get; set; }
  44. /// <summary>
  45. /// The chunked encoding modifies the body of a message in order to
  46. /// transfer it as a series of chunks, each with its own size indicator,
  47. /// followed by an OPTIONAL trailer containing entity-header fields. This
  48. /// allows dynamically produced content to be transferred along with the
  49. /// information necessary for the recipient to verify that it has
  50. /// received the full message.
  51. /// </summary>
  52. bool Chunked { get; set; }
  53. /// <summary>
  54. /// Kind of connection
  55. /// </summary>
  56. ConnectionType Connection { get; set; }
  57. /// <summary>
  58. /// Encoding to use when sending stuff to the client.
  59. /// </summary>
  60. /// <remarks>Default is UTF8</remarks>
  61. Encoding Encoding { get; set; }
  62. /// <summary>
  63. /// Number of seconds to keep connection alive
  64. /// </summary>
  65. /// <remarks>Only used if Connection property is set to ConnectionType.KeepAlive</remarks>
  66. int KeepAlive { get; set; }
  67. /// <summary>
  68. /// Status code that is sent to the client.
  69. /// </summary>
  70. /// <remarks>Default is HttpStatusCode.Ok</remarks>
  71. HttpStatusCode Status { get; set; }
  72. /// <summary>
  73. /// Information about why a specific status code was used.
  74. /// </summary>
  75. string Reason { get; set; }
  76. /// <summary>
  77. /// Size of the body. MUST be specified before sending the header,
  78. /// unless property Chunked is set to true.
  79. /// </summary>
  80. long ContentLength { get; set; }
  81. /// <summary>
  82. /// Kind of content in the body
  83. /// </summary>
  84. /// <remarks>Default is text/html</remarks>
  85. string ContentType { get; set; }
  86. /// <summary>
  87. /// Headers have been sent to the client-
  88. /// </summary>
  89. /// <remarks>You can not send any additional headers if they have already been sent.</remarks>
  90. bool HeadersSent { get; }
  91. /// <summary>
  92. /// The whole response have been sent.
  93. /// </summary>
  94. bool Sent { get; }
  95. /// <summary>
  96. /// Cookies that should be created/changed.
  97. /// </summary>
  98. ResponseCookies Cookies { get; }
  99. /// <summary>
  100. /// Add another header to the document.
  101. /// </summary>
  102. /// <param name="name">Name of the header, case sensitive, use lower cases.</param>
  103. /// <param name="value">Header values can span over multiple lines as long as each line starts with a white space. New line chars should be \r\n</param>
  104. /// <exception cref="InvalidOperationException">If headers already been sent.</exception>
  105. /// <exception cref="ArgumentException">If value conditions have not been met.</exception>
  106. /// <remarks>Adding any header will override the default ones and those specified by properties.</remarks>
  107. void AddHeader(string name, string value);
  108. /// <summary>
  109. /// Send headers and body to the browser.
  110. /// </summary>
  111. /// <exception cref="InvalidOperationException">If content have already been sent.</exception>
  112. void Send();
  113. /// <summary>
  114. /// Make sure that you have specified ContentLength and sent the headers first.
  115. /// </summary>
  116. /// <param name="buffer"></param>
  117. /// <exception cref="InvalidOperationException">If headers have not been sent.</exception>
  118. /// <see cref="IHttpResponse.SendHeaders"/>
  119. /// <param name="offset">offest of first byte to send</param>
  120. /// <param name="count">number of bytes to send.</param>
  121. /// <seealso cref="IHttpResponse.Send"/>
  122. /// <seealso cref="IHttpResponse.SendHeaders"/>
  123. /// <remarks>This method can be used if you want to send body contents without caching them first. This
  124. /// is recommended for larger files to keep the memory usage low.</remarks>
  125. bool SendBody(byte[] buffer, int offset, int count);
  126. /// <summary>
  127. /// Make sure that you have specified ContentLength and sent the headers first.
  128. /// </summary>
  129. /// <param name="buffer"></param>
  130. /// <exception cref="InvalidOperationException">If headers have not been sent.</exception>
  131. /// <see cref="IHttpResponse.SendHeaders"/>
  132. /// <seealso cref="IHttpResponse.Send"/>
  133. /// <seealso cref="IHttpResponse.SendHeaders"/>
  134. /// <remarks>This method can be used if you want to send body contents without caching them first. This
  135. /// is recommended for larger files to keep the memory usage low.</remarks>
  136. bool SendBody(byte[] buffer);
  137. /// <summary>
  138. /// Send headers to the client.
  139. /// </summary>
  140. /// <exception cref="InvalidOperationException">If headers already been sent.</exception>
  141. /// <seealso cref="IHttpResponse.AddHeader"/>
  142. /// <seealso cref="IHttpResponse.Send"/>
  143. /// <seealso cref="IHttpResponse.SendBody(byte[])"/>
  144. bool SendHeaders();
  145. }
  146. /// <summary>
  147. /// Type of HTTP connection
  148. /// </summary>
  149. public enum ConnectionType
  150. {
  151. /// <summary>
  152. /// Connection is closed after each request-response
  153. /// </summary>
  154. Close,
  155. /// <summary>
  156. /// Connection is kept alive for X seconds (unless another request have been made)
  157. /// </summary>
  158. KeepAlive
  159. }
  160. }