martedì 19 maggio 2009

Hello World Wide Web: Part 1, editing and compiling

In the last posts we set up the programming environment, the web server is running and finally we are ready to start with the first FastCGI application: what better than a web version of the classic Hello World, for the occasion renamed Hello World Wide Web? Let’s go!

First we must include the library fcgi_stdio needed by every FastCGI application:

#include “fcgi_stdio.h”

This isn’t the only possibility, the library fcgiapp is an alternative. What are the differences between fcgi_stdio and fcgiapp? The fcgi_stdio library is a layer on top of the fcgiapp library that implement also CGI functionalities, so an application developed with fcgi_stdio can run indifferently in CGI or FastCGI environment without recompilation.

According to the official FastCGI site using the library fcgi_stdio is preferable because the fcgi_stdio library offers several advantages:

Simplicity: there are only 3 new API calls to learn

Familiarity: If you are converting a CGI application to FastCGI, you will find few changes between CGI and FastCGI. We designed our library to make the job of building a FastCGI application as similar as possible to that of building a CGI application: you use the same environment variables, same techniques for parsing query strings, the same I/O routines, and so on.

Convenience: the library provides full binary compatibility between CGI and FastCGI. That is, you can run the same binary as either CGI or FastCGI.

The fcgiapp library is more specific to FastCGI, without trying to provide the veneer of familiarity with CGI.

After the #include, as usual for every C program, there is the main() function:

int main( int argc, char *argv[] ) { ... }

that is composed by a while loop waiting for every HTTP requests:

while( FCGI_Accept() >= 0 ) { ... }

FCGI_Accept function accepts a new request from the HTTP server (i.e. Lighttpd) and creates a CGI-compatible execution environment for the request.

Inside this while loop we must put the real program that generate the HTTP answer. An HTTP answer to a GET or a POST request must be composed of a header and the content (differently from other most popular languages for the web it's mandatory to write the header by-hand every time).

We can easily send both, header and content, to client writing them to the standard output as any regular CGI application, so in our first example we need only two lines of code:

printf( "Content-Type: text/plain\r\n\n\n" );

for header of the HTTP answer (note that the two new-lines , "\n\n", at the end of the string conventionally mark the end of the header) and

printf( "Hello World Wide Web\n" );

that represents what will appear on the browser.

That's all.

The full code of the example is:

#include "fcgi_stdio.h"
int main( int argc, char *argv[] )

while( FCGI_Accept() >= 0 )

printf( "Content-Type: text/plain\n\n" );
printf( "Hello World Wide Web\n" );
return 0;

We can save this code with "helloweb.c" and compile it with this command:

gcc -o helloweb -lfcgi helloweb.c

In the next post we will examine how to deploy helloweb on the web server.


Nessun commento:

Posta un commento