1
0

IHttpResponse.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. event EventHandler<BandWitdhEventArgs> BandWitdhEvent;
  29. /// <summary>
  30. /// The body stream is used to cache the body contents
  31. /// before sending everything to the client. It's the simplest
  32. /// way to serve documents.
  33. /// </summary>
  34. ///
  35. Stream Body { get; }
  36. byte[] RawBuffer { get; set; }
  37. int RawBufferStart { get; set; }
  38. int RawBufferLen { get; set; }
  39. uint requestID { get; }
  40. double RequestTS { get;}
  41. /// <summary>
  42. /// Defines the version of the HTTP Response for applications where it's required
  43. /// for this to be forced.
  44. /// </summary>
  45. string ProtocolVersion { get; set; }
  46. int Priority { get; set; }
  47. /// <summary>
  48. /// The chunked encoding modifies the body of a message in order to
  49. /// transfer it as a series of chunks, each with its own size indicator,
  50. /// followed by an OPTIONAL trailer containing entity-header fields. This
  51. /// allows dynamically produced content to be transferred along with the
  52. /// information necessary for the recipient to verify that it has
  53. /// received the full message.
  54. /// </summary>
  55. bool Chunked { get; set; }
  56. /// <summary>
  57. /// Kind of connection
  58. /// </summary>
  59. ConnectionType Connection { get; set; }
  60. /// <summary>
  61. /// Encoding to use when sending stuff to the client.
  62. /// </summary>
  63. /// <remarks>Default is UTF8</remarks>
  64. Encoding Encoding { get; set; }
  65. /// <summary>
  66. /// Number of seconds to keep connection alive
  67. /// </summary>
  68. /// <remarks>Only used if Connection property is set to ConnectionType.KeepAlive</remarks>
  69. int KeepAlive { get; set; }
  70. /// <summary>
  71. /// Status code that is sent to the client.
  72. /// </summary>
  73. /// <remarks>Default is HttpStatusCode.Ok</remarks>
  74. HttpStatusCode Status { get; set; }
  75. /// <summary>
  76. /// Information about why a specific status code was used.
  77. /// </summary>
  78. string Reason { get; set; }
  79. /// <summary>
  80. /// Size of the body. MUST be specified before sending the header,
  81. /// unless property Chunked is set to true.
  82. /// </summary>
  83. long ContentLength { get; set; }
  84. /// <summary>
  85. /// Kind of content in the body
  86. /// </summary>
  87. /// <remarks>Default is text/html</remarks>
  88. string ContentType { get; set; }
  89. /// <summary>
  90. /// Headers have been sent to the client-
  91. /// </summary>
  92. /// <remarks>You can not send any additional headers if they have already been sent.</remarks>
  93. bool HeadersSent { get; }
  94. /// <summary>
  95. /// The whole response have been sent.
  96. /// </summary>
  97. bool Sent { get; }
  98. /// <summary>
  99. /// Cookies that should be created/changed.
  100. /// </summary>
  101. ResponseCookies Cookies { get; }
  102. /// <summary>
  103. /// Add another header to the document.
  104. /// </summary>
  105. /// <param name="name">Name of the header, case sensitive, use lower cases.</param>
  106. /// <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>
  107. /// <exception cref="InvalidOperationException">If headers already been sent.</exception>
  108. /// <exception cref="ArgumentException">If value conditions have not been met.</exception>
  109. /// <remarks>Adding any header will override the default ones and those specified by properties.</remarks>
  110. void AddHeader(string name, string value);
  111. /// <summary>
  112. /// Send headers and body to the browser.
  113. /// </summary>
  114. /// <exception cref="InvalidOperationException">If content have already been sent.</exception>
  115. void Send();
  116. }
  117. /// <summary>
  118. /// Type of HTTP connection
  119. /// </summary>
  120. public enum ConnectionType
  121. {
  122. /// <summary>
  123. /// Connection is closed after each request-response
  124. /// </summary>
  125. Close,
  126. /// <summary>
  127. /// Connection is kept alive for X seconds (unless another request have been made)
  128. /// </summary>
  129. KeepAlive
  130. }
  131. public class BandWitdhEventArgs : EventArgs
  132. {
  133. /// <summary>
  134. /// Gets received request.
  135. /// </summary>
  136. public int Result;
  137. public int Request;
  138. public BandWitdhEventArgs(int request)
  139. {
  140. Request = request;
  141. Result = request;
  142. }
  143. }
  144. }