Transplantation of CGIC Library
CGIC is a standard C library that supports CGI development. It can be used for free. You only need to make a public declaration in the developed site and program documentation, indicating that the program uses the CGIC library, users can also purchase commercial licenses without making a public statement.
CGIC can provide the following functions: 1. analyze data and automatically correct data sent From defective browsers; 2. transparently receive From data sent by GET or POST methods; 3. files can be uploaded;
4. Set and receive cookies;
5. process the carriage return in the From element in a consistent way;
6. Provides the string, integer, floating point, single-choice or multiple-choice function to receive data;
7. Provides border check for numeric fields;
8. It can convert CGI Environment Variables into non-null strings in C;
9. Provides CGI program debugging means to play back the CGI status during CGI program execution;
In short, CGIC is a powerful Standard C library that supports CGI development and supports multiple operating systems such as Linux, Unix, and Windows.
The following describes the CGIC porting process. Download the source code from the main CGIC site http://www.boutell.com/cgic/and the latest version is 2.05. Download the file at http://www.boutell.com/cgic/cgic205.tar.gzto decompress it and enter it into the source code directory # tar xzf cgic205.tar.gz # cd cgic205 modify the Makefile file and find
CC = gcc, change it to CC = arm-linux-gcc, find AR = ar, change it to AR = arm-linux-ar, find RANLIB = ranlib,
Change it to RANLIB = arm-linux-ranlib. Find gcc cgictest. o-o cgictest. cgi.
Change $ {LIBS} to $ (CC) $ (CFLAGS) cgictest. o-o cgictest. cgi $ {LIBS} and find gcc.
Capture. o-o capture $ {LIBS}, change it to $ (CC) $ (CFLAGS) capture. o-o capture
$ {LIBS}, save and exit.
Then run make to compile the CGIC library libcgic. a. we verify the correctness of the CGIC library by debugging the auxiliary Program capture and the test program cgictest. cgi.
Copy capture and cgictest. cgi to the/nfs/www/cgi-bin directory of the host.
Enter http: // 192.168.67.16/cgi-bin/cgictest. cgi in the address bar of the wks browser. The page displays the CGIC library and Test
All scripts are successfully transplanted. Cgictest. cgi fully demonstrates the functions of the CGIC library. It is best to master the cgictest. CGI program before developing a cgi program based on the CGIC library.
It is also a reference example for users to develop specific applications. HTML template Creation
Web-based application development generally disconnects the interface from the program logic, allowing more
Modify the interface, such as modifying the properties of the interface text and creating a multi-language version without modifying the program logic. The interface is generally made by the artist, while the programmer is responsible for the implementation of specific functions. In HTML, the form
FORM is the most important means of transmitting information. It applies to any browser. There are many elements in the form, including the input text box, single-choice, multi-choice box, button, and so on, which can provide information interaction. Details
For Object Description and syntax, see other HTML books. Based on application requirements, the artist or other designers design the final Web page as a template developed by programmers. The CGI program generally receives form data for data processing, and generates a new page based on the processing result and returns it to the browser. Table
A single data is generally submitted to the server using the POST method, which is obtained by the CGI program. The program must map the interface data with the internal data before proceeding to the next step. CGI program obtains data from the page
It is distinguished by the element name/value. However, it is troublesome for CGI to return to the page. The interface may change after the program development is completed, and some places that need to be processed by the program may not
Form elements. Therefore, for programs, the form element name cannot be used as the basis for differentiation. The general method is to use annotations in HTML <! -Xxx --> to mark.
The programmer needs to follow certain rules in the template for each form element and any other places that need to be processed by the program. For example, the next line of the comment is the form Element row, and the comment mark is created. CGI program can be root
The comment mark is used to determine and process the form Element Information. The program reads the template file row by row and checks whether there are annotation marks. If yes, the next row needs to be processed, and data is assigned to the form element.
Return the page with data to the browser.
The HTML template also needs to focus on the input check. Based on the principle that the earlier the input check, the better, you need to check the data submitted by the user on the user interface. Currently, javascript is generally used.
Script. When a user submits data, the onSubmit method of the form object is called, and the user input can be checked in this method. Common checks are required, Maximum/minimum length
Degree, character, number, email address, IP address, and match with a regular expression.
CGI program development
CGI programs generally receive form data, process data according to application requirements, and generate a new page based on the processing results and return it to the browser. Form data is generally submitted to the service using the POST method.
It is obtained by the CGI program. The program differentiates data based on the element name/value. After data processing is completed, the program reads the corresponding template file, fill in the corresponding data to the HTML text according to the annotation mark
Generate the final page and return it to the browser. The general logic of the program is: 1. security check, whether scripts can be run; 2. process the data submitted by the user, distinguish the data based on the element name in the element name/value, and then process the data according to the application requirements; 3. fill in the processing result filling form, fill the corresponding data in HTML text according to the annotation mark, generate the final page and return it to the browser. For details about the specific code implementation, you can refer to the relevant chapter in "embedded Linux system development details-Based on EP93XX series ARM. (Full text) 1) download and install CGIC
Download the CGIC library from the official website provided above and uncompress the package. There are about 10 files, which are useful:
Cgic. h: header file;
Cgic. c: source code file of CGIC;
Cgictest. c: a cgi program example provided by the author of the CGIC library;
Capture. c: a tool used to debug CGI programs;
Makefile: the script file for installing CGIC;
As you can see, the entire database is actually a cgic. c file, which can be said to be very refined.
We can install CGIC as a dynamic link library of the operating system, so that the source file cgic. c is no longer required during each compilation.
But because of the need (we will see it later), we will modify the cgic. c code, so we will not install it into the system. During each compilation, you only need to put cgic. c and cgic. h In the current folder.
2) test and install
Before writing your own CGI program, you must first use the example program to avoid configuration problems when the program goes wrong, or is there a problem with your program code.
We use the built-in cgictest. c to implement our first c CGI program.
You can create a working directory to store the source code of your CGI program,Copy the cgic. h, cgic. c, and cgictest. c files to this directory, and create a Makefile file with the following content:
- Test. cgi: cgictest. c cgic. h cgic. c
- Gcc-wall cgictest. c cgic. c-o test. cgi
Note that the second line must start with a tab key (and only one) and cannot contain spaces.
After saving the Makefile content, run the make command:
Make
We can see that there should be an additional test. cgi file in the current directory.
Create a cgi-bin directory under the root directory of your website (of course, the name can be obtained at will, but it is often called cgi-bin as a habit), and then assign it
Its permission to execute CGI code. After the permission is modified, restart Apache. After that, put the generated test. cgi In the cgi-bin directory. In this case, we can
Enter the following address for access:
Http: // 127.0.0.1/cgi-bin/test. cgi
If it is normal, a webpage should be displayed. In this way, the CGI program of the first C language runs.
If the browser reports an error, most of the operations are not completed correctly when Apache is configured.
3) the basic idea of using CGIC
The cgic. c Code shows that it defines the main function, while cgictest. c defines a cgiMain function. That is to say, for compiling with CGIC
All CGI programs are from cgic. after the library function completes a series of necessary operations (such as parsing parameters and obtaining system environment variables), it will call your code (defined by you
).
Another point is that the cgi program uses printf to print the HTML page line by line. For example, the Code in cgictest. c:
Fprintf (cgiOut, "<textarea NAME = \" address \ "ROWS = 4 COLS = 40> \ n ");
Fprintf (cgiOut, "Default contents go here. \ n ");
Fprintf (cgiOut, "</textarea> \ n ");
The running result of the above Code is to output a textarea on the page. The first parameter cgiOut is actually stdin, so we can directly use printf without using fprintf. However, fprintf is used to redirect the output during debugging.
This method is very similar to Java Servlet. Servlet also calls the print statement System. out. println (...) To output a page. (However, Java later introduced JSP to overcome this inconvenience .)
But what's different from Servlet is that when using C language, we also need to output the HTML header (declare the document type) by ourselves ):
CgiHeaderContentType ("text/html ");
This statement must be called before all printf statements. The task executed by this statement is:
Void cgiHeaderContentType (char * mimeType ){
Fprintf (cgiOut, "Content-type: % s \ r \ n", mimeType );
}
This statement tells the browser what type of data is sent this time, whether it is an HTML document or a binfile... For an HTML document, it is displayed in the browser window. If it is a bin (Binary) file, the download window is opened, allowing users to choose whether to save the file and save the file path.
After understanding these points, you can write your own CGIC program. Try to create a new file test. c:
Download: test. c
- # Include <stdio. h>
- # Include "cgic. h"
- # Include <string. h>
- # Include <stdlib. h>
- Int cgiMain (){
- CgiHeaderContentType ("text/html ");
- Fprintf (cgiOut, "<HTML> <HEAD> \ n ");
- Fprintf (cgiOut, "<TITLE> My First CGI </TITLE> </HEAD> \ n ");
- Fprintf (cgiOut, "<BODY> <H1> Hello CGIC </H1> </BODY> \ n ");
- Fprintf (cgiOut, "</HTML> \ n ");
- Return 0;
- }
Replace all cgitest. c In the Makefile file with test. c, save it, and run the make command.
When you access it through a browser, a large "Hello CGIC" is displayed on the page ".
From: http://my.chinaunix.net/space.php? Uid = 24810608 & do = blog & id = 330533