Skip to content
it4e edited this page Oct 2, 2016 · 1 revision

FastCGI is a way for the webserver to have a constant connection with your CHL-script, which means the webserver does not have to spawn a new process for every incoming request. This makes for a faster and a more cpu/memory friendly experience for the server. Although FastCGI is a bit more complicated than normal CGI, CHL makes it easy to switch between them both without having to change much code at all.

The only thing you have to do to make use of FastCGI is to create a loop and call a single function for every iteration of the loop. CHL uses the FastCGI library: libfcgi, which makes everything so much easier.

Make sure you have installed and configured everything correctly before you read this tutorial. See setup FastCGI.

Contents

To wait for and accept an incoming request (client) we need to call the function chl_fcgi_next(), which will wait for a request and return 1 whether a client was found, or 0 if something went wrong.

Example 1

#include <chl/chl.h>
#include <chl/fcgi.h>

int main() {
    // Wait for request
    while(chl_fcgi_next()) {
        // Handle request here, normal CHL
        chl_set_default_headers();
        chl_print_headers();

        printf("<p>You connected</p>");
    }
}

As you see the code is very straightforward. First of all we wait for a client to connect and issue an HTTP request. We then handle the request inside of the while loop brackets. The thing that is so great is that you still use the normal CGI functions, even though you have switched to FastCGI, so there are really no new functions to learn or keep track of. Just code everything as you would for a normal CGI application, but put the code in the loop instead.

When writing FastCGI applications in CHL it is a good idea to include the header file 'chl/fcgi.h'. This header file includes various wrapper functions for the standard stdio C functions, printf, puts e.t.c, so that everything written to stdout maps to the server instead.

#include <chl/fcgi.h>

Make sure you also read the main tutorial.

As CHL is open-source, people are able to contribute with their own APIs, plugins and code which means that CHL is constantly upgraded and provided with new features. Do you have an idea for a new CHL feature and want to contribute?

See contribute.

Setup. API. Tutorial. Examples. FastCGI.

Clone this wiki locally