fcgi-tcl.htm

来自「FastCGI,语言无关的、可伸缩架构的CGI开放扩展」· HTM 代码 · 共 367 行 · 第 1/2 页

HTM
367
字号
            <P>            </P>            <UL>               <LI>                  Add the following three lines of code to the function <TT>Tcl_AppInit</TT> after the call to                  <TT>Tcl_Init</TT> and after the comment about calling init procedures: <PRE>    if (FCGI_Init(interp) == TCL_ERROR) {        return TCL_ERROR;    }</PRE>                  This registers four Tcl commands (<TT>FCGI_Accept</TT>, <TT>FCGI_Finish</TT>,                  <TT>FCGI_SetExitStatus</TT>, and <TT>FCGI_StartFilterData</TT>), implemented in <TT>tclFCGI.c</TT>,                  with the Tcl interpreter.                  <P>                  </P>               </LI>            </UL>         </LI>         <LI>            <TT>Makefile.in</TT>:            <P>            </P>            <UL>               <LI>                  Add <TT>tclFCGI.o</TT> to the <TT>GENERIC_OBJS</TT> variable, and add <TT>tclFCGI.c</TT> to the                  <TT>SRCS</TT> variable.                  <P>                     This builds the FastCGI Tcl commands and links them into the Tcl interpreter.                  </P>                  <P>                  </P>               </LI>               <LI>                  Add <TT>-I../fcgi-devel-kit/include -include ../fcgi-devel-kit/include/fcgi_stdio.h</TT> to the                  <TT>CFLAGS</TT> variable.                  <P>                     This includes <TT>fcgi_stdio.h</TT> when compiling C code for the Tcl interpreter, overriding the                     normal <TT>stdio</TT> types, variables, and functions.                  </P>                  <P>                  </P>               </LI>               <LI>                  Add <TT>../fcgi-devel-kit/libfcgi/libfcgi.a</TT> before the <TT>@LIBS@</TT> part of the <TT>LIBS</TT>                  variable.                  <P>                     This links the implementation of <TT>fcgi_stdio.h</TT> into the Tcl interpreter, for use by the                     <TT>FCGI_accept</TT> command and any code that uses <TT>stdio</TT> variables or calls                     <TT>stdio</TT> functions.                  </P>                  <P>                  </P>               </LI>            </UL>            <P>               The last two edits will vary if you use a compiler other than gcc or install the <TT>tcl7.4</TT>               directory somewhere else in relation to the <TT>fcgi-devel-kit</TT> directory.            </P>            <P>            </P>         </LI>         <LI>            <TT>configure.in</TT>:            <P>            </P>            <UL>               <LI>                  Replace the lines <PRE>AC_C_CROSSCC=${CC-cc}</PRE>                  with the lines <PRE>AC_PROG_CCAC_C_CROSS</PRE>                  This selects gcc in preference to other C compilers.                  <P>                  </P>               </LI>               <LI>                  Add the following lines just after the <TT>AC_SUBST(CC)</TT> line: <PRE>AC_CHECK_LIB(socket, main, [LIBS=&quot;$LIBS -lsocket&quot;])AC_CHECK_LIB(nsl, main, [LIBS=&quot;$LIBS -lnsl&quot;])AC_SUBST(LIBS)</PRE>                  This ensures that the socket libraries used by FastCGI are linked into the Tcl interpreter.                  <P>                  </P>               </LI>            </UL>            If GNU autoconf is not available to you, you&#39;ll leave <TT>configure.in</TT> alone and perform the            following steps:            <P>            </P>            <UL>               <LI>                  Execute <PRE>    &gt; SETENV CC gcc</PRE>                  before running <TT>configure</TT>.                  <P>                  </P>               </LI>               <LI>                  If you are running on a SVR4-derived Unix platform, edit <TT>Makefile</TT> to add <TT>-lsocket                  -lnsl</TT> to the <TT>LIBS</TT> value after running <TT>configure</TT>.                  <P>                  </P>               </LI>            </UL>            If you ever re-run <TT>configure</TT>, you&#39;ll need to repeat these steps.            <P>            </P>         </LI>      </UL>      <H3>         <A NAME="S4">4. Writing FastCGI applications in Tcl</A>      </H3>      <P>         The Tcl program <TT>tcl/tiny-tcl-fcgi</TT> performs the same function as the C program         <TT>examples/tiny-fcgi.c</TT> that&#39;s used as an example in the <A HREF="fcgi-devel-kit.htm#S3.1.1">FastCGI         Developer&#39;s Kit document</A>. Here&#39;s what the Tcl version looks like:      </P>      <P>      </P><PRE>#!./tclshset count 0 while {[FCGI_Accept] &gt;= 0 } {    incr count    puts -nonewline &quot;Content-type: text/html\r\n\r\n&quot;    puts &quot;&lt;title&gt;FastCGI Hello! (Tcl)&lt;/title&gt;&quot;    puts &quot;&lt;h1&gt;FastCGI Hello! (Tcl)&lt;/h1&gt;&quot;    puts &quot;Request number $count running on host &lt;i&gt;$env(SERVER_NAME)&lt;/i&gt;&quot;}</PRE>      <P>         If you&#39;ve built Tcl according to the recipe and you have a Web server set up to run FastCGI applications,         load the FastCGI Developer&#39;s Kit Index Page in that server and run this Tcl application now.      </P>      <P>         The script invokes Tcl indirectly via the symbolic link <TT>examples/tclsh</TT>. It does this because HP-UX         has a limit of 32 characters for the first line of a command-interpreter file such as         <TT>examples/tiny-tcl-fcgi</TT>. If you run on HP-UX you won&#39;t want to sprinkle symbolic links to         <TT>tclsh</TT> everywhere, so you should install <TT>tclsh</TT> with a shorter pathname than         <TT>/usr/local/tcl7.4-fcgi/bin/tclsh7.4</TT>.      </P>      <P>         The Tcl command <TT>FCGI_Accept</TT> treats the initial environment differently than the C function         <TT>FCGI_Accept</TT>. The first call to the C function <TT>FCGI_Accept</TT> replaces the initial environment         with the environment of the first request. The first call to the Tcl command <TT>FCGI_Accept</TT> adds the         variable bindings of the first request to the bindings present in the initial environment. So when the first         call to <TT>FCGI_Accept</TT> returns, bindings from the initial environment are still there (unless, due to         naming conflicts, some of them have been overwritten by the first request). The next call to         <TT>FCGI_Accept</TT> removes the bindings made on the previous call before adding a new set for the request         just accepted, again preserving the initial environment.      </P>      <P>         The FastCGI-integrated <TT>tclsh</TT> also includes commands <TT>FCGI_Finish</TT>,         <TT>FCGI_SetExitStatus</TT>, and <TT>FCGI_StartFilterData</TT> that correspond to C functions in         <TT>fcgi_stdio.h</TT>; see the manpages for full information.      </P>      <P>         Converting a Tcl CGI application to FastCGI is not fundamentally different from converting a C CGI         application. You separate the portion of the application that performs one-time initialization from the         portion that performs per-request processing. You put the per-request processing into a loop controlled by         <TT>FCGI_Accept</TT>.      </P>      <P>      </P>      <HR>      <ADDRESS>         <A HREF="mailto:shanzer@openmarket.com">Mike Shanzer // shanzer@openmarket.com</A>      </ADDRESS>   </BODY></HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?