CGI technology and its development

zhaozj2021-02-17  47

As the most important information management and organizational means on the Internet, WWW is constructed from a series of corresponding technologies and applications.

Cheng, its compositional techniques include HTTP, HTML, URL, and CGI. WWW server itself provides some basic success

Enable to complete the client's request and its own management. But different users have different functional requirements, including

Multi-function is the WWW server itself cannot be available (such as online query, etc.), it must provide an expander

Segment to allow users to write extension applications to extend the functionality of the server. CGI (CommongatewayInterface,

That is, a general gateway interface is such a standard extension technology. The following is the basic technology of CGI and its development

have a discussion.

First, CGI technology

1.1cgi proposed

CGI is a standard interface interacting with the external extension application with the WWW server. Outside the CGI standard

Department expansion applications can handle collaborative work data entered by the client (generally WWW browser), completed

The interaction of the client and the server. This is very useful in practical applications. If you can write CGI external expansion

Program to access external databases, client users can make data queries through it and WWW servers. Cgi

Generally divided into two: standard CGI and buffer CGI. All WWW servers should support standard CGI, according to standard CGI

The written program is independent of the specific WWW server. The program written by buffered CGI is related to the WWW server.

1.2cgi work principle

1. Standard CGI

The relationship between the client, server, and the CGI interface and the external program can be represented by Figure 1-1: as shown in the figure above,

The instrument is the channel between the client (such as browser) and the extension. When the client's user has completed certain

Enter the job (such as filling the Form table in the HTML document), issue an HTTP request (called CGI)

Request), after receiving the request, the server daemon has created a child process (called the CGI process).

The CGI child process sets the relevant data of the CGI to environment variables, built between external CGI programs and servers.

Two data channels (standard I / O), then start the URL specified CGI program, and keep the same as the sub process

Step to monitor the execution status of the CGI program. The child process passes the processing result to the server through the standard output stream.

The daemon, the daemon will return the processing results as a response message to the client. External CGI program passed

Environment variables, command line parameters, standard input and output and WWW servers communicate, pass related parameters and

Rational results. * Environment variable: When the server daemon creates a sub-process, set the corresponding

Environment variables and command line parameters to deliver information about clients and servers to the sub-process. *Command Line

Parameters: Command line parameters are only used in the case of IsIndex queries in the HTML document. * Standard input

Out: When the HTTP request mode uses a POST mode, the CGI program comes from standard input streams and related environment variables.

Get client transfer data; if you use GET mode, the CGI program acquires client pass through environment variables.

Data. When the CGI program is to return processing results (generally HTML document) to the client, it passes standard

The output stream passes the result data to the server daemon.

2. Cushion CGI

Standard CGI uses stdin / stdout to perform data communication, this is originally developed (UNIX operation)

The system is determined. But many Windows environments (such as VB and Delphi, etc.) are not supported

In this I / O method, you cannot use them to develop standard CGI applications. Some servers have proposed the concept of buffering CGI. Buffer CGI is also known as WinCGI. At this point, CGI extensions and servers

Communication is made by buffering CGI instead of standard CGI, and the communication between CGI and the server is by standard.

Quasi-CGI interface. The latter is implemented by the built-in buffer processing program of the WWW server. These parts of the relationship can be used

1-2: The working principle of buffering CGI is similar to the standard CGI, and the difference is when the server daemon receives

When the CGI request to the client, the established CGI child process sets the relevant data of the CGI request to an environment variable.

In addition, they are saved in the input buffer; through the buffer handler in the external CGI program and the server

Establish two data channels (input / output buffer). The CGI child process will process the result by the output buffer

Pass to the server daemon. The external CGI program is passed through environment variables and input / output buffers and WWW

The server communicates, passes the relevant parameters and processing results. The meaning of the environment variable here is the same, but this

Some environment variables and their corresponding values ​​are stored in the input buffer. In addition, the client also stores the client in the input buffer.

Transmission data (if used in POST mode). The output buffer is used to store the processing result of the extension.

3. The difference between standard CGI and buffered CGI

For the CGI extension, the most important difference is the I / O of the data: the buffer CGI, the server and CGI

Data exchange between extensions is through a buffer; and the standard CGI is through standard I / O. Use buffer CGI

Choose more development tools to develop the GUI extension under Windows95 and Windows;

The development tools selected by standard CGI must support standard I / O. Only a few WWW servers support buffer CGI,

So based on its expansion program compatibility is not as good as standard CGI.

1.3cgi relationship with other WWW technology

As the standard extension technology of the WWW server, CGI is known from the basic principles of CGI, it and many other

WWW technology is closely related, such as HTTP, HTML, MIME, and URL, etc., the following mainly related to the top two technologies

Study for research.

1. CGI and HTTP protocol

CGI communicates with the client and server via HTTP protocol: * Client user agent sent to the server

The request is an HTTP request message. This message contains the URL value of the CGI extension that handles the user input. * CGI

After the extension is completed, the response to the client is returned to the client is an HTTP acknowledgment message. Therefore, the CGI program is lost

Data must comply with the syntax format of the HTTP response message, which is very important in the development of CGI standards.

2. CGI and HTML language

