1
0

IHttpResponse.cs 5.6 KB

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