C# Class Woopsa.HTTPResponse

Provides a mechanism to send a response to a request.

This class is created internally by the Web Server and thus cannot be created from outside code. To handle requestsList and response, the WebServer.IHTTPRouteHandler interface or the delegate methods in RouteSolver which is available from the WebServer.WebServer.Routes property.

When using a method such as WriteString, WriteHTML, SetHeader, etc., no data is actually sent to the client. Instead, data is buffered until the RouteSolver is done processing, which will automatically send the response to the client.

Inheritance: IDisposable
Datei anzeigen Open project: woopsa-protocol/Woopsa Class Usage Examples

Private Properties

Property Type Description
HTTPResponse System
SendError void
SendError void
SetHeaderIfNotExists void

Public Methods

Method Description
Dispose ( ) : void
Respond ( Stream outputStream ) : void

Writes the response to the output stream.

This method should only be called in certain special cases where the route solving system is being short-circuited somehow, because the route solving system is usually in charge of calling this method.

SetHeader ( string header, string value ) : void

Sets the value of an HTTP Response Header. Many standard headers are defined in WebServer.HTTPHeader, but custom headers can always be manually set in case this is desired.

Headers are sent in the Header: Value format in the HTTP protocol.

SetStatusCode ( int responseCode, string responseMessage ) : void

Set the status and message code of the response

WriteError ( HTTPStatusCode errorCode, string errorMessage ) : void

Sends a nicely-formatted error with specified HTTP Error Code and Error Message to the client. This response is not buffered and thus calling this method will immediately send the response to the client.

WriteError ( HTTPStatusCode errorCode, string errorMessage, string errorContent, string mimeType ) : void

Sends a custom-formatted error with specified HTTP Error Code, Error Message Response Content and mime type to the client. This response is not buffered and thus calling this method will immediately send the response to the client.

WriteHTML ( string html ) : void

Writes a specified string to the response buffer and sets the Content-Type header to be text/html, allowing browsers to render the string as HTML.

WriteStream ( Stream stream ) : void

Writes a stream to the response buffer. This method is used mainly when serving files in a more efficient way. This method does not modify the Content-Type header and if nothing is done, the default value of text/plain will thus be used.

WriteString ( string text ) : void

Writes a specified string to the response buffer. Does not modify the Content-Type header. Default Content-Type is text/plain

Protected Methods

Method Description
Dispose ( bool disposing ) : void
DoError ( HTTPResponse response, Exception exception ) : void

Private Methods

Method Description
HTTPResponse ( ) : System
SendError ( int errorCode, string errorMessage ) : void
SendError ( int errorCode, string errorMessage, string responseContent, string mimeType ) : void
SetHeaderIfNotExists ( string header, string value ) : void

Method Details

Dispose() public method

public Dispose ( ) : void
return void

Dispose() protected method

protected Dispose ( bool disposing ) : void
disposing bool
return void

DoError() protected static method

protected static DoError ( HTTPResponse response, Exception exception ) : void
response HTTPResponse
exception System.Exception
return void

Respond() public method

Writes the response to the output stream.

This method should only be called in certain special cases where the route solving system is being short-circuited somehow, because the route solving system is usually in charge of calling this method.

public Respond ( Stream outputStream ) : void
outputStream Stream The stream in which to copy the output. Does not close the stream
return void

SetHeader() public method

Sets the value of an HTTP Response Header. Many standard headers are defined in WebServer.HTTPHeader, but custom headers can always be manually set in case this is desired.

Headers are sent in the Header: Value format in the HTTP protocol.

public SetHeader ( string header, string value ) : void
header string /// The name of the header, for example: /// /// /// "My-Custom-Header" /// ///
value string /// The value of the header. ///
return void

SetStatusCode() public method

Set the status and message code of the response
public SetStatusCode ( int responseCode, string responseMessage ) : void
responseCode int
responseMessage string
return void

WriteError() public method

Sends a nicely-formatted error with specified HTTP Error Code and Error Message to the client. This response is not buffered and thus calling this method will immediately send the response to the client.
public WriteError ( HTTPStatusCode errorCode, string errorMessage ) : void
errorCode HTTPStatusCode The HTTP Status Code to send
errorMessage string The Error Message to send. For example "Not Found" or "Not Supported"
return void

WriteError() public method

Sends a custom-formatted error with specified HTTP Error Code, Error Message Response Content and mime type to the client. This response is not buffered and thus calling this method will immediately send the response to the client.
public WriteError ( HTTPStatusCode errorCode, string errorMessage, string errorContent, string mimeType ) : void
errorCode HTTPStatusCode The HTTP Status Code to send
errorMessage string The Error Message to send. For example "Not Found" or "Not Supported"
errorContent string
mimeType string
return void

WriteHTML() public method

Writes a specified string to the response buffer and sets the Content-Type header to be text/html, allowing browsers to render the string as HTML.
public WriteHTML ( string html ) : void
html string /// The text to write. Encoding will be solved automatically. ///
return void

WriteStream() public method

Writes a stream to the response buffer. This method is used mainly when serving files in a more efficient way. This method does not modify the Content-Type header and if nothing is done, the default value of text/plain will thus be used.
public WriteStream ( Stream stream ) : void
stream Stream
return void

WriteString() public method

Writes a specified string to the response buffer. Does not modify the Content-Type header. Default Content-Type is text/plain
public WriteString ( string text ) : void
text string /// The text to write. Encoding will be solved automatically. ///
return void