Embedded CGI development a good article

Source: Internet
Author: User
Tags stdin strcmp strtok

http://blog.ednchina.com/longhaihai/255858/Message.aspx#

2 Implementation and configuration of Web Server boa

Under 2.1 uclinux, there are three web server:httpd, thttpd and Boa. HTTPD is the simplest of a web Server, it is the weakest, does not support authentication, and does not support CGI. THTTPD and Boa All support authentication, CGI and so on, the function is all compared. Boa is a single task of a small HTTP server, the source code is open, excellent performance, especially suitable for application in embedded systems. The current Uclinux code already contains the source code for the BOA. Implement Boa under Uclinux, only need to do some configuration and modification to boa. The following is the process of configuring.

(1) Compiling Boa to the kernel

First, you need to compile the boa into the kernel, that is, execute make menuconfig, select Boa under the Network application item in the Application menu. This operation requires recompiling the kernel.

(2) Compiling the configuration file boa.conf

Under the Linux operating system, the configuration of the application is provided in the form of a configuration file, usually in the/etc/directory of the target board or in the/etc/config directory. However, the BOA configuration file Boa.cont generally rotate in the target board/home/httpd/directory.

For example, a typical boa.conf file format is as follows:

ServerName Samsung-arm

Documentroot/home/httpd

scriptalias/cgi-bin/home/httpd/cgi-bin/

Scriptalias/index.html/home/httpd/index.html

It specifies that the HTML page must be placed in the/HOME/HTTPD directory, and that the CGI external extender must be placed under the/home/httpd/cgi-bin directory.

(3) Compile and write the kernel

After recompiling the kernel, you can access the Web Server on the developer board by using the IE browser on your PC by burning the kernel with the burn write tool. For example, enter the IP address of the Development Board HTTP://192.168.0.101, you can access to your own web page index.html. And, by writing a CGI external extender, you can implement Dynamic Web technology, which is described in detail below.

2. 2 implementation and configuration of Linux b0a with MMU platform

For platforms with MMU (memory snap-in), such as Armlinux and Ppclinux, you can download a major version of the BOA release package online. Because it is running on the target system, you need to compile with the cross compilation tool, that is, modify the compiler inside the boa/src/makefile. For example:

cc=/linuxppc/cdk/bin/powerpc-linux-gcc

cpp=/linuxppc/cdk/bin/powerpc-linux-g++

Then execute make directly in the BOA/SRC directory to generate the BOA executable file, compile it into the kernel, and burn it to the storage device to achieve access to the BOA server.

3 CGI program Design Technology

CGI (Common Gateway Interface) is a standard interface for external application extension applications to interact with the WWW server. External extension applications written according to the CGI standard can handle the data entered by the client browser to perform client-server interaction. The CGI specification defines how a Web server sends messages to an extended application, and how it handles content after it receives information about the extended application. CGI provides functionality that many static HTML Web pages cannot implement, such as search engines, web-based database access, and so on.

3. 1 Working principle

(1) The working principle of www and CGI

HTTP protocol is the basis of WWW, it is based on the client/server model, a server can provide services for customers distributed in the network, it is built on the TCP/IP protocol on top of the "No Connection" protocol, each connection only one request processing. On the server, run a daemon to listen to the port, waiting for requests from the customer. When a request arrives, a child process is created to serve the user's connection. Depending on the request, the server returns the HTML file or calls the external application via CGI, returning the processing result. The server interacts with the external program and script through CGI, and the server collects the information provided by the client and sends it to the specified CGI extender, depending on the method that is taken by the clients at the time the request is made. The CGI extender carries out information processing and returns the results to the server, which is then parsed by the server and sends the results back to the client.

The external CGI program communicates with the WWW server, passes the relevant parameters and processing results through environment variables, command line parameters and standard input. The server provides a channel for exchanging information between the client (browser) and the CGI extender. The standard input of the CGI is the standard output of the server, while the standard output of the CGI is the standard input of the server. The client's request is routed to the standard input of the CGI through the standard output of the server, and the CGI processes the information, sends the result to its standard input, and then sends the processing result to the client by the server.

(2) URL encoding

The client browser sends data to the server in the form of an encoding. The encoding is the CRL encoding. The main work of encoding is the escape of the name and value of a form field, in which the spaces in each pair of fields and values are replaced with a plus (+) character, and the characters that are not alphabetic or numeric are replaced by their hexadecimal digits in the form of%hh. HH is the ASCII hexadecimal value of the character. <BR> label will be replaced with "%0d%0a".

Information is arranged in the order in which they appear in the form. The name of the data field and the value of the data field are fonts together by the equal sign (=) character. Each pair name/value is again connected by the "&" character. After these encodings are processed, the form signal becomes a continuous stream of characters, with all the information that will be sent to the server.

Because form input information is encoded and passed to the script, the CGI extender must decode the parameters before using them.

3. 2 CGI external extension programming

Server programs can receive information in three different ways: environment variables, command lines, and standard input. Which method to use is determined by the methods attribute of the <FORM> label.

In "Method=get", the normal way to pass form encoded information to a CGI program is through a command. Most form encoding information is passed through QUERY_STRING environment variables. If "Method=post", the form information is read through standard input. There is also a way to send information to a CGI without using a form, which is to retrieve the information directly behind the URL address, and between the message and the URL with a question mark (. ) to separate.

