CGI (Common Gateway Interface) is a standard interface for Web servers to provide information services. Through this interface, Web servers can execute programs, and return the information output by the program to the browser. Because data on the Web is static, CGI programs can dynamically process viewer requests, such as saving user input information and returning relevant information based on user information. After the client sends a CGI request to the Web server, the web server determines the data transmission method to the CGI program based on the CGI program type, generally, data is transmitted with CGI programs through standard input/output streams and environment variables.
Cgi Input and Output principles
Cgi Input/Output Method: CGI program uses standard input (stdin) and standard output (stdout) for input and output. stdin and stdout are two pre-defined file pointers. You can use the file read/write function to manipulate it.
In addition, the CGI program obtains input through environment variables, except that the environment variables provide some common information, this usually does not include the information you enter on the web page (except when using the get method described below, you can check the environment variable QUERY_STRING to obtain the input data ), stdin is usually used to transmit user input information. In general CGI program development, we need to care about the following environment variables:
Some are environment variables related to Web servers:
- SERVER_NAME Web server name
- Server_port Web server listening address
- Server_protocol Protocol name and version used to send the request
- Server_software Web server name and version
Some of them are related to running CGI:
- Request_method Data transmission (Information Transmission) Method
- Content_length Data Length
- QUERY_STRING Transmitted data
- Remote_addr Customer IP Address
- Remote_host Client Host Name
Some of them are related to the customer:
- Http_user_agent Client browser name
- Http_accept List of MIME types supported by the client
- Http_referer URL of the previous document in the client
The post/get Method Used for input: usually two methods are used for sending data to CGI on the web page: Get/post. The get method attaches the data to the URL and sends the data, for example: /cgi/a_cgi_test.exe? Your_data, CGI program checks the environment variable QUERY_STRING to obtain the input data. The post method sends the data to the stdin input stream of the CGI program. Each variable in form will be sent to the web server in the form of name = value. Multiple Data are separated by &, for example, name = Value & name2 = value2. The name (name, name2) is the input, select, textarea, and other tag names defined in form. The value is the value entered or selected by the user.
With the above knowledge, we can immediately write a simple CGI program. The Code is as follows:
Void main (void) {// This program prints the user input data out of fprintf (stdout, "Content-Type: text/plain/n "); // output a CGI title. The meaning of this line of code will be explained later in char * pszmethod; pszmethod = getenv ("request_method"); If (strncmp (pszmethod, "get ") = 0) {// get method // read the environment variable to obtain the data fprintf (stdout, "input data is:/n % s ", getenv ("QUERY_STRING");} else {// POST method // read stdin to get data int ilength = atoi (getenv ("content_length"); fprintf (stdout, "input data is:/N"); For (INT I = 0; I <ilength; I ++) {char cget = fgetchar (stdin); fputchar (stdout, cget) ;}}>
As mentioned above, a CGI title must be output in the CGI program. There are three types of titles:
- Location: title that specifies the URL to output another document, such as fprintf (stdout, "Location: http://www.vchelp.net//n/n ");
- Content-Type: title, indicating the MIME type of the sent data, such as fprintf (stdout, "Content-Type: text/html/n ");
- Status: title, indicating the HTTP status code, such as fprintf (stdout, "status: 200/n ");
Note that each title must be followed by a line break and an empty line.
MIME types are expressed in the form of types/child types. Below are some common types/child types:
- Text/plain common text type
- Text/HTML text type
- Audio/basic 8-bit audio file format, Suffix:. au
- Video/MPEG file format
- Video/QuickTime file format
- Image/GIF Image File
- Image/JPEG Image File
- Image/X-xbitmap x bitmap graphic file, Suffix:. xbm
With the above knowledge, we can write some CGI programs. First, we need to analyze the input data. The method is: every time a character = is found, it indicates the end of a form variable name; each time the character & is found, it indicates the end of a form variable value. Note that the value of the last variable of the input data does not end. In this way, we can break down the input data into a group of indicators.
However, it will be found that the cgi Input is not rule-based. For example, sometimes a string similar to the following format will appear: filename = Hello & cmd = World + I % 27, this is because the browser encodes some special characters to be uploaded. Therefore, after breaking up the data, the decoding rule is: +: Convert + to a space character; % XX: A special character (%) represented by its hexadecimal ASCII value ). Converts a Value XX to an ASCII character. Perform this conversion for the form variable name and variable value. The following is a CGI program that analyzes form data and sends the result back to the web server.
#include #include #include int htoi(char *); main() { int i,n; char c; printf (″Contenttype: text/plain/n/n″); n=0; if (getenv(″CONTENT-LENGTH″)) n=atoi(getenv(″CONTENT-LENGTH″)); for (i=0; i<n;i++){ int is-eq=0; c=getchar(); switch (c){ case ′&′: c=′/n′; break; case ′+′: c=′ ′; break; case ′%′:{ char s[3]; s[0]=getchar(); s[1]=getchar(); s[2]=0; c=htoi(s); i+=2; } break; case ′=′: c=′:′; is-eq=1; break; }; putchar(c); if (is-eq) putchar(′ ′); } putchar (′/n′); fflush(stdout); } /* convert hex string to int */ int htoi(char *s) { char *digits=″0123456789ABCDEF″; if (islower (s[0])) s[0]=toupper(s[0]); if (islower (s[1])) s[1]=toupper(s[1]); return 16 * (strchr(digits, s[0]) -strchr (digits,′0′)) +(strchr(digits,s[1])-strchr(digits,′0′)); }>
The above program first outputs a MIME header information to the Web server, checks the number of characters in the input, and cyclically checks each character.
When the found character is &, it means the end of a name/value pair, and the program outputs a blank line. When the found character is +, it is converted to null.
If the character is %, it indicates that a two-character hexadecimal value starts. Call the htoi () function to convert the subsequent two characters.
It is an ASCII character. When the character = is found, it means that the end of the name part of a name/value pair, and converts it to a character :.
Finally, output the converted characters to the web server.
To develop a CGI program, follow these steps:
1. Determine whether the data input method is get or post.
2. Read data, separate each received form variable according to the separator and decode the data at the same time.
3. process data.
4. Output CGI title and HTML data.
5. Exit.
Using C language to develop CGI requires you to analyze the input data, but the string processing is not the strength of C language, so I want
We recommend a set of development kits, cgic (available at http://www.boutell.com/boutell/for free ).
I made a few changes to the files provided in the development kit, and used the vc6 author as Lib. After downloading the SDK, you can see what the SDK provides.
Description, this description is very detailed not only provides the example code but also has a detailed explanation of each function.
Some are environment variables related to Web servers:
- SERVER_NAME Web server name
- Server_port Web server listening address
- Server_protocol Protocol name and version used to send the request
- Server_software Web server name and version
Some of them are related to running CGI:
- Request_method Data transmission (Information Transmission) Method
- Content_length Data Length
- QUERY_STRING Transmitted data
- Remote_addr Customer IP Address
- Remote_host Client Host Name
Some of them are related to the customer:
- Http_user_agent Client browser name
- Http_accept List of MIME types supported by the client
- Http_referer URL of the previous document in the client
The post/get Method Used for input: usually two methods are used for sending data to CGI on the web page: Get/post. The get method attaches the data to the URL and sends the data, for example: /cgi/a_cgi_test.exe? Your_data, CGI program checks the environment variable QUERY_STRING to obtain the input data. The post method sends the data to the stdin input stream of the CGI program. Each variable in form will be sent to the web server in the form of name = value. Multiple Data are separated by &, for example, name = Value & name2 = value2. The name (name, name2) is the input, select, textarea, and other tag names defined in form. The value is the value entered or selected by the user.
With the above knowledge, we can immediately write a simple CGI program. The Code is as follows:
Void main (void) {// This program prints the user input data out of fprintf (stdout, "Content-Type: text/plain/n "); // output a CGI title. The meaning of this line of code will be explained later in char * pszmethod; pszmethod = getenv ("request_method"); If (strncmp (pszmethod, "get ") = 0) {// get method // read the environment variable to obtain the data fprintf (stdout, "input data is:/n % s ", getenv ("QUERY_STRING");} else {// POST method // read stdin to get data int ilength = atoi (getenv ("content_length"); fprintf (stdout, "input data is:/N"); For (INT I = 0; I <ilength; I ++) {char cget = fgetchar (stdin); fputchar (stdout, cget) ;}}>
As mentioned above, a CGI title must be output in the CGI program. There are three types of titles:
- Location: title that specifies the URL to output another document, such as fprintf (stdout, "Location: http://www.vchelp.net//n/n ");
- Content-Type: title, indicating the MIME type of the sent data, such as fprintf (stdout, "Content-Type: text/html/n ");
- Status: title, indicating the HTTP status code, such as fprintf (stdout, "status: 200/n ");
Note that each title must be followed by a line break and an empty line.
MIME types are expressed in the form of types/child types. Below are some common types/child types:
- Text/plain common text type
- Text/HTML text type
- Audio/basic 8-bit audio file format, Suffix:. au
- Video/MPEG file format
- Video/QuickTime file format
- Image/GIF Image File
- Image/JPEG Image File
- Image/X-xbitmap x bitmap graphic file, Suffix:. xbm
With the above knowledge, we can write some CGI programs. First, we need to analyze the input data. The method is: every time a character = is found, it indicates the end of a form variable name; each time the character & is found, it indicates the end of a form variable value. Note that the value of the last variable of the input data does not end. In this way, we can break down the input data into a group of indicators.
However, it will be found that the cgi Input is not rule-based. For example, sometimes a string similar to the following format will appear: filename = Hello & cmd = World + I % 27, this is because the browser encodes some special characters to be uploaded. Therefore, after breaking up the data, the decoding rule is: +: Convert + to a space character; % XX: A special character (%) represented by its hexadecimal ASCII value ). Converts a Value XX to an ASCII character. Perform this conversion for the form variable name and variable value. The following is a CGI program that analyzes form data and sends the result back to the web server.
#include #include #include int htoi(char *); main() { int i,n; char c; printf (″Contenttype: text/plain/n/n″); n=0; if (getenv(″CONTENT-LENGTH″)) n=atoi(getenv(″CONTENT-LENGTH″)); for (i=0; i<n;i++){ int is-eq=0; c=getchar(); switch (c){ case ′&′: c=′/n′; break; case ′+′: c=′ ′; break; case ′%′:{ char s[3]; s[0]=getchar(); s[1]=getchar(); s[2]=0; c=htoi(s); i+=2; } break; case ′=′: c=′:′; is-eq=1; break; }; putchar(c); if (is-eq) putchar(′ ′); } putchar (′/n′); fflush(stdout); } /* convert hex string to int */ int htoi(char *s) { char *digits=″0123456789ABCDEF″; if (islower (s[0])) s[0]=toupper(s[0]); if (islower (s[1])) s[1]=toupper(s[1]); return 16 * (strchr(digits, s[0]) -strchr (digits,′0′)) +(strchr(digits,s[1])-strchr(digits,′0′)); }>
The above program first outputs a MIME header information to the Web server, checks the number of characters in the input, and cyclically checks each character.
When the found character is &, it means the end of a name/value pair, and the program outputs a blank line. When the found character is +, it is converted to null.
If the character is %, it indicates that a two-character hexadecimal value starts. Call the htoi () function to convert the subsequent two characters.
It is an ASCII character. When the character = is found, it means that the end of the name part of a name/value pair, and converts it to a character :.
Finally, output the converted characters to the web server.
To develop a CGI program, follow these steps:
1. Determine whether the data input method is get or post.
2. Read data, separate each received form variable according to the separator and decode the data at the same time.
3. process data.
4. Output CGI title and HTML data.
5. Exit.
Using C language to develop CGI requires you to analyze the input data, but the string processing is not the strength of C language, so I want
We recommend a set of development kits, cgic (available at http://www.boutell.com/boutell/for free ).
I made a few changes to the files provided in the development kit, and used the vc6 author as Lib. After downloading the SDK, you can see what the SDK provides.
Description, this description is very detailed not only provides the example code but also has a detailed explanation of each function.