ch1intro.htm

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

HTM
582
字号
      <P>         The main task of converting a CGI program into a FastCGI program is separating the initialization code from         the code that needs to run for each request. The structure should look something like this:      </P>      <BR>      <BR><PRE><A NAME="9471">Initialization code</A><A NAME="9472">Start of response loop</A> <A NAME="9473">  body of response loop</A><A NAME="9474">End of response loop</A></PRE>      <A NAME="9475"></A>      <P>         The <EM>initialization code</EM> is run exactly once, when the application is initialized. Initialization code         usually performs time-consuming operations such as opening databases or calculating values for tables or         bitmaps.      </P>      <P>         <A NAME="9477"></A> The <EM>response loop</EM> runs continuously, waiting for client requests to arrive. The         loop starts with a call to <CODE>FCGI_Accept</CODE>, a routine in the FastCGI library. The         <CODE>FCGI_Accept</CODE> routine blocks program execution until a client requests the FastCGI application.         When a client request comes in, <CODE>FCGI_Accept</CODE> unblocks, runs one iteration of the response loop         body, and then blocks again waiting for another client request. The loop terminates only when the system         administrator or the Web server kills the FastCGI application.      </P>      <BR>      <BR>      <H2>         Initial Environment Variables      </H2>      <A NAME="9786"></A>      <P>         When a FastCGI process starts up, it has not yet accepted a request, and therefore none of the CGI environment         variables are set.      </P>      <P>         <A NAME="9787"></A> You set the initial environment of a FastCGI process started by the <CODE>AppClass</CODE>         directive using the <CODE>-initial-env</CODE> option. The process would use this environment to configure its         options and locate files or databases.      </P>      <P>         <A NAME="9829"></A> In FastCGI processes started by the <CODE>AppClass</CODE> directive with the -affinity         option, the <CODE>FCGI_PROCESS_ID</CODE> variable is set in the initial environment (not in the environment of         a request). <CODE>FCGI_PROCESS_ID</CODE> is a decimal number in the range 0 to N - 1 where N is the number of         processes (argument to the <CODE>-processes</CODE> option to <CODE>AppClass</CODE>). The process would use         <CODE>FCGI_PROCESS_ID</CODE> in conjunction with other variables to locate session-related files or databases         during restart.      </P>      <BR>      <BR>      <H2>         Per-Request Environment Variables      </H2>      <A NAME="9481"></A>      <P>         In general, FastCGI uses the same per-request environment variables as CGI, and you access the values of         environment variables in FastCGI applications just as you would in CGI applications. The only differences are         as follows:      </P>      <BR>      <BR>      <UL>         <LI CLASS="c2">            <A NAME="9483"></A>         </LI>         <LI>            In Authorizer FastCGI applications, the Web server unsets the <CODE>PATH_INFO</CODE>,            <CODE>PATH_TRANSLATED</CODE>, and <CODE>CONTENT_LENGTH</CODE> variables. <A NAME="9484"></A>         </LI>         <LI>            In Filter FastCGI applications, the Web server sets two additional environment variables:             <UL>               <LI CLASS="c2">                  <A NAME="9486"></A>               </LI>               <LI>                  <CODE>FILE_LAST_MOD</CODE>: The Web server sets <CODE>FILE_LAST_MOD</CODE> to the date and time that                  filter input file was last modified. The format is the number of seconds since midnight (UTC),                  January 1, 1970. <A NAME="9488"></A>               </LI>               <LI>                  <CODE>FCGI_DATA_LENGTH</CODE>: The application reads at most <CODE>FCGI_DATA_LENGTH</CODE> bytes from                  the data stream before receiving the end-of-stream indication.               </LI>            </UL>            <A NAME="9490"></A>         </LI>         <LI>            FastCGI sets <CODE>FCGI_ROLE</CODE> for each request to <CODE>RESPONDER</CODE>, <CODE>AUTHORIZER</CODE>, or            <CODE>FILTER</CODE>.         </LI>      </UL>      <H2>         Building FastCGI Applications in C      </H2>      <A NAME="9049"></A>      <P>         The Software Development Toolkit that accompanies WebServer 2.0 contains two libraries, fcgi_stdio and         fcgiapp, for building FastCGI applications in C.      </P>      <P>         <A NAME="9723"></A> The fcgi_stdio library implements our philosophy of making FastCGI applications similar to         CGI applications, and provides full binary compatibility between FastCGI applications and CGI applications:         you can run the same C binary as either CGI or FastCGI.      </P>      <P>         <A NAME="9545"></A> The fcgiapp library is more specific to FastCGI, and doesn&#39;t attempt the veneer of         CGI.      </P>      <P>         <A NAME="9731"></A> We recommend that you use the fcgi_stdio library, and this manual describes the routines         in that library. The documentation for the fcgiapp library is in the code in the development kit.      </P>      <BR>      <BR>      <H2>         Building FastCGI Applications in Perl      </H2>      <A NAME="9581"></A>      <P>         To build FastCGI applications in Perl, you need a FastCGI-savvy version of Perl, plus the FastCGI extension to         Perl. We build FastCGI-savvy versions of the Perl interpreter for several common platforms and make them         available on our Website. For details and examples, see Chapter <A HREF="ch3perl.htm#3659">3, &quot;Developing         FastCGI Applications in Perl,&quot; on page 17</A>.      </P>      <BR>      <BR>      <H2>         Building FastCGI Applications in Tcl      </H2>      <A NAME="9586"></A>      <P>         To build FastCGI applications in Tcl, you need a FastCGI-savvy version of Tcl. We build FastCGI-savvy versions         of the Tcl interpreter for several common platforms and make them available on our Website. For details and         examples, see Chapter <A HREF="ch4tcl.htm#3659">4, &quot;Developing FastCGI Applications in Tcl,&quot; on page         19</A>.      </P>      <BR>      <BR>      <H1>         Implementation Details      </H1>      <A NAME="8066"></A>      <P>         The FastCGI application libraries are designed to shield you from the details of the FastCGI design. This         section is designed for the curious reader who would like some low-level understanding. If you are not curious         about the implementation, you can happily skip this section.      </P>      <P>         <A NAME="8554"></A> As shown in the following figure, CGI applications use the three standard POSIX streams         (<CODE>stdin</CODE>, <CODE>stdout</CODE>, and <CODE>stderr</CODE>), plus environment variables, to communicate         with an HTTP server.      </P>      <P>         <A NAME="8359"></A> <IMG ALT="error-file:TidyOut.log" SRC="ch1intra.gif">      </P>      <P>         <A NAME="4295"></A>      </P>      <BR>      <BR>      <H5>         Figure 1:  Flow of Data in CGI      </H5>      <A NAME="9001"></A>      <P>         The fundamental difference between FastCGI and CGI is that FastCGI applications are long-lived, which means         that the Web Server needs to rendezvous with a running application, rather than starting the application in         order to explicitly communicate with it.      </P>      <P>         <A NAME="9110"></A> The FastCGI implementation basically creates a bidirectional connection between two         processes that have no relationship. FastCGI uses a single connection for all the data associated with an         application -- stdin, stdout, stderr, and environment variables. The data on the connection is encapsulated         using a FastCGI protocol that allows stdin and the environment variables to share the same half connection (on         the way in) and stdout and stderr to share the half connection (on the way out).      </P>      <P>         <A NAME="9020"></A> On the input side, the FastCGI application receives data on the connection, unpacks it to         separate stdin from the environment variables and then invokes the application. On the output side, FastCGI         wraps stdout and stderr with appropriate protocol headers, and sends the encapsulated data out to the server.      </P>      <P>         <A NAME="9032"></A> Since a FastCGI application does not always run on the same node as the HTTP server, we         support two implementations of the connection: a <EM>stream pipe</EM><A HREF="#9645"><SUP>1</SUP></A>, for         communications on the same machine, and TCP streams, for communication when the client and the server are on         different machines.      </P>      <P>         <A NAME="8576"></A> <IMG ALT="error-file:TidyOut.log" SRC="ch1inta1.gif">      </P>      <BR>      <BR>      <H5>         Figure 2:  Flow of Data in FastCGI when server and application are on different machines      </H5>      <H2>         The fcgi_stdio Library: I/O Compatibility      </H2>      <A NAME="8977"></A>      <P>         The implementation for I/O compatibility is that the library <CODE>fcgi_stdio.h</CODE> contains macros to         translate the types and procedures defined in stdio.h into the appropriate FastCGI calls. For example,         consider a FastCGI program written in C containing the following line of code:      </P>      <BR>      <BR><PRE><A NAME="5877">fprintf(stdout, &quot;&lt;H2&gt;Aerobic Juggling&lt;/H2&gt;/n&quot;);</A></PRE>      <A NAME="9659"></A> <CODE>fcgi_stdio.h</CODE>      <P>         header file contains the macro      </P>      <BR>      <BR><PRE><A NAME="6403">#define fprintf FCGI_fprintf</A></PRE>      <A NAME="6402"></A>      <P>         So the preprocessor translates the <CODE>fprintf</CODE> call into the following call:      </P>      <BR>      <BR><PRE><A NAME="6411">FCGI_fprintf(stdout, &quot;&lt;H2&gt;Aerobic Juggling&lt;/H2&gt;/n&quot;);</A></PRE>      <A NAME="5888"></A> <CODE>FCGI_fprintf</CODE>      <P>         takes the same arguments as <CODE>fprintf</CODE>.      </P>      <P>         <A NAME="9664"></A> The implementation of FCGI_fprintf tests the file to see if it is a normal C stream or a         FastCGI stream, and calls the appropriate implementation.      </P>      <P>         <A NAME="6463"></A> The <CODE>fcgi_stdio.h</CODE> header file contains macros to translate calls to all ISO         stdio.h routines (and all conventional Posix additions, such as <CODE>fileno</CODE>, <CODE>fdopen</CODE>,         <CODE>popen</CODE>, and <CODE>pclose</CODE>) into their FastCGI equivalents.      </P>      <BR>      <BR>      <H2>         The fcgi_stdio Library: Binary compatibility      </H2>      <A NAME="9579"></A>      <P>         The fcgi_stdio library provides full binary compatibility between FastCGI applications and CGI applications:         you can run the same C binary as either CGI or FastCGI.      </P>      <P>         <A NAME="9580"></A> The implementation is in FCGI_Accept: the FCGI_Accept function tests its environment to         determine whether the application was invoked as a CGI program or an FastCGI program. If it was invoked as a         CGI program, the request loop will satisfy a single client request and then exit, producing CGI behavior.      </P>      <P>         <A NAME="8957"></A>      </P>      <P>      </P>      <HR>      <BR>       <A HREF="cover.htm">[Top]</A> <A HREF="ap_guide.htm">[Prev]</A> <A HREF="ch2c.htm">[Next]</A> <A HREF=      "ap_guida.htm">[Bottom]</A>       <HR>      <BR>       <SUP>1</SUP><A NAME="9645"></A>      <P>         UNIX Network Programming, W. Richard Stevens, 1990 Prentice-Hall, Section 7.9      </P>      <P>         <!-- This file was created with Quadralay WebWorks Publisher 3.0.3 -->         <!-- -->         <!-- For more information on how this document, and how the rest of -->         <!-- this server was created, email yourEmail@xyzcorp.com -->         <!-- -->         <!-- Last updated: 04/15/96 08:00:13 -->      </P>   </BODY></HTML>

⌨️ 快捷键说明

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