Porting and using of "CGI" Cgic Library

Source: Internet
Author: User
Tags comment tag
Transplantation of Cgic library
Cgic is an open source standard C library that supports CGI development, and can be used for free, with only a public statement in the developed site and program documentation to show that the program uses the Cgic library, and that users can buy a business license without a public statement.
Cgic can provide the following features:
1 analyze the data and automatically correct the data sent by some defective browsers, 2 transparently receive the from data sent by the Get or post method, 3 can accept the uploaded file;
4 can set up and receive cookies;
5 handle carriage returns in the FROM element in a consistent manner;
6 provides strings, integers, floating-point numbers, radio or multiple selection functions to receive data;
7 Provide a boundary check for the number field;
8 can convert the CGI environment variable into a non-empty string in C;
9 provides the debugging means of CGI program, which can replay CGI state when CGI program executes;

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 migration process.      From Cgic's main site http://www.boutell.com/cgic/download source, the current version is 2.05 version. The download file address is: Http://www.boutell.com/cgic/cgic205.tar.gz to extract it and enter the source directory # tar xzf cgic205.tar.gz # cd cgic205 Modify Mak Efile file, 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}, 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}, and save exit.

Then run make to compile, get the Cgic library LIBCGIC.A, we debug the auxiliary program capture and test program cgictest.cgi, to verify the correctness of generating cgic library.

Copy capture and cgictest.cgi to the/nfs/www/cgi-bin directory of the host.
In the workstation's browser address bar input http://192.168.67.16/cgi-bin/cgictest.cgi, you can see the page, indicating that the Cgic library and test scripts were successfully ported.    CGICTEST.CGI a complete display of the Cgic library function, in the development of CGI based on Cgic Library before the best to master the CGICTEST.CGI program, but also the user to develop a specific application reference example. Making an HTML template
The application development of Web mode usually separates the interface from the program logic, allows to change the interface to some extent, such as changing the attributes of the interface text, establishing the multi-language version, etc. without altering the program logic. The interface is generally made by art, and the programmer is responsible for the implementation of the specific functions. In HTML, forms are the primary means of conveying information, and they apply to any browser. There are many elements in the form, including input text boxes, radio boxes, multiple marquee, buttons, and so on, to provide information about the interaction. Specific object descriptions and syntax see other HTML books, which are not covered here.      Depending on the application requirements, the artist or other designer will design the final Web page as a template for developers to develop. CGI program is generally the work of receiving form data, data processing, and finally based on processing results to generate a new page returned to the browser. form data is generally submitted to the server as a post method, obtained by a CGI program, and the program must correspond the interface data with the internal data before it can be processed next. The CGI program gets the data from the page and distinguishes it according to the element name in the element name/value. But the CGI return page is more cumbersome. Because the interface may change after the program is developed, and some places that need to be processed by the program may not have form elements, it is not possible to use the form element name as a basis for the program, and the general method is to mark the annotation <!-xxx--> in HTML.
The programmer needs to create a comment tag for each form element in the template, as well as any other place that needs to be processed, according to certain rules, such as the next line of the annotation, which is the form element row. The CGI program can judge the form element information and process it according to the annotation tag.      The program reads the template file line by row, checks if there are any annotation marks, and if so, the next line needs to be processed, the form element is assigned data, and finally the page with the data can be returned to the browser. The HTML template also needs to be concerned with the input check. According to the principle that the sooner the input check is better, the data submitted by the user needs to be checked in the user interface. This is generally a way to use JavaScript scripts. When the user submits the data, the OnSubmit method of the Form object is invoked, in which the user's input can be checked. Commonly used checks are required, maximum/small length, whether the character, whether the number, email address, IP address is correct, whether to match a regular expression, and so on.


