CGIC library transplantation
CGIC is an open source standard C library that supports CGI development. It can be used free of charge. You only need to have a public statement in the development site and program documentation. , Indicating that the program uses the CGIC library, and users can also purchase commercial licenses without a public statement.
CGIC can provide the following functions:
1 Analyze the data and automatically correct the data sent by some defective browsers;
2 Transparently receive From data sent by GET or POST;
< wbr>3Can accept uploaded files;
4Can set and receive cookies;
5 Handle the carriage return in the From element in a consistent manner;
6 Provide string, integer, floating point numbers , Single-choice or multi-choice function to receive data;
7 Provide boundary check of numeric fields;
8 Able to convert CGI environment variables into non-empty strings in C;
9 Provide debugging methods for CGI programs, which can be played back CGI程序执行时的CGI状态;
总之,CGIC是一个功能比较强大的支持CGI开发的标准C库,并支持Linux, Unix 和Windows等多操作系统。
The following describes the migration process of CGIC.
Download the source code from CGIC’s main site http://www.boutell.com/cgic/, currently The latest version is version 2.05. Unzip it and enter the source directory
# tar xzf cgic205.tar.gz
# cd cgic205
Modify Makefile< /span>, 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 ${LIBS} and change it to $(CC) $(CFLAGS) cgictest.o -o cgictest.cgi ${LIBS}, find gcc capture.o -o capture ${ LIBS}, change it to $(CC) $(CFLAGS) capture.o -o capture ${LIBS}, save and exit.
span> < span style="color:rgb(255,0,0); word-wrap:normal; word-break:normal">and then run make to compile, the obtained CGIC library libcgic.a, we assist with debugging The capture program and the test program cgictest.cgi are used to verify the correctness of the generated CGIC library.
span> < span style="color:rgb(255,0,0); word-wrap:normal; word-break:normal"> Copy capture and cgictest.cgi to the /nfs/www/cgi-bin directory of the host.
Enter in the browser address bar of the workstation http://192.168.67.16/cgi-bin/cgictest.cgi, you can see the page, which means that the CGIC library and test script are transplanted successfully. cgictest.cgi shows the functions of the CGIC library relatively completely. It is best to master the cgictest.cgi program before developing CGI programs based on the CGIC library. It is also a reference example for users to develop specific applications.
HTML template production
Web application development generally separates the interface and program logic Now, it is allowed to change the interface to a certain extent, such as changing the properties of the interface text, establishing a multi-language version, etc., without changing the program logic. The interface is generally made by artists, and the programmer is responsible for the realization of specific functions. In HTML, the form (FORM) is the most important means of transmitting information, and it is suitable for any browser. There are many elements in the form, including input text boxes, single selection boxes, multiple selection boxes, buttons, etc., which can provide information interaction. For specific object description and grammar, please refer to other HTML books, which will not be introduced here. According to application requirements, artists or other designers will design the final Web page as a template for programmers to develop.
The job of a CGI program is generally to receive form data, perform data processing, and finally generate a new page based on the processing result and return it to the browser. The form data is generally submitted to the server by the POST method and is obtained by the CGI program. The program must correspond to the interface data and internal data to be able to proceed with the next step. The CGI program obtains data from the page and distinguishes it according to the element name in the element name/value. But CGI is more troublesome to return to the page. Since the interface may change after the program is developed, and there may be no form elements in some areas that need to be processed by the program, for the program, the name of the form element cannot be used as the basis for distinguishing. The general method is to use the comments in the HTML< !-xxx--> to mark.
< wbr> Programmers need to create a comment mark for each form element and any other place that needs to be processed by the program in the template, according to certain rules, such as the next line of the comment is the form element line. The CGI program can then judge the form element information and process it based on the annotation mark. The program reads the template file line by line and checks for comment marks. If so, the next line needs to be processed, and the form elements are assigned data. Finally, the page with the data can be returned to the browser.
HTML templates also need to pay attention to the input check. According to the principle that the earlier the input check, the better, the data submitted by the user needs to be checked on the user interface. Currently, javascript scripts are generally used. When the user submits data, the onSubmit method of the form object is called, and the user’s input can be checked in this method. Commonly used checks include whether it is necessary, maximum/minimum length, whether it is a character, whether it is a number, email address, whether the IP address is correct, whether it matches a regular expression, and so on.
< span style="font-size:16px; word-wrap:normal; word-break:normal; line-height:24px">
Development of CGI programs
The job of CGI programs is generally to receive form data, process data according to application requirements, and finally generate a new page based on the processing results to return To the browser. The form data is generally submitted to the server by the POST method and obtained by the CGI program. The program distinguishes the data according to the element name in the element name/value. After the data processing is completed, the corresponding template file is read, and the corresponding data is marked according to the comment. Fill it into the HTML text, generate the final page and return it to the browser.
The general logic of the program is:
1. Security check, whether scripts are allowed to run;
2. Process the data submitted by the user, distinguish the data according to the element name in the element name/value, and then process the data according to the application requirements;
3. Fill the form with the processing result, fill the corresponding data into the HTML text according to the annotation mark, and generate the final page and return it to the browser.
For specific code implementation details, users can refer to “Embedded Linux System Detailed development explanation-based on the relevant chapters of the book EP93XX series ARM. (End of full text)
1) Download and install CGIC
After downloading the CGIC library from the official website provided above, unzip the compressed package, there are about 10 files inside,< span style="color:rgb(255,0,0); word-wrap:normal; word-break:normal">What is useful:
cgic.h: header file;
cgic.c: CGIC source code file;
cgictest.c: a CGI program example provided by the author of CGIC library;
capture.c: Tools for debugging CGI programs;
Makefile: The script file for installing CGIC;
As you can see, the entire library is actually a file of cgic.c, which can be said to be very Refined.
We can install CGIC as a dynamic link library of the operating system, so that every time we When compiling, there is no need to have the source file cgic.c.
< span style="color:rgb(255,0,0); word-wrap:normal; word-break:normal">
But due to need (will see later), we will modify the cgic.c code, so we will not install it into the system. Every time you compile, just put cgic.c and cgic.h in the current folder.
2) Test installation
Before you start to write your own CGI program, you must first go through his example program, so that you don’t know the configuration when the program goes wrong later There is a problem, or there is a problem with your program code.
We use his own cgictest.c to implement our first C language CGI program.
You can create a new working directory to store your CGI program source code, < strong>Copy the three files cgic.h, cgic.c, and cgictest.c to this directory, and then create a Makefile with the content:
- test.cgi:cgictest.c cgic.h cgic.c
- gcc -wall cgictest.c cgic .c -o test.cgi
It should be reminded that the beginning of the second line must be a tab key (and only one), and spaces cannot be used.
After saving the contents of the Makefile, execute the make command:
make
We see that there should be an additional test.cgi file in the current directory.
at the root of your website Create a cgi-bin directory under the directory (of course the name can be taken arbitrarily, but as a custom, it is generally called cgi-bin), and then give it the permission to execute CGI code in the Apache configuration file, and restart Apache after the permission is modified. After completion, put the test.cgi just generated in the cgi-bin directory. At this point, we can enter the following address in the browser to access:
http://127.0.0.1/cgi-bin/test.cgi
If normal, you should see a webpage displayed come out. In this way, the first CGI program in C language is up and running.
If the browser reports an error, it is probably because some operations were not completed correctly when configuring Apache.
3) The basic idea of using CGIC
From the code of cgic.c, it can be seen that it defines the main function, and a cgiMain function is defined in cgictest.c. In other words, for CGI programs written with CGIC, they are all entered from the code in cgic.c. After the library function has completed a series of necessary operations (such as parsing parameters and obtaining system environment variables), it will call you The code (entered from the cgiMain you defined).
Another point is, cgi The way the program outputs HTML pages is to use printf to print the page line by line, such as this code in cgictest.c:
fprintf ( cgiOut , “ cgiOut< /span> , “ \ n “ ) ;
The 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 instead of fprintf. However, fprintf is used to redirect the output when debugging.
This method is very similar to Java Servlet. Servlet also outputs a page by calling the print statement System.out.println(…). (But later Java introduced JSP to overcome this inconvenience.)
But the difference from Servlet is that we use C language to output HTML headers (declare the document type) by ourselves:
cgiHeaderContentType ( “ text/html < span style="word-wrap:normal; word-break:normal; color:rgb(139,0,0)">“ ) ;
This statement must be called in all print Before the f statement. The task performed by this statement is actually:
< span style="word-wrap:normal; word-break:normal">void cgiHeaderContentType < span style="word-wrap:normal; word-break:normal; color:olive">( char < span style="word-wrap:normal; word-break:normal; color:gray">* mimeType< /span> ) {
fprintf ( cgiOut , “ Content-type: %s \ r \ n \ r \ n “ , mimeType ) ;
}
this The statement tells the browser, what type of data is passed this time, is it an HTML document or a bin file… If it is an HTML document, it will be displayed through the browser window, if it is a bin (binary) file, the download window will be opened , Let the user choose whether to save the file and the path to save the file.
understand these points After that, you can write your own CGIC program. Try to create a new file test.c:
Download: test.c
- #include<stdio.h>
< li style="margin:0px 0px 0px 30px; padding:0px; border:0px; list-style-type:decimal; list-style-position:initial"> #include “cgic.h“
- #include <string.h>
- #include<stdlib.h>
- intcgiMain(){
- cgiHeaderContentType(“text/html“);
- fprintf(cgiOut, “< HEAD>\n“);
- fprintf(cgiOut, “My First CGI\n“);
fprintf(cgiOut, “Hello CGIC
\n“);
- fprintf(cgiOut, “