CGI program design using C language technology

Source: Internet
Author: User
CGI program design with C language technology I. CGI Overview

CGI (Public Gateway Interface) specifies the interface protocol standard for Web servers to call other executable programs (CGI program. Web servers interact with Web browsers by calling CGI programs
,
That is, the CGI program receives the information sent from the Web browser to the Web server for processing, and then returns the response to the Web server and Web browser. CGI programs generally complete Web pages
Form data processing, database query, and integration with traditional application systems. CGI programs can be written in any programming language, such as Shell script language, Perl,
Fortran, Pascal, and C language. However, CGI programs written in C language have the features of fast execution speed and high security (because C language programs are compiled and executed and cannot be modified.
The CGI interface standard consists of three parts: standard input, environment variable, and standard output.
1. Standard Input
CGI
Like other executable programs, a program can obtain input information from a Web server through standard input (stdin), such as data in Form. This is the so-called POST
Method. This means that the CGI program can be executed in the command line status of the operating system to debug the CGI program. The POST method is a common method. This article will take this method as an example to analyze the method of CGI program design.
Methods, processes, and skills.
2. Environment Variables
The operating system provides many environment variables that define the execution environment of the program and the application can access them. Web server and CGI
The interface also sets some of its own environment variables to pass some important parameters to the CGI program. The cgi get method also uses
The environment variable QUERY-STRING transmits the data in Form to the CGI program.
3. standard output
The CGI program transmits the output information to the Web server through the standard output (stdout. The information sent to the Web server can be in various formats, usually in plain text or HTML text, so that we can debug CGI programs in the command line status and get their output.
Below is a simple CGI program that outputs Form information in HTML directly to a Web browser.

Code#include<stdio.h> 
#include<stdlib.h>
void main() { int,i,n; printf (″Contenttype:text/plain ″); n=0; if(getenv(″CONTENT-LENGTH″)) n=atoi(getenv(CONTENT-LENGTH″)); for (i=0;i putchar(getchar()); putchar (′ ′); fflush(stdout); }

The following is a brief analysis of this program. Prinft ("Contenttype: text/plain
″);

This line uses the standard output to convert the string "Contenttype: text/plain"
"Sent to the Web server. It is a MIME header that tells the Web server that subsequent output is in plain ASCII text format. Note that there are two new line characters in this header, because
The Web server needs to see a blank line before the actual text information starts.
If (getenv ("CONTENT-LENGTH ″))
N = atoi (getenv ("CONTENT-LENGTH ″));

This line first checks whether the CONTENT-LENGTH environment variable exists. The Web server sets this environment variable when calling a CGI program using the POST method, and its text value represents
The number of characters in the input sent by the Web server to the CGI program, so we use the function atoi ()
Convert the value of this environment variable to an integer and assign it to Variable n. Note that the Web server does not terminate its output with the file Terminator, so if you do not check the environment variable CONTENT-
The CGI program cannot know when the input is complete.
For (I = 0; I <n; I ++)
Putchar (getchar ());

This row starts from 0 cycles to (CONTENT-LENGTH-1) and copies each character read in the standard input directly to the standard output, that is, all input is sent back to the Web server in ASCII format.
In this example, we can summarize the general working process of the CGI program into the following points.
1. Check the CONTENT-LENGTH environment variable to determine the number of inputs;
2. Use getchar () or other file read functions cyclically to obtain all input values;
3. process the input in a corresponding way;
4. Use the header "Contenttype:" To tell the Web server the format of the output information;
5. Use printf (), putchar (), or other file write functions to send the output to the Web server.
In short, the main task of the CGI program is to get the input information from the Web server for processing, and then send the output result back to the Web server.

II. Environment Variables
Environment variables are text strings (name/value pairs) that can be set by OS Shell or other programs or accessed by other programs. They are simple means for Web servers to pass data to CGI programs. They are called environment variables because they are global variables and can be accessed by any program.
The following are some environment variables that are often used in CGI programming.
HTTP-REFERER: the URL of the web page that calls the CGI program.
REMOTE-HOST: The machine name and domain name of the Web browser that calls the CGI program.
REQUEST-
METHOD: refers to the METHOD used when the Web server transmits data to the CGI program, divided into GET and POST methods. The GET method only uses environment variables (such as QUERY-
STRING) transmits data to the CGI program, while the POST method transmits data to the CGI program through environment variables and standard input. Therefore, the POST method can easily transmit a large amount of data to the CGI program.
.
SCRIPT-NAME: the CGI program NAME.
QUERY-STRING: When the POST method is used, the data in Form is placed in QUERY-STRING and passed to the CGI program.
CONTENT-TYPE: mime type passed to CGI program data, usually "applica tion/x-www-form-url encodede ″, it is the data encoding type that passes data to CGI programs through the POST method in HTML Form. It is called the URL encoding type.
CONTENT-LENGTH: the number of data characters (in bytes) passed to the CGI program ).
In C language programs, you can use the getenv () library function to access environment variables. For example:
Name1 = value1 & name2 = value2 & name3 = value3 & name4 = value4 &......
Its
The name is the INPUT, SELECT, TEXTAREA, and other Tag names defined in Form. The value is the user INPUT or the selected Tag value. This format is URL encoding.
To analyze and decode it. To analyze this data stream, the CGI program must first split the data stream into a group of name/value pairs. This can be done by finding the following two characters in the input stream.

From: http://www.svn8.com/c/c/2010020119923.html

CGI (Public Gateway Interface) specifies the interface protocol standard for Web servers to call other executable programs (CGI program. Web servers interact with Web browsers by calling CGI programs
,
That is, the CGI program receives the information sent from the Web browser to the Web server for processing, and then returns the response to the Web server and Web browser. CGI programs generally complete Web pages
Form data processing, database query, and integration with traditional application systems. CGI programs can be written in any programming language, such as Shell script language, Perl,
Fortran, Pascal, and C language. However, CGI programs written in C language have the features of fast execution speed and high security (because C language programs are compiled and executed and cannot be modified.
The CGI interface standard consists of three parts: standard input, environment variable, and standard output.
1. Standard Input
CGI
Like other executable programs, a program can obtain input information from a Web server through standard input (stdin), such as data in Form. This is the so-called POST
Method. This means that the CGI program can be executed in the command line status of the operating system to debug the CGI program. The POST method is a common method. This article will take this method as an example to analyze the method of CGI program design.
Methods, processes, and skills.
2. Environment Variables
The operating system provides many environment variables that define the execution environment of the program and the application can access them. Web server and CGI
The interface also sets some of its own environment variables to pass some important parameters to the CGI program. The cgi get method also uses
The environment variable QUERY-STRING transmits the data in Form to the CGI program.
3. standard output
The CGI program transmits the output information to the Web server through the standard output (stdout. The information sent to the Web server can be in various formats, usually in plain text or HTML text, so that we can debug CGI programs in the command line status and get their output.
Below is a simple CGI program that outputs Form information in HTML directly to a Web browser.

Code#include<stdio.h> 
#include<stdlib.h>
void main() { int,i,n; printf (″Contenttype:text/plain ″); n=0; if(getenv(″CONTENT-LENGTH″)) n=atoi(getenv(CONTENT-LENGTH″)); for (i=0;i putchar(getchar()); putchar (′ ′); fflush(stdout); }

The following is a brief analysis of this program. Prinft ("Contenttype: text/plain
″);

This line uses the standard output to convert the string "Contenttype: text/plain"
"Sent to the Web server. It is a MIME header that tells the Web server that subsequent output is in plain ASCII text format. Note that there are two new line characters in this header, because
The Web server needs to see a blank line before the actual text information starts.
If (getenv ("CONTENT-LENGTH ″))
N = atoi (getenv ("CONTENT-LENGTH ″));

This line first checks whether the CONTENT-LENGTH environment variable exists. The Web server sets this environment variable when calling a CGI program using the POST method, and its text value represents
The number of characters in the input sent by the Web server to the CGI program, so we use the function atoi ()
Convert the value of this environment variable to an integer and assign it to Variable n. Note that the Web server does not terminate its output with the file Terminator, so if you do not check the environment variable CONTENT-
The CGI program cannot know when the input is complete.
For (I = 0; I <n; I ++)
Putchar (getchar ());

This row starts from 0 cycles to (CONTENT-LENGTH-1) and copies each character read in the standard input directly to the standard output, that is, all input is sent back to the Web server in ASCII format.
In this example, we can summarize the general working process of the CGI program into the following points.
1. Check the CONTENT-LENGTH environment variable to determine the number of inputs;
2. Use getchar () or other file read functions cyclically to obtain all input values;
3. process the input in a corresponding way;
4. Use the header "Contenttype:" To tell the Web server the format of the output information;
5. Use printf (), putchar (), or other file write functions to send the output to the Web server.
In short, the main task of the CGI program is to get the input information from the Web server for processing, and then send the output result back to the Web server.

II. Environment Variables
Environment variables are text strings (name/value pairs) that can be set by OS Shell or other programs or accessed by other programs. They are simple means for Web servers to pass data to CGI programs. They are called environment variables because they are global variables and can be accessed by any program.
The following are some environment variables that are often used in CGI programming.
HTTP-REFERER: the URL of the web page that calls the CGI program.
REMOTE-HOST: The machine name and domain name of the Web browser that calls the CGI program.
REQUEST-
METHOD: refers to the METHOD used when the Web server transmits data to the CGI program, divided into GET and POST methods. The GET method only uses environment variables (such as QUERY-
STRING) transmits data to the CGI program, while the POST method transmits data to the CGI program through environment variables and standard input. Therefore, the POST method can easily transmit a large amount of data to the CGI program.
.
SCRIPT-NAME: the CGI program NAME.
QUERY-STRING: When the POST method is used, the data in Form is placed in QUERY-STRING and passed to the CGI program.
CONTENT-TYPE: mime type passed to CGI program data, usually "applica tion/x-www-form-url encodede ″, it is the data encoding type that passes data to CGI programs through the POST method in HTML Form. It is called the URL encoding type.
CONTENT-LENGTH: the number of data characters (in bytes) passed to the CGI program ).
In C language programs, you can use the getenv () library function to access environment variables. For example:
Name1 = value1 & name2 = value2 & name3 = value3 & name4 = value4 &......
Its
The name is the INPUT, SELECT, TEXTAREA, and other Tag names defined in Form. The value is the user INPUT or the selected Tag value. This format is URL encoding.
To analyze and decode it. To analyze this data stream, the CGI program must first split the data stream into a group of name/value pairs. This can be done by finding the following two characters in the input stream.

From: http://www.svn8.com/c/c/2010020119923.html

Related Article

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.