CGI technology and its development (1)

zhaozj2021-02-16  46

CGI technology and its development (1)

Sender: Pitts (Pain and Happy), News District: Delphi Title: CGI Technology and Its Development Station: BBS Shuimu Tsinghua Station (Tue Oct 13 10:00:13 1998) As the most important information management on the Internet and Organizational means, WWW consists of a series of corresponding technologies and applications, and its compositional techniques include HTTP, HTML, URL, and CGI. The WWW server itself provides some basic functions to complete the client's request and its own management. However, different users have different functional requirements, with many of these features that the WWW server itself cannot be provided (such as online query, etc.), which must provide an extended means to allow users to write extension applications to extend the functionality of the server. The CGI (CommONAnterface, the general gateway interface) is such a standard extension technology. The basic techniques and development thereof of CGI are discussed below. I. CGI technology 1.1cgi proposes CGI is a standard interface for external extension applications and WWW servers interact. The external extension application written in accordance with the CGI standard can handle the collaborative work data entered by the client (generally WWW browser) to complete the interaction of the client and the server. This is very useful in practical applications. If you can write CGI external extensions to access external databases, client users can perform data queries through it and WWW servers. CGI is generally divided into two: standard CGI and buffer CGI. All WWW servers should support standard CGIs, and the program written in standard CGI is independent of the specific WWW server. The program written by buffered CGI is related to the WWW server. 1.2cgi working principle 1. Standard CGI client, server, CGI interface and external program relationship can be represented by Figure 1-1: As shown above, the server is the channel between the client (such as browser) and the extension. When the client's user completes a certain input work (such as filling the Form table filling in the HTML document), then the server daemon receives the request after receiving the request, it creates a child process (called CGI process). The CGI child process sets the relevant data of the CGI request to an environment variable. Two data channels (standard I / O) are established between the external CGI program and the server, then start the URL specified CGI program, and keep synchronization with the sub-process. Monitor the execution status of the CGI program. The child process passes the processing result to the server daemon through the standard output stream, and the daemon will then send the processing results as a reply message to the client. The external CGI program communicates with the WWW server through environment variables, command line parameters, standard input and output, passing the parameters and processing results. * Environment variable: When the server daemon creates a child process runs the CGI program, set the corresponding environment variable and command line parameters to deliver information about the client and the server 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 and output: When the HTTP request mode uses a POST mode, the CGI program obtains client transfer data through standard input streams and related environment variables; if using Get mode, the CGI program acquires client transfer data through environment variables. When the CGI program returns to the processing result (typically HTML document) to the client, it passes the result data to the server daemon through the standard output stream. 2. Buffer CGI Standard CGI uses stdin / stdout to perform data communication, which is determined by its initial development environment (UNIX operating system).

However, many Windows environments (such as VB and Delphi et al.) Are not supported in this I / O method, and they cannot be used to develop standard CGI applications. Some servers have proposed the concept of buffering CGI. Buffer CGI is also known as WinCGI. At this point, the CGI extension and the server are communicating between the CGI instead of the standard CGI, and the communication between the cgi and the server is through standard CGI interfaces. The latter is implemented by the built-in buffer processing program of the WWW server. The relationship between these parts can be represented by Figure 1-2: The working principle of buffering CGI is similar to the standard CGI, and the established CGI child process sets the relevant data settings of the CGI request when the server daemon receives the client's CGI request. In addition to environment variables, they are saved in the input buffer; two data channels (input / output buffers) are established between the external CGI program and the server through the buffer handler. The CGI child process passes the processing result to the server daemon by the output buffer. Here the external CGI program communicates with the WWW server through the environment variable and the input / output buffer, passes the relevant parameters and processing results. The meaning of environment variables here is the same, but these environment variables and their corresponding values ​​are stored in the input buffer. In addition, the input buffer also stores the transfer data of the client (if POST mode is used). The output buffer is used to store the processing result of the extension. 3. The difference between the standard CGI and the buffered CGI is for the CGI extension. The most important difference is the data of the data I / O. Data exchange between the buffer CGI, the server and the CGI extension is through the buffer; and the standard CGI is through standard I / O. Use buffer CGI to select more development tools, you can develop the GUI extension under Windows95 and Windows; and the development tools selected using standard CGI must support standard I / O. Only a few WWW servers support buffer CGI, so it is worse than standard CGI based on its extension compatibility. 1.3cgi relationship with other WWW technology CGI as standard expansion technology of WWW servers, is known from the basic principles of CGI, which is closely related to many other WWW technology, such as HTTP, HTML, MIME, and URL, etc. Studies with the relationship between the first two technologies. 1. CGI and HTTP protocol CGI communicate through the client and server through the HTTP protocol: * The client-side user agent sent to the server is an HTTP request message. This message contains the URL value of the CGI extension that handles the user input. * The CGI extension After the processing is over, the response returns to the client is an HTTP acknowledgment message. Therefore, the CGI program output 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 CGI Extended Output Data (HTTP Answers Message) generally have two: Document that meets MIME types (the most common HTML document, representation as text / html); point to the URL link to other documents. Both of these methods are related to the HTML language, and the organization's organization must comply with HTML grammar format. 1.4CGi development Several issues generally have two cases of human-computer interaction: local interaction and interaction through network transmission.

