Initial revision
[catagits/fcgi2.git] / doc / fcgi-devel-kit.htm
1 <html>
2 <head><title>FastCGI Developer's Kit</title>
3 </head>
4
5 <body bgcolor="#FFFFFF" text="#000000" link="#cc0000" alink="#000011" 
6 vlink="#555555">
7
8 <center>
9 <a href="/fastcgi/words">
10     <img border=0 src="/kit/images/fcgi-hd.gif" alt="[[FastCGI]]"></a>
11 </center>
12 <br clear=all>
13 <h3><center>FastCGI Developer's Kit</center></h3>
14
15 <!--Copyright (c) 1996 Open Market, Inc.                                    -->
16 <!--See the file "LICENSE.TERMS" for information on usage and redistribution-->
17 <!--of this file, and for a DISCLAIMER OF ALL WARRANTIES.                   -->
18
19 <center>
20 Mark R. Brown<br>
21 Open Market, Inc.<br>
22 <p>
23
24 Document Version: 1.08<br>
25 11 June 1996<br>
26 </center>
27 <p>
28
29 <h5 align=center>
30 Copyright &copy; 1996 Open Market, Inc.  245 First Street, Cambridge,
31   MA 02142 U.S.A.<br>
32 Tel: 617-621-9500 Fax: 617-621-1703 URL:
33   <a href="http://www.openmarket.com/">http://www.openmarket.com/</a><br>
34 $Id: fcgi-devel-kit.htm,v 1.1 1997/09/16 15:36:26 stanleyg Exp $ <br>
35 </h5>
36 <hr>
37
38 <ul type=square>
39   <li><a HREF = "#S1">1. Introduction</a>
40   <li><a HREF = "#S2">2. Getting started</a>
41   <li><a HREF = "#S3">3. Writing applications</a>
42   <ul type=square>
43     <li><a HREF = "#S3.1">3.1 Using the <tt>fcgi_stdio</tt> library</a>
44     <li><a HREF = "#S3.2">3.2 Using the <tt>fcgiapp</tt> library</a>
45     <li><a HREF = "#S3.3">3.3 Using Perl and Tcl</a>
46     <li><a HREF = "#S3.4">3.4 Using Java</a>
47   </ul>
48   <li><a HREF = "#S4">4. Running applications</a>
49   <ul type=square>
50     <li><a HREF = "#S4.1">4.1 Using a Web server that supports FastCGI</a>
51     <li><a HREF = "#S4.2">4.2 Using <tt>cgi-fcgi</tt> with any Web server</a>
52   </ul>
53   <li><a HREF = "#S5">5. Known problems</a>
54   <li><a HREF = "#S6">6. Getting support</a>
55 </ul>
56
57 <hr>
58
59
60
61 <h3><a NAME = "S1">1. Introduction</a></h3>
62
63 FastCGI is an open extension to CGI that provides high performance
64 for all Internet applications without the penalties of Web server
65 APIs.<p>
66
67 FastCGI is designed to be layered on top of existing Web server
68 APIs.  For instance, the <tt>mod_fastcgi</tt> Apache module adds
69 FastCGI support to the Apache server.  FastCGI can also be used,
70 with reduced functionality and reduced performance, on any Web server
71 that supports CGI.<p>
72
73 This FastCGI Developer's Kit is designed to make developing FastCGI
74 applications easy.  The kit currently supports FastCGI
75 applications written in C/C++, Perl, Tcl, and Java.<p>
76
77 This document:
78
79 <ul type=square>
80   <li>Describes how to configure and build the
81       kit for your development platform.
82
83   <li>Tells how to write applications using the
84       libraries in the kit.
85
86   <li>Tells how to run applications using Web servers that support
87       FastCGI or using any Web server and <tt>cgi-fcgi</tt>.
88 </ul>
89
90 The kit includes a <a href="fastcgi-whitepaper/fastcgi.htm">technical white
91 paper</a>, <tt>doc/fastcgi-whitepaper/fastcgi.htm</tt>.
92 You should read at least the first three sections of the
93 technical white paper before starting to write FastCGI applications.
94 The <a href="fcgi-perf.htm">performance paper</a> will help you understand
95 how application design affects performance with FastCGI.<p>
96
97 The <a href ="fcgi-spec.html">FastCGI Specification</a>,
98 <tt>doc/fcgi-spec.html</tt>, defines the interface between a FastCGI
99 application and a Web server that supports FastCGI.  The software in
100 the kit implements the specification.  You don't need to
101 read the specification in order to write applications.<p>
102
103 Additional information is provided in the 
104 <a href ="http://www.fastcgi.com/words/FAQ.htm">FAQ</a> document, which
105 contains frequently asked questions about application development 
106 using FastCGI, as well as some general information.<p>
107
108 Experience with CGI programming will be extremely valuable in writing FastCGI
109 applications.  If you don't have enough experience with CGI programming,
110 you should read one of the popular books on the topic or study the
111 <a href = "http://hoohoo.ncsa.uiuc.edu/cgi/">NCSA CGI page</a>.
112 For a more formal treatment of CGI/1.1 see the
113 <a href = "http://ds.internic.net/internet-drafts/draft-robinson-www-interface-01.txt">Internet Draft CGI 1.1 Specification</a>.<p>
114
115
116
117 <h3><a NAME = "S2">2. Getting started</a></h3>
118
119 The kit is a compressed tar (tar.Z) file,
120 distributed via the
121 <a href = "http://www.fastcgi.com/applibs/">www.fastcgi.com/applibs</a>
122 Web page.  Unpacking the tar file creates a new directory
123 <tt>fcgi-devel-kit</tt>.<p>
124
125 Open the kit's index page, <tt>fcgi-devel-kit/index.html</tt>, using
126 the "Open File" command in your Web browser.  The index page gives you
127 an overview of the kit structure and helps you navigate the kit. The
128 index page also contains links that run some example applications, but
129 the applications won't work when index.html is opened using the "Open
130 File" command because they aren't aren't being accessed through a Web
131 server.<p>
132
133 In order to use the kit in earnest you'll need a Web server that you
134 control, a Web server running with your user ID.  The Web server will
135 be starting FastCGI applications that you will need to debug; this
136 will be a lot more convenient for you if these processes run with your
137 user ID.  It is best to have a Web server that supports FastCGI.
138 <a href = "#S4">Section 4</a> discusses Web server issues.<p>
139
140 If you can, keep the kit on a file system accessible from your
141 personal workstation, do your builds on your workstation, and run your
142 Web server on your workstation.  If that's not possible, arrange a
143 configuration such that the kit is accessible from the machine that's
144 going to run your Web server, and build the kit and your applications
145 on a machine that's configured exactly the same way (same processor
146 architecture, operating system, etc.) as the machine that's going to
147 run your Web server.<p>
148
149 To build the kit you execute this sequence of commands
150 in the <tt>fcgi-devel-kit</tt> directory:<p>
151
152 <pre>
153     % ./configure
154     % make
155 </pre>
156
157 We've built and exercised the kit on these platforms
158 (listed in alphabetical order):<p>
159
160 <ul type=square>
161     <li>
162         BSD/OS 1.1 (Intel Pentium), gcc
163     <li>
164         Digital UNIX V3.2 148 (Alpha), gcc/cc
165     <li>
166         Hewlett-Packard HP-UX A.09.05 C and B.10.01 A (PA-RISC), gcc/cc
167     <li>
168         IBM AIX 1 4 (RS/6000), gcc
169     <li>
170         Silicon Graphics IRIX 5.3 11091812 (MIPS), gcc
171     <li>
172         Sun Solaris 2.4 and 2.5 (SPARC), gcc/cc
173     <li>
174         Sun SunOS 4.1.4 (SPARC), gcc
175 </ul>
176
177 Once you've built the kit, follow the directions in
178 <a href = "#S4">Section 4</a> to bring up your Web server
179 and run the example applications.<p>
180
181
182
183 <h3><a NAME = "S3">3. Writing applications</a></h3>
184
185
186 <h4><a NAME = "S3.1">3.1 Using the <tt>fcgi_stdio</tt> library</a>
187 </h4>
188
189 The <tt>fcgi_stdio</tt> library provides
190 the easiest transition for C CGI programs and C CGI
191 programmers to FastCGI.  Using this library your application
192 can run using either CGI or FastCGI, with the same binary
193 for both situations.<p>
194
195 To introduce the <tt>fcgi_stdio</tt> library
196 we give a pair of examples: a tiny CGI program and the translation of this
197 program to FastCGI.  These two example programs are included in the kit.<p>
198
199 The CGI program is <tt>examples/tiny-cgi.c</tt>:<p>
200
201 <pre>
202     #include &lt;stdio.h&gt;
203     #include &lt;stdlib.h&gt;
204
205     void main(void)
206     {
207         int count = 0;
208         printf("Content-type: text/html\r\n"
209                "\r\n"
210                "&lt;title&gt;CGI Hello!&lt;/title&gt;"
211                "&lt;h1&gt;CGI Hello!&lt;/h1&gt;"
212                "Request number %d running on host &lt;i&gt;%s&lt;/i&gt;\n",
213                ++count, getenv("SERVER_NAME"));
214     }
215 </pre>
216
217 The key features of this tiny CGI program are:<p>
218
219 <ul type = square>
220     <li>
221         The program sends data to the Web server by writing to
222         <tt>stdout</tt>, using <tt>printf</tt> in this example.  The CGI
223         program first sends a <tt>Content-type</tt> header, then a
224         small HTML document.  The program includes <tt>stdio.h</tt> in
225         order to get access to the <tt>printf</tt> function.<p>
226     <li>
227         The program obtains parameters provided by the Web server by
228         reading environment variables.  The CGI program reads the
229         <tt>SERVER_NAME</tt> variable using <tt>getenv</tt> and
230         includes the value in the HTML document.  The program includes
231         <tt>stdlib.h</tt> in order to get access to the
232         <tt>getenv</tt> function.<p>
233 </ul>
234
235 The <tt>count</tt> variable is degenerate in this example;
236 the CGI program runs a single request, so the request number
237 is always one.  This variable will be more interesting
238 in the FastCGI example.<p>
239
240 <a NAME = "S3.1.1">The</a>
241 corresponding FastCGI program is <tt>examples/tiny-fcgi.c</tt>:<p>
242
243 <pre>
244     #include "fcgi_stdio.h"
245     #include &lt;stdlib.h&gt;
246
247     void main(void)
248     {
249         int count = 0;
250         while(FCGI_Accept() &gt;= 0)
251             printf("Content-type: text/html\r\n"
252                    "\r\n"
253                    "&lt;title&gt;FastCGI Hello!&lt;/title&gt;"
254                    "&lt;h1&gt;FastCGI Hello!&lt;/h1&gt;"
255                    "Request number %d running on host &lt;i&gt;%s&lt;/i&gt;\n",
256                     ++count, getenv("SERVER_NAME"));
257     }
258 </pre>
259
260 The key features of this tiny FastCGI program are:<p>
261
262 <ul type = square>
263     <li>
264         The program is structured as a loop that begins by calling the
265         function <tt>FCGI_Accept</tt>.  The <tt>FCGI_Accept</tt>
266         function blocks until a new request arrives for the program to
267         execute.  The program includes <tt>fcgi_stdio.h</tt> in order
268         to get access to the <tt>FCGI_Accept</tt> function.<p>
269     <li>
270         Within the loop, <tt>FCGI_Accept</tt> creates a
271         CGI-compatible world.  <tt>printf</tt> and <tt>getenv</tt>
272         operate just as in the CGI program.  <tt>stdin</tt> and
273         <tt>stderr</tt>, not used by this tiny program, also
274         operate just as in a CGI program.<p>
275 </ul>
276
277 The <tt>count</tt> variable increments each time through the loop, so
278 the program displays a new request number each time.  You can use
279 the reload button in your browser to demonstrate this, once you've
280 got the program built and running.<p>
281
282
283 <h4>Building the program</h4>
284
285 If you can build <tt>examples/tiny-cgi.c</tt>,
286 it will be straightforward for you to build
287 <tt>examples/tiny-fcgi.c</tt>.  You need to:<p>
288
289 <ul type = square>
290     <li>
291         Add the directory containing the
292         <tt>fcgi_stdio.h</tt> header to the compiler's include search
293         path.  The kit calls this directory <tt>include</tt>.<p>
294     <li>
295         Add the library <tt>libfcgi.a</tt> to the linker's command
296         line so that it will be searched when linking.  The
297         <tt>libfcgi.a</tt> library implements the functions defined in
298         <tt>fcgi_stdio.h</tt>.  The kit calls the directory containing
299         this library <tt>libfcgi</tt>.<p>
300     <li>
301         Determine whether or not the linker on your
302         platform searches the Berkeley socket
303         library by default, and if not, add linker directives to
304         force this search.<p>
305 </ul>
306
307 See <tt>examples/Makefile</tt> (created by <tt>configure</tt>)
308 for a Makefile that builds both
309 programs.  Autoconf handles the platform-dependent linking issues; to
310 see how, examine <tt>configure.in</tt> and
311 <tt>examples/Makefile.in</tt>.<p>
312
313
314 <h4>Running the program</h4>
315
316 <a href = "#S4">Section 4</a> is all about
317 how to run FastCGI applications.<p>
318
319 You can use CGI to run application binaries built with the
320 <tt>fcgi_stdio</tt> library.  The <tt>FCGI_Accept</tt>
321 function tests its environment to determine how the application was
322 invoked.  If it was invoked as a CGI program, the first
323 call to FCGI_Accept is essentially a no-op and the second call
324 returns <tt>-1</tt>.  In effect, the request loop disappears.<p>
325
326 Of course, when a FastCGI application is run using CGI it does not
327 get the benefits of FastCGI.  For instance, the application exits
328 after servicing a single request, so it cannot maintain
329 cached information.<p>
330
331
332 <h4>Implementation details</h4>
333
334 <tt>fcgi_stdio.h</tt> works by first including <tt>stdio.h</tt>, then
335 defining macros to replace essentially all of the types and procedures
336 defined in <tt>stdio.h</tt>.  (<tt>stdio.h</tt> defines a few
337 procedures that have nothing to do with <tt>FILE *</tt>, such as
338 <tt>sprintf</tt> and <tt>sscanf</tt>; <tt>fcgi_stdio.h</tt> doesn't
339 replace these.)  For instance, <tt>FILE</tt> becomes
340 <tt>FCGI_FILE</tt> and <tt>printf</tt> becomes <tt>FCGI_printf</tt>.
341 You'll only see these new names if you read <tt>fcgi_stdio.h</tt> or
342 examine your C source code after preprocessing.<p>
343
344 Here are some
345 consequences of this implementation technique:<p>
346
347 <ul type = square>
348     <li>
349         On some platforms the implementation will break if you include
350         <tt>stdio.h</tt> after including <tt>fcgi_stdio.h</tt>,
351         because <tt>stdio.h</tt> often defines macros for functions such
352         as <tt>getc</tt> and <tt>putc</tt>.  Fortunately, on most
353         platforms <tt>stdio.h</tt> is protected against multiple
354         includes by lines near the top of the file that look like
355     <pre>
356     #ifndef _STDIO_H
357     #define _STDIO_H
358     </pre>
359         The specific symbol used for multiple-include protection,
360         <tt>_STDIO_H</tt> in this example, varies from platform to
361         platform.  As long as your platform protects <tt>stdio.h</tt>
362         against multiple includes, you can forget about this issue.<p>
363     <li>
364         If your application passes <tt>FILE *</tt> to functions
365         implemented in libraries for which you have source code, then
366         you'll want to recompile these
367         libraries with <tt>fcgi_stdio.h</tt> included.
368         Most C compilers provide a command-line option for including
369         headers in a program being compiled; using such a compiler feature
370         allows you to rebuild your libraries without making source changes.
371         For instance the gcc command line
372     <pre>
373     gcc -include /usr/local/include/fcgi_stdio.h wonderlib.c
374     </pre>
375         causes gcc to include <tt>fcgi_stdio.h</tt> before it even
376         begins to read the module <tt>wonderlib.c</tt>.<p>
377     <li>
378         If your application passes <tt>FILE *</tt> to functions
379         implemented in libraries
380         for which you do not have source code, then
381         you'll need to include the headers for these libraries
382         <i>before</i> you include <tt>fcgi_stdio.h</tt>.
383         You can't pass the <tt>stdin</tt>,
384         <tt>stdout</tt>, or <tt>stderr</tt> streams produced by
385         <tt>FCGI_Accept</tt> to any functions implemented by these
386         libraries.  You can pass a stream on a Unix file to a library
387         function by following this pattern:
388     <pre>
389     FILE *myStream = fopen(path, "r");
390     answer = MungeStream(FCGI_ToFile(myStream));
391     </pre>
392         Here <tt>MungeStream</tt> is a library function that you can't
393         recompile and <tt>FCGI_ToFile</tt> is a macro that converts
394         from <tt>FCGI_FILE *</tt> to <tt>FILE *</tt>.  The macro
395         <tt>FCGI_ToFile</tt> is defined in <tt>fcgi_stdio.h</tt>.<p>
396 </ul>
397
398
399 <h4>Converting CGI programs</h4>
400
401 The main task in converting a CGI program into a FastCGI program is
402 separating the code that needs to execute once, initializing the
403 program, from the code that needs to run for each request.
404 In our tiny example, initializing the <tt>count</tt> variable
405 is outside the loop, while incrementing the <tt>count</tt> variable
406 goes inside.<p>
407
408 Retained application state may be an issue.  You must ensure that
409 any application state created in processing one request has no
410 unintended effects on later requests.  FastCGI offers the possibility
411 of significant application performance improvements, through caching;
412 it is up to you to make the caches work
413 correctly.<p>
414
415 Storage leaks may be an issue.  Many CGI programs don't worry about
416 storage leaks because the programs don't run for long enough for
417 bloating to be a problem.  When converting to FastCGI, you
418 can either use a tool such as
419 <a href ="http://www.pure.com/"><i>Purify</i></a> from Pure Software
420 to discover and fix storage
421 leaks, or you can run a C garbage collector such as
422 <a href ="http://www.geodesic.com/"><i>Great Circle</i></a>
423 from Geodesic Systems.<p>
424
425
426 <h4>Limitations</h4>
427
428 Currently there are some limits to the compatibility provided
429 by the <tt>fcgi_stdio</tt> library:<p>
430
431 <ul type = square>
432     <li>
433         The library does not provide FastCGI versions of
434         the functions <tt>fscanf</tt> and <tt>scanf</tt>.  If you wish
435         to apply <tt>fscanf</tt> or <tt>scanf</tt> to <tt>stdin</tt>
436         of a FastCGI program, the workaround is to read lines or other
437         natural units into memory and then call <tt>sscanf</tt>.  If
438         you wish to apply <tt>fscanf</tt> to a stream on a Unix file,
439         the workaround is to follow the pattern:
440     <pre>
441     FILE *myStream = fopen(path, "r");
442     count = fscanf(FCGI_ToFile(myStream), format, ...);
443     </pre>
444 </ul>
445
446 <h4>Reference documentation</h4>
447
448 The <a href = "FCGI_Accept.3"><tt>FCGI_Accept</tt> manpage</a>,
449 <tt>doc/FCGI_Accept.3</tt>, describes the function in the traditional
450 format.<p>
451
452 The <a href = "FCGI_Finish.3"><tt>FCGI_Finish</tt></a>
453 (<tt>doc/FCGI_Finish.3</tt>),
454 <a href = "FCGI_SetExitStatus.3"><tt>FCGI_SetExitStatus</tt></a>
455 (<tt>doc/FCGI_SetExitStatus.3</tt>), and
456 <a href = "FCGI_StartFilterData.3"><tt>FCGI_StartFilterData</tt></a>
457 (<tt>doc/FCGI_StartFilterData.3</tt>)
458 manpages document capabilities of the <tt>fcgi-stdio</tt>
459 library that are not illustrated above.<p>
460
461
462 <h4><a NAME = "S3.2">3.2 Using the <tt>fcgiapp</tt> library</a></h4>
463
464 The <tt>fcgiapp</tt> library is a second C library for FastCGI.  It
465 does not provide the high degree of source code compatibility provided
466 by <tt>fcgi_stdio</tt>; in return, it does not make such heavy use of
467 <tt>#define</tt>.  <tt>fcgi_stdio</tt> is implemented as
468 a thin layer on top of <tt>fcgiapp</tt>.<p>
469
470 Applications built using the <tt>fcgiapp</tt> library cannot run
471 as CGI programs; that feature is provided at the <tt>fcgi_stdio</tt>
472 level.<p>
473
474 Functions defined in <tt>fcgiapp</tt> are named using the prefix
475 <tt>FCGX_</tt> rather than <tt>FCGI_</tt>.  For instance,
476 <tt>FCGX_Accept</tt> is the <tt>fcgiapp</tt> version of
477 <tt>FCGI_Accept</tt>.<p>
478
479 Documentation of the <tt>fcgiapp</tt> library takes the form
480 of extensive comments in the header file <tt>include/fcgiapp.h</tt>.
481 The sample programs <tt>examples/tiny-fcgi2.c</tt> and
482 <tt>examples/echo2.c</tt> illustrate how to use
483 <tt>fcgiapp</tt>.<p>
484
485
486 <h4><a NAME = "S3.3">3.3 Using Perl and Tcl</a></h4>
487
488 A major advantage of the FastCGI approach to high-performance Web
489 applications is its language-neutrality.  CGI scripts written in
490 popular languages such as Perl and Tcl can be evolved into
491 high-performance FastCGI applications.<p>
492
493 We have produced FastCGI-integrated Perl and Tcl
494 interpreters.  Doing so was easy, since Perl and Tcl
495 are conventional C applications and <tt>fcgi_stdio</tt> was
496 designed for converting conventional C applications.  Essentially no source
497 code changes were required in these programs; a small amount
498 of code was added in order to make <tt>FCGI_Accept</tt> and other
499 FastCGI primitives available in these languages.  And because
500 these interpreters were developed using <tt>fcgi_stdio</tt>, they
501 run standard Perl and Tcl applications (e.g. CGI scripts) as well
502 as FastCGI applications.<p>
503
504 See the
505 <a href = "http://www.fastcgi.com/applibs/">www.fastcgi.com/applibs</a>
506 Web page for downloadable Perl and Tcl binaries for selected platforms.
507 Because many users of Perl and Tcl run extended versions of these languages,
508 the kit includes separate companion documents describing
509 <a href ="fcgi-perl.htm">how to build FastCGI-integrated Perl</a>
510 and <a href ="fcgi-tcl.htm">how to build FastCGI-integrated Tcl</a>.<p>
511
512 Here are the Perl and Tcl versions of <tt>tiny-fcgi</tt>:<p>
513 <pre>
514 #!./perl
515 use FCGI;
516 $count = 0;
517 while(FCGI::accept() >= 0) {
518     print("Content-type: text/html\r\n\r\n",
519           "&lt;title&gt;FastCGI Hello! (Perl)&lt;/title&gt;\n",
520           "&lt;h1&gt;FastCGI Hello! (Perl)&lt;/h1&gt;\n";
521           "Request number ",  ++$count,
522           " running on host &lt;i&gt";$env(SERVER_NAME)&lt;/i&gt;");
523 }
524 </pre>
525
526 <pre>
527 #!./tclsh
528 set count 0 
529 while {[FCGI_Accept] &gt;= 0 } {
530     incr count
531     puts -nonewline "Content-type: text/html\r\n\r\n"
532     puts "&lt;title&gt;FastCGI Hello! (Tcl)&lt;/title&gt;"
533     puts "&lt;h1&gt;FastCGI Hello! (Tcl)&lt;/h1&gt;"
534     puts "Request number $count running on host &lt;i&gt;$env(SERVER_NAME)&lt;/i&gt;"
535 }
536 </pre>
537
538 Converting a Perl or Tcl CGI application to FastCGI is not fundamentally
539 different from converting a C CGI application to FastCGI.  You separate
540 the portion of the application that performs one-time
541 initialization from the portion that performs per-request
542 processing.  You put the per-request processing into a loop
543 controlled by <tt>FCGI::accept</tt> (Perl) or <tt>FCGI_Accept</tt>
544 (Tcl).
545
546
547
548 <h4><a NAME = "S3.4">3.4 Using Java</a></h4>
549
550 Java is not just for browser-based applets.  It is already suitable for
551 writing some Web server applications, and its range of applicability will
552 only grow as Java compilers and other Java tools improve.  Java's
553 modules, garbage collection, and threads are especially valuable
554 for writing long-lived application servers.<p>
555
556 The <tt>FCGIInterface</tt> class provides facilities for Java
557 applications analogous to what <tt>fcgi_stdio</tt> provides for C
558 applications.  Using this library your Java application can run using
559 either CGI or FastCGI.<p>
560
561 The kit includes separate companion document on
562 <a href ="fcgi-java.htm">using FastCGI with Java</a>.  The
563 source code for FastCGI classes is contained in
564 directory <tt>java/src</tt>
565 and the compiled code in <tt>java/classes</tt>.<p>
566
567 Here is the Java version of <tt>tiny-fcgi</tt>:<p>
568
569 <pre>
570 import FCGIInterface;
571
572 class TinyFCGI {        
573     public static void main (String args[]) {           
574         int count = 0;
575         while(new FCGIInterface().FCGIaccept()>= 0) {
576             count ++;
577             System.out.println("Content-type: text/html\r\n\r\n");
578             System.out.println(
579                     "&lt;title&gt;FastCGI Hello! (Java)&lt;/title&gt;");
580             System.out.println("&lt;h1&gt;FastCGI Hello! (Java)&lt;/h1&gt;");
581             System.out.println(
582                     "request number " + count + " running on host &lt;i&gt;" +
583                     System.getProperty("SERVER_NAME") + "&lt;/i&gt;");
584         }
585     }
586 }
587 </pre>
588 <p>
589
590
591 <h3><a NAME = "S4">4. Running applications</a></h3>
592
593
594 <h3><a NAME = "S4.1">4.1 Using a Web server that supports FastCGI</a></h3>
595
596 For a current listing of Web servers that support FastCGI,
597 see the <a HREF = "http://www.fastcgi.com/servers">www.fastcgi.com/servers</a>
598 Web page.<p>
599
600 All of the Web servers that support FastCGI perform management of
601 FastCGI applications.  You don't need to start and stop FastCGI
602 applications; the Web server takes care of this.  If an application
603 process should crash, the Web server restarts it.<p>
604
605 Web servers support FastCGI via new configuration directives.
606 Since these directives are server-specific, get more information
607 from the documentation that accompanies each server.<p>
608
609 The directory
610 <a HREF = "../examples/conf"><tt>examples/conf</tt></a> contains config
611 files designed to run the example programs included in the FastCGI
612 Developer's Kit.  Each config file contains specific installation
613 instructions.<p>
614
615 The more advanced example programs take advantage of special features
616 of the Open Market Secure WebServer, such as anonymous ticketing
617 and support for the Authorizer role.  If you don't have this server,
618 download a
619 <a HREF= "http://www.openmarket.com/store/eval/swsg.htm">free
620 evaluation copy</a> to run the examples.<p>
621
622
623 <h3><a NAME = "S4.2">4.2 Using <tt>cgi-fcgi</tt> with any Web server</a></h3>
624
625 The program <tt>cgi-fcgi</tt> allows you to run FastCGI applications
626 using any Web server that supports CGI.<p>
627
628 Here is how <tt>cgi-fcgi</tt> works.  <tt>cgi-fcgi</tt> is a
629 standard CGI program that uses Unix domain or TCP/IP sockets
630 to communicate with
631 a FastCGI application.  <tt>cgi-fcgi</tt> takes the path name
632 or host/port name of a
633 listening socket as a parameter and <tt>connect</tt>s to the FastCGI
634 application listening on that socket.  <tt>cgi-fcgi</tt> then forwards the CGI
635 environment variables and <tt>stdin</tt> data to the FastCGI
636 application, and forwards the <tt>stdout</tt> and <tt>stderr</tt> data
637 from the FastCGI application to the Web server.  When the FastCGI
638 application signals the end of its response, <tt>cgi-fcgi</tt> flushes
639 its buffers and exits.<p>
640
641 Obviously, having <tt>cgi-fcgi</tt> is not as good as having
642 a server with integrated FastCGI support:<p>
643
644 <ul>
645   <li>Communication is slower than with a Web server that avoids the
646       fork/exec overhead on every FastCGI request.
647
648   <li><tt>cgi-fcgi</tt> does not perform application management,
649       so you need to provide this yourself.
650
651   <li><tt>cgi-fcgi</tt> supports only the Responder role.
652 </ul>
653
654 But <tt>cgi-fcgi</tt> does allow you to
655 develop applications that retain state in memory between connections,
656 which often provides a major performance boost over normal CGI.  And
657 all the applications you develop using <tt>cgi-fcgi</tt>
658 will work with Web servers that have integrated support for
659 FastCGI.<p>
660
661 The file <tt>examples/tiny-fcgi.cgi</tt> demonstrates
662 a way to use <tt>cgi-fcgi</tt> to run a typical application,
663 in this case the <tt>examples/tiny-fcgi</tt> application:
664
665 <pre>
666     #!../cgi-fcgi/cgi-fcgi -f
667     -connect sockets/tiny-fcgi tiny-fcgi
668 </pre>
669
670 On most Unix platforms, executing this command-interpreter file runs
671 <tt>cgi-fcgi</tt> with arguments <tt>-f</tt> and
672 <tt>examples/tiny-fcgi.cgi</tt>.  (Beware: On some
673 Unix platforms, including HP-UX, the first line of a command-interpreter file
674 cannot contain more than 32 characters, including the newline;
675 you may need to install the <tt>cgi-fcgi</tt> application in a standard
676 place like <tt>/usr/local/bin</tt> or create a symbolic link
677 to the <tt>cgi-fcgi</tt> application in the directory containing
678 your application.)
679 The <tt>cgi-fcgi</tt> program reads the command-interpreter file and
680 connects to the FastCGI application whose listening socket is
681 <tt>examples/sockets/tiny-fcgi</tt>.<p>
682
683 Continuing the example, if
684 <tt>cgi-fcgi</tt>'s connection attempt fails, it
685 creates a new process
686 running the program <tt>examples/tiny-fcgi</tt> and listening on
687 socket <tt>examples/sockets/tiny-fcgi</tt>.  Then <tt>cgi-fcgi</tt>
688 retries the connection attempt, which now should succeed.<p>
689
690 The <tt>cgi-fcgi</tt> program has two other modes of operation.
691 In one mode it connects to applications but does not start them;
692 in the other it starts applications but does not connect
693 to them.  These modes are required when using TCP/IP.  The
694 <a href = "cgi-fcgi.1"><tt>cgi-fcgi</tt> manpage</a>,
695 <tt>doc/cgi-fcgi.1</tt>, tells the full story.<p>
696
697 To run the example applications using <tt>cgi-fcgi</tt>, start your
698 Web server and give it the directory <tt>fcgi-devel-kit</tt> as the
699 root of its URL space.  If the machine running your server is called
700 <tt>bowser</tt> and your server is running on port <tt>8888</tt>,
701 you'd then open the URL <tt>http://bowser:8888/index.html</tt> to
702 reach the kit's index page.  Now the links on the index page that run
703 example applications via <tt>cgi-fcgi</tt> should be active.<p>
704
705
706
707 <h3><a NAME = "S5">5. Known problems</a></h3>
708
709 On Digital UNIX 3.0 there's a problem with Unix domain listening
710 sockets on NFS file systems.  The symptom when using cgi-fcgi is an
711 exit status of 38 (<tt>ENOTSOCK</tt>: socket operation on non-socket),
712 but cgi-fcgi may dump core in this case when compiled optimized.
713 Work-around: Store your Unix domain listening
714 sockets on a non NFS file system, upgrade to Digital UNIX 3.2, or use
715 TCP sockets.<p>
716
717 On AIX there's a problem with shared listening sockets.
718 The symptoms can include application core dumps and kernel panic.
719 Work-around: Run a single FastCGI application server per listening
720 socket.<p>
721
722
723
724 <h3><a NAME = "S6">6. Getting support</a></h3>
725
726 The mailing list <tt>fastcgi-developers@openmarket.com</tt>
727 is used for discussions of issues in developing FastCGI applications.
728 Topics include announcement of FastCGI-capable Web servers or
729 changes to such servers, announcement of new application libraries
730 or changes to such libraries, announcement of known bugs, discussion
731 of design trade-offs in FastCGI application programming, and discussion
732 of development plans and experiences.  To join the
733 list, send a message to
734 <a href = "mailto:fastcgi-developers-request@openmarket.com">fastcgi-developers-request@openmarket.com</a>
735 with a message body consisting of the word "subscribe"
736 (leaving off the quotes).<p>
737
738 Mail sent to this list is archived and available on the
739 World-Wide Web at
740 <pre>
741     http://www.fastcgi.com/mail/
742 </pre>
743
744 Open Market Secure WebServer customers get FastCGI
745 support where they get server support.<p>
746
747
748
749 <hr>
750
751 <address>
752 &#169 1996, Open Market, Inc. / mbrown@openmarket.com
753 </address>
754
755 </body>
756 </html>