Official document:http://www.fastcgi.com/devkit/doc/fcgi-devel-kit.htm
< p>Official website header file:http://www.fastcgi.com/devkit/include/fcgi_stdio.h
Introduction
The FastCGI software development kit is used to simplify the conversion of existing CGI programs into FastCGI programs or to write FastCGI programs.
I/O function package
There are two function packages in the development kit: fcgi_stdio and fcgiapp, which must be in your program Contains one of these packages:
fcgi_stdio.h fcgiapp.h
The fcgi_stdio package is the top-level package of the fcgiapp package, which is transforming CGI Program or writing a new FastCGI program, we strongly recommend you to use it. With this library, your binary programs can be run as CGI or FASTCGI programs (Original: Thefcgi_stdio library provides the easiest transition for C CGI programs and C CGI programmers to FastCGI. Using this library your application can run using either CGI or FastCGI, with the same binary for both situations.).
The fcgiapp package is another fastcgi library. It does not provide as much compatibility as the fcgi_stdio package; therefore, it does not use as many macros. The fcgi_stdio package is a layer running on fcgiapp.
Programs based on the fcgiapp package cannot be run as CGI programs. This feature is only available at the fcgi_stdio layer.
The functions in the fcgiapp package start with FCGX_ instead of start with FCGI_. For example: FCGX_Accept is the fcgiapp version of FCGI_Accept.
The description documents of the functions in the fcgiapp package are included in the header file include/fcgiapp.h in the form of extended comments. The example programs examples/tiny-fcgi2.c and examples/echo2.c explain how to use the fcgiapp package.
(Original:
The fcgiapp library is a second C library for FastCGI. It does not provide the high degree of source code compatibility provided byfcgi_stdio; in return, it does not make such heavy use of#define.fcgi_stdio is implemented as a thin layer on top of fcgiapp.
Applications built using the fcgiapp library cannot run as CGI programs; that feature is provided at thefcgi_stdio level.< /p>
Functions defined in fcgiapp are named using the prefix FCGX_ rather thanFCGI_. For instance,FCGX_Accept tt> is thefcgiapp version ofFCGI_Accept.
Documentation of the fcgiapp library takes the form of extensive comments in the header fileinclude/fcgiapp.h. The sample programsexamples/tiny-fcgi2.c andexamples/echo2.c illustrate how to use< tt>fcgiapp.)
This article mainly introduces the fcgi_stdio package, which has the following advantages:
is simple: as long as there are three A new API needs to be learned.
Easy to understand: If you are converting a CGI program into a FastCGI program, you will find that there are very few differences between a CGI program and a FastCGI program. When we design the function library, we try to make FastCGI applications easy to understand, so that we use the same environment variables, the same query string parsing technology, and the same I when we create a new FastCGI program. /O procedures and so on.
Convenience: This library function provides perfect compatibility between CGI and FastCGI binary files. Therefore, whether it is CGI or FastCGI, it runs the same.
The fastcgi code structure developed by fcgi_stdio package
FastCGI code composition, divide your code into two independent parts:
1. Initialization part: only execute once
2. Response loop part: This part is executed every time the FastCGI script is called ) >= 0) {//loop condition
# response loop body
}
until a client requests it When the FCGI_Accept block is executed, it returns 0. If there is a system failure, or the system administrator terminates the process, Accept will return -1.
If the application is called as a CGI program, the first time Accept is called, it returns 0, and the second time it always returns -1, resulting in CGI behavior. (Please refer to “FCGI_Accept (3)” on page 20 for details)
Note that encourages the use of small scripts in CGI, but encourages the use of combinations in FastCGI Style script. You can reimagine the global structure of your program to get the high performance of FastCGI.
Example 1: TinyFastCGI
This is an example of a simple FastCGI answering program written in C language:
#include "fcgi_stdio.h"#includeint count;void initialize(void){ count=0;}void main(void){ initialize(); while ( FCGI_Accept() >= 0) {printf( "Content-type: text/html " " " " FastCGI Hello! (C, fcgi_stdio library) " "FastCGI Hello! (C, fcgi_stdio library)
" "Request number %d running on host %s ", ++ count, getenv( "SERVER_HOSTNAME") ); }}
Example 2: Raw data generator
Think about it, a response application generates the Nth original data.
A CGI application will have no effective way to solve this problem. For example, if the user accesses the raw data for the 50,000th time, the CGI application is not allowed to start counting from the first raw data until the 50,000th. If the application terminates, the data that she has accumulated with her hard work will also disappear. .
If a client wants to access the 4900th original data, the server must restart accumulation.
Since we can maintain this state, FastCGI applications are more effective for such problems. A FastCGI application can calculate an extended table of source data during the initialization phase and maintain different ranges of the table. When the client requests a particular raw data, the circular response needs to be queried from the table.
Here is an example of the original data code:
# include "fcgi_stdio.h"#include#include #define POTENTIALLY_PRIME 0#define COMPOSITE 1#define VALS_IN_SIEVE_TABLE 1000000#define MAX_NUMBER_OF_PRIME_NUMBERS 78600long int NUMBER_NUMBER prime_void_long int sieve_table; initialize_prime_table(void){ long int prime_counter = 1; long int current_prime = 2, c, d; prime_table[prime_counter] = current_prime; while (current_prime = 0) {printf( "Content-type: text/html " ); printf( " Prime FastCGI " "Prime FastCGI
" ); query_string = getenv( "QUERY_STRING" ); if( query_string == NULL) {printf("Usage: Specify a positive number in the query string. ");} else {query_string = strchr(query_string,'=') + 1; n = strtol(query_string); if (n <1) {printf( "The query string'%s' is not a positive number. ", query_string );} else if (n> MAX_NUMBER_OF_PRIME_NUMBERS) {printf( "The number %d is too large for this program. ", n );} else {printf( "The %ldth prime number is %ld. ", n, prime_table [n] );}} }}
This application has an obvious overhead during initialization, but the subsequent access is fast.
Official document:http://www.fastcgi.com/devkit/doc/fcgi-devel-kit.htm
< p>Official website header file:http://www.fastcgi.com/devkit/include/fcgi_stdio.h
Introduction
The FastCGI software development kit is used to simplify the conversion of existing CGI programs into FastCGI programs or to write FastCGI programs.