fix validation, organization, and links
[catagits/fcgi2.git] / doc / fastcgi-whitepaper / fastcgi.htm
1 <HTML>\r
2    <HEAD>\r
3       <TITLE>\r
4          FastCGI\r
5       </TITLE>\r
6       <META NAME="GENERATOR" CONTENT="Internet Assistant for Microsoft Word 2.0z">\r
7 <STYLE TYPE="text/css">\r
8  div.c5 {font-family: Arial Narrow; font-size: 80%; text-align: center}\r
9  span.c4 {font-family: Courier}\r
10  tt.c3 {font-family: Courier}\r
11  div.c2 {text-align: center}\r
12  div.c1 {color: #FFFFFF; font-family: Impact; font-size: 120%; text-align: center}\r
13 </STYLE>\r
14    </HEAD>\r
15    <!--Copyright (c) 1996 Open Market, Inc.                                    -->\r
16    <!--See the file "LICENSE.TERMS" for information on usage and redistribution-->\r
17    <!--of this file, and for a DISCLAIMER OF ALL WARRANTIES.                   -->\r
18    <!-- $Id: fastcgi.htm,v 1.3 2001/11/27 01:13:29 robs Exp $ -->\r
19    <BODY>\r
20       <P>\r
21          Open Market, Inc.\r
22       </P>\r
23       <P>\r
24       </P>\r
25       <DIV CLASS="c1">\r
26          Technical White Paper<BR>\r
27       </DIV>\r
28       <H1>\r
29          FastCGI:<BR>\r
30           A High-Performance Web Server Interface\r
31       </H1>\r
32       <P>\r
33          April 1996<!--Please send comments to:-->\r
34       </P>\r
35       <HR>\r
36       <!-- payne@openmarket.com-->\r
37       <H2>\r
38          1. Introduction\r
39       </H2>\r
40       <P>\r
41          The surge in the use of the Web by business has created a tremendous need for server extension applications\r
42          that create dynamic content. These are the applications that will allow businesses to deliver products,\r
43          services, and messages whose shape and content are in part determined by the interaction with, and knowledge\r
44          of, the customers to which they are delivered.\r
45       </P>\r
46       <P>\r
47          This important movement away from static Web content is pushing the limits and exposing the weaknesses of the\r
48          environment in which these applications are currently bound: CGI (Common Gateway Interface). Most importantly\r
49          it does not offer the performance these applications require. A new communication infrastructure is needed to\r
50          connect Web servers with these new applications. This is what led Open Market to develop FastCGI.\r
51       </P>\r
52       <P>\r
53          FastCGI is a fast, open, and secure Web server interface that solves the performance problems inherent in CGI,\r
54          without introducing the overhead and complexity of proprietary APIs (Application Programming Interfaces).\r
55       </P>\r
56       <P>\r
57          This paper assumes that the reader has basic familiarity with Web technology and developing Web applications.\r
58       </P>\r
59       <H3>\r
60          Common Gateway Interface\r
61       </H3>\r
62       <P>\r
63          The de facto standard interface for Web server applications is CGI, which was first implemented in the NCSA\r
64          server. CGI has many benefits:\r
65       </P>\r
66       <UL>\r
67          <LI>\r
68             <B>Simplicity.</B> <SPAN>It is easy to understand.</SPAN>\r
69          </LI>\r
70          <LI>\r
71             <B>Language independence.</B> CGI applications can be written in nearly any language.\r
72          </LI>\r
73          <LI>\r
74             <B>Process isolation.</B> Since applications run in separate processes, buggy applications cannot crash the\r
75             Web server or access the server&#39;s private internal state.\r
76          </LI>\r
77          <LI>\r
78             <B>Open standard.</B> Some form of CGI has been implemented on every Web server.\r
79          </LI>\r
80          <LI>\r
81             <B>Architecture independence.</B> CGI is not tied to any particular server architecture (single threaded,\r
82             multi-threaded, etc.).\r
83          </LI>\r
84       </UL>\r
85       <P>\r
86          CGI also has some significant drawbacks. The leading problem is performance: Since a new process is created\r
87          for each request and thrown away when the request is done, efficiency is poor.\r
88       </P>\r
89       <P>\r
90          CGI also has limited functionality: It only supports a simple &quot;responder&quot; role, where the\r
91          application generates the response that is returned to the client. CGI programs can&#39;t link into other\r
92          stages of Web server request processing, such as authorization and logging.\r
93       </P>\r
94       <H3>\r
95          Server APIs\r
96       </H3>\r
97       <P>\r
98          In response to the performance problems for CGI, several vendors have developed APIs for their servers. The\r
99          two most notable are NSAPI from Netscape and ISAPI from Microsoft. The freely available Apache server also has\r
100          an API.\r
101       </P>\r
102       <P>\r
103          Applications linked into the server API may be significantly faster than CGI programs. The CGI\r
104          startup/initialization problem is improved, because the application runs in the server process and is\r
105          persistent across requests. Web server APIs also offer more functionality than CGI: you can write extensions\r
106          that perform access control, get access to the server&#39;s log file, and link in to other stages in the\r
107          server&#39;s request processing.\r
108       </P>\r
109       <P>\r
110          However, APIs sacrifice all of CGI&#39;s benefits. Vendor APIs have the following problems:\r
111       </P>\r
112       <UL>\r
113          <LI>\r
114             <B>Complexity.</B> Vendor APIs introduce a steep learning curve, with increased implementation and\r
115             maintenance costs.\r
116          </LI>\r
117          <LI>\r
118             <B>Language dependence.</B> Applications have to be written in a language supported by the vendor API\r
119             (usually C/C++). Perl, the most popular language for CGI programs, can&#39;t be used with any existing\r
120             vendor API.\r
121          </LI>\r
122          <LI>\r
123             <B>No process isolation.</B> <SPAN>Since the applications run in the server&#39;s address space, buggy\r
124             applications can corrupt the core server (or each other). A malicious or buggy application can compromise\r
125             server security, and bugs in the core server can corrupt applications.</SPAN>\r
126          </LI>\r
127          <LI>\r
128             <B>Proprietary.</B> Coding your application to a particular API locks you into a particular vendor&#39;s\r
129             server.\r
130          </LI>\r
131          <LI>\r
132             <B>Tie-in to server architecture.</B> API applications have to share the same architecture as the server:\r
133             If the Web server is multi-threaded, the application has to be thread-safe. If the Web server has\r
134             single-threaded processes, multi-threaded applications don&#39;t gain any performance advantage. Also, when\r
135             the vendor changes the server&#39;s architecture, the API will usually have to change, and applications\r
136             will have to be adapted or rewritten.\r
137          </LI>\r
138       </UL>\r
139       <H3>\r
140          FastCGI\r
141       </H3>\r
142       <P>\r
143          The FastCGI interface combines the best aspects of CGI and vendor APIs. Like CGI, FastCGI applications run in\r
144          separate, isolated processes. FastCGI&#39;s advantages include:\r
145       </P>\r
146       <UL>\r
147          <LI>\r
148             <B>Performance.</B> FastCGI processes are persistent-they are reused to handle multiple requests. This\r
149             solves the CGI performance problem of creating new processes for each request.\r
150          </LI>\r
151          <LI>\r
152             <B>Simplicity, with easy migration from CGI.</B> The FastCGI application library (described on page 9)\r
153             simplifies the migration of existing CGI applications. Applications built with the application library can\r
154             also run as CGI programs, for backward compatibility with old Web servers.\r
155          </LI>\r
156          <LI>\r
157             <B>Language independence.</B> Like CGI, FastCGI applications can be written in any language, not just\r
158             languages supported by the vendor API.\r
159          </LI>\r
160          <LI>\r
161             <B>Process isolation.</B> A buggy FastCGI application cannot crash or corrupt the core server or other\r
162             applications. A malicious FastCGI application cannot steal any secrets (such as session keys for\r
163             encryption) from the Web server.\r
164          </LI>\r
165          <LI>\r
166             <B>Non-proprietary.</B> FastCGI is supported in all of Open Market&#39;s server products, and support is\r
167             under development for other Web servers, including the freely available Apache and NCSA servers, as well as\r
168             commercial servers from Microsoft and Netscape.\r
169          </LI>\r
170          <LI>\r
171             <B>Architecture independence.</B> The FastCGI interface is not tied to a particular server architecture.\r
172             Any Web server can implement the FastCGI interface. Also, FastCGI does not impose any architecture on the\r
173             application: applications can be single or multi-threaded, regardless of the threading architecture of the\r
174             Web server.\r
175          </LI>\r
176          <LI>\r
177             <B>Support for distributed computing.</B> FastCGI provides the ability to run applications remotely, which\r
178             is useful for distributing load and managing external Web sites.\r
179          </LI>\r
180       </UL>\r
181       <P>\r
182          The following sections describe the FastCGI interface, protocol, application library, and support in Open\r
183          Market&#39;s WebServer products.\r
184       </P>\r
185       <H2>\r
186          2. FastCGI Interface\r
187       </H2>\r
188       <P>\r
189          The functionality provided by the FastCGI interface is very similar to that provided by CGI. To best\r
190          understand the FastCGI protocol, we review the CGI interface here. Basic CGI request processing proceeds as\r
191          follows:\r
192       </P>\r
193       <OL>\r
194          <LI>\r
195             For each request, the server creates a new process and the process initializes itself.\r
196          </LI>\r
197          <LI>\r
198             The Web server passes the request information (such as remote host, username, HTTP headers, etc.) to the\r
199             CGI program in environment variables.\r
200          </LI>\r
201          <LI>\r
202             The Web server sends any client input (such as user-entered field values from an HTML form) to the CGI\r
203             program&#39;s standard input.\r
204          </LI>\r
205          <LI>\r
206             The CGI program writes any output to be returned to the client on standard output. Error information\r
207             written to standard error is logged by the Web server.\r
208          </LI>\r
209          <LI>\r
210             When the CGI process exits, the request is complete.\r
211          </LI>\r
212       </OL>\r
213       <P>\r
214          FastCGI is conceptually very similar to CGI, with two major differences:\r
215       </P>\r
216       <UL>\r
217          <LI>\r
218             FastCGI processes are persistent: after finishing a request, they wait for a new request instead of\r
219             exiting.\r
220          </LI>\r
221          <LI>\r
222             Instead of using operating system environment variables and pipes, the FastCGI protocol multiplexes the\r
223             environment information, standard input, output and error over a single full-duplex connection. This allows\r
224             FastCGI programs to run on remote machines, using TCP connections between the Web server and the FastCGI\r
225             application.\r
226          </LI>\r
227       </UL>\r
228       <P>\r
229          Request processing in a single-threaded FastCGI application proceeds as follows:\r
230       </P>\r
231       <OL>\r
232          <LI>\r
233             The Web server creates FastCGI application processes to handle requests. The processes may be created at\r
234             startup, or created on demand.\r
235          </LI>\r
236          <LI>\r
237             The FastCGI program initializes itself, and waits for a new connection from the Web server.\r
238          </LI>\r
239          <LI>\r
240             When a client request comes in, the Web server opens a connection to the FastCGI process. The server sends\r
241             the CGI environment variable information and standard input over the connection.\r
242          </LI>\r
243          <LI>\r
244             The FastCGI process sends the standard output and error information back to the server over the same\r
245             connection.\r
246          </LI>\r
247          <LI>\r
248             When the FastCGI process closes the connection, the request is complete. The FastCGI process then waits for\r
249             another connection from the Web server.\r
250          </LI>\r
251       </OL>\r
252       <P>\r
253          FastCGI applications can run locally (on the same machine as the Web server) or remotely. For local\r
254          applications, the server uses a full-duplex pipe to connect to the FastCGI application process. For remote\r
255          applications, the server uses a TCP connection.\r
256       </P>\r
257       <P>\r
258          FastCGI applications can be single-threaded or multi-threaded. For single threaded applications, the Web\r
259          server maintains a pool of processes (if the application is running locally) to handle client requests. The\r
260          size of the pool is user configurable. Multi-threaded FastCGI applications may accept multiple connections\r
261          from the Web server and handle them simultaneously in a single process. (For example, Java&#39;s built-in\r
262          multi-threading, garbage collection, synchronization primitives, and platform independence make it a natural\r
263          implementation language for multi-threaded FastCGI applications.)\r
264       </P>\r
265       <H3>\r
266          Remote FastCGI\r
267       </H3>\r
268       <P>\r
269          FastCGI&#39;s ability to run applications remotely (over a TCP connection) provides some major benefits. These\r
270          benefits are described in this section, along with some of the security issues that affect remote FastCGI\r
271          applications.\r
272       </P>\r
273       <H4>\r
274          FastCGI with Firewalls\r
275       </H4>\r
276       <P>\r
277          Applications that run on organizational (external) Web servers and depend on internal databases can be a\r
278          challenge to administer. Figure 1 shows a typical organization, with an external Web server, a firewall\r
279          restricting access to the internal network, and internal databases and applications.\r
280       </P>\r
281       <P>\r
282       </P>\r
283       <DIV CLASS="c2">\r
284          <IMG ALT="error-file:TidyOut.log" SRC="img00001.gif"><A NAME="_Ref352505891">Figure 1</A>\r
285       </DIV>\r
286       <P>\r
287          With CGI and vendor APIs, the application has to run on the Web server machine. This means the server\r
288          administrator has to replicate the necessary database information onto the system hosting the Web server\r
289          (which may be difficult to do in an automated way without compromising firewall security). Or, the\r
290          administrator may build a &quot;bridge&quot; that allows access through the Web server to internal databases\r
291          and applications (which is effectively re-inventing remote FastCGI).\r
292       </P>\r
293       <P>\r
294          With remote FastCGI, the applications can run on the internal network, simplifying the administrator&#39;s\r
295          job. When used with appropriate firewall configuration and auditing, this approach provides a secure,\r
296          high-performance, scalable way to bring internal applications and data to the external network.\r
297       </P>\r
298       <H4>\r
299          Load Distribution\r
300       </H4>\r
301       <P>\r
302          For resource-intensive CGI and API applications, the Web server machine quickly becomes the bottleneck for\r
303          overall throughput. The usual way to solve this performance problem is to buy a bigger, faster Web server\r
304          machine, or to partition the Web site across several Web servers.\r
305       </P>\r
306       <P>\r
307          With remote FastCGI, the resource-intensive applications can be moved off the Web server machine, giving the\r
308          server administrator additional flexibility in configuring the Web server. The administrator can configure\r
309          FastCGI applications &quot;behind the scenes&quot; without having to change any content links or the external\r
310          view of the Web site. The administrator can use several smaller, inexpensive server machines for applications,\r
311          and can tailor each machine to the application it is hosting.\r
312       </P>\r
313       <H4>\r
314          Security Issues with Remote FastCGI\r
315       </H4>\r
316       <P>\r
317          The two security issues with remote FastCGI connections are authentication and privacy. FastCGI applications\r
318          should only accept connections from Web servers that they trust (the application library includes support for\r
319          IP address validation). Future versions of the protocol will include support for applications authenticating\r
320          Web servers, as well as support for running remote connections over secure transport protocols such as SSL or\r
321          PCT.<!--This pargraph needs to be made stronger, going into the issues in a little more detail.-->\r
322       </P>\r
323       <H3>\r
324          The FastCGI Protocol\r
325       </H3>\r
326       <P>\r
327          This section offers a brief introduction to the protocol used on the connection between the Web server and\r
328          FastCGI application. Most application developers will use the FastCGI application library and won&#39;t have\r
329          to worry about the protocol details. However, specialized applications are free to implement the FastCGI\r
330          protocol directly.\r
331       </P>\r
332       <P>\r
333          FastCGI uses a simple packet record format on the connection between the application and the Web server. The\r
334          same record format is used in both directions and is outlined in Figure 2.\r
335       </P>\r
336       <P>\r
337       </P>\r
338       <DIV CLASS="c2">\r
339          <IMG ALT="error-file:TidyOut.log" SRC="img00002.gif"><A NAME="_Ref352404075">Figure 2</A>\r
340       </DIV>\r
341       <P>\r
342          The protocol version field specifies the version of the FastCGI protocol that is in use. The type field\r
343          specifies the type of the record (described in the following section). The request ID identifies this record\r
344          to a particular request, allowing multiple requests to be multiplexed over a single connection. The data\r
345          length field specifies the number of data bytes that follow.\r
346       </P>\r
347       <P>\r
348          The different FastCGI packet types are:\r
349       </P>\r
350       <TABLE>\r
351          <TR>\r
352             <TD WIDTH="186">\r
353                <TT CLASS="c3">FCGI_PARAMS</TT> \r
354             </TD>\r
355             <TD WIDTH="228">\r
356                Used for sending name/value pairs (CGI environment variables) from the Web server to the application.\r
357             </TD>\r
358          </TR>\r
359          <TR>\r
360             <TD WIDTH="186">\r
361                <TT CLASS="c3">FCGI_STDIN</TT> \r
362             </TD>\r
363             <TD WIDTH="228">\r
364                Used for sending the standard input from the Web server to the application.\r
365             </TD>\r
366          </TR>\r
367          <TR>\r
368             <TD WIDTH="186">\r
369                <TT CLASS="c3">FCGI_DATA</TT> \r
370             </TD>\r
371             <TD WIDTH="228">\r
372                Used for sending filter data to the application (for more information, see the filter role described on\r
373                page 7.)\r
374             </TD>\r
375          </TR>\r
376          <TR>\r
377             <TD WIDTH="186">\r
378                <TT CLASS="c3">FCGI_STDOUT</TT> \r
379             </TD>\r
380             <TD WIDTH="228">\r
381                Used to send standard output from the application to the Web server.\r
382             </TD>\r
383          </TR>\r
384          <TR>\r
385             <TD WIDTH="186">\r
386                <TT CLASS="c3">FCGI_STDERR</TT> \r
387             </TD>\r
388             <TD WIDTH="228">\r
389                Used to send standard error information from the application to the Web server.\r
390             </TD>\r
391          </TR>\r
392          <TR>\r
393             <TD WIDTH="186">\r
394                <TT CLASS="c3">FCGI_END_REQUEST</TT> \r
395             </TD>\r
396             <TD WIDTH="228">\r
397                Ends the request (can be sent by either the server or the application).\r
398             </TD>\r
399          </TR>\r
400       </TABLE>\r
401       <P>\r
402       </P>\r
403       <P>\r
404          For complete protocol details, see the <I>FastCGI Protocol Specification</I>, available from the Web site\r
405          listed at the end of this paper.\r
406       </P>\r
407       <H2>\r
408          3. Application Roles\r
409       </H2>\r
410       <P>\r
411          A major problem with CGI is its limited functionality: CGI programs can only provide simple responses to\r
412          requests. FastCGI provides expanded functionality with support for three different application\r
413          &quot;roles&quot;:\r
414       </P>\r
415       <UL>\r
416          <LI>\r
417             <B>Responder.</B> This is the basic FastCGI role, and corresponds to the simple functionality offered by\r
418             CGI today.\r
419          </LI>\r
420          <LI>\r
421             <B>Filter.</B> The FastCGI application filters the requested Web server file before sending it to the\r
422             client.\r
423          </LI>\r
424          <LI>\r
425             <B>Authorizer.</B> The FastCGI program performs an access control decision for the request (such as\r
426             performing a username/password database lookup).\r
427          </LI>\r
428       </UL>\r
429       <P>\r
430          Other roles will be defined in the future. For instance, a &quot;logger&quot; role would be useful, where the\r
431          FastCGI program would receive the server&#39;s log entries for real-time processing and analysis.\r
432       </P>\r
433       <P>\r
434          The roles are described in more detail in the following sections.\r
435       </P>\r
436       <H3>\r
437          Responder Role\r
438       </H3>\r
439       <P>\r
440          FastCGI&#39;s Responder role is identical to the functionality provided by CGI today. When a request comes\r
441          into the server, the FastCGI program generates the response that&#39;s returned to the client (typically an\r
442          HTML page).\r
443       </P>\r
444       <H3>\r
445          <A NAME="_Ref352404524">Filter Role</A>\r
446       </H3>\r
447       <P>\r
448          The Filter role allows a FastCGI application to process a requested file before it is returned to the client.\r
449       </P>\r
450       <P>\r
451          Let&#39;s assume that the Web server is configured so that all files with the .<TT CLASS="c3">sgml</TT>\r
452          extension are processed by a SGML-to-HTML FastCGI filter application, and the user accesses the following URL:\r
453       </P>\r
454       <P>\r
455          <TT CLASS="c3">/document.sgml</TT>\r
456       </P>\r
457       <P>\r
458          After the Web server makes an access control decision and maps this URL to a content file, it invokes the\r
459          FastCGI filter application with this file available as input. The FastCGI program&#39;s HTML output is sent\r
460          back to the client, just as in the responder role. The process is outlined in Figure 3.\r
461       </P>\r
462       <P>\r
463       </P>\r
464       <DIV CLASS="c2">\r
465          <IMG ALT="error-file:TidyOut.log" SRC="img00003.gif"><A NAME="_Ref352560526">Figure 3</A>\r
466       </DIV>\r
467       <P>\r
468          Filter applications can significantly improve performance by caching filter results (the server provides the\r
469          modification time in the request information so that applications can flush the cache when the server file has\r
470          been modified).\r
471       </P>\r
472       <P>\r
473          The Filter role is useful for:\r
474       </P>\r
475       <UL>\r
476          <LI>\r
477             On-the-fly format conversions\r
478          </LI>\r
479          <LI>\r
480             Dynamic documents (such as documents with embedded SQL queries, or dynamic advertisement insertion)\r
481          </LI>\r
482          <LI>\r
483             Applying a standard template: headers, footers, and backgrounds\r
484          </LI>\r
485       </UL>\r
486       <H3>\r
487          Authorizer Role\r
488       </H3>\r
489       <P>\r
490          The Authorizer role allows a FastCGI application to make an access control decision for a request. The FastCGI\r
491          application is invoked with all of the request information, just as in the Responder role. If the authorizer\r
492          application generates a &quot;200 OK&quot; HTTP result, the Web server assumes that access is allowed and\r
493          proceeds with the request. (The Web server may process other access checks, including other FastCGI\r
494          authorizers, before access is ultimately allowed.) If the application generates any other response, that\r
495          response is returned to the client and the request is ended. The response can be any valid HTTP response,\r
496          including &quot;Access Denied&quot; or &quot;Redirect&quot;.\r
497       </P>\r
498       <P>\r
499          The Authorizer role is useful for:\r
500       </P>\r
501       <UL>\r
502          <LI>\r
503             Access control based on username and password, where the user information is looked up in an external\r
504             database.\r
505          </LI>\r
506          <LI>\r
507             Complex access policies, such as time-of-day based access.\r
508          </LI>\r
509          <LI>\r
510             Smart-card challenge/response authentication.\r
511          </LI>\r
512          <LI>\r
513             Dynamic redirects, where the user is sent to different pages based on the request profile.\r
514          </LI>\r
515       </UL>\r
516       <H2>\r
517          <A NAME="_Ref352251764">4. FastCGI Application Library</A>\r
518       </H2>\r
519       <P>\r
520          Open Market has developed a FastCGI application library that implements the FastCGI protocol (hiding the\r
521          protocol details from the developer). This library makes implementing FastCGI programs as easy as writing CGI\r
522          applications.\r
523       </P>\r
524       <P>\r
525          The application library provides a replacement for the C language standard I/O (stdio) routines, such as <TT\r
526          CLASS="c3">printf()</TT> and <TT CLASS="c3">gets()</TT>. The library converts references to standard input,\r
527          standard output, and standard error to the FastCGI protocol. References to other files &quot;fall\r
528          through&quot; to the underlying operating system standard I/O routines.\r
529       </P>\r
530       <P>\r
531          This approach has several benefits:\r
532       </P>\r
533       <UL>\r
534          <LI>\r
535             Developers don&#39;t have to learn a new API to develop FastCGI applications.\r
536          </LI>\r
537          <LI>\r
538             Existing CGI programs can be migrated with minimal source changes (CGI migration is described in more\r
539             detail in the following section).\r
540          </LI>\r
541          <LI>\r
542             FastCGI interpreters for Perl, Tcl, and other interpreted languages can be built without modifying the\r
543             interpreter source code.\r
544          </LI>\r
545       </UL>\r
546       <P>\r
547          Here&#39;s a simple FastCGI application:\r
548       </P>\r
549       <BR>\r
550       <BR>\r
551 <PRE>\r
552     #include &lt;fcgi_stdio.h&gt;\r
553 \r
554     void main(void)\r
555     {\r
556         int count = 0;\r
557         while(FCGI_Accept() &gt;= 0) {\r
558             printf(&quot;Content-type: text/html\r\n&quot;);\r
559             printf(&quot;\r\n&quot;);\r
560             printf(&quot;Hello world!&lt;br&gt;\r\n&quot;);\r
561             printf(&quot;Request number %d.&quot;, count++);\r
562         }\r
563         exit(0);\r
564     }\r
565 </PRE>\r
566       <P>\r
567          This application returns a &quot;Hello world&quot; HTML response to the client. It also keeps a counter of the\r
568          number of times it has been accessed, displaying the value of the counter at each request.\r
569       </P>\r
570       <P>\r
571          The <TT>fcgi_stdio.h</TT> header file provides the FastCGI replacement routines for the C standard I/O\r
572          library. The <TT>FCGI_Accept()</TT> routine accepts a new request from the Web server.\r
573       </P>\r
574       <H3>\r
575          Migrating Existing CGI Programs\r
576       </H3>\r
577       <P>\r
578          The application library was designed to make migration of existing CGI programs as simple as possible. Many\r
579          applications can be converted by adding a loop around the main request processing code and recompiling with\r
580          the FastCGI application library. FastCGI applications have the following structure, with an initialization\r
581          section and a request processing loop:\r
582       </P>\r
583       <P>\r
584          <I>Initialize application;<BR>\r
585          </I> <TT>while(FCGI_Accept() &gt;= 0) {</TT><BR>\r
586           <I>Process request</I>;<BR>\r
587           <TT>}</TT>\r
588       </P>\r
589       <P>\r
590          To ease migration to FastCGI, executables built with the application library can run as either CGI or FastCGI\r
591          programs, depending on how they are invoked. The library detects the execution environment and automatically\r
592          selects FastCGI or regular I/O routines, as appropriate.\r
593       </P>\r
594       <P>\r
595          After migration, developers can clean up their FastCGI applications for best performance:\r
596       </P>\r
597       <UL>\r
598          <LI>\r
599             Fix any resource leaks. Many CGI programs do not attempt to manage memory or close files, because they\r
600             assume the world is going to be cleaned up when they exit. (If you don&#39;t want to clean up your program,\r
601             you can just have your process assume that it is leaking memory and exit after processing some fixed number\r
602             of requests.) Purify from Pure Software is one of a number of excellent tools for finding leaks and other\r
603             memory use problems.\r
604          </LI>\r
605          <LI>\r
606             Fix any problems with retained application state. The application must ensure that any state that it\r
607             creates in processing one request has no unintended effects on later requests.\r
608          </LI>\r
609          <LI>\r
610             Collapse functionality. A common practice with CGI applications is to implement many small programs, with\r
611             one function per program. CGI encourages this, because smaller programs load faster. With FastCGI, it&#39;s\r
612             better to have related functionality in a single executable, so there are fewer processes to manage and\r
613             applications can take advantage of sharing cached information across functions.\r
614          </LI>\r
615       </UL>\r
616       <P>\r
617          Applications written in Perl, Tcl, and other scripting languages can be migrated by using a language\r
618          interpreter built with the application library. FastCGI-integrated Tcl and Perl interpreters for popular Unix\r
619          platforms are available from Open Market. The interpreters are backward-compatible: They can run standard Tcl\r
620          and Perl applications.\r
621       </P>\r
622       <H2>\r
623          5. FastCGI in the Open Market WebServer\r
624       </H2>\r
625       <P>\r
626          This section describes the FastCGI support in the following Open Market server products:\r
627       </P>\r
628       <UL>\r
629          <LI>\r
630             Open Market WebServer V2.0\r
631          </LI>\r
632          <LI>\r
633             Open Market Secure WebServer V2.0\r
634          </LI>\r
635          <LI>\r
636             Open Market Secure WebServer (Global) V2.0\r
637          </LI>\r
638       </UL>\r
639       <P>\r
640          For more information about FastCGI support, see the <I>Open Market WebServer Installation and Configuration\r
641          Guide</I>.\r
642       </P>\r
643       <H3>\r
644          Server Configuration\r
645       </H3>\r
646       <P>\r
647          FastCGI applications are configured with the server&#39;s configuration file. Configuration has two parts.\r
648       </P>\r
649       <P>\r
650          First, the server administrator defines an <I>application class</I>. For local applications, the application\r
651          class specifies the details of running the FastCGI application, such as:\r
652       </P>\r
653       <UL>\r
654          <LI>\r
655             The pathname of the application executable.\r
656          </LI>\r
657          <LI>\r
658             Any arguments and environment variables to pass to the process at startup.\r
659          </LI>\r
660          <LI>\r
661             The number of processes to run.\r
662          </LI>\r
663       </UL>\r
664       <P>\r
665          For remote applications, the class configuration information includes the host and TCP port to connect to. The\r
666          Web server assumes that the FastCGI application has been started on the remote host. If a request comes in and\r
667          the server can&#39;t connect to the FastCGI TCP port, the server logs an error and returns an error page to\r
668          the client.\r
669       </P>\r
670       <P>\r
671          The second configuration step is mapping the application class to a role:\r
672       </P>\r
673       <UL>\r
674          <LI>\r
675             For responder roles, the administrator configures some part of the URL space to be handled by the FastCGI\r
676             application. For example, all URLs beginning with <SPAN CLASS="c4">/rollcall/</SPAN> might be handled by\r
677             the employee database application.\r
678          </LI>\r
679          <LI>\r
680             For filter roles, the administrator configures a file extension to be handled by a filter application. For\r
681             example, all files with the <SPAN CLASS="c4">.sql</SPAN> extension could be handled by a SQL query lookup\r
682             filter.\r
683          </LI>\r
684          <LI>\r
685             For authorizer roles, the administrator configures an authorizer application in the same manner as other\r
686             access methods (hostname, username/password, etc.) A request must pass <I>all</I> access control checks\r
687             (possibly including multiple FastCGI authorizers) before access is allowed.\r
688          </LI>\r
689       </UL>\r
690       <H3>\r
691          Basic FastCGI\r
692       </H3>\r
693       <P>\r
694          To simplify migration for existing CGI programs, the WebServer provides a simple way to install new FastCGI\r
695          programs without having to reconfigure the server. However, this approach doesn&#39;t offer all of the\r
696          performance benefits of FastCGI application classes.\r
697       </P>\r
698       <P>\r
699          The WebServer treats any file with the extension <SPAN CLASS="c4">.fcg</SPAN> as a FastCGI application. When a\r
700          request corresponds to such a file, the WebServer creates a new FastCGI process to handle the request, and\r
701          shuts down the process when the request is complete (just as in CGI). In this mode of operation performance is\r
702          comparable to CGI. Future versions of the WebServer will improve performance by automatically caching\r
703          processes and re-using them for subsequent requests.\r
704       </P>\r
705       <H3>\r
706          Session Affinity\r
707       </H3>\r
708       <P>\r
709          FastCGI programs can improve performance by caching information in the application process. For applications\r
710          that require frequent but expensive operations such as validating a username/password in an external database\r
711          for each request, this technique can significantly improve performance.\r
712       </P>\r
713       <P>\r
714          To improve the effectiveness of this technique, the WebServer implements <I>session affinity</I>. When session\r
715          affinity is enabled, the WebServer arranges for all requests in a user session to be handled by the same\r
716          FastCGI application process. What constitutes a &quot;session&quot; is configurable. The default configuration\r
717          uses the WebServer&#39;s built-in session tracking facility to identify user sessions. However, the server\r
718          administrator can use any part of the request information for the session affinity mapping: the URL path, the\r
719          client&#39;s hostname, the username, etc.\r
720          <!--Talk about applications that need to hold onto resources for the user (such as open connections to the database).-->\r
721       </P>\r
722       <H2>\r
723          6. FastCGI Performance Analysis\r
724       </H2>\r
725       <P>\r
726          How fast is FastCGI? The answer depends on the application. This section contains some real FastCGI\r
727          performance measurements, as well as guidelines for estimating the FastCGI speedup.\r
728       </P>\r
729       <H3>\r
730          FastCGI vs CGI\r
731       </H3>\r
732       <P>\r
733          We measured the relative performance of CGI, FastCGI, and static files on the Open Market WebServer, using a\r
734          simple application that generates a fixed number of output bytes. The following table shows the measured\r
735          request processing time for different request types on a typical platform. The times are measured from the\r
736          client perspective and include client, server, and application processing time.\r
737       </P>\r
738       <TABLE BORDERCOLOR="#000000" BORDER="2">\r
739          <TR>\r
740             <TD WIDTH="72">\r
741                <DIV CLASS="c5">\r
742                   Static file\r
743                </DIV>\r
744             </TD>\r
745             <TD WIDTH="180">\r
746                <DIV CLASS="c5">\r
747                   21ms + 0.19ms per Kbyte\r
748                </DIV>\r
749             </TD>\r
750          </TR>\r
751          <TR>\r
752             <TD WIDTH="72">\r
753                <DIV CLASS="c5">\r
754                   FastCGI\r
755                </DIV>\r
756             </TD>\r
757             <TD WIDTH="180">\r
758                <DIV CLASS="c5">\r
759                   22ms + 0.28ms per Kbyte\r
760                </DIV>\r
761             </TD>\r
762          </TR>\r
763          <TR>\r
764             <TD WIDTH="72">\r
765                <DIV CLASS="c5">\r
766                   CGI\r
767                </DIV>\r
768             </TD>\r
769             <TD WIDTH="180">\r
770                <DIV CLASS="c5">\r
771                   59ms + 0.37ms per Kbyte\r
772                </DIV>\r
773             </TD>\r
774          </TR>\r
775       </TABLE>\r
776       <P>\r
777          FastCGI performance is comparable to serving static files, and significantly better than CGI (clearly showing\r
778          the high overhead for process creation). Real applications have an additional time component: process\r
779          initialization, which should be added to overall request processing time.\r
780       </P>\r
781       <P>\r
782          Let&#39;s use this data to estimate the speedup from migrating a typical database CGI application to FastCGI.\r
783          Assume the application takes 50ms to initialize the database connection and generates 5K of output data.\r
784          Request performance can be computed as follows:\r
785       </P>\r
786       <TABLE>\r
787          <TR>\r
788             <TD WIDTH="108">\r
789                CGI\r
790             </TD>\r
791             <TD WIDTH="331">\r
792                59ms + 50ms + (0.37ms)(5) = 111ms\r
793             </TD>\r
794          </TR>\r
795          <TR>\r
796             <TD WIDTH="108">\r
797                FastCGI\r
798             </TD>\r
799             <TD WIDTH="331">\r
800                22ms + (0.28ms)(5) = 23ms\r
801             </TD>\r
802          </TR>\r
803       </TABLE>\r
804       <P>\r
805          In this example, FastCGI has a 5x performance advantage over CGI, mostly due to savings from not having to\r
806          create and initialize new processes for each request.<!--Need to talk about FastCGI vs proprietary APIs.-->\r
807       </P>\r
808       <H2>\r
809          7. Conclusions\r
810       </H2>\r
811       <P>\r
812          Today&#39;s Web business applications need a platform that&#39;s fast, open, maintainable, straightforward,\r
813          stable, and secure. FastCGI&#39;s design meets these requirements, and provides for a logical extension from\r
814          proven and widely deployed CGI technology. This allows developers to take advantage of FastCGI&#39;s benefits\r
815          without losing their existing investment in CGI applications.<!--Need to talk about NT.-->\r
816           \r
817          <!--Need to give &quot;more punch&quot; to this conclusion: include info about uses for FastCGI (accessing legacy data in databases, access control, distributed applications, apps that have to run in multiple OS environments. -->\r
818       </P>\r
819       <H2>\r
820          8. For More Information\r
821       </H2>\r
822       <P>\r
823          For more information about Open Market and our products, visit our Web site at:<SPAN CLASS=\r
824          "c4">http://www.openmarket.com/</SPAN>\r
825       </P>\r
826       <P>\r
827          For more information about the FastCGI protocol and the developer&#39;s kit, and the latest information about\r
828          FastCGI standardization and support in other Web servers, visit the FastCGI project page at:<SPAN CLASS=\r
829          "c4">http://www.openmarket.com/fastcgi/</SPAN>\r
830       </P>\r
831    </BODY>\r
832 </HTML>\r
833 \r