Principles of CGI technology

Source: Internet
Author: User

First, CGI technology

The proposed 1.1 CGI

CGI is a standard interface for external extension applications interacting with the WWW server. External extension applications written in accordance with the CGI standard can handle collaborative work data entered by the client (typically www browser), completing client-server interaction. This is useful in practical applications, such as the ability to write CGI external extensions to access external databases, and the client user can use it and the WWW server to query data. CGI is generally divided into two types: standard CGI and buffered CGI. All WWW servers should support standard CGI, and programs written according to standard CGI are independent of the specific WWW server. The program written by buffered CGI is related to WWW server.

How the 1.2 CGI works

1. Standard CGI

The relationship between the client, server, CGI interface and external programs can be represented in Figure 1-1 (composer: entries): As shown, the server is the channel between the client (such as the browser) and the extender. When a client's user finishes a certain input work (such as populating the form table in an HTML document) and sends an HTTP request (called a CGI request) to the server, a child process (called a CGI process) is created after the server daemon receives the request. The CGI subprocess sets the data for CGI requests to environment variables, establishes two data channels (standard I/O) between the external CGI program and the server, and then initiates the CGI program specified by the URL and synchronizes with the child process to monitor the execution status of the CGI program. The child process passes the processing results to the server daemon through a standard output stream, and the daemon sends the processing results back to the client as a reply message. The external CGI program communicates with the WWW server through environment variables, command line parameters, standard input and output, and passes parameters and processing results.
* Environment variables: When the server daemon creates a child process to run a CGI program, set the appropriate environment variables and command-line arguments to pass information about the client and the server to the child process.
* Command Line arguments: command-line arguments are used only if there are sindex queries in the HTML document.
* Standard input and output: When the HTTP request mode is post, the CGI program obtains the client transfer data through the standard input stream and the environment variables, and when the Get method is used, the CGI program obtains the client transfer data directly through the environment variable. When a CGI program returns a processing result (typically an HTML document) to the client, it passes the resulting data to the server daemon through a standard output stream.

2. Buffered CGI

Standard CGI uses stdin/stdout for data communication, as determined by its initial development environment (Unix operating system). However, many programming tools in Windows environments (such as VB and Delphi) do not support this type of I/O, and they cannot be used to develop standard CGI-based applications. So some servers put forward the concept of buffering CGI. Buffered CGI is also known as wincgi. The CGI extension communicates with the server by buffering CGI instead of the standard CGI, while buffering the communication between the CGI and the server is through the standard CGI interface. The latter is implemented by the built-in buffer handler of the WWW server. The relationship of these parts can be shown in Figure 1-2 (composer: entries): Buffered CGI works similar to standard CGI, but when the server daemon receives a CGI request from the client, the CGI child process sets the data for the CGI request into an environment variable. They are also saved in the input buffer, and the buffer handler establishes two data channels (input/output buffers) between the external CGI program and the server. The CGI subprocess passes the processing results to the server daemon through an output buffer. Here the external CGI program communicates with the WWW server through environment variables and input/output buffers, passing parameters and processing results. The meaning of the environment variable here is the same as above, but these environment variables and their corresponding values are saved in the input buffer. In addition, the input buffer also holds the client's transfer data (for example, in post mode). The output buffers are used to store the processing results of the Extender.

3. The difference between standard CGI and buffered CGI

The main difference for CGI extenders is the difference in the I/O to the data: for buffered CGI, the data exchange between the server and the CGI extender is through the buffer, whereas standard CGI is through standard I/O. Using buffered CGI, you can choose more development tools to develop GUI extensions under WINDOWS95 and WindowsNT, while the development tools chosen using standard CGI must support standard I/O. Only a handful of WWW servers support buffering CGI, so extender compatibility is not as good as standard CGI based on it.

1. 3 The relationship between CGI and other WWW technologies

CGI as a standard extension of the WWW server technology, by the above-mentioned CGI principle, it is closely related to many other WWW technology, such as HTTP, HTML, mime and URL, the following mainly on its relationship with the first two technologies.

1. CGI and HTTP protocol

CGI communicates with the client and the server through the HTTP protocol: * The request sent by the client user agent to the service is an HTTP request message. The message contains a URL value for the CGI extender that handles user input. After the *cgi extender finishes processing, the response returned to the client is an HTTP reply message. Therefore, the output data of the CGI program must conform to the syntax format of the HTTP reply message, which is very important in the development of CGI-based standards.

2. CGI and HTML language

There are generally two types of output data (HTTP reply messages) for CGI extenders: documents that conform to MIME types (most commonly HTML documents, represented as text/html), and URL links to other documents. Both are related to the HTML language, and the organization of the data must conform to the HTML syntax format.

1. 4 Several problems of CGI development

