📄 fcgi-java.htm
字号:
<I>fcgiapp</I> C library. To understand where these methods occur we need to look briefly at the FastCGI redefinition of standard I/O. </P> <P> Java defines standard I/O in the <I>java.System</I> class as follows: </P> <P> public static InputStream in = new BufferedInputStream(new FileInputStream(FileDescriptor.in), 128);<BR> public static PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out), 128), true);<BR> public static PrintStream err = new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.err), 128), true); </P> <P> The File Descriptors <I>in</I>, <I>out</I>, <I>err</I> are constants set to 0, 1 and 2 respectively. </P> <P> The FastCGI interface redefines <I>java.System in, out</I>, and <I>err</I> by replacing the File streams with Socket streams and inserting streams which know how to manage the FastCGI protocol between the Socket streams and the Buffered streams in the above definitions. </P> <P> For those cases where the FCGI application needs to bypass the standard I/O streams, it can directly access the methods of the FCGI input and output streams which roughly correspond to the functions in the C <I>fcgiapp</I> library. These streams can be accessed via the <I>request</I> class variable in FCGIInterface. Each Request object has instance variables that refer to an FCGIInputStream, and to two FCGIOutputStreams associated with that request. </P> <H3> <A NAME="S4">4. Environment Variables</A> </H3> <P> Java does not use the C <I>environ</I> list. Nor is there a <I>getenv</I> command that reads system environment variables. This is intentional for reasons of portability and security. Java has an internal dictionary of properties which belongs to the System class. These System properties are <I>name/value</I> associations that constitute the Java environment. When a Java application starts up, it reads in a file with default properties. As we have seen, additional System properties may be inserted by using the -D <I>Java</I> command argument. </P> <P> For CGI, where the Java application is invoked from a .cgi script that, in turn, invokes the Java interpreter, this script could read the environment and pass the variables to the Java application either by writing a file or by creating -D options on the fly. Both of these methods are somewhat awkward. </P> <P> For FastCGI Java applications, the environment variables are obtained from the FastCGI web server via <TT>FCGI_PARAMS</TT> records that are sent to the application at the start of each request. The FastCGI interface stores the original startup properties, combines these with the properties obtained from the server, and puts the new set of properties in the System properties dictionary. The only parameter that has to be specifically added at startup time is the FCGI_PORT parameter for the Socket creation. In the future, we expect that even this parameter won't be needed, since its use is due to an acknowledged rigidity in the JDK's implementation of sockets. </P> <P> </P> <H3> <A NAME="S4">5. Further examples: EchoFCGI and Echo2FCGI</A> </H3> <P> The next two examples illustrate the points made in the last two sections. EchoFCGI and Echo2FCGI both echo user input and display the application's environment variables. EchoFCGI reads the user input from System.in, while Echo2FCGI reads the user input directly from the intermediate FastCGI input stream. </P> <H4> A. EchoFCGI </H4><PRE>import FCGIInterface;import FCGIGlobalDefs;import java.io.*;class EchoFCGI { public static void main (String args[]) { int status = 0; while(new FCGIInterface().FCGIaccept()>= 0) { System.out.println("Content-type: text/html\n\n"); System.out.println("<html>"); System.out.println( "<head%gt;<TITLE>FastCGI echo </TITLE></head>"); System.out.println("<body>"); System.out.println( "<H2>FastCGI echo</H2>"); System.out.println("<H3>STDIN</H3>"); for ( int c = 0; c != -1; ) { try { c = System.in.read(); } catch(IOException e) { System.out.println( "<br><b>SYSTEM EXCEPTION"); Runtime rt = Runtime.getRuntime(); rt.exit(status); } if (c != -1) { System.out.print((char)c); } } System.out.println( "<H3>Environment Variables:</H3>"); System.getProperties().list(System.out); System.out.println("</body>"); System.out.println("</html>"); } } }</PRE> <H4> B. Echo2FCGI </H4><PRE>import FCGIInterface;import FCGIGlobalDefs;import FCGIInputStream;import FCGIOutputStream;import FCGIMessage;import FCGIRequest;import java.io.*;class Echo2FCGI { public static void main (String args[]) { int status = 0; FCGIInterface intf = new FCGIInterface(); while(intf.FCGIaccept()>= 0) { System.out.println("Content-type: text/html\n\n"); System.out.println("<html>"); System.out.println( "<head><TITLE>FastCGI echo </TITLE></head>"); System.out.println("<body>"); System.out.println("<H2>FastCGI echo</H2>"); System.out.println("<H3>STDIN:</H3">); for ( int c = 0; c != -1; ) { try { c = intf.request.inStream.read(); } catch(IOException e) { System.out.println( "<br><b>SYSTEM EXCEPTION"); Runtime rt = Runtime.getRuntime(); rt.exit(status); } if (c != -1) { System.out.print((char)c); } } System.out.println( "<H3>Environment Variables:</H3>"); System.getProperties().list(System.out); System.out.println(<"/body>"); System.out.println("</html>"); } } }</PRE> <H4> C. Running these Examples </H4> <H5> Configuring </H5> <P> As with TinyFCGI, you need to configure the web server to recognize these two FastCGI applications. Your configuration now looks like this: </P> <P> </P><PRE>ExternalAppClass java1 -host hostname:portNumResponder java1 fcgi-devel-kit/examples/TinyFCGIExternalAppClass java2 -host hostname:portNumAResponder java2 fcgi-devel-kit/examples/EchoFCGIExternalAppClass java3 -host hostname:porNumBResponder java3 fcgi-devel-kit/examples/Echo2FCGI</PRE> <P> Note that the application classes and port numbers are different for each application. </P> <H5> Running </H5> <P> As with TinyFCGI, you need to run these programs with the -D option using FCGI_PORT and the appropriate port number. To get some data for standard input we have created two html pages with forms that use a POST method. These are echo.html and echo2.html. You must edit these .html files to expand the path to <I>fcgi-devel-kit/examples</I> to a full path. Once the appropriate Java program is running, point your browser at the corresponding HTML page, enter some data and select the <I>go_find</I> button. </P> <H3> <A NAME="S6">6. FastCGI Java Classes</A> </H3> <P> The Java FastCGI classes are included in both source and byte code format in <I>fcgi-devel-kit/java/src</I> and :<I>fcgi-devel-kit/java/classes</I> respectively. The following is a brief description of these classes: </P> <P> </P> <DL> <DT CLASS="c4"> FCGIInterface </DT> <DD> This class contains the FCGIaccept method called by the FastCGI user application. This method sets up the appropriate FastCGI environment for communication with the web server and manages FastCGI requests.<BR> </DD> <DT CLASS="c4"> FCGIInputStream </DT> <DD> This input stream manages FastCGI internal buffers to ensure that the user gets all of the FastCGI messages associated with a request. It uses FCGIMessage objects to interpret these incoming messages.<BR> </DD> <DT CLASS="c4"> FCGIOutputStream </DT> <DD> This output stream manages FastCGI internal buffers to send user data back to the web server and to notify the server of various FCGI protocol conditions. It uses FCGIMessage objects to format outgoing FastCGI messages.<BR> </DD> <DT CLASS="c4"> FCGIMessage </DT> <DD> This is the only class that understands the actual structure of the FastCGI messages. It interprets incoming FastCGI records and constructs outgoing ones..<BR> </DD> <DT CLASS="c4"> FCGIRequest </DT> <DD> This class currently contains data fields used by FastCGI to manage user requests. In a multi-threaded version of FastCGI, the role of this class will be expanded.<BR> </DD> <DT CLASS="c4"> FCGIGlobalDefs </DT> <DD> This class contains definitions of FastCGI constants. </DD> </DL> <HR> <ADDRESS> <A HREF="mailto:harris@openmarket.com">Steve Harris // harris@openmarket.com</A> </ADDRESS> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -