Brief Guide to writing CGI programs in C

Source: Internet
Author: User
Tags microsoft frontpage

Abstr:
CGI specifies the interface protocol standard for the Web server to call other executable programs (CGI program. The Web server calls the CGI program to interact with the web browser. 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. This article analyzes in detail the methods, processes and skills for CGI program design using C language.
Body:

CGI program design in C Language

1. cgi Overview
CGI (Common Gateway Interface: Public Gateway Interface) specifies the interface protocol standard for the Web server 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.
The following is a simple CGI program that outputs the form information in HTML directly to the we B browser.
# Include <stdio. h>
# Include <stdib. h>
Main ()
{
Int I, N;
Printf ("content type: 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 ("content type: text/plain/n ″);
This line uses the standard output to send the string "content type: 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 (c ){
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 (C );
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 "content type: 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 "content type: text/html/n" to output the 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 HTML source code, and any HTML structure can be used in the HTML source code, such as hyperlinks, images, forms, and calls to 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, you should select the appropriate CGI programming language based on your needs and personal interests.

 

 

 

 

 

 

 

 

Turbo C ++ is a flexible and efficient programming language. As c ++ builder continues to update, Turbo C ++ is still an excellent development environment in windows, especially in CGI program development, it has many advantages that are incomparable with other programming environments. It has high code efficiency, fast compilation and running speed, and is secure and environmentally friendly (do not add any information to the Windows registry ), is one of the preferred web development languages for webmaster.

The CGI program programming examples in this article are developed, debugged, and run in the following environments:

1. Windows 9x Operating System
2. sambar4.2 integrated server software (Free Download http://www.sambar.com)
3. Turbo C ++ 3.0 development environment

The installation conditions of the above software are:

After Windows 9x software is installed, you need to install the TCP/IP protocol. When installing the Sambar server software, the installation path is C: /Sambar and Standard CGI path and Windows CGI path are C:/Sambar/CGI-BIN and C:/Sambar/CGI-WIN, web server port is 8080, the web root directory is C:/Sambar/docs.

Let's take a look at the example of a piece of text displayed in the Standard CGI program, which is almost a "hello World" variant (entry-level example for most languages ).

Example 1: display a piece of text in a browser.

# Include <stdio. h>
# Include <stdlib. h>
Void main ()
{
Printf ("HTTP/1.00 OK/N ");
Printf ("Content-Type: text/html/N ");
Printf ("<HTML>/N ");
Printf ("<HTML> <title> TC cgitest1 </title> <body>/N ");
Printf ("Printf ("this is a server-side CGI program compiled with Turbo C ++ 3.0 <br>/N ");
Printf ("It's cool! </Center> Printf ("</body> }

Compile the above program under Turbo C ++ into cgitest1.exe and save it under the C:/Sambar/CGI-BIN directory. Open ie on the Local Computer and enter http: // localhost: 8080/cgi-bin/cgitest1.exe in the URL, after the execution, the browser will display "Hello world". This is a server-side CGI program written in Turbo C ++ 3.0. It's cool!" . From the above section, we can see that the CGI program written by Turbo C ++ is output to stdout, which is actually output to the browser page.

Example 2: Write Access counters in CGI Mode

The counter is often used on Web pages. It can record the number of times the site is accessed. It is usually displayed in text or graphics, next, let's talk about the text display method (process it and combine the counting value with the corresponding GIF image to display the counting value in the graphic mode ).

First, create cgitest2.dat in C:/Sambar/docs and write the Initial counter value in the text file, for example, 9871

# Include <stdio. h>
# Include <stdlib. h>
# Include <ctype. h>
# Include <conio. h>
# Include <string. h>
Void main ()
{
Long int counter;
Char newvalue [10];
Char invalue [10];
File * fileh;
Fileh = fopen ("C: // Sambar // docs // cgitest2.dat", "R + T ");
Fgets (invalue, 10, fileh );
Fclose (fileh );
Counter = atol (invalue );
Ultoa (counter + 1, newvalue, 10 );
Fileh = fopen ("C: // Sambar // docs // cgitest2.dat", "R + W ");
Fputs (newvalue, fileh );
Fclose (fileh );
Printf ("HTTP/1.00 OK/N ");
Printf ("Content-Type: text/html/N ");
Printf ("<HTML>/N ");
Printf ("<HTML> <title> TC cgitest2 </title> <body>/N ");
Printf ("Printf ("</body> }

Compile the above program to form cgitest2.exe and enter the C:/Sambar/CGI-BIN subdirectory, enter http: // localhost: 8080/cgi-bin/cgitest2.exe will display 9871 after execution. After you press the refresh button, its Count value will be automatically added.

Next let's take a look at the commonly used CGI Source code.

First, create the C:/Sambar/docs/myprogs/bookrec folder, and write a book. htm file under the file. It is the original HTM file used to store the customer's messages. Its content is as follows:

<HTML>
<Head>
<Meta http-equiv = "Content-Type"
Content = "text/html; charsets = gb_2312-80">
<Meta name = "generator" content = "Microsoft FrontPage express 2.0">
<Title> customer message board </title>
</Head>
<Body background = ".../myprogs/bookrec/back1_1.gif" bgcolor = "# ffffff"
TEXT = "#000000" link = "#000000" vlink = "#000000" alink = "#000000">
<P align = "center"> <font color = "# paia0" size = "7"> <B> Leave a message for a visitor </B> </font>
<Font color = "#0000a0" size = "5"> </font> </P>
<P align = "Left"> <a href = ".../myprogs/bookrec/addbook.htm"> <font
Color = "# 0000ff"> Add new message </font> </a> <font color = "# 0000ff"> </font> <
Href = ".. /.. /.. /.. /.. /"> <font color =" # 0000ff "> return to the home page of this site </font> </a> </P>
<HR>
<! -- Top -->
<B> it is very good. </B> <br>
Songyq & lt; <a href = "mailto: SONGYQ@371.NET"> SONGYQ@371.NET
</A> & gt; <HR>
</Body>
</Html>

The above book. HTM has two links: one is to add new messages and the other is to return to the home page of this site. Addbook.htm is executed to add new messages. It is obviously a form used to enter messages, while returning to the home page of this site is simply executed .. /.. /.. /.. /.. /, return the URL of the browser to the starting position of the site, that is, http: // localhost: 8080/

The above book can be directly displayed. htm is used to display the user's message, but it is best to display it through the CGI program. In this way, you can remove the need to press the refresh button after adding a new message (if the book is directly displayed. htm must be refreshed to see the newly added message ). The following program cgitest3.exe is used to display the above book. htm

# Include <stdio. h>
Void main ()
{
Char invalue [512];
File * fileh;

Printf ("HTTP/1.00 OK/N ");
Printf ("Content-Type: text/html/N ");
Printf ("<HTML>/N ");
Fileh = fopen ("C: // Sambar // docs // myprogs // bookrec // book.htm", "R + T ");
While (! Feof (fileh ))
{
Fgets (invalue, 500, fileh );
Printf (invalue );
}
Fclose (fileh );
}

The function of program cgitest3.exe is. open the HTM file, read a row, and display a row on the browser. when the HTM content changes, execute cgitest3.exe to display all the content. In addition to displaying the user's message, the following is the content of the "addbook.htm" message:

<HTML>
<Head>
<Meta http-equiv = "Content-Type"
Content = "text/html; charsets = gb_2312-80">
<Meta name = "generator" content = "Microsoft FrontPage express 2.0">
<Title> Add new message </title>
</Head>

<Body background = ".../myprogs/bookrec/back1_1.gif" bgcolor = "# ffffff"
TEXT = "#000000" link = "#000000" vlink = "#000000" alink = "#000000">
<P align = "center"> <font color = "#0000a0" size = "7"> <B> Add a new message </B> </font>
<Br>
<Br>
</P>

<Form action = "/cgi-bin/bookrec.exe" method = "get">
<Table border = "0" cellpadding = "5">
<Tr>
<TD> last name (<I> required </I>) </TD>
<TD> <! -- Webbot bot = "validation"
S-display-name = "ACVD" s-data-type = "string"
B-allow-letters = "true" --> <input type = "text"
Size = "40" name = "name"> </TD>
</Tr>
<Tr>
<TD> email </TD>
<TD> <input type = "text" size = "40" name = "email"> </TD>
</Tr>
<Tr>
<TD valign = "TOP"> your message (<I> required </I>) </TD>
<TD> <textarea name = "MSG" rows = "12" Cols = "60"> </textarea> </TD>
</Tr>
<Tr>
<TD> </TD>
<TD> <input type = "Submit" value = "Submit"> </TD>
</Tr>
</Table>
</Form>

<P align = "center"> This program only uses the HTM Markup Language and CGI compilation language. After submission, you can see a new message. </P>
<P align = "center"> CNPC <br> Technology Development Center <br> All rights reserved for the repair and manufacturing center </P>
<P align = "center"> Email: <a href = "mailto: wxzx@petrotech.com.cn">
Wxzx@petrotech.com.cn </a> <br>
Tel: 010-84522288-8164-1 </P>
</Body>
</Html>

The addbook.htm of tables is a form that is sent to/cgi-bin/bookrec.exe three useful parameters: name, email, and MSG, while the/cgi-bin/logs file <! -- Top --> sequence's Turbo C ++ source code cgitest4.cpp:

# Include <stdio. h>
# Include <stdlib. h>
# Include <ctype. h>
# Include <conio. h>
# Include <string. h>

Void main ()
{
Char * cstring;
Char estring1 [512], estring2 [512], estring3 [512];
Char invalue [512];
Int check;
Char * Param, * temp;
Char * cmpstr, * orgstr;
Char * oldfile, * newfile, * ostring;

File * fileh, * filej;

Cmpstr = "<! -- Top --> ";
Cstring = getenv ("QUERY_STRING ");
Strcpy (ostring, cstring );

Strcpy (estring1, cstring );

Param = strstr (estring1 ,"&");
Strcpy (estring2, Param );
Strcpy (Param, "/0 ");
Temp = estring2 + 1;
Strcpy (estring2, temp );

Param = strstr (estring2 ,"&");
Strcpy (estring3, Param );
Strcpy (Param, "/0 ");
Temp = estring3 + 1;
Strcpy (estring3, temp );

Param = strstr (estring1, "= ");
Temp = Param + 1;
Strcpy (estring1, temp );

Param = strstr (estring2, "= ");
Temp = Param + 1;
Strcpy (estring2, temp );

Param = strstr (estring3, "= ");
Temp = Param + 1;
Strcpy (estring3, temp );

// Name, email, MSG are seperated
Filej = fopen ("C: // Sambar // docs // myprogs // bookrec // nbook.htm", "W ");
Fileh = fopen ("C: // Sambar // docs // myprogs // bookrec // book.htm", "R ");
While (! Feof (fileh ))
{
Fgets (invalue, 500, fileh );
Strcpy (orgstr, invalue );
Strcpy (orgstr + 10, "/0 ");
Check = stricmp (orgstr, cmpstr );
If (check = 0 ){
// Fputs new comments from guest
Fputs (invalue, filej );
Fputs ("<B>", filej );

Fputs (estring3, filej );
Fputs ("</B> <br>/N", filej );
Fputs (estring1, filej );
Fputs ("& lt; <a href =", filej );
Strset (temp, 34 );
Strcpy (temp + 1, "/0 ");
Fputs (temp, filej );
Fputs ("mailto:", filej );
Fputs (estring2, filej );
Fputs (temp, filej );
Fputs (">", filej );
Fputs (estring2, filej );
Fputs ("</a> & gt; <HR>/N", filej );
}
Else
Fputs (invalue, filej );
}
Fclose (fileh );
Fclose (filej );

Oldfile = "C: // Sambar // docs // myprogs // bookrec // book.htm ";
Newfile = "C: // Sambar // docs // myprogs // bookrec // nbook.htm ";
Remove (oldfile );
Rename (newfile, oldfile );

Printf ("Your message has been added <br>/N ");
Printf ("<a href = ");
Printf (temp );
Printf ("/cgi-bin/cgitest3.exe ");
Printf (temp );
Printf ("> see guest book </a> <br>/N ");

Printf ("<a href = ");
Printf (temp );
Printf ("../../../");
Printf (temp );
Printf ("> return to home page </a>/N ");
}

Compile the above program to generate cgitest4.exe, put in C:/Sambar/CGI-BIN directory. Enter http: // localhost: 8080/docs/myprogs/bookrec/addbook.htm at the URL of the browser, enter: me at the name, enter: Me@263.net at the mailbox, enter this is realy a nice guest book in the message. after you press submit, the following information is displayed:

Your message has been added
See guest book
Return to home page

The last two lines have a hyperlink. Click see guest book to view your messages.

The above Program introduces the general method of compiling CGI programs with TC. These instances are all runable, but they need to be further improved to apply them to site construction, consider more pitfall, otherwise there will be problems. The problem with cgitest4 is that all input Chinese characters are encoded as % XX hexadecimal numbers (because of the encoding results, CGI programs can accurately identify and remove illegal characters such as & #. Therefore, CGI programs must replace % xx with the original Chinese characters, otherwise, you will see % XX instead of Chinese characters on the message board.

 

 

More

Http://topic.okbase.net/200506/2005061509/1891477.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.