CGI programming with C language technology
First, CGI overview
The
CGI (Public gateway Interface) sets the interface protocol standard for Web servers to invoke other executable programs (CGI programs). The Web server invokes the CGI program implementation and web browser interaction
, which means that the CGI program accepts the Web browser's message to the Web server, processes it, and returns the response to the Web server and Web browser. The CGI program generally completes the processing of form data, database query and integration with traditional application system in Web pages. CGI programs can be written in any programming language, such as the Shell scripting language, Perl, Fortran, Pascal, C, and so on. But the CGI program written in C language has the characteristics of fast execution, high security (because C language program is compiled and can not be modified). The
CGI interface standard includes standard input, environment variables, and standard output.
1. Standard input
CGI programs, like other executable programs, get input from the Web server through standard input (stdin), such as the data in form, which is called the Post method of passing data to a CGI program. This means executing CGI programs on the operating system command line State and debugging CGI programs. Post method is a common method, this article will take this method as an example, analysis of CGI program design methods, processes and techniques.
2. Environment variables
The operating system provides a number of environment variables that define the execution environment of the program, which the application can access. The Web server and the CGI interface also set up some of their own environment variables to pass some important parameters to the CGI program. The CGI Get method also passes the data in the form to the CGI program through the environment variable query-string.
3. Standard output
A CGI program transmits output information to a Web server through standard output (stdout). The information that is sent to the Web server can be in a variety of formats, usually in plain text or HTML text, so that we can debug the CGI program in the command line state and get their output.
The following is a simple CGI program that prints the information in HTML form 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 row passes the string ″contenttype:text/plain″ to the Web server through standard output. It is a MIME header message that tells the Web server that subsequent output is in plain ASCII text. 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 row first checks whether the environment variable content-length exists. The Web server sets this environment variable when it invokes a CGI program that uses the Post method, and its literal value represents the number of characters in the input that the Web server sends to the CGI program, so we use the function atoi () to convert the value of this environment variable to an integer. and assign to the variable N. Note that the Web server does not terminate its output as a file terminator, so if you do not check the environment variable content-length,cgi The program will not know when the input is finished
for (i=0;i<n;i++)
Putchar (GetChar ());
This line copies every character read in the standard input directly to the standard output from 0 loops to (content-length-1) times, that is, all input is returned to the Web server in ASCII form.
In this example, we can summarize the general working process of a CGI program as follows.
1. Determine the number of inputs by examining the environment variable content-length;
2. Cycle using GetChar () or other file read function to get all the input;
3. Processing input in the appropriate way;
4. Through the ″contenttype:″ header information, the output information format to tell the Web server;
5. The output is routed to the Web server by using printf () or Putchar () or other file write functions.
In short, the main task of a CGI program is to get input from a Web server, process it, and then send the output back to the Web server.
second, environment variables
Environment variables are text strings (name/value pairs) that can be set by the OS shell or other programs, and can be accessed by other programs. They are a simple way for Web servers to pass data to CGI programs, and they are called environment variables because they are global variables and can be accessed by any program.
Here are some of the environment variables that are often used in CGI programming.
Http-referer: The URL of the Web page that invokes the CGI program.
Remote-host: The machine name and domain name of the Web browser that invoked the CGI program.
Request-method: Refers to the method used when the Web server passes data to a CGI program, which is divided into get and post two methods. The Get method transmits data to the CGI program only through environment variables (such as query-string), and the Post method transmits data to the CGI program through environment variables and standard input, so the post method can transmit more data to the CGI program conveniently.
Script-name: The name of the CGI program.
Query-string: When you use the Post method, the data in the form is finally placed in the query-string and passed to the CGI program.
Content-type: The MIME type passed to the CGI program data, usually the ″applica Tion/x-www-form-url Encodede″, which is the data encoding type that transmits data to the CGI program from the HTML form in the Post method, is called the URL encoding type.
Content-length: The number of data characters (bytes) passed to the CGI program.
In the C language program, to access environment variables, you can use the getenv () library function. For example:
Name1=value1&name2=value2&name3=value3&name4=value4& .....
The first name is the input, select, or textarea (tag) name defined in form, and the value is the user input or selection of the value of the mark. This format is URL-coded, which needs to be analyzed and decoded in the program. To parse this data stream, the CGI program must first decompose the data stream into a group of name/value pairs. This can be done by looking up the following two characters in the input stream.
Go from: http://www.svn8.com/c/c/2010020119923.html