The principle and implementation of CGI interface

Source: Internet
Author: User
Tags stdin
The principle and implementation of CGI interface

CGI interface principle and implementation (2012-12-7 over)

  

1.CGI definition:

CGI (Commongateway Interface) is a tool for HTTP servers to "talk" to programs on your or other machines, and its programs must run on a network server.

2.CGI function:

The vast majority of CGI programs are used to interpret input information from forms, to produce corresponding processing on the server, or to feed feedback to the browser. CGI programs make Web pages interactive.

3.CGI Operating Environment:

CGI programs run on a server in a CERN or NCSA format on UNIX operating systems. CGI programs are also widely used on servers in other operating systems, such as Windows NT and Windows95, and they are also applicable to various types of machines.

4.CGI Processing steps:

⑴ sends user requests to the server over the Internet.

The ⑵ server receives the user request and gives it to the CGI program for processing.

The ⑶CGI program transmits the processing results to the server.

The ⑷ server sends the results back to the user.

5.CGI Server configuration:

In many servers Cgi-bin is the only directory where CGI scripts can be placed.

  

On the Windows platform, copy the debug or release version of the. exe program for a C or C + + program to the Cgi-bin directory (as shown in the figure above), and change the. CGI to the same function as the following 2 graphs.

  

  

The Cgi-bin directory is where the CGI scripts are stored. These scripts enable the WWW server and browser to run external programs without starting another program. It is a program that runs on a Web server and is triggered by input from the viewer.

CGI programs do not run smoothly on the server, and if you want to make them run smoothly on the server and handle the user's requests exactly, you need to set up the server that you are using.

Configuration: Place a CGI program in a specific directory or with a specific extension, depending on the type of server used and its settings.