The former refers to the client user's input data to process locally, and then returns the processing result to the user, and the common development tool has JavaScript (Netscape development) and VBScript (Microsoft development); the latter refers to the client user input. The data is transferred to the WWW server via the network, and the processing result is returned to the client user after the server processing, and the common development technology is WWW server extension technology (such as CGI, API, etc.). The interaction implementation of data transmission through the network based on standard CGI technology is mainly discussed here. For this human-computer interaction, there are three links that need to be resolved: how to obtain data transmitted by the client, how to extract valid data and process these data, how to return to the client. The following is a combination of related technologies, talking about the solutions of these three aspects. 1. The acquisition of client transfer data is known from Chapter 3 CGI. When the server daemon receives the CGI request submitted by the client user agent (such as browser), the CGI child process sets related to the CGI request content. Environment variables and establish channels (ie, standard I / O) between servers and external CGI programs. The CGI program can obtain data input by client users through environment variables, standard I / O or command line parameters. The data acquisition is related to the HTTP method used by the request, and the request method used by the user. Users generally have three ways through CGI request data: HTMLFORM table, isindex, click on the picture (ISMAP or ImageMaps). The latter two ways are input data from the user through the command line parameters; in the C language (the example below), the CGI program can obtain these parameter values ​​with ARGC and Argv [INT]. The in the previous way depends on the HTTP request method; but no matter what method is used, the environment variable will be used to pass the request content. ● Get a lot of environment variable 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. * The version number of the CGI standard interface used by the GATEWAY-INTERFACE CGI program. If the CGI1.1 used is used, the variable is represented as "CGI / 1.1" * Request-Method HTTP request method. The request method used by the CGI request is determined based on the variable value to determine whether the client transfer data is obtained through the STDIN or through environment variable Query-String. * Query-string query-string is the data after "?" In the CGI program URL. When using the IsIndex query or Form table using the GET method, client transfer data can be obtained by reading the variable. * Content-Length Content-Length indicates the number of bytes of the client transferred data. * Content-Type Content-Type indicates the data encoding type of the client transfer data. All environment variables and their values ​​can be obtained using the -Environ (int) function; the corresponding value of the specified environment variable can be obtained using the GetENV (Constchar *) function. ● HTTP Request Method The CGI request submitted by the client user agent is an HTTP request, including an HTTP request method. The main use of the HTTP protocol definition is commonly used in GET and POST. The Method property of the client Form table is used to set the request method whose default is Get.

If you use the get method in Form, the CGI program obtains the client transfer data through the environment variable query-string. If you use a Post method in Form, the CGI program reads the client transfer data via the Stdin by obtaining the number of bytes of the client through the Content-length. 2. Extraction of effective data and processing the general format of client transmission data obtained by the above-described manner: Name [1] = value [1] & name [2] = value [2] &. . . Name [i] = value [i]. . . Name [n] = value [n] (1ι = iι = n) where Name [i] represents the variable name, it is the name of a input field in the Form table; Value [i] represents a variable value, it is the user in Form The value entered in a input field in the table. Each pair of "name = value" serials of the client transfer data is separated by '&' characters, and its data encoding type can be obtained from environment variable content-type. CGI / 1.1 supports "Application / X-WWW-Form-Urlen-Coded" encoding method. Like the encoding method of the URL, follow the two rules: Space in the data (ASCII code value 32) encodes a ' ' number; reserved character encoding into "% xx" form, "XX" is the character ASCII The hexadecimal representation of the value is "% 24", "?", "?", "% 3F". Therefore, to obtain the input data of the client user, the above-described data must be separated and decoded. Data separation processing can be implemented using functions, and Strchr (), and the data is decoded to scan the entire data string, and "% XX" in the data string is restored to the corresponding ASCII code. After extracting the valid data, many other processing, such as database queries, etc. This treatment is the same as normal programming. 3. After returning to the client, the response information is passed to the server through the standard output stream, and then returned by the server to the requesting client. The response information of its output is an HTTP response message, which is generally composed of two parts: response and response data. Common responses include three headers: Content-Type (Data Coding Type, Expressed with MIME Type), Location (URL of a specific document, does not directly output content to the client) and STATUS (processing The status code and state description of the result). The HTTP response head consists of the same text as the same line, and the basic format of each line is: "Skull Name: Domain Content." The response head and the responses are separated by one ordered LF (or CR / LF). The responding body is the output data of the CGI extension program, and its data type should be consistent with the content-type value.

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

New Post(0)