1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
5 FastCGI Programmer's Guide - Chapter 2, Developing FastCGI Applications in C
7 <STYLE TYPE="text/css">
9 background-color: #ffffff;
11 li.c2 {list-style: none}
12 div.c1 {text-align: center}
16 <A HREF="cover.htm">[Top]</A> <A HREF="ch1intro.htm">[Prev]</A> <A HREF="ch3perl.htm">[Next]</A> <A HREF=
17 "ap_guida.htm">[Bottom]</A>
23 2 Developing FastCGI<BR>
29 This chapter explains how to code FastCGI applications in C and how to build them into executables.
32 <A NAME="4230"></A> If you are converting a CGI application into a FastCGI application, in many cases you will
33 only need to add a few lines of code. For more complex applications, you may also need to rearrange some code.
42 The FastCGI Software Development Kit that accompanies Open Market WebServer 2.0 includes I/O libraries to
43 simplify the job of converting existing CGI applications to FastCGI or writing new FastCGI applications. There
44 are two libraries in the kit: fcgi_stdio and fcgiapp. You must include one of these header files in your
54 <CODE>fcgi_stdio.h</CODE> <A NAME="4237"></A>
57 <CODE>fcgiapp.h</CODE>
62 The <CODE>fcgi_stdio</CODE> library is a layer on top of the <CODE>fcgiapp</CODE> library, and we recommend
63 strongly that you use it, both for converting existing CGI applications and for writing new FastCGI
64 applications. The fcgi_stdio library offers several advantages:
73 Simplicity: there are only 3 new API calls to learn <A NAME="5828"></A>
76 Familiarity: If you are converting a CGI application to FastCGI, you will find few changes between CGI and
77 FastCGI. We designed our library to make the job of building a FastCGI application as similar as possible
78 to that of building a FastCGI application: you use the same environment variables, same techniques for
79 parsing query strings, the same I/O routines, and so on. <A NAME="5817"></A>
82 Convenience: the library provides full binary compatibility between CGI and FastCGI. That is, you can run
83 the same binary as either CGI or FastCGI.
88 The fcgiapp library is more specific to FastCGI, without trying to provide the veneer of familiarity with CGI.
89 This manual describes the fcgi_stdio library; the fcgiapp library is documented in the header files that
90 accompany the development kit.
99 To structure code for FastCGI, you separate your code into two sections:
108 Initialization section, which is executed only once. <A NAME="4201"></A>
111 Response loop section, which gets executed every time the FastCGI script gets called.
116 A response loop typically has the following format:
121 <A NAME="4203">while (FCGI_Accept() >= 0) {
123 <A NAME="4204"># body of response loop
130 The <CODE>FCGI_Accept</CODE> blocks until a client request comes in, and then returns 0. If there is a system
131 failure, or the system administrator terminates the process, Accept will return -1.
134 <A NAME="5852"></A> If the application was invoked as a CGI program, the first call to Accept returns 0 and
135 the second always returns -1, producing CGI behavior. (See <A HREF="apaman.htm#95683">"FCGI_Accept
136 (3)" on page 21</A> for details.)
139 <A NAME="5909"></A> Also note that the CGI world encourages small scripts, whereas FastCGI encourages
140 combining scripts. You may choose to rethink the overall structure of your applications to take better
141 advantage of FastCGI performance gains.
146 Example 1: TinyFastCGI
148 <A NAME="4263">Here is a simple example of a responder FastCGI application written in C:</A><BR>
151 #include "fcgi_stdio.h" /* fcgi library; put it first*/<BR>
152 #include <stdlib.h>
156 void initialize(void)
163 /* Initialization. */
167 while (FCGI_Accept() >= 0) {
168 printf("Content-type: text/html\r\n"
170 "<title>FastCGI Hello! (C, fcgi_stdio library)</title>"
171 "<h1>FastCGI Hello! (C, fcgi_stdio library)</h1>"
172 "Request number %d running on host <i>%s</i>\n",
173 ++count, getenv("SERVER_HOSTNAME"));
178 Example 2: Prime Number Generator
182 Consider a responder application that generates the n-th prime number.
185 <A NAME="5217"></A> A CGI application would have no efficient way of solving this problem. For example, if the
186 user asks for the 50,000th prime number, a CGI application would have to calculate the first prime number,
187 then the second, and so on, up until the 50,000th. The application would then terminate, taking with it all
188 its hard-earned calculations. If a client then asks for the 49,000th prime number, the server will have to
189 spawn a new CGI application which will have to start calculating prime numbers from scratch.
192 <A NAME="4315"></A> FastCGI applications can be much more efficient at this sort of problem, since they can
193 maintain state. A FastCGI application can calculate an extensive table of prime numbers in its initialization
194 phase and then keep the table around indefinitely. Whenever a client requests a particular prime number, the
195 response loop merely needs to look it up in the table.
198 <A NAME="4343"></A> Here is the code for the prime number example:
203 #include "fcgi_stdio.h"
204 #include <stdlib.h>
205 #include <string.h>
207 #define POTENTIALLY_PRIME 0
209 #define VALS_IN_SIEVE_TABLE 1000000
210 #define MAX_NUMBER_OF_PRIME_NUMBERS 78600
212 /* All initialized to POTENTIALLY_PRIME */
213 long int sieve_table[VALS_IN_SIEVE_TABLE];
214 long int prime_table[MAX_NUMBER_OF_PRIME_NUMBERS];
215 /* Use Sieve of Erastothenes method of building
216 a prime number table. */
218 initialize_prime_table(void)
220 long int prime_counter=1;
221 long int current_prime=2, c, d;
223 prime_table[prime_counter]=current_prime;
225 while (current_prime < VALS_IN_SIEVE_TABLE) {
226 /* Mark off composite numbers. */
227 for (c = current_prime; c <= VALS_IN_SIEVE_TABLE;
228 c += current_prime) {
229 sieve_table[c] = COMPOSITE;
232 /* Find the next prime number. */
233 for (d=current_prime+1; sieve_table[d] == COMPOSITE; d++);
234 /* Put the new prime number into the table. */
235 prime_table[++prime_counter]=d;
246 initialize_prime_table();
248 while(FCGI_Accept() >= 0) {
250 * Produce the necessary HTTP header.
252 printf("Content-type: text/html\r\n"
255 * Produce the constant part of the HTML document.
257 printf("<title>Prime FastCGI</title>\n"
258 "<h1>Prime FastCGI</h1>\n");
260 * Read the query string and produce the variable part
261 * of the HTML document.
263 query_string = getenv("QUERY_STRING");
264 if(query_string == NULL) {
265 printf("Usage: Specify a positive number in the query string.\n");
267 query_string = strchr(query_string, `=') + 1;
268 n = strtol(query_string);
270 printf("The query string `%s' is not a positive number.\n",
272 } else if(n > MAX_NUMBER_OF_PRIME_NUMBERS) {
273 printf("The number %d is too large for this program.\n", n);
275 printf("The %ldth prime number is %ld.\n", prime_table[n]);
278 } /* while FCGI_Accept */
283 This application has a noticeable start up cost while it initializes the table, but subsequent accesses are
293 This section explains how to build and debug FastCGI applications written in C.
296 <A NAME="4629"></A> The C preprocessor needs to know the location of the <CODE>fcgi_stdio.h</CODE> header
297 file, which is at the following pathname:
302 <A NAME="4642"><EM>$toolkit</EM>/include/fcgi_stdio.h
307 where <EM>$toolkit</EM> symbolizes the directory in which you have installed the Software Development Kit for
311 <A NAME="4760"></A> The linker needs to know the location of the <CODE>libfcgi.a</CODE> library file, which is
312 at the following pathname:
317 <A NAME="4647"><EM>$toolkit</EM>/libfcgi/libfcgi.a
322 If your linker does not search the Berkeley socket library, then you must add linker directives to force this
326 <A NAME="4773"></A> We provide a sample application <CODE>Makefile</CODE> at the following pathname:
331 <A NAME="4649"><EM>$toolkit</EM>/examples/Makefile
336 This <CODE>Makefile</CODE> contains the necessary rules and pathnames to build the C FastCGI applications
337 accompanying the toolkit. To build all the applications, type:
342 <A NAME="4653">$ ./configure<BR>
351 Memory leaks are seldom a problem in CGI programming because CGI applications rarely run long enough to be
352 concerned with leaks. However, memory leaks can become a problem in FastCGI applications, particularly if each
353 call to a popular FastCGI application causes additional memory to leak.
356 <A NAME="4785"></A> When converting to FastCGI, you can either use a tool such as Purify from Pure Software to
357 discover and fix storage leaks or you can run a C garbage collector such as Great Circle from Geodesic
367 <A HREF="cover.htm">[Top]</A> <A HREF="ch1intro.htm">[Prev]</A> <A HREF="ch3perl.htm">[Next]</A> <A HREF=
368 "ap_guida.htm">[Bottom]</A>
371 <!-- This file was created with Quadralay WebWorks Publisher 3.0.3 -->
373 <!-- For more information on how this document, and how the rest of -->
374 <!-- this server was created, email yourEmail@xyzcorp.com -->
376 <!-- Last updated: 04/15/96 08:00:16 -->