The CGI extension program output data (HTTP answer message) generally has two types: conforming to MIME type documents (most

Universal is HTML document, expressed as text / html; pointing to the URL link to other documents. These two ways

Both are related to the HTML language, and the organization's organization must comply with the HTML grammar format.

Several questions developed by 1.4cgi

WWW-based human machine interaction generally has two cases: local interaction and interaction through network transmission. The former means

The input data of the client user is handled locally, and then returns the processing result to the user, often

The development tools have JavaScript (Netscape Development) and VBScript (Microsoft Development);

It means that the data entered by the client user is transmitted to the WWW server via the network, and the server will be at the end of the server.

Returning to client users, common development technologies are WWW server extensions (such as CGI, API, etc.). The interaction implementation of data transmission through the network based on standard CGI technology is mainly discussed here. Human machine

The implementation of interaction, there are three main links to be resolved: How to get the data transmitted by the client, how to extract

Effective data and process these data, how to return to the client. The following is combined with relevant technologies, talk about this

Solution of three aspects.

1. Acquisition of client transfer data

As can be seen from the principle of the third chapter CGI, when the server daemon receives the client user agent (such as browser)

When the submitted CGI request, the CGI child process created will set the environment variable related to the CGI request content, and

Estably the channel (ie, standard I / O) communication between the server and the external CGI program. CGI program can pass the environment

Variables, standard I / O or command line parameters Get data input by client users. Data acquisition and request

The HTTP method is related to the requesting method used by the user. The user requests data by CGI

There are three ways: HTMLFORM table, isindex, click on the picture (ISMAP or ImageMaps). Last two

The way is to pass the user's input data by command line parameters; in the C language (the example below),

The CGI program can get these parameter values ​​with ARGC and Argv [INT]. The former way depends on HTTP.

Ask; but no matter what method is used, the environment variable will be used to pass the request content.

● Get environment variables

There are many categories of environment variables, including details of the client and the server. In the general CGI program development,

The following environment variables plays an important role in data transfer.

* GATEWAY-INTERFACE

The version number of the CGI standard interface used by the CGI program. If the CGI1.1 used, this variable is expressed as

"Cgi / 1.1"

* Request-Method

HTTP request method. According to this variable value, it is possible to determine the request method used by the CGI request to determine through stdin.

Or through environmental variable Query-String Get client transfer data.

* Query-string

Query-string is data after "?" In the CGI program URL. When using the IsIndex query or form table

When using a GET method, client transfer data can be obtained by reading the variable.

* Content-Length

Content-length indicates the number of bytes of the client transfer data.

* Content-Type

Content-Type indicates the data encoding type of the client transfer data.

Use the -Environ (int) function to get all environment variables and their values;

The corresponding value of the specified environment variable is obtained using the GetENV (Constchar *) function.

● HTTP request method

The CGI request submitted by the client user agent is an HTTP request, including the HTTP request method. HTTP protocol definition

It is common to have GET and POST in the request method.

The Method property of the client Form table is used to set the request method whose default is Get. If used in Form

Get method, the CGI program obtains the client transfer data through environment variable query-string. If in Form

Using the Post method, the CGI program obtains the number of bytes of the client through Content-Length,

Read the client transfer data via stdin.

2. Extraction and processing of effective data

The general format of the client transmitted data obtained by the above-described manner is: Name [1] = value [1] & name [2] = value [2] &. . . Name [i]

= Value [i]. . . Name [n] = value [n]

(1ι = iι = n)

Where Name [i] represents a variable name, it is the name of a input field in the Form table; Value [i] represents a variable

Value, it is the value entered in a single input field in the Form table. Every pair of "name = value" for client transfer data

The string is separated by '&' characters, and its data encoding type can be obtained from the environment variable content-type. CGI / 1.1

Edition supports "Application / X-WWW-Form-Urlen-Coded" encoding method. This encoding method and

Like the URL encoding, follow two rules: spaces in the data (ASCII code value 32) encodes the ' ' number;

Reserved characters encoded into "% xx" form, "XX" is a hexadecimal representation of the character ASCII value, such as "$"

The encoding is "% 24", "?" Is encoded as "% 3F". Therefore, to obtain the input data of the client user,

The above acquired data must be separated and decoded. Utilize STRTOK (), strChr (), etc.

Data separation processing can be realized, and the decoding of the data needs to scan the entire data string, and the data string

"% Xx" restores the corresponding ASCII code. Many other processing may also be performed after extracting valid data.

Such as database queries, etc. This treatment is the same as normal programming.

3. Return to the client

After the CGI program is over, the response information is passed to the server through the standard output stream, and then the server is returned.

Out of the requested client. The response information of its output is an HTTP response message, which is generally composed of two parts: response

And answer data. Common responses include three headers: Content-Type (Data Coding Type, with MIME Class)

Model representation), Location (URL of a specific document, does not directly output content to the client, output the URL)

And status (status code and status code for processing results). The HTTP response head consists of the same text as a few lines.

The basic format of each line is: "Skull Name: This domain content." Use a holiday and the responses plus LF (or

CR / LF) separation. The response body is the output data of the CGI extension program, and its data type should be with Content-Type

The value is consistent. The output of the CGI program can be implemented with a standard I / O function such as Printf (), PUTS ().

4. Development of CGI procedures and general processes

During the development of the CGI program, you can work according to the actual situation (the interface, actual needs and programmer experience provided by the server)

Wait, select programming language, such as C / C , Perl, TCL, AnyunixShell, VB, AppleScript. If you choose a language such as C / C , you must compile it into an executable;

The corresponding interpreter must be installed.

转载请注明原文地址:https://www.9cbs.com/read-30411.html

New Post(0)