Transplantation and use of CGIC library

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 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 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:

  1. test.cgi:cgictest.c cgic.h cgic.c
  2. 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 , \ 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
  1. #include<stdio.h>
  2. < li style="margin:0px 0px 0px 30px; padding:0px; border:0px; list-style-type:decimal; list-style-position:initial"> #include cgic.h

  3. #include <string.h>
  4. #include<stdlib.h>
  5. intcgiMain(){
  6. cgiHeaderContentType(text/html);
  7. fprintf(cgiOut, < HEAD>\n);
  8. fprintf(cgiOut, My First CGI\n);
  9. fprintf(cgiOut, 

    Hello CGIC

    \n);
  10. fprintf(cgiOut, \n);
  11. return 0;
  12. }

把Makefile文件中的cgitest.c全部换称test.c,保存,再执行make命令即可。此时通过浏览器访问,会在页面上看到一个大大的“Hello CGIC”。

CGIC库的移植
    

   CGIC是一个支持CGI开发的开放源码的标准C库,可以免费使用,只需要在开发的站点和程序文档中有个公开声明即可,表明程序使用了CGIC库,用户也可以购买商业授权而无需公开声明。
     CGIC能够提供以下功能:

   1   分析数据,并自动校正一些有缺陷的浏览器发来的数据;

   2   透明接收用GET或 POST方法发来的From数据;

   3   能接受上传文件;
    4   能够设置和接收cookies;
    5   用一致的方式处理From元素里的回车;
    6   提供字符串,整数,浮点数,单选或多选功能来接收数据;
    7   提供数字字段的边界检查;
    8   能够将CGI环境变量转化成C中的非空字符串;
    9   提供CGI程序的调试手段,能够回放CGI程序执行时的CGI状态;

总之,CGIC是一个功能比较强大的支持CGI开发的标准C库,并支持Linux, Unix 和Windows等多操作系统。

以下描述CGIC的移植过程。

   从CGIC的主站点http://www.boutell.com/cgic/下载源码,当前最新版本是2.05版。将其解压并进入源码目录

     # tar xzf cgic205.tar.gz

     # cd cgic205

     修改Makefile文件,找到CC=gcc,将其改成CC=arm-linux-gcc,找到AR=ar,将其改成AR=arm-linux-ar,找到RANLIB=ranlib,将其改成RANLIB=arm-linux-ranlib。找到gcc cgictest.o -o cgictest.cgi ${LIBS},将其改成$(CC) $(CFLAGS) cgictest.o -o cgictest.cgi ${LIBS},找到gcc capture.o -o capture ${LIBS},将其改成$(CC) $(CFLAGS) capture.o -o capture ${LIBS},并保存退出。

    然后运行make进行编译,得到的CGIC库libcgic.a,我们通过调试辅助程序capture和测试程序cgictest.cgi,来验证生成CGIC库的正确性。

    将capture和cgictest.cgi拷贝到主机的/nfs/www/cgi-bin目录下。

     在工作站的浏览器地址栏输入http://192.168.67.16/cgi-bin/cgictest.cgi,可以看到页面,表示CGIC库和测试脚本都移植成功。 cgictest.cgi比较完整的展现了CGIC库的功能,在开发基于CGIC库的CGI程序前最好先掌握cgictest.cgi程序,也是用户开发特定应用程序时的参考范例。

   HTML模板的制作
Web方式的应用开发一般都会将界面和程序逻辑脱离开来,允许在一定程度下更改界面,如改变界面文本的属性,建立多语言版本等,而无需改动程序逻辑。界面一般由美工来进行制作,而程序员负责具体功能的实现。在 HTML中,表单 (FORM)是最主要的传递信息的手段,它适用于任何浏览器。表单中有很多元素,包括输入文本框,单选框,多选框,按钮,等等,可以提供信息的交互。具体对象说明和语法请参见其他HTML书籍,在这里不作介绍。根据应用需求,美工或其他设计人员将最后的Web页面设计出来,作为程序员进行开发的模板。

     CGI程序的工作一般就是接收表单数据,进行数据处理,最后根据处理结果生成新的页面返回给浏览器。 表单数据一般是以POST方法提交给服务器,由CGI程序获得,程序必须要将界面数据和内部数据对应起来才能够进行下一步的处理。 CGI程序从页面获取数据就根据元素名字/值中的元素名字来进行区分。但CGI返回页面就比较麻烦。由于界面在程序开发完成后还有可能会改变,而且有些需要程序处理的地方可能没有表单元素,因此对程序来说,不能以表单元素名作为区分的基础,一般方法是采用HTML中的注释来标记。


     程序员需要在模板中为每一个表单元素以及其他任何需要程序处理的地方,按照一定规则,如注释的下一行就是表单元素行,建立其注释标记。 CGI程序就可以根据注释标记来判断表单元素信息并进行处理。程序逐行读取模板文件,检查有无注释标记,如有的话,则下一行需要进行处理,给表单元素赋上数据,最后就可以返回带数据的页面给浏览器。

     HTML模板还需要关注的是输入的检查。根据输入检查越早越好的原则,需要在用户界面上就对用户提交的数据进行检查。目前一般是采用javascript脚本的方式。当用户提交数据时,表单对象的onSubmit方法就会被调用,在该方法里就可以对用户的输入进行检查。常用的检查有是否必需、最大/小长度、是否字符、是否数字、email地址、IP地址是否正确、是否匹配一个正则表达式等。

CGI程序的开发
     CGI程序的工作一般就是接收表单数据,根据应用需求进行数据处理,最后根据处理结果生成新的页面返回给浏览器。表单数据一般是以POST方法提交给服务器,由CGI程序获得,程序根据元素名字/值中的元素名字来区分数据,完成数据处理后,再读取相应的模板文件,根据注释标记将对应的数据填充到HTML文本中去,生成最后的页面返回给浏览器。

     程序一般逻辑为:

     1.   安全性检查,是否允许运行脚本;

     2.   处理用户提交的数据,根据元素名字/值中的元素名字来区分数据,然后根据应用需求进行数据处理;

     3.   将处理结果填充表单,根据注释标记将对应的数据填充到HTML文本中去,生成最后的页面返回给浏览器。

     关于具体的代码实现细节,用户可以参考《嵌入式Linux系统开发详解-基于EP93XX系列ARM》一书的相关章节。 (全文完)

1) CGIC的下载安装

2) 测试安装

  1. test.cgi:cgictest.c cgic.h cgic.c
  2.    gcc -wall cgictest.c cgic.c -o test.cgi

make

http://127.0.0.1/cgi-bin/test.cgi

3) 使用CGIC的基本思路

fprintf ( cgiOut ,  \ n ) ;

cgiHeaderContentType ( text/html ) ;

void  cgiHeaderContentType ( char  * mimeType )  {
  
  fprintf ( cgiOut ,  Content-type: %s \ r \ n \ r \ n ,  mimeType ) ;
}

下载:  test.c

  1. #include <stdio.h>
  2. #include cgic.h
  3. #include <string.h>
  4. #include <stdlib.h>
  5. int cgiMain() {
  6. cgiHeaderContentType(text/html);
  7. fprintf(cgiOut, \n);
  8. fprintf(cgiOut, My First CGI\n);
  9. fprintf(cgiOut, 

    Hello CGIC

    \n);
  10. fprintf(cgiOut, \n);
  11. return 0;
  12. }

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1624 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.