The Apache network server is configured in/var/www/cgi-bin (the directory location of the author's computer as shown in the following figure). Executable files compiled by C + + can be converted to files with a. cgi extension.

To change the initial configuration method:

<directory "/var/www/cgi-bin" >

AllowOverride None

Options execcgi

Order Allow,deny

Allow from all

</Directory>

<directory "/var/www/cgi-bin" >

Options All

</Directory>

  

The 6.CGI interface standard includes three parts of standard input, environment variable and standard output.

Introduced

1. Standard input

CGI programs, like other executable programs, can get input from the Web server via standard input (stdin), such as 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. The Post method is a common method.

2. Environment variables

The operating system provides many 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

The CGI program transmits output information to the 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.

7. Environment variables

Environment variables are text strings (name/value pairs) that can be set by Osshell 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.

Environment variables

Significance

server_name

The host name and IP address of the CGI script at runtime.

Server_software

The type of your server is: cern/3.0 or ncsa/1.3.

Gateway_interface

Run the CGI version. For UNIX servers, this is cgi/1.1.

Server_protocol

The HTTP protocol that the server is running. This is http/1.0.

Server_port

The TCP port that the server is running, usually the Web server is 80.

Request_method

POST or get, depending on how your form is submitted.

Http_accept

The content-types that the browser can receive directly can have the HTTP Accept header definition.

Http_user_agent

Additional information about the name, version, and other platform of the browser submitting the form.

Http_referer

URL that submits the text of the form, not all browsers emit this information, do not rely on it

Path_info

Additional path information that is emitted by the browser through the Get method.

Path_translated

The system-defined path information in the PATH_INFO.

Script_name

The path to the CGI script is displayed in the URL (e.g.,/cgi-bin/thescript).

Query_string

The script parameter or form entry (if it is submitted with get). Query_string contains the arguments that follow the question mark in the URL.

Remote_host

The host name for submitting the script, this value cannot be set.

Remote_addr

The host IP address that submitted the script.

Remote_user

The name of the user who submitted the script. If the server's authentication is activated, this value can be set.

Remote_ident

If the Web server is running in ident (a protocol that confirms the user's connection to you), the system that submits the form is running ident, which contains the ident return value.

Content_Type

If the form is submitted by post, the value will be application/x-www-form-urlencoded. In the form that uploads the file, Content-type is a multipart/form-data.

Content_length

For the form submitted by post, the number of bytes in the standard input port.

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.

: Get method transmits data to CGI program only through environment variable (such as query-string), and post method transmits data to CGI program through environment variable and standard input, so post method can transmit more data to CGI program conveniently.

Problem

Get method

Pass parameters in the form embedded in the URL. For CGI programs, the parameters passed in Get method are received by the transformation variable "query-string".

1 The content of the parameter as the URL information, the user can see;

2 has the size limit.

Post method

The CGI program receives parameters from the standard input. Unlike get methods, the contents of a parameter are not available from the URL information, and there is no limit to the size.

Opposite to get method problem 1, 2).

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:
if (getenv (″content-length″)) N=atoi (getenv (″content-length″));
Note that it is best to call the program two times getenv (): first check for the existence of the environment variable, and then use the environment variable for the second time. This is because the function getenv () returns a null (NULL) pointer if the given environment variable name does not exist, and if you refer to it directly without first checking, the CGI program crashes when the environment variable does not exist.

8. The working principle of CGI

CGI is a standard interface for a Web server to provide information services, through which a Web server can execute programs and return information about the output of the program to the browser. Because the data on the web is static, the CGI program is able to dynamically process the viewer's request, such as saving the user input information, returning the relevant information according to the user's information, and so on. When a client sends a CGI request to a Web server, the Web server determines how the data is transmitted to the CGI program according to the type of CGI program, typically passing data between the standard input/output stream and the environment variable to the CGI program.

  

CGI input and output principle

CGI input/Output methods: CGI programs are input and output through standard input (stdin) and standard output (STDOUT), and STDIN and STDOUT are two predefined file pointers. You can manipulate it by using a file read-write function.

In addition, CGI programs are input through environment variables, except that the environment variable provides some common information and usually does not include information that the user enters in the Web page (in addition to using the Get method below, the input data is checked by checking the environment variable query_string), StdIn is often used to pass information entered by the user.

The Post/get method to use when typing: When a Web page sends data to a CGI, it usually takes two methods: The Get/post,get method appends the data to the URL and sends it, such as:/cgi/a_cgi_test.exe Your_data, The CGI program obtains the input data by checking the environment variable query_string.

Example one, the following figure is a GET method!

  

The corresponding procedure for the above diagram is:

2012-12-5 Get C Program example ...

void Main (void) {//This program prints the data entered by the user fprintf (stdout, "content-type:text/plain\n\n");//output a CGI title, the meaning of this line of code will explain char * Pszmethod; Pszmethod =getenv ("Request_method"); if (strcmp (Pszmethod, "get") = = 0) {//get method//Read environment variable to fetch data printf ("This is getmethod!\n"); printf ("server_name:%s\n", Getenv ("SERVER_NAME")); printf ("remote_addr:%s\n", getenv ("REMOTE_ADDR")); fprintf (stdout, "input data is:%s\n", getenv ("query_string")); else {//POST method//stdin to fetch the data intilength=atoi (getenv ("content_length")); printf ("is postmethod!\n"); fprint F (stdout, "input data is:\n"); for (int i=0;i<ilength;i++) {char cget=fgetc (stdin); FPUTC (cget,stdout);}} }

Example two, the following figure is the Post program example:

  

  

  

  void Unencode (char *src, Char *last, char *dest) {//str = hello+there%21 Skip data= here ...//last =; to end.//dest=; empty string. Decoding principle//Principle 1: ' + ' change '; Principle 2: '%xx ' becomes the corresponding 16 ASCII code value; for (; Src!= last; src++, dest++) {if (*src = = ' + ') {*dest = ';} else if (*src = = '% ') {int code; if (sscanf (src+1, "%2 X ", &code)!= 1) {code = ';} *dest = code; SRC +=2; else {*dest = *src;}} *dest = ' \ n '; *++dest = ' n '; } intmain (void) {char *lenstr; char Input[maxinput], data[maxinput], long len, printf ("%s%c%c\n", "content-type:text/ Html;charset=iso-8859-1 ", 13,10); printf ("<title>response</title>\n"); Lenstr =getenv ("content_length"); printf ("Content_length =%s\n", lenstr); if (lenstr = NULL | | SSCANF (Lenstr, "%ld", &len)!=1 | | Len > MaxLen) {printf ("<p>error ininvocation-wrong FORM probably.");} else {FILE *f; fgets (input, len+1, STD in); Add by Ycy Gets the string from the input stream. Unencode (Input+extra, input+len,data); f = fopen (DataFile, "a"); if (f = = NULL) {printf ("<p>sorry,cannOT store your data. "); else {fputs (data, f);//add Byycy stores the data in the corresponding file. fclose (f); printf ("<p>thank you! Your contribution has been stored. ");} return 0; }

The request process is:

(1) The Send Send button-> (2) calls post.cgi-> (3) to store the data in Data\data.txt.

Example three, the following figure is the Get/post program example.

Comprehensive example: Extensions on the top two programs (applying Get methods and Query_string)

  

  

As shown in the two illustration, on the basis of the form (the CGI interface's form is simply an extension operation implemented in C or C + + based on the HTML language), the submit button corresponds to another CGI interface (6.exe or 6.cgi), so that the Get method and the Query_ The string parameter can complete the output operation.

The Post method then feeds the data into the stdin input stream of the CGI program. Each variable in the form is name=value to the Web server, with & separated by multiple data, such as: Name=value&name2=value2. The name (name,name2) is the name of the input, select, or textarea (TAG) defined in form, and the value is the value of the user's input or selection.

As mentioned above, the CGI program output must first output a CGI title, the title has the following three categories:

· Location: Title, indicating the URL to output another document, such as fprintf (stdout, "Location: \ n \ nthe");

· Content-type: A caption that indicates the MIME type of the data being sent, such as fprintf (stdout, "content-type:text/html\n\n");

· Status: Title, indicating the HTTP status code, such as fprintf (stdout, "status:200\n\n");

Note that each heading must be followed by a newline and a blank line.

MIME types are represented as type/subtype, and the following are combinations of commonly used types/subtypes:

· Text/plain Plain Text type

· text/html HTML-formatted text type

· Audio/basic eight-bit sound file format with a suffix of. au

· Video/mpeg MPEG file format

· Video/quicktime QuickTime file format

· Image/gif gif graphics file

· Image/jpeg JPEG graphics file

· Image/x-xbitmap x Bitmap graphics file, suffix to. xbm

With the above knowledge we can write a few CGI programs, the first need to analyze the input data, the method is: every time find the character =, mark the end of the name of a form variable; whenever a character & is found, marks the end of a form variable value. Please note that the value of the last variable in the input data does not end with &. This allows us to decompose the input data into a set of values.

But then it turns out that CGI input is not a rule, for example, sometimes a string of input symbols similar to the following format appears: Filename=hello&cmd=world+i%27, this is because the browser encodes some of the special characters that are uploaded, So when the data is decomposed, it needs to be decoded,

The decoding rules are:

1) +: convert + to spaces;

2)%xx: A special character (% as a signifier) represented by its hexadecimal ASCII code value. Converts it to the appropriate ASCII character based on the value XX. This conversion is done for both the form variable name and the variable value.


Turn from: http://www.educity.cn/wenda/86802.html

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.