The following is a detailed introduction to the application examples of GPIO (general input/output) of the Web remote monitoring arm chip.

(1) Get method

A Get method is a request to a data that is used to obtain a static document. When you use the Get method, the CGI program gets the data from the environment variable query_string. In order to handle client requests, CGI must parse the strings in the query_string. You should select the Get method when you need to obtain data from the server and do not change the data on the server, but if the request contains more than a certain length of the string, typically 1024 bytes, you can only select the Post method. The Get method sends the request information by appending the parameters that follow the URL. These parameters will be placed in the environment variable query_string and passed to the CGI program. The form format of the Get method and the CGI decoder can refer to the implementation of the Post method.

(2) Post method

The Post method is generally used when the browser passes data from a completed form to the server, and the Post method must be used when the data sent exceeds 1024 bytes. When using the Post method, the Web server transmits data to the standard input stdin of the CGI program. The length of the data being sent exists in the environment variable content_length, and the data format of the Post method is:

Variable1=value1&variable2=value2&etc

The CGI program must check the REQUEST_METHOD environment variable to determine whether the post method is used and decide whether to read stdin. The forms that the Post method defines in an HTML document are as follows:

<form method=post action= "/cgi-bin/cgi_gpio.cgi" >

<input type= "RADIO" Name=rb value= "0" >operate p0<br>

<input type= "RADIO" Name=rb value= "1" >operate p1<br>

<input type= "RADIO" Name=rb value= "2" >operate p2<br>

<input name= "OK" type=submit value= "OK" ><INPUT>

Name= "Cancel" type=reset value= "reset" ></FORM>

The server script that it invokes is/cgi/bin/cgi_gpio.cgi. The decoding of form forms in the CGI extender can refer to the following procedure:

/*function getpostvars*/

Char **getpostvars () {

int i;

int content_length;

Char **postvars;

Char *postinput;

Char **pairlist;

int paircount=0;

CHR *nvpair;

Char *eqpos;

Postinput=getenv ("Content_length");//Get the number of bytes passed to program data

if (!postinput)

Exit ();

if (!content_length=atoi (postinput))//Get information length

Exit (1);

if (!) ( postinput= (char*) malloc (content_length+1))

Exit (1);

if (!fread (Postinput,content_length,1,stadin))

Exit (1);

postinput[content_length]= ' 0 ';

for (i=0;postinput[i];i++)

if (postinput[i]== ' + ')

Postinput[i]= '; Handling of Add-ease

pairlist= (char * *) malloc (256*sizeof (char *));

paircount=0;

Nvpair=strtok (Postinput, "&");//fragment The information from the position where the "&" character appears, and then process the results sequentially

while (Nvpair) {

Pairlist[paircount++]=strdup (Nvpair);

if (!) ( PAIRCOUNT%256))

Pairlist= (char**) realloc (Pairlist, (paircount+256) *sizeof (char**));

Nvpair=strtok (NULL, "&");

}

pairlist[paircount]=0;

Postvars= (char**) malloc ((paircount*2+1) *sizeof (char * *));

for (i=0;i<paircount;i++) {

if (EQPOS=STRCHR (pairlist[i], ' = ')) {

*eqpos= ' 0 ';

Unescape_url (Postvars[i*2+1]=strdup (eqpos+1))//Invoke Unescape_url function to continue decoding

}else{

Unescape_url (postvars[i*2+1]) =strdup (""));

}

postvars[paircount*2]=0;

for (i=0;pairlist[i];i++)

Free (pairlist[i]);

Free (pairlist);

Free (postinput);

return postvars;

}

The Unescape_url function then calls the X2C function, decoding the special character (not byte or number) from its%HH representation to a text character.

/*unescape_url function*/

static void Unescape_url (char *url) {

int x,y;

for (X=0,y=0;url[y];++x,++y) {

if ((url[x]=url[y]) = = '% ') {

URL[X]=X2C (&url[y+1]);

y+=2;

}

}

url[x]= ' 0 ';

}

(3) Direct URL plus parameter transfer method

This is a way to send information to a CGI without using the form. It appends the information directly to the URL address, and between the message and the URL with the number (. ) to separate. For example, for a cgi_gpio.cgi script, you can start from the following link:

<a href=/cgi-gpio.cgi!? Flag=0 Operate p0</a>

<a href>/*cgi-bin/cgi_gpio.cgi?flag=1 Operate p1</a>

<a href=/cgi-bin_gpio.cgi?flag=2 Operate p2</a>

.

.

.

The following code can be used in the CGI extender to receive information: Char *get_input;//is used to receive environment variables

.

.

.

Get_input=getenv ("query_string");

if (get_input) {

Get_input=strdup (Get_input);

printf ("Query_string if%s", get_input);

}

/* Judge Flag=x Information * *

if (!strcmp (Get_input, "flag=0")

...//operate p0

else if (!strcmp (Get_input, "flag=1")

...//operate P1

Else

...//operate P2

The above three methods can be selected according to different application situations and application requirements.

Conclusion

Embedded Web server system can be widely used in many fields, such as remote monitoring of automation equipment, embedded GSM short message platform and remote home medical. Moreover, with the deepening of Internet application, the embedded Internet technology will be widely used and developed.

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.