SourceForge.net Logo Click here to download Mr_SCGI from Sourceforge

Mr_SCGI.

Mr_SCGI is a lightweight library that provides SCGI server functionality for web based applications written in C. It provides a servlet container style API for facilities such as Request inspection, Session management, cookies and memory management.

Applications written using this library run as a daemon process and so do not have the resource costs of starting up a new process for each request, unlike CGI or PHP applications. They have much lower memory and processor requirements than Java Servlet/JSP applications.

SCGI

The SCGI (Simple Common Gateway Interface) protocol is a replacement for the CGI protocol. It is a standard for applications to interface with HTTP servers. It is similar to FastCGI but is designed to be easier to implement. see http://en.wikipedia.org/wiki/SCGI

An Apache module mod_scgi is available from http://www.mems-exchange.org/software/files/mod_scgi/

See below for mod_scgi configuration.

Developing applications using Mr_SCGI - Overview

Applications using the library will be run as a daemon process. The library itself contains the "main(int argc, char * argv[])" function, and requires the application developer to provide the following functions:-

void app_initialisation(int argc, char * argv[])

- this is called once with the start-up parameters

void app_termination()

- this is called once when the server process is terminated.

void app_process_request(CGI_Request * request, CGI_Response * response)

- this is called for each request.

A simple helloworld style program would look something like the following:-

#include "mrscgi_server.h"

void app_initialisation(int argc, char * argv[]) {}

void app_process_request(CGI_Request * request, CGI_Response * response)

{

UOC_Xmltag * xml = cgi_response_get_xml(response);

mrhtml_p(xml, "Hello World.");

}

void app_termination() {}

It would compile with:-

gcc -Wall -o helloworld helloworld.c -lmrscgi -lpthread

Note the linking with pthread (posix threads). Mr_SCGI applications are multithreaded - care must be taken to ensure thread-safety throughout. Part of the API is provided to assist with this.

When the compiled application is executed, it will go to the background and wait for SCGI requests. These would be sent from, for example, Apache mod_scgi.

By default, the application listens on port 4000, but the port may be specified when executing the application with the command line parameter --port.

e.g. Executing

./helloworld --port 4001

will cause the application to listen on port 4001.

Apache mod_scgi configuration

mod_scgi will forward http requests to the scgi server as specified in its Apache configuration.

Given the following configuration:-

 <Location "/helloworld/scgi">

 SCGIServer 127.0.0.1:4000

 SCGIHandler On

 SetHandler scgi-handler

 </Location>

all requests starting with http://<server>/helloworld/scgi will be sent to the application listening on port 4000