If you are converting a CGI application into a FastCGI application, in many cases you will only need to add a few lines of code. For more complex applications, you may also need to rearrange some code.
fcgi_stdio.h
fcgiapp.h
fcgi_stdio
library is a layer on top of the fcgiapp
library, and we recommend strongly that you use it, both for converting existing CGI applications and for writing new FastCGI applications. The fcgi_stdio library offers several advantages:
while (FCGI_Accept() >= 0) { # body of response loop }The
FCGI_Accept
blocks until a client request comes in, and then returns 0. If there is a system failure, or the system administrator terminates the process, Accept will return -1. If the application was invoked as a CGI program, the first call to Accept returns 0 and the second always returns -1, producing CGI behavior. (See "FCGI_Accept (3)" on page 21 for details.)
Also note that the CGI world encourages small scripts, whereas FastCGI encourages combining scripts. You may choose to rethink the overall structure of your applications to take better advantage of FastCGI performance gains.
Example 1: TinyFastCGI
Here is a simple example of a responder FastCGI application written in C:
#include "fcgi_stdio.h" /* fcgi library; put it first*/
#include <stdlib.h>
int count;
void initialize(void)
{
count=0;
}
void main(void)
{
/* Initialization. */
initialize();
/* Response loop. */
while (FCGI_Accept() >= 0) {
printf("Content-type: text/html\r\n"
"\r\n"
"<title>FastCGI Hello! (C, fcgi_stdio library)</title>"
"<h1>FastCGI Hello! (C, fcgi_stdio library)</h1>"
"Request number %d running on host <i>%s</i>\n",
++count, getenv("SERVER_HOSTNAME"));
}
}
Example 2: Prime Number Generator
Consider a responder application that generates the n-th prime number.
#include "fcgi_stdio.h"
#include <stdlib.h>
#include <string.h>
#define POTENTIALLY_PRIME 0
#define COMPOSITE 1
#define VALS_IN_SIEVE_TABLE 1000000
#define MAX_NUMBER_OF_PRIME_NUMBERS 78600
/* All initialized to POTENTIALLY_PRIME */
long int sieve_table[VALS_IN_SIEVE_TABLE];
long int prime_table[MAX_NUMBER_OF_PRIME_NUMBERS];
/* Use Sieve of Erastothenes method of building
a prime number table. */
void
initialize_prime_table(void)
{
long int prime_counter=1;
long int current_prime=2, c, d;
prime_table[prime_counter]=current_prime;
while (current_prime < VALS_IN_SIEVE_TABLE) {
/* Mark off composite numbers. */
for (c = current_prime; c <= VALS_IN_SIEVE_TABLE;
c += current_prime) {
sieve_table[c] = COMPOSITE;
}
/* Find the next prime number. */
for (d=current_prime+1; sieve_table[d] == COMPOSITE; d++);
/* Put the new prime number into the table. */
prime_table[++prime_counter]=d;
current_prime=d;
}
}
void main(void)
{
char *query_string;
long int n;
initialize_prime_table();
while(FCGI_Accept() >= 0) {
/*
* Produce the necessary HTTP header.
*/
printf("Content-type: text/html\r\n"
"\r\n");
/*
* Produce the constant part of the HTML document.
*/
printf("<title>Prime FastCGI</title>\n"
"<h1>Prime FastCGI</h1>\n");
/*
* Read the query string and produce the variable part
* of the HTML document.
*/
query_string = getenv("QUERY_STRING");
if(query_string == NULL) {
printf("Usage: Specify a positive number in the query string.\n");
} else {
query_string = strchr(query_string, `=') + 1;
n = strtol(query_string);
if(n < 1) {
printf("The query string `%s' is not a positive number.\n",
query_string);
} else if(n > MAX_NUMBER_OF_PRIME_NUMBERS) {
printf("The number %d is too large for this program.\n", n);
} else
printf("The %ldth prime number is %ld.\n", prime_table[n]);
}
}
} /* while FCGI_Accept */
}
This application has a noticeable start up cost while it initializes the table, but subsequent accesses are fast. Building
This section explains how to build and debug FastCGI applications written in C.fcgi_stdio.h
header file, which is at the following pathname:
$toolkit/include/fcgi_stdio.hwhere $toolkit symbolizes the directory in which you have installed the Software Development Kit for FastCGI.
The linker needs to know the location of the
libfcgi.a
library file, which is at the following pathname:
$toolkit/libfcgi/libfcgi.aIf your linker does not search the Berkeley socket library, then you must add linker directives to force this search.
We provide a sample application
Makefile
at the following pathname:
$toolkit/examples/MakefileThis
Makefile
contains the necessary rules and pathnames to build the C FastCGI applications accompanying the toolkit. To build all the applications, type:
$ ./configure
$ make
When converting to FastCGI, you can either use a tool such as Purify from Pure Software to discover and fix storage leaks or you can run a C garbage collector such as Great Circle from Geodesic Systems.