class FastCGI.FCGIApplication

Main FastCGI listener class.

This class manages a connection to a webserver by listening on a given port on localhost and receiving FastCGI requests by a webserver like Apache or nginx.

In FastCGI terms, this class implements the responder role. Refer to section 6.2 of the FastCGI specification for details.

Use OnRequestReceived to get notified of received requests. You can call Run to enter an infinite loopand let the app handle everything. Alternatively, if you want to control the execution flow by yourself, call Listen to start accepting connections. Then repeatedly call Process to handle incoming requests.

If you want to manage the socket connection details by yourself, or for testing purposes, you can also call ProcessSingleRecord instead of any of the above methods.

See the below example to learn how to accept requests. For more detailed information, have a look at the Request class.

If you need to fiddle with the FastCGI packets itself, see the Record class and read the FastCGI specification.

Examples

// Create a new FCGIApplication, will accept FastCGI requests
var app = new FCGIApplication();

// Handle requests by responding with a 'Hello World' message
app.OnRequestReceived += (sender, request) => {
    request.WriteResponseASCII("HTTP/1.1 200 OK\nContent-Type:text/html\n\nHello World!");
    request.Close();
};
// Start listening on port 19000
app.Run(19000);

// You now need a webserver like nginx or Apache to pass incoming requests
// via FastCGI to your application.

Methods

  • void Listen (int port)
    Starts listening for connections on the given port.
    Will only accept connections from localhost. Use the Listen overload of this method to specify where to listen for connection.

  • void Listen (System.Net.IPEndPoint endPoint)
    Starts listening for connections on the given IP end point.

  • bool Process ()
    Processes all data available on the current FastCGI connection and handles the received data.
    Call this repeatedly to process incoming requests. Alternatively, you can call Run once, which will call Listen and execute this method in an infinite loop. Internally, this method manages reconnections on the network socket and calls ProcessSingleRecord . Returns true if a record was read, false otherwise.

  • bool ProcessStream (Stream inputStream, Stream outputStream)
    Reads and handles all Records available on the custom inputStream and writes responses to outputStream.
    Use Process if you don't need a custom stream, but instead want to process the records on the current FastCGI connection. Returns true if a record was read, false otherwise.

  • bool ProcessSingleRecord (Stream inputStream, Stream outputStream)
    Tries to read and handle a Record from inputStream and writes responses to outputStream.
    Use ProcessStream to process all records on a stream. Returns true if a record was read, false otherwise.

  • void StopListening ()
    Stops listening for incoming connections.

  • void Stop ()

  • void Run (int port)
    This method never returns! Starts listening for FastCGI requests on the given port.
    Use OnRequestReceived to react to incoming requests. Internally, this simply calls Listen and enters an infinite loop of Process calls.

Events

  • OnRequestReceived
    Will be called whenever a request has been received.
    Please note that multiple requests can be open at the same time. This means that this event may fire multiple times before you call Request.Close on the first one.

Properties and Fields

  • bool Connected
    True iff this application is currently connected to a webserver.

  • int Timeout
    The read/write timeouts in miliseconds for the listening socket, the connections, and the streams.
    Zero or -1 mean infinite timeout.

  • System.Net.Sockets.Socket ListeningSocket
    The main listening socket that is used to establish connections.

  • List<FastCGI.FCGIStream> OpenConnections
    A list of open FCGIStream connections.
    When a connection is accepted from ListeningSocket , it is added here. Contains all connections that were still open after the last Process call.

  • bool IsStopping

  • Dictionary<int, FastCGI.Request> OpenRequests
    A dictionary of all open Requests , indexed by the FastCGI request id.

class FastCGI.FCGIStream

  • Extends System.Net.Sockets.NetworkStream*

Represents a FastCGI connection to a webserver.

At any given time, a single FCGIApplication may have any number of open FCGIStream connections (including zero). No attempt is made to limit that number. This is basically just an extension of ystem.Net.Sockets.NetworkStream , with the underlying Socket exposed.

