Add the .exe extension when appropriate to the clean target.
[catagits/fcgi2.git] / doc / fastcgi-prog-guide / ch1intro.htm
CommitLineData
6791223e 1<html><head><title>FastCGI Programmer's Guide - Chapter 1, The Fast Common Gateway Interface</title></head>
0198fd3c 2<body bgcolor=#ffffff>
3
4<a href="cover.htm">[Top]</a> <a href="ap_guide.htm">[Prev]</a> <a href="ch2c.htm">[Next]</a> <a href="ap_guida.htm">[Bottom]</a>
5<hr><br>
6
7<a name="9432">
8<center><h1>1 The Fast Common<br>Gateway Interface</h1></center>
6791223e 9</a><a name="7982"></a>
0198fd3c 10The Fast Common Gateway Interface (FastCGI) is an enhancement to the existing CGI (Common Gateway Interface), which is a standard for interfacing external applications with Web servers. <p>
6791223e 11<a name="8373"></a>
0198fd3c 12FastCGI is a proposed open standard and we expect both free and commercial Web servers to support it. FastCGI is included in Open Market WebServer and Secure WebServer, versions 2.0 and greater.<p>
6791223e 13<a name="8485">
0198fd3c 14<h1> Advantages of FastCGI</h1>
6791223e 15</a><a name="8369"></a>
0198fd3c 16FastCGI extends and enhances the CGI model in several ways:<p>
6791223e 17<ul><a name="7832"></a>
0198fd3c 18<li>FastCGI enables applications to persist between client requests, eliminating application start up overhead and allowing the application to maintain state between client calls.
6791223e 19<a name="7995"></a>
0198fd3c 20<li>FastCGI enables applications to reside on remote systems (rather than having to reside on the same system as the Web server)
6791223e 21<a name="7997"></a>
0198fd3c 22<li>FastCGI enables additional flexibility in application functionality, with explicit support for applications that do client authentication and filtering of input.
6791223e 23</ul><a name="8396">
0198fd3c 24<h2> Long-lived Applications</h2>
6791223e 25</a><a name="8458"></a>
0198fd3c 26CGI applications are ephemeral and short-lived: each time a client requests a CGI application, the server asks the operating system to spawn a new CGI process. After the CGI process satisfies the request, the server kills it. The server spawns and subsequently kills a new process for each client request. <p>
6791223e 27<a name="8459"></a>
0198fd3c 28FastCGI applications are long-lived, and can persist between client calls. The server spawns the FastCGI process once and it continues to run and satisfy client requests until it is explicitly terminated. You can also ask the Web server to start multiple copies of a FastCGI application, if you expect that concurrent processing will improve the application's performance. <p>
6791223e 29<a name="5761"></a>
0198fd3c 30Long-lived applications have two important advantages over short-lived applications:<p>
6791223e 31<ul><a name="7138"></a>
0198fd3c 32<li>A short-lived application pays start up overhead on every request; a long-lived application spreads the overhead over many requests. For an application that has a heavy start up cost, such as opening a database, doing initialization on every call can be very inefficient. Reinitializing for every client is also very inefficient for Perl programs, where the interpreter reads through the entire program before executing any of it.
6791223e 33<a name="9204"></a>
0198fd3c 34<li>A long-lived application can cache information in memory between requests, allowing it to respond more quickly to later requests.
6791223e 35</ul><a name="8733"></a>
0198fd3c 36FastCGI is not the only way to get a long-lived application on the Web, however. For example, there are many existing search engines that are implemented as long-lived applications.<p>
6791223e 37<a name="8734"></a>
0198fd3c 38In most cases, these applications rely on customized Web servers. In other words, since most Web servers do not support long-lived applications, a programmer must code this support into a Web server. This approach requires a tremendous amount of work and also ties the application to a particular server.<p>
6791223e 39<a name="8735"></a>
0198fd3c 40Another way to get a long-lived application is to write code that calls routines from the Web server's API. This alternative involves a lot of extra coding, ties the application to a particular Web server, and introduces problems of maintainability, scalability, and security.<p>
6791223e 41<a name="8736"></a>
0198fd3c 42We believe that FastCGI is the most general and flexible strategy for building long-lived Web applications.<p>
6791223e 43<a name="8445">
0198fd3c 44<h2> Separating Application and Server</h2>
6791223e 45</a><a name="8446"></a>
0198fd3c 46CGI applications must run on the same node as the Web server; FastCGI applications can run on any node that can be reached from your Web server using TCP/IP protocols. For example, you might want to run the FastCGI application on a high-speed computer server or database engine, and run the Web server on a different node.<p>
6791223e 47<a name="8406">
0198fd3c 48<h2> FastCGI "Roles"</h2>
6791223e 49</a><a name="8777"></a>
0198fd3c 50CGI and FastCGI applications are effective ways to allow an application to act as an extension to the Web server. CGI provides no explicit support for different kinds of applications: under CGI, every application receives an HTTP request, does something with it, and generates an HTTP response. FastCGI provides explicit support for several common "roles" that applications can play. <p>
6791223e 51<a name="8769"></a>
0198fd3c 52The three roles supported by the WebServer 2.0 are: <p>
6791223e 53<ul><a name="8409"></a>
0198fd3c 54<li>Responder
6791223e 55<a name="8410"></a>
0198fd3c 56<li>Filter
6791223e 57<a name="8411"></a>
0198fd3c 58<li>Authorizer
6791223e 59</ul><a name="8412">
0198fd3c 60<h3> Responder Applications</h3>
6791223e 61</a><a name="8679"></a>
0198fd3c 62A <em>responder</em> application is the most basic kind of FastCGI application: it receives the information associated with an HTTP request and generates an HTTP response. Responder is the role most similar to traditional CGI programming, and most FastCGI applications are responders.<p>
6791223e 63<a name="8680">
0198fd3c 64<h3> Filter Applications</h3>
6791223e 65</a><a name="8681"></a>
0198fd3c 66A <em>filter</em> FastCGI application receives the information associated with an HTTP request, plus an extra stream of data from a file stored on the Web server, and generates a "filtered" version of the data stream as an HTTP response. <p>
6791223e 67<a name="8421"></a>
0198fd3c 68With filter applications, the system administrator maps a particular MIME-type to a particular filter FastCGI application. When a client requests a URL with that MIME-type, the Web server invokes the filter application, which processes the file at the specified URL and sends a response (usually HTML text) back to the client.<p>
6791223e 69<a name="8422"></a>
0198fd3c 70For example, suppose you write a filter FastCGI application that converts SGML text to HTML, and map the extension .sgml (MIME-type SGML) to your filter FastCGI application. Now, suppose that a user requests the following URL:<p>
6791223e 71<pre><a name="8423">
0198fd3c 72/www.aerjug.com/docs/chap1.sgml
73</a>
6791223e 74</pre><a name="8424"></a>
0198fd3c 75Given this URL, the Web server passes <code>chap1.sgml</code> as input to your filter FastCGI application, which processes <code>chap1.sgml</code> and returns an HTML version of it to the requesting client.<p>
6791223e 76<a name="8425">
0198fd3c 77<h3> Authorizer Applications</h3>
6791223e 78</a><a name="8426"></a>
0198fd3c 79An <em>authorizer</em> FastCGI application receives the information in an HTTP request header and generates a decision whether to authorize the request.<p>
6791223e 80<a name="8428"></a>
0198fd3c 81To mark a FastCGI application as having the authorizer role, the system administrator names the application inside the server configuration file, using a directive called <code>AuthorizeRegion</code>. (See the Open Market Web Server manual for information on server configuration directives.)<p>
6791223e 82<a name="8429"></a>
0198fd3c 83When a client requests a URL that meets the <code>AuthorizeRegion </code>criteria, the Web server calls your authorizer FastCGI application. If your application grants authorization (by returning a response code of 200), the Web server resumes execution of commands in the <code>AuthorizeRegion</code> section. If your application denies authorization (by returning any other response code), the Web server stops processing subsequent commands in the <code>AuthorizeRegion</code> section, and returns the response from your FastCGI application to the client.<p>
6791223e 84<a name="8431"></a>
0198fd3c 85Authorizer applications can return headers containing environment variables. Other CGI or FastCGI programs accessing this request (including other authorizers) can access these environment variables. The headers must have the following format:<p>
6791223e 86<pre><a name="8432">
0198fd3c 87Variable-<em>name</em>: <em>value</em>
88</a>
6791223e 89</pre><a name="8433"></a>
0198fd3c 90For example, the following header<p>
6791223e 91<pre><a name="8434">
0198fd3c 92Variable-AUTH_METHOD: database lookup
93</a>
6791223e 94</pre><a name="8435"></a>
0198fd3c 95causes the environment variable <code>AUTH_METHOD</code> to be set to <code>"database lookup"</code> for this request. Other CGI or FastCGI applications running on this request can access the value of <code>AUTH_METHOD</code>. <p>
6791223e 96<a name="8437"></a>
0198fd3c 97Authorizer applications cannot successfully read from standard input. Any attempts to read from standard input result in an immediate EOF.<p>
6791223e 98<a name="8438"></a>
0198fd3c 99All data that authorizer applications write to standard error will get written to the traditional server error logs. <p>
6791223e 100<a name="4207">
0198fd3c 101<h1> Writing FastCGI Applications</h1>
6791223e 102</a><a name="9301"></a>
0198fd3c 103The work involved in writing a FastCGI application depends in large part on the I/O libraries that you use. This manual describes how to write FastCGI applications in terms of the Open Market libraries, which are available for C, Perl, and Tcl. FastCGI is an open standard and you are welcome to build your own libraries for other languages as well, but this manual focuses on building FastCGI applications in the context of the Open Market libraries.<p>
6791223e 104<a name="9443"></a>
0198fd3c 105<p>
6791223e 106<a name="9450"></a>
0198fd3c 107In general, the goal of the libraries is to make the job of writing a FastCGI application as much like writing a CGI application as possible. For example, you use the same techniques for query string decoding, HTML output to stdout, use of environment variables, and so on. When you use our libraries, porting CGI applications to FastCGI is mostly a matter of restructuring the code to take advantage of FastCGI features and libraries. <p>
6791223e 108<a name="9469">
0198fd3c 109<h2> Code Structure</h2>
6791223e 110</a><a name="9470"></a>
0198fd3c 111The main task of converting a CGI program into a FastCGI program is separating the initialization code from the code that needs to run for each request. The structure should look something like this:<p>
6791223e 112<pre><a name="9471">
0198fd3c 113Initialization code
114</a>
115<a name="9472">
116Start of response loop
117</a>
118<a name="9473">
119 body of response loop
120</a>
121<a name="9474">
122End of response loop
123</a>
6791223e 124</pre><a name="9475"></a>
0198fd3c 125The <em>initialization code</em> is run exactly once, when the application is initialized. Initialization code usually performs time-consuming operations such as opening databases or calculating values for tables or bitmaps. <p>
6791223e 126<a name="9477"></a>
0198fd3c 127The <em>response loop</em> runs continuously, waiting for client requests to arrive. The loop starts with a call to <code>FCGI_Accept</code>, a routine in the FastCGI library. The <code>FCGI_Accept</code> routine blocks program execution until a client requests the FastCGI application. When a client request comes in, <code>FCGI_Accept</code> unblocks, runs one iteration of the response loop body, and then blocks again waiting for another client request. The loop terminates only when the system administrator or the Web server kills the FastCGI application.<p>
6791223e 128<a name="9480">
0198fd3c 129<h2> Initial Environment Variables</h2>
6791223e 130</a><a name="9786"></a>
0198fd3c 131When a FastCGI process starts up, it has not yet accepted a request, and therefore none of the CGI environment variables are set.<p>
6791223e 132<a name="9787"></a>
0198fd3c 133You set the initial environment of a FastCGI process started by the <code>AppClass </code>directive using the <code>-initial-env</code> option. The process would use this environment to configure its options and locate files or databases.<p>
6791223e 134<a name="9829"></a>
0198fd3c 135In FastCGI processes started by the <code>AppClass</code> directive with the -affinity option, the <code>FCGI_PROCESS_ID</code> variable is set in the initial environment (not in the environment of a request). <code>FCGI_PROCESS_ID</code> is a decimal number in the range 0 to N - 1 where N is the number of processes (argument to the<code> -processes</code> option to <code>AppClass</code>). The process would use <code>FCGI_PROCESS_ID </code>in conjunction with other variables to locate session-related files or databases during restart.<p>
6791223e 136<a name="9785">
0198fd3c 137<h2> Per-Request Environment Variables</h2>
6791223e 138</a><a name="9481"></a>
0198fd3c 139In general, FastCGI uses the same per-request environment variables as CGI, and you access the values of environment variables in FastCGI applications just as you would in CGI applications. The only differences are as follows:<p>
6791223e 140<ul><a name="9483"></a>
0198fd3c 141<li>In Authorizer FastCGI applications, the Web server unsets the <code>PATH_INFO</code>, <code>PATH_TRANSLATED</code>, and <code>CONTENT_LENGTH</code> variables.
6791223e 142<a name="9484"></a>
0198fd3c 143<li>In Filter FastCGI applications, the Web server sets two additional environment variables:
6791223e 144<ul>
145<a name="9486"></a>
0198fd3c 146<li><code>FILE_LAST_MOD</code>: The Web server sets <code>FILE_LAST_MOD</code> to the date and time that filter input file was last modified. The format is the number of seconds since midnight (UTC), January 1, 1970.
6791223e 147<a name="9488"></a>
0198fd3c 148<li><code>FCGI_DATA_LENGTH</code>: The application reads at most <code>FCGI_DATA_LENGTH</code> bytes from the data stream before receiving the end-of-stream indication.
6791223e 149</ul>
150<a name="9490"></a>
0198fd3c 151<li>FastCGI sets <code>FCGI_ROLE</code> for each request to <code>RESPONDER</code>, <code>AUTHORIZER</code>, or <code>FILTER</code>.
6791223e 152</ul><a name="9048">
0198fd3c 153<h2> Building FastCGI Applications in C</h2>
6791223e 154</a><a name="9049"></a>
0198fd3c 155The Software Development Toolkit that accompanies WebServer 2.0 contains two libraries, fcgi_stdio and fcgiapp, for building FastCGI applications in C. <p>
6791223e 156<a name="9723"></a>
0198fd3c 157The fcgi_stdio library implements our philosophy of making FastCGI applications similar to CGI applications, and provides full binary compatibility between FastCGI applications and CGI applications: you can run the same C binary as either CGI or FastCGI. <p>
6791223e 158<a name="9545"></a>
0198fd3c 159The fcgiapp library is more specific to FastCGI, and doesn't attempt the veneer of CGI. <p>
6791223e 160<a name="9731"></a>
0198fd3c 161We recommend that you use the fcgi_stdio library, and this manual describes the routines in that library. The documentation for the fcgiapp library is in the code in the development kit.<p>
6791223e 162<a name="9570">
0198fd3c 163<h2> Building FastCGI Applications in Perl</h2>
6791223e 164</a><a name="9581"></a>
0198fd3c 165To build FastCGI applications in Perl, you need a FastCGI-savvy version of Perl, plus the FastCGI extension to Perl. We build FastCGI-savvy versions of the Perl interpreter for several common platforms and make them available on our Website. For details and examples, see Chapter <a href="ch3perl.htm#3659">3, "Developing FastCGI Applications in Perl," on page 17</a>.<p>
6791223e 166<a name="9562">
0198fd3c 167<h2> Building FastCGI Applications in Tcl</h2>
6791223e 168</a><a name="9586"></a>
0198fd3c 169To build FastCGI applications in Tcl, you need a FastCGI-savvy version of Tcl. We build FastCGI-savvy versions of the Tcl interpreter for several common platforms and make them available on our Website. For details and examples, see Chapter <a href="ch4tcl.htm#3659">4, "Developing FastCGI Applications in Tcl," on page 19</a>.<p>
6791223e 170<a name="8360">
0198fd3c 171<h1> Implementation Details</h1>
6791223e 172</a><a name="8066"></a>
0198fd3c 173The FastCGI application libraries are designed to shield you from the details of the FastCGI design. This section is designed for the curious reader who would like some low-level understanding. If you are not curious about the implementation, you can happily skip this section.<p>
6791223e 174<a name="8554"></a>
0198fd3c 175As shown in the following figure, CGI applications use the three standard POSIX streams (<code>stdin</code>, <code>stdout</code>, and <code>stderr</code>), plus environment variables, to communicate with an HTTP server. <p>
6791223e 176<a name="8359"></a>
0198fd3c 177<img src="ch1intra.gif"><p>
6791223e 178<a name="4295"></a>
0198fd3c 179<p>
6791223e 180<a name="8575">
0198fd3c 181<h5>Figure 1:&#32; Flow of Data in CGI</h5>
6791223e 182</a><a name="9001"></a>
0198fd3c 183The fundamental difference between FastCGI and CGI is that FastCGI applications are long-lived, which means that the Web Server needs to rendezvous with a running application, rather than starting the application in order to explicitly communicate with it.<p>
6791223e 184<a name="9110"></a>
0198fd3c 185The FastCGI implementation basically creates a bidirectional connection between two processes that have no relationship. FastCGI uses a single connection for all the data associated with an application -- stdin, stdout, stderr, and environment variables. The data on the connection is encapsulated using a FastCGI protocol that allows stdin and the environment variables to share the same half connection (on the way in) and stdout and stderr to share the half connection (on the way out).<p>
6791223e 186<a name="9020"></a>
0198fd3c 187On the input side, the FastCGI application receives data on the connection, unpacks it to separate stdin from the environment variables and then invokes the application. On the output side, FastCGI wraps stdout and stderr with appropriate protocol headers, and sends the encapsulated data out to the server.<p>
6791223e 188<a name="9032"></a>
0198fd3c 189Since a FastCGI application does not always run on the same node as the HTTP server, we support two implementations of the connection: a <em>stream pipe</em><a href="#9645"><sup>1</sup></a>, for communications on the same machine, and TCP streams, for communication when the client and the server are on different machines.<p>
6791223e 190<a name="8576"></a>
0198fd3c 191<img src="ch1inta1.gif"><p>
6791223e 192<a name="7549">
0198fd3c 193<h5>Figure 2:&#32; Flow of Data in FastCGI when server and application are on different machines</h5>
194</a><a name="7874">
195<h2> The fcgi_stdio Library: I/O Compatibility</h2>
6791223e 196</a><a name="8977"></a>
0198fd3c 197The implementation for I/O compatibility is that the library <code>fcgi_stdio.h</code> contains macros to translate the types and procedures defined in stdio.h into the appropriate FastCGI calls. For example, consider a FastCGI program written in C containing the following line of code:<p>
6791223e 198<pre><a name="5877">
0198fd3c 199fprintf(stdout, "&lt;H2&gt;Aerobic Juggling&lt;/H2&gt;/n");
200</a>
6791223e 201</pre><a name="9659"></a>
0198fd3c 202<code>fcgi_stdio.h</code> header file contains the macro<p>
6791223e 203<pre><a name="6403">
0198fd3c 204#define fprintf FCGI_fprintf
205</a>
6791223e 206</pre><a name="6402"></a>
0198fd3c 207So the preprocessor translates the <code>fprintf</code> call into the following call:<p>
6791223e 208<pre><a name="6411">
0198fd3c 209FCGI_fprintf(stdout, "&lt;H2&gt;Aerobic Juggling&lt;/H2&gt;/n");
210</a>
6791223e 211</pre><a name="5888"></a>
0198fd3c 212<code>FCGI_fprintf</code> takes the same arguments as <code>fprintf</code>. <p>
6791223e 213<a name="9664"></a>
0198fd3c 214The implementation of FCGI_fprintf tests the file to see if it is a normal C stream or a FastCGI stream, and calls the appropriate implementation.<p>
6791223e 215<a name="6463"></a>
0198fd3c 216The <code>fcgi_stdio.h</code> header file contains macros to translate calls to all ISO stdio.h routines (and all conventional Posix additions, such as <code>fileno</code>, <code>fdopen</code>, <code>popen</code>, and <code>pclose</code>) into their FastCGI equivalents. <p>
6791223e 217<a name="9678">
0198fd3c 218<h2> The fcgi_stdio Library: Binary compatibility</h2>
6791223e 219</a><a name="9579"></a>
0198fd3c 220The fcgi_stdio library provides full binary compatibility between FastCGI applications and CGI applications: you can run the same C binary as either CGI or FastCGI. <p>
6791223e 221<a name="9580"></a>
0198fd3c 222The implementation is in FCGI_Accept: the FCGI_Accept function tests its environment to determine whether the application was invoked as a CGI program or an FastCGI program. If it was invoked as a CGI program, the request loop will satisfy a single client request and then exit, producing CGI behavior.<p>
6791223e 223<a name="8957"></a>
0198fd3c 224<p>
6791223e 225
0198fd3c 226<hr><br>
227
228<a href="cover.htm">[Top]</a> <a href="ap_guide.htm">[Prev]</a> <a href="ch2c.htm">[Next]</a> <a href="ap_guida.htm">[Bottom]</a>
229<hr><br>
230
6791223e 231<sup>1</sup><a name="9645"></a>
0198fd3c 232UNIX Network Programming, W. Richard Stevens, 1990 Prentice-Hall, Section 7.9<p>
6791223e 233
0198fd3c 234
235<!-- This file was created with Quadralay WebWorks Publisher 3.0.3 -->
236<!-- -->
237<!-- For more information on how this document, and how the rest of -->
238<!-- this server was created, email yourEmail@xyzcorp.com -->
239<!-- -->
240<!-- Last updated: 04/15/96 08:00:13 -->
241
242</body>
243</html>