Write CGI programs in C Language

Source: Internet
Author: User

Write CGI in C Language Program 1. cgi overview CGI (Public Gateway Interface) specifies the interface protocol standard for Web servers to call other executable programs (CGI programs. 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. the standard input CGI program, like other executable programs, can obtain 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. The standard output 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. The following is a simple CGI program that outputs the form information in HTML directly to the we B browser. Reference # include <stdio. h> # include <stdib. h> main () {int, I, n; printf (Response contenttype: text/plainnn encoding); n = 0; If (getenv (Response Content-Length Encoding 〃)) N = atoi (getenv (Content-Length bytes); for (I = 0; I putchar (getchar (); putchar ('n' '); fflush (stdout );} the following is a brief analysis of this program. Prinft (plain contenttype: text/plainnn plaintext); this line transmits the string plain contenttype: text/plainnn plaintext to the Web server through standard output. 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 (Response Content-Length duration) n = atoi (getenv (Response Content-Length duration); this row 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 putchar (getchar (); this row loops from 0 to (Content-Length-1) copy each character in the standard input to the standard output directly, that is, send all the input 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 response contenttype: Response Header to tell the Web server the format of the output information. write functions using printf (), putchar (), or other files 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. 2. 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: the MIME type that is passed to the CGI program data. It is usually the condition applica tion/X-WWW-form-URL encodede transform 〃, 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 (Response Content-Length Encoding) n = atoi (getenv (Response Content-Length Encoding); 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 and sends them to the web server, the Web server then passes the request to the CGI program. 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: special characters expressed with 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. Reference # include <stdio. h> # include <stdlib. h> # include <strings. h> int htoi (char *); main () {int I, n; char C; printf (character contenttype: text/plainnn character); n = 0; if (getenv (Response Content-Length Encoding) n = atoi (getenv (Response Content-Length Encoding); 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 = Limit 0123456789abcdef limit; 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 The MIME header information is sent to the Web server. Check the number of characters in the input and check each character cyclically. 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 an HTML output CGI program. The output consists of the MIME header information and actual information. The two parts are separated by a blank line. We have seen how to use MIME header information such as plain cont enttype: text/plainnn plain, 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/htmlnn encode to output htmlSource 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 hyperchain, image, form, and other CGI program calls. That is to say, we can dynamically generate HTML source code output in the CGI program. Below is a simple example. Reference # include <stdio. h> # include <string. h> main () {printf (Response contenttype: text/html \ n response); printf (Response <HTML> \ n response 〃); printf (outputs  Article We mainly introduce how to use C to write CGI programs for table processing. For forms that use the "method = get" attribute (or do not have the method attribute, get is the default value), CGI defines it as: when the form is sent to the server for disconnection, the data in the form is stored in an environment variable called QUERY_STRING on the server. The processing of such forms is relatively simple. You only need to read the environment variables. This is different for different languages. In the C language, you can use the library function getenv (defined in the standard library function stdlib) to access the environment variable value as a string. After obtaining the data in the string, you can use some tips to convert the type. This is relatively simple. The standard output in the CGI program (such as the stdout file stream in C) is also redefined. It does not generate any output content on the server, but is redirected to the client browser. In this way, if you write a c cgi program and output an HTML document to its stdout, the HTML document will be displayed in the browser of the client. This is also a basic principle of CGI programs. Let's take a look at the specific program implementation. The following is an HTML form: <form action = "/cgi-bin/mult. cgi "> <p> enter the multiplier and quilt multiplier below, and press OK to see the result. <Input name = "M" size = "5"> <input name = "N" size = "5"> <br> <input type = "Submit" values = "OK "> </form> the functions we want to implement are simple, is to multiply the input values in the form and then output the result. In fact, this function can be fully implemented using Java Script, but in order to make the program as simple as possible, I chose this small multiplication as an example. The following is the CGI program that processes the form, which corresponds to the action attribute value in the form tag. Reference # include <stdio. h> # include <stdlib. h> int main (void) {char * data; long M, N; printf ("% S % C", "Content-Type: text/html; charset = gb2312 ", 13, 10); printf (" <title> multiplication result </title> "); printf (" Programming Language For example, ASP, PHP, and JSP all have most of the CGI programming functions, it is indeed much easier than CGI programming in whatever language. Therefore, when programming on the server side, these Script Programming Languages are generally considered first. CGI is used only when they cannot solve the problem, for example, when some underlying programming is required.

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.