Add the .exe extension when appropriate to the clean target.
[catagits/fcgi2.git] / doc / fastcgi-prog-guide / ch1intro.htm
1 <html><head><title>FastCGI Programmer's Guide - Chapter 1, The Fast Common Gateway Interface</title></head>
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>
9 </a><a name="7982"></a>
10 The 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>
11 <a name="8373"></a>
12 FastCGI 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>
13 <a name="8485">
14 <h1> Advantages of FastCGI</h1>
15 </a><a name="8369"></a>
16 FastCGI extends and enhances the CGI model in several ways:<p>
17 <ul><a name="7832"></a>
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.
19 <a name="7995"></a>
20 <li>FastCGI enables applications to reside on remote systems (rather than having to reside on the same system as the Web server)
21 <a name="7997"></a>
22 <li>FastCGI enables additional flexibility in application functionality, with explicit support for applications that do client authentication and filtering of input.
23 </ul><a name="8396">
24 <h2> Long-lived Applications</h2>
25 </a><a name="8458"></a>
26 CGI 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>
27 <a name="8459"></a>
28 FastCGI 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>
29 <a name="5761"></a>
30 Long-lived applications have two important advantages over short-lived applications:<p>
31 <ul><a name="7138"></a>
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.
33 <a name="9204"></a>
34 <li>A long-lived application can cache information in memory between requests, allowing it to respond more quickly to later requests.
35 </ul><a name="8733"></a>
36 FastCGI 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>
37 <a name="8734"></a>
38 In 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>
39 <a name="8735"></a>
40 Another 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>
41 <a name="8736"></a>
42 We believe that FastCGI is the most general and flexible strategy for building long-lived Web applications.<p>
43 <a name="8445">
44 <h2> Separating Application and Server</h2>
45 </a><a name="8446"></a>
46 CGI 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>
47 <a name="8406">
48 <h2> FastCGI "Roles"</h2>
49 </a><a name="8777"></a>
50 CGI 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>
51 <a name="8769"></a>
52 The three roles supported by the WebServer 2.0 are: <p>
53 <ul><a name="8409"></a>
54 <li>Responder
55 <a name="8410"></a>
56 <li>Filter
57 <a name="8411"></a>
58 <li>Authorizer
59 </ul><a name="8412">
60 <h3> Responder Applications</h3>
61 </a><a name="8679"></a>
62 A <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>
63 <a name="8680">
64 <h3> Filter Applications</h3>
65 </a><a name="8681"></a>
66 A <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>
67 <a name="8421"></a>
68 With 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>
69 <a name="8422"></a>
70 For 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>
71 <pre><a name="8423">
72 /www.aerjug.com/docs/chap1.sgml
73 </a>
74 </pre><a name="8424"></a>
75 Given 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>
76 <a name="8425">
77 <h3> Authorizer Applications</h3>
78 </a><a name="8426"></a>
79 An <em>authorizer</em> FastCGI application receives the information in an HTTP request header and generates a decision whether to authorize the request.<p>
80 <a name="8428"></a>
81 To 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>
82 <a name="8429"></a>
83 When 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>
84 <a name="8431"></a>
85 Authorizer 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>
86 <pre><a name="8432">
87 Variable-<em>name</em>: <em>value</em>
88 </a>
89 </pre><a name="8433"></a>
90 For example, the following header<p>
91 <pre><a name="8434">
92 Variable-AUTH_METHOD: database lookup
93 </a>
94 </pre><a name="8435"></a>
95 causes 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>
96 <a name="8437"></a>
97 Authorizer applications cannot successfully read from standard input. Any attempts to read from standard input result in an immediate EOF.<p>
98 <a name="8438"></a>
99 All data that authorizer applications write to standard error will get written to the traditional server error logs. <p>
100 <a name="4207">
101 <h1> Writing FastCGI Applications</h1>
102 </a><a name="9301"></a>
103 The 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>
104 <a name="9443"></a>
105 <p>
106 <a name="9450"></a>
107 In 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>
108 <a name="9469">
109 <h2> Code Structure</h2>
110 </a><a name="9470"></a>
111 The 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>
112 <pre><a name="9471">
113 Initialization code
114 </a>
115 <a name="9472">
116 Start of response loop
117 </a>
118 <a name="9473">
119    body of response loop
120 </a>
121 <a name="9474">
122 End of response loop
123 </a>
124 </pre><a name="9475"></a>
125 The <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>
126 <a name="9477"></a>
127 The <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>
128 <a name="9480">
129 <h2> Initial Environment Variables</h2>
130 </a><a name="9786"></a>
131 When a FastCGI process starts up, it has not yet accepted a request, and therefore none of the CGI environment variables are set.<p>
132 <a name="9787"></a>
133 You 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>
134 <a name="9829"></a>
135 In 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>
136 <a name="9785">
137 <h2> Per-Request Environment Variables</h2>
138 </a><a name="9481"></a>
139 In 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>
140 <ul><a name="9483"></a>
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.
142 <a name="9484"></a>
143 <li>In Filter FastCGI applications, the Web server sets two additional environment variables:
144 <ul>
145 <a name="9486"></a>
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. 
147 <a name="9488"></a>
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.
149 </ul>
150 <a name="9490"></a>
151 <li>FastCGI sets <code>FCGI_ROLE</code> for each request to <code>RESPONDER</code>, <code>AUTHORIZER</code>, or <code>FILTER</code>.
152 </ul><a name="9048">
153 <h2> Building FastCGI Applications in C</h2>
154 </a><a name="9049"></a>
155 The Software Development Toolkit that accompanies WebServer 2.0 contains two libraries, fcgi_stdio and fcgiapp, for building FastCGI applications in C. <p>
156 <a name="9723"></a>
157 The 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>
158 <a name="9545"></a>
159 The fcgiapp library is more specific to FastCGI, and doesn't attempt the veneer of CGI. <p>
160 <a name="9731"></a>
161 We 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>
162 <a name="9570">
163 <h2> Building FastCGI Applications in Perl</h2>
164 </a><a name="9581"></a>
165 To 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>
166 <a name="9562">
167 <h2> Building FastCGI Applications in Tcl</h2>
168 </a><a name="9586"></a>
169 To 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>
170 <a name="8360">
171 <h1> Implementation Details</h1>
172 </a><a name="8066"></a>
173 The 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>
174 <a name="8554"></a>
175 As 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>
176 <a name="8359"></a>
177 <img src="ch1intra.gif"><p>
178 <a name="4295"></a>
179 <p>
180 <a name="8575">
181 <h5>Figure 1:&#32; Flow of Data in CGI</h5>
182 </a><a name="9001"></a>
183 The 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>
184 <a name="9110"></a>
185 The 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>
186 <a name="9020"></a>
187 On 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>
188 <a name="9032"></a>
189 Since 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>
190 <a name="8576"></a>
191 <img src="ch1inta1.gif"><p>
192 <a name="7549">
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>
196 </a><a name="8977"></a>
197 The 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>
198 <pre><a name="5877">
199 fprintf(stdout, "&lt;H2&gt;Aerobic Juggling&lt;/H2&gt;/n");
200 </a>
201 </pre><a name="9659"></a>
202 <code>fcgi_stdio.h</code> header file contains the macro<p>
203 <pre><a name="6403">
204 #define fprintf FCGI_fprintf
205 </a>
206 </pre><a name="6402"></a>
207 So the preprocessor translates the <code>fprintf</code> call into the following call:<p>
208 <pre><a name="6411">
209 FCGI_fprintf(stdout, "&lt;H2&gt;Aerobic Juggling&lt;/H2&gt;/n");
210 </a>
211 </pre><a name="5888"></a>
212 <code>FCGI_fprintf</code> takes the same arguments as <code>fprintf</code>. <p>
213 <a name="9664"></a>
214 The 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>
215 <a name="6463"></a>
216 The <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>
217 <a name="9678">
218 <h2> The fcgi_stdio Library: Binary compatibility</h2>
219 </a><a name="9579"></a>
220 The 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>
221 <a name="9580"></a>
222 The 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>
223 <a name="8957"></a>
224 <p>
225
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  
231 <sup>1</sup><a name="9645"></a>
232 UNIX Network Programming, W. Richard Stevens, 1990 Prentice-Hall, Section 7.9<p>
233
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>