1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
\r
5 FastCGI Programmer's Guide - Chapter 1, The Fast Common Gateway Interface
\r
7 <STYLE TYPE="text/css">
\r
9 background-color: #ffffff;
\r
11 li.c2 {list-style: none}
\r
12 div.c1 {text-align: center}
\r
16 <A HREF="cover.htm">[Top]</A> <A HREF="ap_guide.htm">[Prev]</A> <A HREF="ch2c.htm">[Next]</A> <A HREF=
\r
17 "ap_guida.htm">[Bottom]</A>
\r
23 1 The Fast Common<BR>
\r
29 The Fast Common Gateway Interface (FastCGI) is an enhancement to the existing CGI (Common Gateway Interface),
\r
30 which is a standard for interfacing external applications with Web servers.
\r
33 <A NAME="8373"></A> FastCGI is a proposed open standard and we expect both free and commercial Web servers to
\r
34 support it. FastCGI is included in Open Market WebServer and Secure WebServer, versions 2.0 and greater.
\r
39 Advantages of FastCGI
\r
43 FastCGI extends and enhances the CGI model in several ways:
\r
52 FastCGI enables applications to persist between client requests, eliminating application start up overhead
\r
53 and allowing the application to maintain state between client calls. <A NAME="7995"></A>
\r
56 FastCGI enables applications to reside on remote systems (rather than having to reside on the same system
\r
57 as the Web server) <A NAME="7997"></A>
\r
60 FastCGI enables additional flexibility in application functionality, with explicit support for applications
\r
61 that do client authentication and filtering of input.
\r
65 Long-lived Applications
\r
69 CGI applications are ephemeral and short-lived: each time a client requests a CGI application, the server asks
\r
70 the operating system to spawn a new CGI process. After the CGI process satisfies the request, the server kills
\r
71 it. The server spawns and subsequently kills a new process for each client request.
\r
74 <A NAME="8459"></A> FastCGI applications are long-lived, and can persist between client calls. The server
\r
75 spawns the FastCGI process once and it continues to run and satisfy client requests until it is explicitly
\r
76 terminated. You can also ask the Web server to start multiple copies of a FastCGI application, if you expect
\r
77 that concurrent processing will improve the application's performance.
\r
80 <A NAME="5761"></A> Long-lived applications have two important advantages over short-lived applications:
\r
89 A short-lived application pays start up overhead on every request; a long-lived application spreads the
\r
90 overhead over many requests. For an application that has a heavy start up cost, such as opening a database,
\r
91 doing initialization on every call can be very inefficient. Reinitializing for every client is also very
\r
92 inefficient for Perl programs, where the interpreter reads through the entire program before executing any
\r
93 of it. <A NAME="9204"></A>
\r
96 A long-lived application can cache information in memory between requests, allowing it to respond more
\r
97 quickly to later requests.
\r
100 <A NAME="8733"></A>
\r
102 FastCGI is not the only way to get a long-lived application on the Web, however. For example, there are many
\r
103 existing search engines that are implemented as long-lived applications.
\r
106 <A NAME="8734"></A> In most cases, these applications rely on customized Web servers. In other words, since
\r
107 most Web servers do not support long-lived applications, a programmer must code this support into a Web
\r
108 server. This approach requires a tremendous amount of work and also ties the application to a particular
\r
112 <A NAME="8735"></A> Another way to get a long-lived application is to write code that calls routines from the
\r
113 Web server's API. This alternative involves a lot of extra coding, ties the application to a particular
\r
114 Web server, and introduces problems of maintainability, scalability, and security.
\r
117 <A NAME="8736"></A> We believe that FastCGI is the most general and flexible strategy for building long-lived
\r
123 Separating Application and Server
\r
125 <A NAME="8446"></A>
\r
127 CGI applications must run on the same node as the Web server; FastCGI applications can run on any node that
\r
128 can be reached from your Web server using TCP/IP protocols. For example, you might want to run the FastCGI
\r
129 application on a high-speed computer server or database engine, and run the Web server on a different node.
\r
134 FastCGI "Roles"
\r
136 <A NAME="8777"></A>
\r
138 CGI and FastCGI applications are effective ways to allow an application to act as an extension to the Web
\r
139 server. CGI provides no explicit support for different kinds of applications: under CGI, every application
\r
140 receives an HTTP request, does something with it, and generates an HTTP response. FastCGI provides explicit
\r
141 support for several common "roles" that applications can play.
\r
144 <A NAME="8769"></A> The three roles supported by the WebServer 2.0 are:
\r
150 <A NAME="8409"></A>
\r
153 Responder <A NAME="8410"></A>
\r
156 Filter <A NAME="8411"></A>
\r
163 Responder Applications
\r
165 <A NAME="8679"></A>
\r
167 A <EM>responder</EM> application is the most basic kind of FastCGI application: it receives the information
\r
168 associated with an HTTP request and generates an HTTP response. Responder is the role most similar to
\r
169 traditional CGI programming, and most FastCGI applications are responders.
\r
174 Filter Applications
\r
176 <A NAME="8681"></A>
\r
178 A <EM>filter</EM> FastCGI application receives the information associated with an HTTP request, plus an extra
\r
179 stream of data from a file stored on the Web server, and generates a "filtered" version of the data
\r
180 stream as an HTTP response.
\r
183 <A NAME="8421"></A> With filter applications, the system administrator maps a particular MIME-type to a
\r
184 particular filter FastCGI application. When a client requests a URL with that MIME-type, the Web server
\r
185 invokes the filter application, which processes the file at the specified URL and sends a response (usually
\r
186 HTML text) back to the client.
\r
189 <A NAME="8422"></A> For example, suppose you write a filter FastCGI application that converts SGML text to
\r
190 HTML, and map the extension .sgml (MIME-type SGML) to your filter FastCGI application. Now, suppose that a
\r
191 user requests the following URL:
\r
196 <A NAME="8423">/www.aerjug.com/docs/chap1.sgml
\r
199 <A NAME="8424"></A>
\r
201 Given this URL, the Web server passes <CODE>chap1.sgml</CODE> as input to your filter FastCGI application,
\r
202 which processes <CODE>chap1.sgml</CODE> and returns an HTML version of it to the requesting client.
\r
207 Authorizer Applications
\r
209 <A NAME="8426"></A>
\r
211 An <EM>authorizer</EM> FastCGI application receives the information in an HTTP request header and generates a
\r
212 decision whether to authorize the request.
\r
215 <A NAME="8428"></A> To mark a FastCGI application as having the authorizer role, the system administrator
\r
216 names the application inside the server configuration file, using a directive called
\r
217 <CODE>AuthorizeRegion</CODE>. (See the Open Market Web Server manual for information on server configuration
\r
221 <A NAME="8429"></A> When a client requests a URL that meets the <CODE>AuthorizeRegion</CODE> criteria, the Web
\r
222 server calls your authorizer FastCGI application. If your application grants authorization (by returning a
\r
223 response code of 200), the Web server resumes execution of commands in the <CODE>AuthorizeRegion</CODE>
\r
224 section. If your application denies authorization (by returning any other response code), the Web server stops
\r
225 processing subsequent commands in the <CODE>AuthorizeRegion</CODE> section, and returns the response from your
\r
226 FastCGI application to the client.
\r
229 <A NAME="8431"></A> Authorizer applications can return headers containing environment variables. Other CGI or
\r
230 FastCGI programs accessing this request (including other authorizers) can access these environment variables.
\r
231 The headers must have the following format:
\r
236 <A NAME="8432">Variable-<EM>name</EM>: <EM>value</EM>
\r
239 <A NAME="8433"></A>
\r
241 For example, the following header
\r
246 <A NAME="8434">Variable-AUTH_METHOD: database lookup
\r
249 <A NAME="8435"></A>
\r
251 causes the environment variable <CODE>AUTH_METHOD</CODE> to be set to <CODE>"database lookup"</CODE>
\r
252 for this request. Other CGI or FastCGI applications running on this request can access the value of
\r
253 <CODE>AUTH_METHOD</CODE>.
\r
256 <A NAME="8437"></A> Authorizer applications cannot successfully read from standard input. Any attempts to read
\r
257 from standard input result in an immediate EOF.
\r
260 <A NAME="8438"></A> All data that authorizer applications write to standard error will get written to the
\r
261 traditional server error logs.
\r
266 Writing FastCGI Applications
\r
268 <A NAME="9301"></A>
\r
270 The work involved in writing a FastCGI application depends in large part on the I/O libraries that you use.
\r
271 This manual describes how to write FastCGI applications in terms of the Open Market libraries, which are
\r
272 available for C, Perl, and Tcl. FastCGI is an open standard and you are welcome to build your own libraries
\r
273 for other languages as well, but this manual focuses on building FastCGI applications in the context of the
\r
274 Open Market libraries.
\r
277 <A NAME="9443"></A>
\r
280 <A NAME="9450"></A> In general, the goal of the libraries is to make the job of writing a FastCGI application
\r
281 as much like writing a CGI application as possible. For example, you use the same techniques for query string
\r
282 decoding, HTML output to stdout, use of environment variables, and so on. When you use our libraries, porting
\r
283 CGI applications to FastCGI is mostly a matter of restructuring the code to take advantage of FastCGI features
\r
291 <A NAME="9470"></A>
\r
293 The main task of converting a CGI program into a FastCGI program is separating the initialization code from
\r
294 the code that needs to run for each request. The structure should look something like this:
\r
299 <A NAME="9471">Initialization code
\r
301 <A NAME="9472">Start of response loop
\r
303 <A NAME="9473"> body of response loop
\r
305 <A NAME="9474">End of response loop
\r
308 <A NAME="9475"></A>
\r
310 The <EM>initialization code</EM> is run exactly once, when the application is initialized. Initialization code
\r
311 usually performs time-consuming operations such as opening databases or calculating values for tables or
\r
315 <A NAME="9477"></A> The <EM>response loop</EM> runs continuously, waiting for client requests to arrive. The
\r
316 loop starts with a call to <CODE>FCGI_Accept</CODE>, a routine in the FastCGI library. The
\r
317 <CODE>FCGI_Accept</CODE> routine blocks program execution until a client requests the FastCGI application.
\r
318 When a client request comes in, <CODE>FCGI_Accept</CODE> unblocks, runs one iteration of the response loop
\r
319 body, and then blocks again waiting for another client request. The loop terminates only when the system
\r
320 administrator or the Web server kills the FastCGI application.
\r
325 Initial Environment Variables
\r
327 <A NAME="9786"></A>
\r
329 When a FastCGI process starts up, it has not yet accepted a request, and therefore none of the CGI environment
\r
333 <A NAME="9787"></A> You set the initial environment of a FastCGI process started by the <CODE>AppClass</CODE>
\r
334 directive using the <CODE>-initial-env</CODE> option. The process would use this environment to configure its
\r
335 options and locate files or databases.
\r
338 <A NAME="9829"></A> In FastCGI processes started by the <CODE>AppClass</CODE> directive with the -affinity
\r
339 option, the <CODE>FCGI_PROCESS_ID</CODE> variable is set in the initial environment (not in the environment of
\r
340 a request). <CODE>FCGI_PROCESS_ID</CODE> is a decimal number in the range 0 to N - 1 where N is the number of
\r
341 processes (argument to the <CODE>-processes</CODE> option to <CODE>AppClass</CODE>). The process would use
\r
342 <CODE>FCGI_PROCESS_ID</CODE> in conjunction with other variables to locate session-related files or databases
\r
348 Per-Request Environment Variables
\r
350 <A NAME="9481"></A>
\r
352 In general, FastCGI uses the same per-request environment variables as CGI, and you access the values of
\r
353 environment variables in FastCGI applications just as you would in CGI applications. The only differences are
\r
360 <A NAME="9483"></A>
\r
363 In Authorizer FastCGI applications, the Web server unsets the <CODE>PATH_INFO</CODE>,
\r
364 <CODE>PATH_TRANSLATED</CODE>, and <CODE>CONTENT_LENGTH</CODE> variables. <A NAME="9484"></A>
\r
367 In Filter FastCGI applications, the Web server sets two additional environment variables:
\r
370 <A NAME="9486"></A>
\r
373 <CODE>FILE_LAST_MOD</CODE>: The Web server sets <CODE>FILE_LAST_MOD</CODE> to the date and time that
\r
374 filter input file was last modified. The format is the number of seconds since midnight (UTC),
\r
375 January 1, 1970. <A NAME="9488"></A>
\r
378 <CODE>FCGI_DATA_LENGTH</CODE>: The application reads at most <CODE>FCGI_DATA_LENGTH</CODE> bytes from
\r
379 the data stream before receiving the end-of-stream indication.
\r
382 <A NAME="9490"></A>
\r
385 FastCGI sets <CODE>FCGI_ROLE</CODE> for each request to <CODE>RESPONDER</CODE>, <CODE>AUTHORIZER</CODE>, or
\r
386 <CODE>FILTER</CODE>.
\r
390 Building FastCGI Applications in C
\r
392 <A NAME="9049"></A>
\r
394 The Software Development Toolkit that accompanies WebServer 2.0 contains two libraries, fcgi_stdio and
\r
395 fcgiapp, for building FastCGI applications in C.
\r
398 <A NAME="9723"></A> The fcgi_stdio library implements our philosophy of making FastCGI applications similar to
\r
399 CGI applications, and provides full binary compatibility between FastCGI applications and CGI applications:
\r
400 you can run the same C binary as either CGI or FastCGI.
\r
403 <A NAME="9545"></A> The fcgiapp library is more specific to FastCGI, and doesn't attempt the veneer of
\r
407 <A NAME="9731"></A> We recommend that you use the fcgi_stdio library, and this manual describes the routines
\r
408 in that library. The documentation for the fcgiapp library is in the code in the development kit.
\r
413 Building FastCGI Applications in Perl
\r
415 <A NAME="9581"></A>
\r
417 To build FastCGI applications in Perl, you need a FastCGI-savvy version of Perl, plus the FastCGI extension to
\r
418 Perl. We build FastCGI-savvy versions of the Perl interpreter for several common platforms and make them
\r
419 available on our Website. For details and examples, see Chapter <A HREF="ch3perl.htm#3659">3, "Developing
\r
420 FastCGI Applications in Perl," on page 17</A>.
\r
425 Building FastCGI Applications in Tcl
\r
427 <A NAME="9586"></A>
\r
429 To build FastCGI applications in Tcl, you need a FastCGI-savvy version of Tcl. We build FastCGI-savvy versions
\r
430 of the Tcl interpreter for several common platforms and make them available on our Website. For details and
\r
431 examples, see Chapter <A HREF="ch4tcl.htm#3659">4, "Developing FastCGI Applications in Tcl," on page
\r
437 Implementation Details
\r
439 <A NAME="8066"></A>
\r
441 The FastCGI application libraries are designed to shield you from the details of the FastCGI design. This
\r
442 section is designed for the curious reader who would like some low-level understanding. If you are not curious
\r
443 about the implementation, you can happily skip this section.
\r
446 <A NAME="8554"></A> As shown in the following figure, CGI applications use the three standard POSIX streams
\r
447 (<CODE>stdin</CODE>, <CODE>stdout</CODE>, and <CODE>stderr</CODE>), plus environment variables, to communicate
\r
448 with an HTTP server.
\r
451 <A NAME="8359"></A> <IMG ALT="error-file:TidyOut.log" SRC="ch1intra.gif">
\r
454 <A NAME="4295"></A>
\r
459 Figure 1: Flow of Data in CGI
\r
461 <A NAME="9001"></A>
\r
463 The fundamental difference between FastCGI and CGI is that FastCGI applications are long-lived, which means
\r
464 that the Web Server needs to rendezvous with a running application, rather than starting the application in
\r
465 order to explicitly communicate with it.
\r
468 <A NAME="9110"></A> The FastCGI implementation basically creates a bidirectional connection between two
\r
469 processes that have no relationship. FastCGI uses a single connection for all the data associated with an
\r
470 application -- stdin, stdout, stderr, and environment variables. The data on the connection is encapsulated
\r
471 using a FastCGI protocol that allows stdin and the environment variables to share the same half connection (on
\r
472 the way in) and stdout and stderr to share the half connection (on the way out).
\r
475 <A NAME="9020"></A> On the input side, the FastCGI application receives data on the connection, unpacks it to
\r
476 separate stdin from the environment variables and then invokes the application. On the output side, FastCGI
\r
477 wraps stdout and stderr with appropriate protocol headers, and sends the encapsulated data out to the server.
\r
480 <A NAME="9032"></A> Since a FastCGI application does not always run on the same node as the HTTP server, we
\r
481 support two implementations of the connection: a <EM>stream pipe</EM><A HREF="#9645"><SUP>1</SUP></A>, for
\r
482 communications on the same machine, and TCP streams, for communication when the client and the server are on
\r
483 different machines.
\r
486 <A NAME="8576"></A> <IMG ALT="error-file:TidyOut.log" SRC="ch1inta1.gif">
\r
491 Figure 2: Flow of Data in FastCGI when server and application are on different machines
\r
494 The fcgi_stdio Library: I/O Compatibility
\r
496 <A NAME="8977"></A>
\r
498 The implementation for I/O compatibility is that the library <CODE>fcgi_stdio.h</CODE> contains macros to
\r
499 translate the types and procedures defined in stdio.h into the appropriate FastCGI calls. For example,
\r
500 consider a FastCGI program written in C containing the following line of code:
\r
505 <A NAME="5877">fprintf(stdout, "<H2>Aerobic Juggling</H2>/n");
\r
508 <A NAME="9659"></A> <CODE>fcgi_stdio.h</CODE>
\r
510 header file contains the macro
\r
515 <A NAME="6403">#define fprintf FCGI_fprintf
\r
518 <A NAME="6402"></A>
\r
520 So the preprocessor translates the <CODE>fprintf</CODE> call into the following call:
\r
525 <A NAME="6411">FCGI_fprintf(stdout, "<H2>Aerobic Juggling</H2>/n");
\r
528 <A NAME="5888"></A> <CODE>FCGI_fprintf</CODE>
\r
530 takes the same arguments as <CODE>fprintf</CODE>.
\r
533 <A NAME="9664"></A> The implementation of FCGI_fprintf tests the file to see if it is a normal C stream or a
\r
534 FastCGI stream, and calls the appropriate implementation.
\r
537 <A NAME="6463"></A> The <CODE>fcgi_stdio.h</CODE> header file contains macros to translate calls to all ISO
\r
538 stdio.h routines (and all conventional Posix additions, such as <CODE>fileno</CODE>, <CODE>fdopen</CODE>,
\r
539 <CODE>popen</CODE>, and <CODE>pclose</CODE>) into their FastCGI equivalents.
\r
544 The fcgi_stdio Library: Binary compatibility
\r
546 <A NAME="9579"></A>
\r
548 The fcgi_stdio library provides full binary compatibility between FastCGI applications and CGI applications:
\r
549 you can run the same C binary as either CGI or FastCGI.
\r
552 <A NAME="9580"></A> The implementation is in FCGI_Accept: the FCGI_Accept function tests its environment to
\r
553 determine whether the application was invoked as a CGI program or an FastCGI program. If it was invoked as a
\r
554 CGI program, the request loop will satisfy a single client request and then exit, producing CGI behavior.
\r
557 <A NAME="8957"></A>
\r
563 <A HREF="cover.htm">[Top]</A> <A HREF="ap_guide.htm">[Prev]</A> <A HREF="ch2c.htm">[Next]</A> <A HREF=
\r
564 "ap_guida.htm">[Bottom]</A>
\r
567 <SUP>1</SUP><A NAME="9645"></A>
\r
569 UNIX Network Programming, W. Richard Stevens, 1990 Prentice-Hall, Section 7.9
\r
572 <!-- This file was created with Quadralay WebWorks Publisher 3.0.3 -->
\r
574 <!-- For more information on how this document, and how the rest of -->
\r
575 <!-- this server was created, email yourEmail@xyzcorp.com -->
\r
577 <!-- Last updated: 04/15/96 08:00:13 -->
\r