using System; using System.IO; using System.Net; using System.Text; namespace OSHttpServer { /// /// Response that is sent back to the web browser / client. /// /// A response can be sent if different ways. The easiest one is /// to just fill the Body stream with content, everything else /// will then be taken care of by the framework. The default content-type /// is text/html, you should change it if you send anything else. /// /// The second and slighty more complex way is to send the response /// as parts. Start with sending the header using the SendHeaders method and /// then you can send the body using SendBody method, but do not forget /// to set ContentType and ContentLength before doing so. /// /// /// public void MyHandler(IHttpRequest request, IHttpResponse response) /// { /// /// } /// public interface IHttpResponse { /// /// The body stream is used to cache the body contents /// before sending everything to the client. It's the simplest /// way to serve documents. /// Stream Body { get; set; } byte[] RawBuffer { get; set; } int RawBufferStart { get; set; } int RawBufferLen { get; set; } uint requestID { get; } /// /// Defines the version of the HTTP Response for applications where it's required /// for this to be forced. /// string ProtocolVersion { get; set; } int Priority { get; set; } /// /// The chunked encoding modifies the body of a message in order to /// transfer it as a series of chunks, each with its own size indicator, /// followed by an OPTIONAL trailer containing entity-header fields. This /// allows dynamically produced content to be transferred along with the /// information necessary for the recipient to verify that it has /// received the full message. /// bool Chunked { get; set; } /// /// Kind of connection /// ConnectionType Connection { get; set; } /// /// Encoding to use when sending stuff to the client. /// /// Default is UTF8 Encoding Encoding { get; set; } /// /// Number of seconds to keep connection alive /// /// Only used if Connection property is set to ConnectionType.KeepAlive int KeepAlive { get; set; } /// /// Status code that is sent to the client. /// /// Default is HttpStatusCode.Ok HttpStatusCode Status { get; set; } /// /// Information about why a specific status code was used. /// string Reason { get; set; } /// /// Size of the body. MUST be specified before sending the header, /// unless property Chunked is set to true. /// long ContentLength { get; set; } /// /// Kind of content in the body /// /// Default is text/html string ContentType { get; set; } /// /// Headers have been sent to the client- /// /// You can not send any additional headers if they have already been sent. bool HeadersSent { get; } /// /// The whole response have been sent. /// bool Sent { get; } /// /// Cookies that should be created/changed. /// ResponseCookies Cookies { get; } /// /// Add another header to the document. /// /// Name of the header, case sensitive, use lower cases. /// Header values can span over multiple lines as long as each line starts with a white space. New line chars should be \r\n /// If headers already been sent. /// If value conditions have not been met. /// Adding any header will override the default ones and those specified by properties. void AddHeader(string name, string value); /// /// Send headers and body to the browser. /// /// If content have already been sent. void Send(); /// /// Make sure that you have specified ContentLength and sent the headers first. /// /// /// If headers have not been sent. /// /// offest of first byte to send /// number of bytes to send. /// /// /// This method can be used if you want to send body contents without caching them first. This /// is recommended for larger files to keep the memory usage low. bool SendBody(byte[] buffer, int offset, int count); /// /// Make sure that you have specified ContentLength and sent the headers first. /// /// /// If headers have not been sent. /// /// /// /// This method can be used if you want to send body contents without caching them first. This /// is recommended for larger files to keep the memory usage low. bool SendBody(byte[] buffer); /// /// Send headers to the client. /// /// If headers already been sent. /// /// /// bool SendHeaders(); } /// /// Type of HTTP connection /// public enum ConnectionType { /// /// Connection is closed after each request-response /// Close, /// /// Connection is kept alive for X seconds (unless another request have been made) /// KeepAlive } }