The development of CGI programs
CGI program is generally the work of receiving form data, according to the application requirements for data processing, and finally based on the processing results generated a new page returned to the browser.      form data is typically submitted to the server as a post method, obtained by the CGI program, the program distinguishes the data according to the element name in the element name/value, finishes data processing, reads the corresponding template file, fills the corresponding data into the HTML text according to the annotation mark, and generates the final page to return to the browser.   The general logic of the program is: 1.   Security check, whether the script is allowed to run; 2.   Processing the data submitted by the user, distinguishing the data according to the element name in the element name/value, and then processing the data according to the application requirement 3.      Fills the form with the processing results, fills the corresponding data into the HTML text according to the annotation token, and generates the final page to return to the browser. For detailed code implementation details, users can refer to the "Embedded Linux system development detailed-based on the EP93XX series arm," the relevant chapters. (End of full text) 1) cgic Download installation

After downloading the Cgic library from the official web site provided above, unpack the package, which has about 10 files, which is useful:
Cgic.h: Header file;
Cgic.c:cgic source code file;
An example of a CGI program provided by the author of the Cgictest.c:cgic Library;
CAPTURE.C: A tool for debugging CGI programs;
Makefile: Installs the Cgic script file;
As you can see, the entire library is actually cgic.c a file, 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 compile, we do not need to have cgic.c this source file.


But as needed (which will be seen later), we will modify the CGIC.C code so we do 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 writing your own CGI program, be sure to go through his example program, so that when the program goes wrong, you don't know if there is a problem with the configuration or your program code.
We used his own cgictest.c to implement his first C-language CGI program.
You can create a new working directory to store your CGI program source code, copy cgic.h, CGIC.C, cgictest.c three files to this directory, and then create a makefile file that reads: test.cgi: cgictest.c cgic.h cgic.c gcc-wall cgictest.c cgic.c-o

You need to be reminded that the second line must begin with a TAB key (and only one) and you cannot use spaces.
After you save the contents of the makefile, execute the Make command:
make

We see that there should be an extra test.cgi file in the current directory.

Create a Cgi-bin directory in the root directory of your Web site (of course the name can be arbitrary, but as a custom, generally called cgi-bin), and then in the Apache configuration file to give it the ability to execute CGI code, after the permissions are modified to restart Apache. When you are done, put the test.cgi you just generated into 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 Web page being displayed. In this way, the first C-language CGI program is running.
If the browser complains, then most of the Apache is configured when some operations do not complete correctly. 3) The basic idea of using cgic

As you can see from the CGIC.C code, it defines the main function, and a cgimain function is defined in CGICTEST.C. That is, the CGI program written using Cgic is entered from the code in the CGIC.C, and after the library function completes a series of necessary operations (such as parsing parameters, obtaining system environment variables), it will invoke your code (from your defined Cgimain entry).

Another point is that CGI programs output HTML pages by using printf to print a line of pages, such as the one in cgictest.c: fprintf (cgiout, <textarea name= \ Address \) Rows=4 cols=40> \ n ");
fprintf (Cgiout, "Default contents go.) \ n ");
fprintf (Cgiout, "</textarea> n");

The result of this code is the output of a textarea on the page. The first argument cgiout is actually stdin, so we can use printf directly without using fprintf. However, the fprintf is used to redirect output when debugging.
This approach is very similar to the Java servlet, and the servlet System.out.println (...) by invoking the print statement. To output a page. (But then Java introduced a JSP to overcome this inconvenience.) )
But unlike the servlet, we have to export our HTML headers (declaring the document type) by using the C language: Cgiheadercontenttype ("text/html");

The call to this statement must precede all printf statements. And the task that this statement performs is actually: void Cgiheadercontenttype (char * mimetype) {
fprintf (Cgiout, "Content-type:%s \ r \ n \ r \ n", mimetype);
}

This statement tells the browser what kind of data it is coming from, whether it is an HTML document, or a bin file ... If it is an HTML document, it is displayed in a browser window, and if it is a bin (binary) file, the download window opens, allowing the user to choose whether to save the file and save the path to the file.

After understanding these points, you can write your own cgic program. Create a new file test.c try: Download: test.c #include <stdio.h> #include "cgic.h" #include <string.h> #include <stdlib.h > int Cgimain () {Cgiheadercontenttype ("text/html"); fprintf (Cgiout, "

Replace all cgitest.c in makefile file with TEST.c, save, then execute make command.
At this point, access through the browser, you will see a large "Hello cgic" on the page.

from:http://my.chinaunix.net/space.php?uid=24810608&do=blog&id=330533

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.