CGI program design in C Language

Source: Internet
Author: User

CGI in C LanguageProgramDesign

1. cgi Overview

CGI (Public Gateway Interface) specifies the interface protocol standard for Web servers to call other executable programs (CGI program. The Web server calls the CGI program to implement interaction with the web browser, that is, the CGI program receives and processes the information sent to the Web server by the web browser, send the response to the Web server and Web browser. CGI programs generally process form data on web pages, query databases, and integrate with traditional application systems. CGI programs can be written in any programming language, such as shell scripting 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 programs, like other executable programs, can get input information from the Web server through standard input (stdin), such as data in form, this is the so-called POST method for passing data to CGI programs. 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 commonly used method. This article uses this method as an example to analyze the methods, processes, and techniques of CGI program design.

2. Environment Variables

The operating system provides many environment variables that define the execution environment of the program and the application can access them. The Web server and CGI interface also set some environment variables to pass some important parameters to the CGI program. The CGI get method also transmits the data in form to the CGI program through the Environment Variable query-string.

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.

# Include <stdio. h>

# Include <stdlib. h>

Main ()

{

Int, I, N;

Printf ("contenttype: text/plain \ n ″);

N = 0;

If (getenv ("Content-Length ″))

N = atoi (getenv (Content-Length ″));

For (I = 0; I <n; I ++)

Putchar (getchar ());

Putchar ('\ n ′);

Fflush (stdout );

}

The following is a brief analysis of this program.

Prinft ("contenttype: text/plain \ n ″);

This line uses the standard output to send the string "contenttype: text/plain \ n" 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. Its text value indicates the number of characters in the input sent by the Web server to the CGI program. Therefore, we use the atoi () function () 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. Therefore, if the Content-Length environment variable is not checked, the CGI program cannot know when the input ends.

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: the method used when the web server transmits data to the CGI program. There are two methods: Get and post. The get method only transmits data to CGI programs through environment variables (such as query-string), while the POST method transmits data to CGI programs through environment variables and standard input, therefore, the POST method can easily transmit a large amount of data to CGI programs.

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:

If (getenv ("Content-Length ″))

N = atoi (getenv ("Content-Length ″));

Please note that it is best to call getenv twice in the program: Check whether the environment variable exists for the first time, and then use the environment variable for the second time. This is because the getenv () function returns a null (null) pointer when the given environment variable name does not exist. If you reference it directly without checking it first, if the environment variable does not exist, the CGI program will crash.

Iii. Analysis and decoding of from input

1. Analyze name/value pairs

When a user submits an HTML form, the Web browser first encodes the data in form in the form of name/value pairs, sends the data to the Web server, and then passes the data to the CGI program by the Web server. The format is as follows:

Name1 = value1 & name2 = value2 & name3 = value3 & name4 = value4 &...

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, which needs to be analyzed and decoded in the program. 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.

Every time the character = is found, it indicates the end of a form variable name; every 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.

After a name/value pair is decomposed, some special characters in the input must be converted into ASCII characters. These special characters are:

+: Convert + to a space character;

% XX: a special character expressed 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 <stdio. h>

# Include <stdlib. h>

# Include <strings. h>

Int htoi (char *);

Main ()

{

Int I, N;

Char C;

Printf ("contenttype: text/plain \ 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 ?? {

Cas ′&′:

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 ??;

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 "&" character is found, it means that a name/value pair ends, and the program outputs a blank line. When the character is found to be +, it is converted to a space; when the character is %, it indicates the start of a two-character hexadecimal value. Call the htoi () function to convert the subsequent two characters into the corresponding ASCII characters; when the character is found to be =, 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.

4. Generate HTML output

the output produced by CGI consists of the MIME header information and actual information. The two parts are separated by a blank line. We have seen how to use the MIME header information "Cont enttype: text/plain \ n", printf (), put char () and other function calls to output plain ASCII text to the web server. In fact, we can also use the MIME header information "C ontenttype: text/html \ n "to output HTML Source Code to the web server. Note that there must be an empty line after any MIME header information. Once this MIME header is sent to the server we B, the Web browser will regard the subsequent text output as an HTML source Code , in the HTML source code, you can use any HTML structure, such as hyperlinks, images, and forms, and call other CGI sequences. That is to say, we can dynamically generate HTML source code output in the CGI program. Below is a simple example.

# Include <stdio. h>

# Include <string. h>

Main ()

{

Printf ("contenttype: text/html \ n ″);

Printf ("<HTML> \ n ″);

Printf ("
Printf ("<body> <br> \ n ″);

Printf ("<H2> This is an HTML page generated from with I n a CGI program... </H2> \ n ″);

Printf ("<HR> <p> \ n ″);

Printf ("<a href =" ../output.html # Two "> <B> go back to out put.html page <

/B> </a> \ n ″);

Printf ("</body> \ n ″);

Printf ("
Fflush (stdout );

}

The above CGI program simply uses the printf () function to generate the HTML source code. Note that if the output string contains double quotation marks, there must be a backslash (\) before it, because the entire HTML code string is already in double quotation marks, therefore, the double quotation marks in the HTML code string must be escaped using a backslash.

V. Conclusion

This article analyzes in detail the methods, processes, and skills for CGI program design using C language. Although the CGI program in C language runs fast and has high reliability, compared with Perl, C language lacks powerful string processing capabilities. Therefore, in practical application, select the appropriate CGI programming language based on your needs and hobbies.

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.