Porting embedded web server boa (III)

Source: Internet
Author: User
Porting embedded web server boa (III)

Author: Li qianguang, Zheng Zhe

This article is excerpted from the relevant chapter in the author "embedded Linux system development details-Based on ep93xx series arm.

Currently, CGI and server scripts, such as JSP and ASP, are used to generate dynamic web pages in Web technology. However, the latter requires the Web server to support the running of these scripts. Generally, only CGI support is provided for Embedded Web servers. Therefore, web applications are based on CGI in embedded devices.ProgramDevelopment. CGI (Common Gate intergace) is a program running on a web server that provides an interface for HTML pages on the same client. Let's take a look at the actual example: there is a message book on a common personal homepage. The message book works like this: Users enter some information, such as names, next, the user clicks "Leave a message" (currently working on the client), and the browser sends the information to the CGI program on the server, the CGI program processes the data on the server according to the predefined method. In this example, the user-submitted information is stored in the specified file, finally, the CGI program sends back a page with the words "End of leaving" to the client. You can see it in the browser.
Before CGI programming, we should first understand some HTML knowledge.

CGI can be used in many waysProgramming LanguageIncluding C, C ++, and Perl. However, in the development of embedded devices, Perl and other explanatory languages are generally not used, this language also requires support modules for interpretation and execution, which will occupy storage space and memory. The most common method is to write it in C, of course, however, C is not very suitable for developing programs that require a large number of string operations such as CGI, and programming is cumbersome. Therefore, for a professional developer, the first thing I think is whether there are reusable libraries to support quick and efficient CGI program development. Fortunately, there are many open source code C libraries that support CGI development. Here we will only introduce cgic. If you are interested, you can search for other C libraries on the Internet.

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 provides the following functions:

1. analyze data and automatically correct data sent from defective browsers;

2. transparently receive from data sent using the get or POST method;

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. Decompress the package and enter the source code directory.

# Tar xzf cgic205.tar.gz

# Cd cgic205

Modify the MAKEFILE file, find cc = GCC, change it to CC = arm-Linux-GCC, find AR = AR, and change it to AR = arm-Linux-ar, find ranlib = ranlib and change it to ranlib = arm-Linux-ranlib. Find GCC cgictest. o-O cgictest. change CGI $ {libs} to $ (CC) $ (cflags) cgictest. o-O cgictest. CGI $ {libs}, find GCC capture. o-O capture $ {libs} and 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 shows that the cgic library and test script are successfully transplanted. Cgictest. CGI fully demonstrates the functions of the cgic library. It is best to master cgictest before developing CGI programs based on the cgic library. CGI is also a reference example for users to develop specific applications.

HTML template Creation

Web application development usually removes the interface and program logic, allowing you to change the interface to a certain extent, such as changing the properties of the interface text and creating multilingual versions, 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, 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. For more information about 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. Form data is generally submitted to the server using the POST method and obtained by the CGI program. The program must correspond the interface data to the internal data for further processing. The CGI program gets data from the page based on the element name/value. However, it is troublesome for CGI to return to the page. 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, for the program, the form element name cannot be used as the basis for differentiation, the general method is to use comments 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. The CGI program can judge and process the form Element Information Based on the annotation mark. 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 the data is assigned to the form element, finally, you can 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 scripts are generally used. 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 include required, Maximum/minimum length, characters, numbers, email addresses, IP addresses, and regular expressions.

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 server using the POST method, which is obtained by the CGI program. The program differentiates data based on the element name/value. After the data is processed, it reads the corresponding template file, fill in the corresponding data to HTML text according to the annotation mark, and generate the final page and return it to the browser.

The general logic of the program is:

1. Check whether scripts can be run;

2. process the data submitted by the user, differentiate the data according to 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, and fill the corresponding data in the HTML text according to the annotation mark, and generate the final page and return it to the browser.

SpecificCodeFor implementation details, you can refer to the relevant chapter in "embedded Linux system development details-Based on ep93xx series arm. (Full text)

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.