Data interaction between CGI program and web

CGI is the abbreviation of Common Gateway Interface. Through CGI, data interaction between client browser and server can be realized.

The CGI communication system consists of HTML pages displayed on the front end and cgi programs running on the server. The web client displays the relevant html files, which are generally stored in the directory /www. CGI programs are generally files with a .cgi suffix, and the storage path is generally /www.

The structure diagram of the CGI communication system is as follows:


 Step 1: The web client initiates an HTTP request to the server;

Step 2: The environment variable sends data to the CGI program;

Step 3: Redirect CGI standard output to the pipeline, and the server obtains the CGI output data through the pipeline;

Step 4: The server sends the HTTP response result to the client;

In the process of interaction between the web client and the server CGI program, the following conditions need to be preceded: first, parse the GET form, establish environment variables; second, establish a communication channel between the server and the CGI program, and redirect the standard output of the CGI program The pipe read port to the server.

When the client initiates an HTTP request, the browser sends a URL to the server, and the server points to an application program according to the URL. Which cgi programs the server executes is determined by the request initiated by the browser. Each CGI program can only process one user request, and when a CGI program process is activated, an environment variable belonging to the process is created.

The following is an example of a front-end page submission form, the code is as follows:

<div class="login">
<form method="post" action="/checklogin.cgi">
<div class="item user"><input type="text" placeholder="请输入用户名" name="username" id="username"/></div>
<div class="item pass"><input type="password" placeholder="请输入密码" name="password" id="password"/></div>
<div class="btn"><button type="submit">登录</div>
</form>
</div>

Pass the user name and password to the checklogin.cgi program through POST, the user data from the client will be stored in the standard input of the CGI process, and the length of the user data will be assigned to CONTENT_LENGTH in the environment variable, and the client will send it by POST The data has a corresponding MIME type, which is recorded in the environment variable CONTENT_TYPE. Checklogin.cgi verifies the correctness of the user name and password. After verification, the page jumps according to the business logic.

When the CGI program is called by the HTTP server, the environment variables contain a lot of useful information, including the current URL, GET parameters, client IP address, request headers and so on. The relevant environment variables are as follows:

REQUEST_METHOD Information transmission method between server and CGI program
QUERY_STRING Information transmitted when using GET
CONTENT_LENGTH Effective message length in STDIO
CONTENT_TYPE Indicates the MIME type of the message sent
CONTENT_FILE File name used to transmit data when using Windows HTTPd/WinCGI standard
PATH_INFO Path information
PATH_TRANSLATED The full path name of the CGI program
SCRIPT_NAME The name of the CGI program called

The environment variable is a memory area that saves user information. When the client sends a CGI request through the browser, the server looks for the local corresponding CGI program and executes it. While executing the CGI program, the server saves the user's information in environment variables. The CGI program first queries the information transmission method REQUEST_METHOD between the server and the CGI program . If it is POST, it takes the len data from the len of the environment variable and then to the corresponding standard input of the process. If it is GET, the user data is in the QUERY_STRING of the environment variable .

Guess you like

Origin blog.csdn.net/weixin_38293850/article/details/108056184