Properties and Fields

  • bool IsConnected
    True iff the connection is still open.

  • System.Net.Sockets.Socket Socket
    The underlying socket of the

class FastCGI.Record

A FastCGI Record.

See section 3.3 of the FastCGI Specification for details.

Methods

  • Dictionary<string, Byte[]> GetNameValuePairs ()

  • void SetNameValuePairs (Dictionary<string, Byte[]> nameValuePairs)

  • int WriteToStream (Stream stream)
    Writes this record to the given stream.

  • void Send (Stream stream)
    Used internally. Writes the record to the given stream. Used for sending records to the webserver.

  • string ToString ()

  • bool Equals (Object obj)

Properties and Fields

  • FastCGI.Record.RecordType Type
    The RecordType of this record.

  • int RequestId
    The request id associated with this record.

  • Byte[] ContentData
    The data contained in this record.

Static Methods

  • Dictionary<string, Byte[]> ReadNameValuePairs (Stream stream)
    Tries to read a dictionary of name-value pairs from the given stream
    This method does not make any attempt to make sure whether this record actually contains a set of name-value pairs. It will return nonsense or throw an EndOfStreamException if the record content does not contain valid name-value pairs.

  • uint ReadVarLength (Stream stream)
    Reads a length from the given stream, which is encoded in one or four bytes.
    See section 3.4 of the FastCGI specification for details.

  • void WriteVarLength (Stream stream, uint len)
    Writes a length from the given stream, which is encoded in one or four bytes.
    See section 3.4 of the FastCGI specification for details.

  • ushort ReadInt16 (Stream stream)
    Reads a 16-bit integer from the given stream.

  • void WriteInt16 (Stream stream, ushort v)
    Writes a 16-bit integer to the given stream.

  • FastCGI.Record ReadRecord (Stream stream)
    Reads a single Record from the given stream.
    Returns the retreived record or null if no record could be read. Will block if a partial record is on the stream, until the full record has arrived or a timeout occurs.

  • FastCGI.Record CreateStdout (Byte[] data, int requestId)
    Creates a Stdout record from the given data and request id

  • FastCGI.Record CreateEndRequest (int requestId)
    Creates a EndRequest record with the given request id

  • FastCGI.Record CreateGetValuesResult (int maxConnections, int maxRequests, bool multiplexing)
    Creates a GetValuesResult record from the given config values.

class FastCGI.Request

A FastCGI request.

A request usually corresponds to a HTTP request that has been received by the webserver (see the FastCGI specification for details).

You will probably want to use WriteResponse or its helper methods to output a response and then call Close . Use FCGIApplication.OnRequestReceived to be notified of new requests.

Remember to call Close when you wrote the complete response.

Methods

  • string GetParameterASCII (string name)
    Returns the parameter with the given name as an ASCII encoded string.

  • string GetParameterUTF8 (string name)
    Returns the parameter with the given name as an UTF-8 encoded string.

  • void WriteResponse (Byte[] data)
    Appends data to the response body.
    The given data will be sent immediately to the webserver as a single stdout record.

  • void WriteResponseASCII (string data)
    Appends an ASCII string to the response body.
    This is a helper function, it converts the given string to ASCII bytes and feeds it to WriteResponse .

  • void WriteResponseUtf8 (string data)
    Appends an UTF-8 string to the response body.
    This is a helper function, it converts the given string to UTF-8 bytes and feeds it to WriteResponse .

  • void Close ()
    Closes this request.

Properties and Fields

  • Stream ResponseStream
    The stream where responses to this request should be written to. Only write FastCGI records here, not the raw response body. Use WriteResponse for sending response data.

  • bool KeepAlive

  • int RequestId
    The id for this request, issued by the webserver

  • string Body
    The request body.
    For POST requests, this will contain the POST variables. For GET requests, this will be empty.

  • Dictionary<string, Byte[]> Parameters
    The FastCGI parameters received by the webserver, in raw byte arrays.
    Use GetParameterASCII and GetParameterUTF8 to get strings instead of byte arrays.