As the most important information management and organization means on the internet, WWW is structured by a series of corresponding technologies and applications.
Including HTTP, HTML, URL, and CGI. The WWW server itself provides some basic skills
Yes, to complete client requests and management. However, different users have different functional requirements.
The function is not provided by the WWW server itself (such as online query). It must provide an extension
To allow users to write extended applications to expand server functions. CGI (commongatewayinterface,
Is a standard extension technology. The following describes the basic CGI technology and its development.
.
I. cgi Technology
Proposal of 1.1cgi
CGI is a standard interface for external extended applications to interact with WWW servers. External Files written according to CGI standards
The extension application can process the collaborative work data input by the client (generally the WWW browser) to complete
The interaction between the client and the server. This is very useful in practical applications. For example, you can write CGI external extensions.
Program to access the external database, the client user can use it and WWW server for data query. CGI
There are generally two types: Standard CGI and buffer CGI. All www servers should support Standard CGI according to Standard CGI
The compiled program has nothing to do with the specific WWW server. The programs written by the buffer CGI are related to the WWW server.
1.2cgi Working Principle
1. Standard CGI
The relationship between the client, server, CGI interface, and external program can be shown in Figure 1-1, as shown in
A server is a channel between a client (such as a browser) and an extension. When the client user completes
Input work (such as filling the form table in the HTML document) and then send an HTTP request (CGI
After receiving the request, the server daemon creates a sub-process (CGI process ).
The CGI sub-process sets the relevant data of the CGI request to an environment variable and creates
Establish two data channels (standard I/O), start the CGI program specified by the URL, and maintain the same
Step to monitor the execution status of CGI programs. The sub-process passes the processing result to the server through the standard output stream.
The daemon sends the processing result to the client as a response message. The external CGI program passes through
The environment variables, command line parameters, and standard input and output communicate with the WWW server, passing relevant parameters and
Result. * Environment variable: when the server daemon creates a child process to run the CGI program, set the corresponding
Environment Variables and command line parameters to pass information about the client and server to the sub-process. * Command line
Parameter: the command line parameter is only used when an isindex query is available in the HTML document. * Standard input
Output: When the HTTP request mode adopts the POST method, the CGI program uses the standard input stream and related environment variables.
Obtain the data transmitted by the client. For example, if the get method is used, the CGI program directly obtains the data transmitted by the client through the environment variable.
Input data. When the CGI program returns the processing result (generally an HTML document) to the client
The output stream transmits the result data to the server daemon.
2. Buffer CGI
Standard CGI uses stdin/stdout for data communication, which is the initial development environment (UNIX operations
System. However, many programming tools (such as VB and Delphi) in Windows do not support
With this I/O method, you cannot use them to develop standard CGI-based applications. So some
The server puts forward the concept of buffer CGI. The buffer CGI is also known as wincgi. At this time, the CGI Extension Program and the server
The communication between CGI is buffered instead of the Standard CGI, while the communication between CGI and the server is buffered
Quasi-CGI interface. The latter is implemented by the built-in buffer handler of the WWW server. Link availability diagram of these parts
1-2 indicates that the working principle of the buffer CGI is similar to that of the Standard CGI. The difference is that when the server daemon receives
When sending a CGI request to the client, the created CGI sub-process sets the relevant data of the CGI request as an environment variable.
They are also saved in the input buffer. The buffer handler is used between the external CGI program and the server.
Create two data channels (input/output buffer ). The CGI sub-process outputs the processing result through the buffer.
To the server daemon. Here, the external CGI program uses the environment variables and the input/output buffer
The Server communicates with each other and transmits relevant parameters and processing results. The significance of environment variables is the same as above,
Environment variables and their corresponding values are stored in the input buffer. In addition, the input buffer also stores the client
(For example, the post mode is used ). The output buffer is used to store the processing results of the Extended Program.
3. Differences between Standard CGI and buffer CGI
For CGI extensions, the main difference lies in the differences in data I/O: For the buffer CGI, server and CGI
The data exchange between extended programs is through the buffer, while the Standard CGI is through the standard I/O. Use the buffer CGI
Select more development tools to develop GUI extensions under Windows 95 and Windows snt.
Standard CGI development tools must support standard I/O. Only a few www servers support buffer CGI,
Therefore, the compatibility of the extension program based on it is not as good as that of the Standard CGI.
Relationship between 1.3cgi and other WWW Technologies
CGI is a standard extension technology for WWW servers. According to the above CGI basic principle, it and many other
WWW technology is closely related, such as HTTP, HTML, mime, and URL. The following focuses on its relationship with the first two technologies.
Department for research.
1. cgi and HTTP protocols
CGI communicates with the server through http: * The user agent of the client sends
A request is an HTTP request message. The message contains the URL value of the CGI extension that processes user input. * CGI
After processing, the extension returns an HTTP Response Message to the client. Therefore, CGI program input
Output data must conform to the syntax format of the HTTP response message, which is very important in CGI-based development.
2. cgi and HTML language
There are two types of CGI extended output data (HTTP Response Message): mime-type documents (the most
Generally, HTML documents are represented as text/html); URLs that point to other documents. These two methods
Are related to the HTML language, and the data organization must comply with the HTML syntax format.
1.4cgi development issues
Generally, WWW-based human-computer interaction involves local interaction and network transmission. The former refers
The input data of the client user is processed locally on the client, and then the processing result is returned to the user.
See the development tools for JavaScript (Netscape development) and VBScript (Microsoft development ).
The data input by the client user is transmitted to the WWW server over the network.
Results are returned to client users. Common development technologies are WWW Server Extension Technologies (such as CGI and API ).
This article mainly discusses the interaction of data transmission over the network based on the Standard CGI technology. For this type of man-machine
There are three steps to implement Interaction: how to obtain the data transmitted by the client and how to extract
And how to return a response to the client. Next we will talk about this in combination with relevant technologies.
Three problems are solved.
1. Obtain the data transmitted by the client
According to the principles of Chapter 3 CGI, when the server daemon receives a client user proxy (such as a browser)
When a CGI request is submitted, the created CGI sub-process sets environment variables related to the CGI request content, and
Establish a channel for communication between the server and external CGI programs (standard I/O ). CGI programs can use the environment
Variable, standard I/O or command line parameters to obtain the data input by the client user. Data acquisition and request collection
The HTTP method used is related to the request method used by the user. User requests data through CGI 1
There are three methods: htmlform table and isindex. You can click an image (ismap or imagemaps ). Last two
You can use command line parameters to pass user input data. In C language (the example below is also true ),
CGI programs can use argc and argv [int] to obtain these parameter values. The previous method depends on HTTP requests.
However, no matter which method is used, environment variables are used to pass the request content.
● Obtain environment variables
There are many types of environment variables, including detailed information about the client and server. In general CGI program development,
The following environment variables play an important role in data transmission.
* Gateway-Interface
The version number of the CGI standard interface used by the CGI program. For example, in cgi1.1, this variable is
"Cgi/1.1"
* Request-Method
HTTP request method. Based on the variable value, you can determine the request method used by the CGI request to determine whether it is based on stdin.
Or use the environment variable query-string to obtain the data transmitted by the client.
* Query-string
Query-string is "?" in the CGI program URL ″? Data after. When isindex query or form table is used
When the get method is used, the client can transmit data by reading the variable.
* Content-Length
Content-Length indicates the number of bytes transmitted by the client.
* Content-Type
Content-Type indicates the encoding type of the data transmitted by the client.
Use the-environ (INT) function to obtain all environment variables and their values;
Use the getenv (constchar *) function to obtain the corresponding value of the specified environment variable.
● HTTP Request Method
The CGI request submitted by the client user agent is an HTTP request, including the HTTP request method. HTTP Protocol definition
Common Request methods include get and post.
The method attribute of the form table on the client is used to set the request method. The default value is get. If you use
The get method. The CGI program uses the environment variable query-string to obtain the data transmitted by the client. If
Using the POST method, the CGI program obtains the number of bytes of data transmitted by the client through Content-Length,
Use stdin to read data transmitted from the client.
2. Extract and process valid data
The data transmitted by the client obtained through the preceding method is generally in the following format:
Name [1] = value [1] & name [2] = value [2] &... name [I]
= Value [I]... name [N] = value [N]
(1 rows = I rows = N)
Name [I] indicates the variable name, which is the name of an input field in the form table; value [I] indicates the variable.
Value, which is the value entered by the user in an input field of the form table. "Name = value" for each pair of data transmitted by the client ″
The string is separated by '&'. The data encoding type can be obtained from the Content-Type environment variable. CGI/1.1
Only the "Application/X-WWW-form-urlencoded" encoding method is supported. This encoding method and
The URL is encoded in the same way and follows two rules: encode the space (ASCII code value 32) in the data into '+;
Encode the reserved characters in the format of "% xx". "XX" indicates the hexadecimal representation of the ASCII value of the character, for example, "$ ″
Is encoded as "% 24 ″,″? "Is encoded as" % 3f ″. Therefore, to obtain the input data of the client user,
The data obtained above must be separated and decoded. Use functions such as strtok () and strchr ()
Data can be separated, and data decoding requires scanning the entire data string
"% Xx" is restored to the corresponding ASCII code. After extracting valid data, many other operations may be performed,
Such as database query. This processing is the same as normal programming.
3. Return a response to the client
After the CGI program finishes processing, it passes the response information to the server through the standard output stream, and then the server returns the response to
The client that sends the request. The Response Message is an HTTP response message, which consists of two parts: the response header.
And response data. Common Response Headers include three header fields: Content-Type (data encoding type, with mime class
Type), location (the URL of a specific document. In this case, the URL is not output directly to the client)
And status (status code and description of the processing result ). The HTTP Response Header consists of several lines of text in the same format,
The basic format of each line is "header Domain Name: content of this domain ″. An empty line is used to add LF (or
Cr/LF) are separated. The response body is the output data of the CGI extension. Its data type should be consistent with the Content-Type
The values are consistent. CGI program output can be implemented using standard I/O functions such as printf () and puts.
4. cgi program development and general process
The CGI program can be developed based on the actual situation (the interface provided by the server, actual needs, and programmer experience ).
) Select a programming language, such as C/C ++, Perl, TCL, anyunixshell, VB, and applescript. For example
If C/C ++ and other languages are used, they must be compiled into executable files. If Perl and other interpretation languages are used, the server
The interpreter must be installed.