Project Description

A FastCGI library for .NET, implemented in F#.

Supports a large number of concurrent connections.

Offers an easy to use C# interface.

Supports F#'s asynchronous workflows (improving resource usage and performance).

Tested with IIS, Apache, nginx and lightTPD.

C# example:

using System;
using System.Net;
using System.IO;
using System.Globalization;
using System.Text;

using FastCGI;

namespace FastCGIApp
{
    class Program
    {
        static void HandleRequest(Request request, Response response)
        {
            // receive HTTP content
            byte[] content = request.Stdin.GetContents();

            // access server variables
            string serverSoftware = request.ServerSoftware.GetValueOrDefault();
            string method = request.RequestMethod.Value;

            // access HTTP headers
            string userAgent = request.Headers[RequestHeader.HttpUserAgent];
            string cookieValue = request.GetCookieValue("Keks").GetValueOrDefault();

            // set HTTP headers
            response.SetHeader(ResponseHeader.HttpExpires,
                               Response.ToHttpDate(DateTime.Now.AddDays(1.0)));
            response.SetCookie(new Cookie("Keks", "yummy"));

            // send HTTP content
            response.PutStr(
                @"<html>
                   <body>
                    <p>Hello World!</p>
                    <p>Server: " + serverSoftware + @"</p>
                    <p>User Agent: " + userAgent + @"</p>
                    <p>Received cookie value: " + cookieValue + @"</p>
                    <p>Content length as read: " + content.Length + @"</P>
                    <p>Request method: " + method + @"</p>
                   </body>
                  </html>"
                );
        }

        static void Main(string[] args)
        {
            Options config = new Options();
            config.Bind = BindMode.CreateSocket;
            config.EndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9000);
            config.OnError = Console.WriteLine;
            Server.Start(HandleRequest, config);
        }
    }
}

 

F# example:

open System
open System.Net

open FastCGI

let configuration = Options(Bind = BindMode.CreateSocket,
                            EndPoint = IPEndPoint(IPAddress.Parse("127.0.0.1"), 9000),
                            ErrorLogger = (Console.WriteLine : string -> unit))

serverLoop configuration
   (fun request response ->
      async {
         // receive HTTP content
         let! content = request.Stdin.AsyncGetContents()

         // access server variables
         let serverSoftware = match request.ServerSoftware with
                              | None -> "unknown"
                              | Some s -> s
         let reqMethod = request.RequestMethod.Value

         // access HTTP headers
         let userAgent = request.Headers.[HttpUserAgent]
         let cookieValue = match request.GetCookieValue "Keks" with
                           | None -> "unset"
                           | Some c -> c

         // set HTTP headers
         response.SetHeader (ResponseHeader.HttpExpires, Response.ToHttpDate (DateTime.Now.AddDays 1.0))
         response.SetCookie (Cookie("Keks", "yummy"))

         // send HTTP content
         do! @"<html>
                  <body>
                  <p>Hello World!</p>
                  <p>Server: " + serverSoftware + @"</p>
                  <p>User Agent: " + userAgent + @"</p>
                  <p>Received cookie value: " + cookieValue + @"</p>
                  <p>Content length as read: " + string(content.Length) + @"</P>
                  <p>Request method: " + reqMethod + @"</p>
                  </body>
               </html>"
               |> response.AsyncPutStr
       }
   )
|> Async.RunSynchronously


Visit https://github.com/wmeyer/SharpCGI for the current development branch.

Last edited Apr 12, 2011 at 11:42 PM by meyerwolfgang, version 6