WWW-based human-computer interaction generally has two kinds of situations: local interaction and transmission over the network. The former refers to the client user's input data is processed locally on the client, then returns the processing results to the user, common development tools are JavaScript (Netscape development) and VBScript (Microsoft Development) The latter means that the data entered by the client user is transmitted over the network to the WWW server, and the processing results are returned to the client users after the processing of the server, the common development technology is WWW Server extension technology (such as CGI,API, etc.). This paper mainly discusses the interactive implementation of data transmission over the network based on standard CGI technology. The realization of this kind of human-computer interaction, there are three main aspects need to solve: how to get the data transmitted by the client, how to extract valid data and process the data, how to return the reply to the client. The following is a discussion of the solutions to these three issues, respectively, with relevant technologies.

1. Acquisition of data transmitted by clients

In chapter three, the CGI principle shows that when the server daemon receives a CGI request submitted by a client user agent (such as a browser), the CGI subprocess that is created sets the environment variables associated with the content of the CGI request and establishes a channel (that is, standard I/O) that communicates with the external CGI program. CGI programs can obtain data entered by client users through environment variables, standard I/O, or command-line parameters. The acquisition of data is related to the HTTP method used by the request and the way the user uses the request. There are three ways users can request data via CGI: HtmlForm table, Isindex, clickable image (Ismap or imagemaps). The latter two methods pass the user's input data through command-line arguments, and in the C language (and the example below), CGI programs can use ARGC and Argv[int] to obtain these parameter values. The previous approach depends on the HTTP request method, but regardless of the method used, the environment variable is used to pass the request content.

Gets the environment variable environment variable in a variety of categories, including client and server-side details. In the development of general CGI program, 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. such as the use of CGI1. Version 1, the variable is represented as "cgi/1.1"

*request-method HTTP request method. Depending on the value of the variable, the request method used by the CGI request can be judged to determine whether the client transmits data through stdin or through the environment variable query-string.

*query-string

Query-string is in the CGI program URL "?" After the data. When using the Get method with a Isindex query or form table, the client transmits data that can be obtained by reading the variable.

*content-length

Content-length represents the number of bytes that the client transmits data.

*content-type

Content-type represents the type of data encoding the client transmits data to. All environment variables and their values can be obtained using the-environ (int) function, and the corresponding values of the specified environment variables can be obtained using the getenv (constchar*) function.

HTTP request method

The CGI request submitted by the Client User agent is an HTTP request, which includes the HTTP request method. The HTTP protocol defines a request method that is commonly used primarily for get and post. The method property of the Client form table is used to set the request methods whose default value is get. If you use the Get method in a form, the CGI program obtains the client transfer data through the environment variable query-string. If you use the Post method in a form, the CGI program transmits the data through the stdin read client by content-length the number of bytes that the client transmits.

2. Extraction and processing of effective data

The general format of the client-transmitted data obtained by the above method is:

Name[1]=value[1]&name[2]=value[2]&...name=value ...name[n]=value[n]

  
Where Name[i] represents the variable name, which is the name of an input field in the form table; Value[i] represents the value of the variable, which is the value entered by the user in an input field in the form table. Each pair of ″name=value″ strings that the client transmits data is delimited by the ′&′ character, and its data encoding type can be obtained from the environment variable Content-type. Version cgi/1.1 only supports the "application/x-www-form-urlencoded" encoding method. This encoding is the same as the URL encoding, followed by two rules: The Data Space (ASCII code value 32) encoded into ′+′ number, reserved characters encoded into ″%xx″ form, ″xx″ is the hexadecimal representation of the ASCII value of the character, such as ″$″ encoding ″%24″,″?″ encoded as ″ %3f″. Therefore, to obtain the input data of the client user, the data obtained above must be separated and decoded. Using function Strtok (), STRCHR () and so on can realize the data separation processing, and the decoding of the data needs to scan the whole data string, and restore the ″%xx″ in the data string to the corresponding ASCII code. After extracting valid data, many other processes, such as database queries, are also possible. This is the same process as normal programming.

3. Returning an answer to the client

After the CGI process finishes processing, the reply message is passed to the server through the standard output stream and returned by the server to the requesting client. Its output response information is an HTTP reply message, which generally consists of two parts: the answer header and the answer data. Common answer headers include three header fields: Content-type (data encoding type, represented by MIME type), location (URL of a particular document, which does not output the URL directly to the client) and status (status code and status description of the processing result). The HTTP reply header consists of several lines of identical text, with the basic format of each line: ″ header domain: The domain content ″. Between the answering head and the response body is separated by a blank line plus LF (or CR/LF). The response body is the output data of the CGI extender, and its data type should be consistent with the Content-type value. The output of the CGI program can be implemented using standard I/O functions such as printf (), puts ().

4. Development and general flow of CGI programs

In the process of developing a CGI program, you can choose a programming language, such as C/c++,perl,tcl,anyunixshell,vb,applescript, based on the actual situation (server-provided interfaces, actual requirements, and programmer experience). If you use languages such as C + +, you must compile the executable file, and if you choose to interpret languages such as Perl, the server must have the appropriate interpreter installed.

Principles of CGI technology

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.