Disable Nagle algorithm for TCP based connections because
[catagits/fcgi2.git] / doc / fcgi-perl.gut
CommitLineData
0198fd3c 1Integrating FastCGI with Perl-5
2/fastcgi/words
3fcgi-hd.gif
4[FastCGI]
5<center>Integrating FastCGI with Perl-5</center>
6
7<!--Copyright (c) 1996 Open Market, Inc. -->
8<!--See the file "LICENSE.TERMS" for information on usage and redistribution-->
9<!--of this file, and for a DISCLAIMER OF ALL WARRANTIES. -->
10
11<h5 align=center>
12Copyright &copy; 1996 Open Market, Inc. 245 First Street, Cambridge,
13 MA 02142 U.S.A.<br>
14Tel: 617-949-7000 URL:
15 <a href="http://www.openmarket.com/">http://www.openmarket.com/</a><br>
16$Id: fcgi-perl.gut,v 1.1 1997/09/16 15:36:26 stanleyg Exp $ <br>
17</h5>
18<hr>
19
20<ul type=square>
21 <li><a HREF = "#S1">1. Introduction</a>
22 <li><a HREF = "#S2">2. Perl with sfio and an FCGI module</a>
23 <li><a HREF = "#S3">3. Perl with fcgi_stdio and an FCGI module</a>
24 <ul type=square>
25 <li><a HREF = "#S3.1">3.1 Basic recipe</a>
26 <li><a HREF = "#S3.2">3.2 Semi-advanced recipe</a>
27 <li><a HREF = "#S3.3">3.3 Advanced recipe</a>
28 </ul>
29 <li><a HREF = "#S4">4. Writing FastCGI applications in Perl</a>
30</ul>
31
32
33<H3><A NAME = "S1"> 1. Introduction</A></H3>
34Perl (Practical Extraction and Report Language) is a scripting language that
35is often used for CGI programming. Perl is freely available as a
36source kit.<p>
37
38FastCGI has been integrated with Perl in two different ways:
39<ol>
40 <li>By writing a module that plugs into any Perl interpreter that's
41 been built with sfio, a stdio alternative from AT&T.
42 <li>By writing a module that plugs into any Perl interpreter that's
43 been built with FastCGI's fcgi_stdio library
44 layering over stdio.
45</ul>
46The first approach, implemented by Sven Verdoolaege
47(skimo@breughel.ufsia.ac.be), is probably the better of the two,
48since sfio is a generally useful addition to Perl.
49The second approach, implemented by engineers at Open Market,
50predates the availability of an sfio-integrated
51Perl and demonstrates that the fcgi_stdio library
52can be used with a substantial C application.<p>
53
54The two approaches
55are compatible at the Perl source code level; a Perl
56application developed using
57one approach can be run using the other. And both approaches
58result in a general-purpose Perl interpreter, not a Perl interpreter
59that's only usable for FastCGI applications.<p>
60
61This memo documents both approaches and explains a small
62Perl FastCGI application.<p>
63
64
65<h3><a name ="S2"> 2. Perl with sfio and an FCGI module</a></h3>
66
67As of release 5 patch 3 subpatch 2 (5.003.02), Perl has announced an optional
68support for sfio (safe/fast string/file I/O), which is an alternative
69to stdio that AT&T distributes freely. An advantage of sfio over stdio
70is that sfio provides the ability to implement
71new stream classes that don't simply transfer sequential bytes to or from
72a file descriptor. This flexibility is exactly what FastCGI needs in order
73to implement the standard I/O streams in a manner that's
74transparent to applications.<p>
75
76Perl interpreters incorporating sfio are not widely available in
77binary form, so most likely you'll have to build your own.
78Your build should go smoothly if you follow the instructions
79below. The instructions assume:<p>
80
81<ul>
82 <li>You are building Perl 5.0 patch level 3 subpatch level 2 (5.003.02)
83 or higher. That's the first Perl release to support sfio.<p>
84</ul>
85<P>
86
87Follow these steps to build a Perl with sfio:<p>
88
89<ol>
90 <li>Obtain sfio source code from
91 <a href="ftp://ftp.funet.fi/pub/languages/perl/CPAN/src/misc">
92 ftp://ftp.funet.fi/pub/languages/perl/CPAN/src/misc</a><p>
93
94 <li>Unpack the tar file using <tt>tar xvf</tt> command. <EM>$sfio</EM>
95 will be used as a shorthand for the directory in which sfio package
96 is installed.<p>
97
98 <li>Update your $PATH variable as specified in <tt>$sfio/README</tt> and
99 run <tt>make</tt> command in the <tt>$sfio/src/lib/sfio</tt> subdirectory.<p>
100
101 <li>Rename or delete the file <tt>$sfio/include/stdio.h</tt>, since it may
102 interfere in the further build process.<p>
103
104 <li>Obtain Perl source (version 5 subversion 003 patchlevel 2 or higher) from
105 <a href="http://fohnix.metronet.com/perlinfo/source/5.0/unsupported">
106 http://fohnix.metronet.com/perlinfo/source/5.0/unsupported</a><p>
107
108 <li>Unpack the tar file using <tt>tar xvf</tt> command. <EM>$perl</EM> is
109 used as a shorthand for the directory that is created.<p>
110
111 <li>Configure, build, and install Perl as follows:
112
113<pre>
114% cd $perl
115% ./Configure -Duseperlio -Dusesfio
116% make
117% make test
118% make install
119</pre><p>
120
121There are certain Configure questions that must be answered
122differently when building Perl with sfio:<p>
123
124<DL>
125
126<DT><EM>Perl5 can now use alternate file IO mechanisms to ANSI stdio.
127However these are experimental and may cause problems with some
128extension modules.
129Use stdio as with previous versions? [y] </EM></DT>
130<DD>
131You should answer no.
132</DD><P>
133
134<DT><EM>Any additional cc flags?</EM></DT>
135<DD>
136You should use the following cc flags along with any defaults that Perl
137Configure supplied:
138<UL>
139<LI> <strong>-I<em>$sfio</em>/include</strong>
140</UL>
141</DD><P>
142
143<DT><EM>Any additional ld flags (NOT including libraries):</EM></DT>
144<DD>
145You should specify the following <tt>ld</tt> flags:
146<UL>
147<LI> <strong>-L<em>$sfio</em>/lib</strong>
148</UL>
149</DD><P>
150
151<DT><EM>Additional Libraries:</EM></DT>
152<DD>
153Check that <strong>-lsfio</strong> is one of the specified libraries. Press
154return key to continue.
155</DD><P>
156</DL>
157
158<b>NOTE</b>: If you did not install Perl as a root user, make sure to
159correctly set environment variable <tt>PERL5LIB</tt> to indicate the location
160of Perl libraries. For example, if you installed Perl binary into the
161<tt>$INSTALL</tt> subdirectory and you are running Solaris, the following
162will set your proper library path:
163<pre>
164% setenv PERL5LIB $INSTALL/lib:$INSTALL/lib/sun4-solaris/perl5.003_02
165</pre>
166</ul>
167<p>
168
169 <li>Obtain Perl/Sfio module for FastCGI support from
170 <a href="ftp://ftp.funet.fi/pub/languages/perl/CPAN/authors/id/SKIMO">
171 ftp://ftp.funet.fi/pub/languages/perl/CPAN/authors/id/SKIMO</a><p>
172
173 <li>Unpack FCGI module using <tt>tar</tt> command. We use <tt>$sfiomod</tt>
174 to denote the subdirectory that is created in the process.<p>
175
176 <li>Build and install the module with the following commands:
177<pre>
178% cd $sfiomod
179% $INSTALL/bin/perl Makefile.PL
180% make
181% make test
182% make install
183</pre>
184</ol>
185
186
187<H3><a NAME = "S3">3. Perl with fcgi_stdio and an FCGI module</a></H3>
188
189<H4><a NAME = "S3.1">3.1 Basic recipe</a></H4>
190
191Here are the assumptions embedded in the following recipe:
192<UL>
193
194<LI>You are building Perl 5.0 Patch 2 (5.002) or higher, since
195all examples that are provided are based on that release.
196<P></P>
197
198<LI>You have gcc version 2.7 installed on your system, and use it in the
199build. gcc is convenient because it supports the <tt>-include</tt>
200command-line option that instructs the C preprocessor to include a specific
201file before processing any other include files. This allows you to include
202<tt>fcgi_stdio.h</tt> without modifying Perl source files. (The reason for
203specifying gcc version 2.7 is that I have experienced bad behavior with an
204earlier version and the <tt>-include</tt> flag -- the C preprocessor died
205with SIGABRT.)
206<P></P>
207
208<LI> <EM>$fcgi</EM> is used as shorthand for the full path of the FastCGI
209developers kit.
210</UL>
211<P>
212If those are valid assumptions, follow these steps:
213<OL>
214<LI> Pull the Perl source kit from
215<A HREF="http://www.metronet.com/perlinfo/src/latest.tar.gz">
216http://www.metronet.com/perlinfo/src/latest.tar.gz</A>
217<P>
218There are good sources of information on Perl at:
219<UL>
220<LI> <A HREF="http://www.perl.com/">http://www.perl.com/</A>
221<LI> <A HREF="http://www.metronet.com/perlinfo/">http://www.metronet.com/perlinfo/</A>
222</UL>
223</P>
224</A>
225<LI> Unpack the tar file in the parent directory of the FastCGI kit
226directory, so that the perl directory is a sibling of <tt>fcgi-devel-kit</tt>.
227<EM>$perl</EM> is used as shorthand for the full path of the directory
228in which Perl is installed.
229<p>
230<LI> Copy the version specific and the common files from
231<tt>fcgi-devel-kit/perl-5</tt> into the Perl-5 kit.
232<PRE>
233> cd $perl
234> mv perl.c perl.c.orig
235> mv proto.h proto.h.orig
236> mv Configure Configure.orig
237> cp -r ../fcgi-devel-kit/perl-5/perl5.002/* .
238> cp -r ../fcgi-devel-kit/perl-5/common/* .
239</PRE>
240<P>
241The files you are copying contain the Perl-5 FCGI extension, some
242files modified from the distribution, and a script to simplify the
243configuration process.
244</P>
245<LI> Set environment variables.
246The Perl-5 FastCGI configuration process requires that the environment
247variable <TT>FCGIDIR</TT> be set to the top level directory of the FastCGI
248development kit.
249<PRE>
250> setenv FCGIDIR <EM>$fcgi</EM>
251</PRE>
252If you do not want to use <tt>gcc</tt> to build Perl you can set the
253environment variable <TT>CC</TT> to the desired compiler. For example:
254<PRE>
255> setenv CC gcc2.7
256</PRE>
257By default Perl's installation prefix is /usr/local, so binaries get
258installed in /usr/local/bin, library files get installed into
259/usr/local/lib/perl, etc. If you want to specify a different installation
260prefix set the environment variable <tt>PERL_PREFIX</tt>.
261<PRE>
262> setenv PERL_PREFIX /usr/local/perl5-fcgi
263</PRE>
264<LI> Run fcgi-configure.
265<PRE>
266> ./fcgi-configure
267</PRE>
268<P>
269<TT>fcgi-configure</TT> is a wrapper around Perl's <tt>Configure</tt> script.
270It sets some variables according the the value of some environment variables,
271and runs Perl's <tt>Configure</tt> script
272in such a way that it does not prompt the
273user for any input. 90% of the time this should work without a problem.
274If for some reason this does not work for you, you'll have to
275follow the steps in the next section.<p>
276<LI> Run make.
277<PRE>
278> make
279</PRE>
280<LI> Install the newly built Perl-5.
281<PRE>
282> make install
283</PRE>
284</OL><p>
285
286
287<H4><a NAME = "S3.2">3.2 Semi-advanced recipe</a></H4>
288
289If you do not have experience configuring and building Perl, you
290should find someone who does. Perl can be pretty intimidating to configure
291since it asks you a large number of irrelevant-seeming
292questions that you won't know how to answer.<p>
293<P>
294<OL>
295<LI>Go into the top level directory of the Perl distribution and run
296<tt>Configure</tt>.
297<PRE>
298> cd $perl
299> ./Configure
300</PRE>
301<LI>
302There are some questions that you are going to
303have to answer differently when building FastCGI into Perl.
304These are described below:
305<P></P>
306<DL>
307<DT><EM>Use which C compiler?</EM></DT>
308<DD>
309You should specify <tt>gcc</tt>.
310</DD>
311<P></P>
312<DT><EM>Any additional cc flags?</EM></DT>
313<DD>
314You should use the following cc flags along with any defaults that Perl
315Configure supplied:
316<UL>
317<LI> <strong>-I<em>$fcgi</em>/include</strong>
318<LI> <strong>-include <em>$fcgi</em>/include/fcgi_stdio.h</strong>
319</UL>
320This assumes you are using GCC.
321</DD>
322<P></P>
323
324<DT><EM>Any additional ld flags (NOT including libraries):</EM></DT>
325<DD>
326You should specify the following <tt>ld</tt> flags:
327<UL>
328<LI> <strong>-L<em>$fcgi</em>/libfcgi</strong>
329</UL
330</DD>
331<P></P>
332
333<DT><EM>Additional Libraries:</EM></DT>
334<DD>
335add <strong>-lfcgi</strong> to the list of additional libraries.
336It should be added before -lc.
337</DD>
338<P></P>
339
340<DT><EM>What extensions do you wish to load dynamically?</EM></DT>
341<DD>
342If you can support dynamic extensions, <tt>Configure</tt>
343will ask which of the
344supplied extensions should be loaded dynamically. Since we copied the FCGI
345extension into the Perl source directory it should be one of the ones in the
346default list. If you want FCGI to be dynamically loaded you should specify
347it here, otherwise leave it out.
348</DD>
349<P></P>
350
351<DT><EM>What extensions do you wish to load statically?</EM></DT>
352<DD>
353If you do not support Dynamic extensions this is the only question about
354extensions you would get asked. You should specify FCGI here if you did not
355get asked about dynamic extensions (or did not specify FCGI as a dynamic
356extension).
357</DD>
358</DL>
359<P></P>
360<LI> Copy in the new <tt>proto.h</tt>.
361<P>
362The file proto.h has some macros that conflict with the FastCGI macros.
363The version of <tt>proto.h</tt> supplied in the FastCGI kit
364includes these changes:<p>
365<UL>
366<LI> At the beginning of the file it adds the following lines:
367<PRE>
368#ifdef _FCGI_STDIO
369#undef printf
370#endif
371</PRE>
372<LI> At the bottom it adds:
373<PRE>
374#ifdef _FCGI_STDIO
375#define printf FCGI_printf
376#endif
377</PRE>
378</UL>
379<LI> Copy in the new <tt>perl.c</tt>.
380<P>
381Perl-5.002 has a bug in <tt>perl.c</tt> that has a great
382chance of getting exercised
383with FastCGI. A fix has been sumbitted to the Perl developers and hopefully
384it'll make it into perl-5.003. It was a one line fix, here is a diff for the
385curious:
386<PRE>
387*** perl.c 1996/03/15 17:10:10 1.1
388--- perl.c 1996/03/15 17:11:23
389***************
390*** 405,410 ****
391--- 405,411 ----
392 if (e_fp) {
393 if (Fflush(e_fp) || ferror(e_fp) || fclose(e_fp))
394 croak("Can't write to temp file for -e: %s", Strerror(errno));
395+ e_fp = Nullfp;
396 argc++,argv--;
397 scriptname = e_tmpname;
398 }
399</PRE>
400Pretty straightforward.<p>
401<LI> Build and install Perl.
402<PRE>
403> make
404<EM>[...]</EM>
405> make install
406</PRE>
407</UL>
408</P>
409<H4><a NAME = "S3.3">3.3 Advanced recipe</a></H4>
410
411<P>
412If you already have a Perl-5 package that has been configured, and you do
413not really want to re-run Configure, you can take the following steps.
414</P>
415<P ALIGN=CENTER><STRONG>THIS IS NOT RECOMMENDED</STRONG></P>
416<P>
417Edit config.sh with your favorite editor and modify the following lines:
418<DL>
419<DT><EM>cc</EM></DT>
420<DD>
421Change to use gcc if you are not using it already.
422</DD>
423<P></P>
424
425<DT><EM>ccflags</EM> AND <EM>cppflags</EM></DT>
426<DD>
427Add the following flags:
428<UL>
429<LI> <strong>-I<em>$fcgi</em>/include</strong>
430<LI> <strong>-include <em>$fcgi</em>/include/fcgi_stdio.h</strong>
431</UL>
432This assumes you are using GCC. See the above section on assumptions
433</DD>
434<P></P>
435
436<DT><EM>extensions</EM> AND <EM>known_extensions</EM></DT>
437<DD>
438Add FCGI to the list of extensions
439</DD>
440<P></P>
441
442<DT><EM>ldflags</EM></DT>
443<DD>
444Add -L $fcgi/libfcgi to the list.
445</DD>
446<P></P>
447
448<DT><EM>libs</EM></DT>
449<DD>
450Add -lfcgi to the list of libraries, should be added before -lc.
451</DD>
452<P></P>
453<DT><EM>static_ext</EM><STRONG> or </STRONG><EM>dynamic_ext</EM></DT>
454<DD>
455Add FCGI to the list of statically or dynamically loaded extensions.
456</DD>
457<P></P>
458<DT><EM>d_stdio_cnt_lval, d_stdio_ptr_lval, d_stdiobase, d_stdstdio</EM></DT>
459<DL>
460Change all of these to undef.
461</DL>
462<P>
463One you have edited config.sh you should do a "make Makefile depend all".
464If you're paranoid like me you may want to do a "make clean" first.
465</P>
466
467
468<H3><A NAME = "S4"> 4. Writing FastCGI applications in Perl</A></H3>
469<P>
470The Perl program <tt>examples/tiny-perl-fcgi</tt> performs the same function as
471the C program <tt>examples/tiny-fcgi</tt> that's used as an example in the
472<A HREF="fcgi-devel-kit.html#S3.1.1">FastCGI Developer's Kit document</A>.
473Here's what the Perl version looks like:
474</P>
475<pre>
476#!./perl
477use FCGI;
478$count = 0;
479while(FCGI::accept() >= 0) {
480 print("Content-type: text/html\r\n\r\n",
481 "&lt;title&gt;FastCGI Hello! (Perl)&lt;/title&gt;\n",
482 "&lt;h1&gt;FastCGI Hello! (Perl)&lt;/h1&gt;\n",
483 "Request number ", $++count,
484 " running on host &lt;i&gt;$ENV('SERVER_NAME')&lt;/i&gt;");
485}
486</pre>
487
488If you've built Perl according to the recipe and you have a Web server set
489up to run FastCGI applications, load the FastCGI Developer's Kit Index Page
490in that server and run this Perl application now.<p>
491
492The script invokes Perl indirectly via the symbolic link
493<tt>examples/perl</tt>. It does this because HP-UX has a limit of 32
494characters for the first line of a command-interpreter file such as
495<tt>examples/tiny-perl-fcgi</tt>. If you run on HP-UX you won't want
496to sprinkle symbolic links to perl everywhere, so you should
497choose a <tt>PERL_PREFIX</tt> shorter than <tt>/usr/local/perl5-fcgi</tt>.<p>
498
499You need to be aware of the following bug. If the
500initial environment to a FastCGI Perl application is empty (contains
501no name-value pairs) then when the first call to <tt>FCGI::accept</tt>
502returns, the environment will <i>still</i> be empty,
503i.e. <tt>%ENV</tt> will contain no associations. All the variables
504associated with the first request are lost. There are two known
505workarounds:<p>
506
507<ul>
508 <li>
509 In your Perl application, enumerate <tt>%ENV</tt> using
510 <tt>each</tt> before entering the <tt>FCGI::accept</tt>
511 loop. The program <tt>examples/tiny-perl-fcgi</tt>
512 contains code for this.<p>
513 <li>
514 In configuring your application be sure to set at least one
515 initial environment variable. You do this with the
516 <tt>AppClass -initial-env</tt> directive to the Web server,
517 or by running <tt>cgi-fcgi</tt> in a non-empty environment.
518</ul><p>
519
520The Perl subroutine <tt>FCGI::accept</tt> treats the initial
521environment differently than the C function <tt>FCGI_Accept</tt>. The
522first call to the
523C function <tt>FCGI_Accept</tt> replaces the initial environment with
524the environment of the first request. The first call to the Perl subroutine
525<tt>FCGI::accept</tt> adds the variable bindings of the first request
526to the bindings present in the initial environment. So when the first
527call to <tt>FCGI::accept</tt> returns, bindings from the initial
528environment are still there (unless, due to naming conflicts, some of
529them have been overwritten by the first request). The next call to
530<tt>FCGI::accept</tt> removes the bindings made on the previous call
531before adding a new set for the request just accepted, again preserving
532the initial environment.<p>
533
534The Perl <tt>FCGI</tt> module also includes
535subroutines <tt>FCGI::finish</tt>, <tt>FCGI::set_exit_status</tt>,
536and <tt>FCGI::start_filter_data</tt> that correspond to
537C functions in <tt>fcgi_stdio.h</tt>; see the manpages for
538full information.<p>
539
540Converting a Perl CGI application to FastCGI is not fundamentally
541different from converting a C CGI application. You separate
542the portion of the application that performs one-time
543initialization from the portion that performs per-request
544processing. You put the per-request processing into a loop
545controlled by <tt>FCGI::accept</tt>.<p>
546