Linux CGI Programming Basics __HTML5

Source: Internet
Author: User
Tags html form stdin
1. Why use CGI.

As you can see, any HTML is a static Web page, and it does not implement some complex functionality, and CGI is achievable for us. Such as:

A. List the files in a directory on the server and manipulate the files in the directory;

B. Implement serial communication via CGI;

C. Implement database interface;

D. Implementation from the camera to read a picture displayed on the Web page ... Wait, 2. What CGI is.

The CGI full name is Common Gate Intergace, which, in physics, is a program that runs on the server and provides an interface to the client HTML page. 3. CGI programming language

You can use any kind of high-level language that you are familiar with, C,c++,c Shell,perl and VB. 4. CGI Security

In fact, CGI is relatively secure, at least more securely than ActiveX controls that are not digitally signed. Unless you intentionally add a command to destroy the server in your program, there will be no serious consequences.
In short, CGI is the interface used to communicate HTML forms and server-side programs (interface). Say it's an interface, which means that CGI is not a language, but a set of specifications that can be applied by other languages. In theory, you can write CGI programs in any programming language, as long as you are programmed to conform to something defined by the CGI specification. C is one of the preferred languages for CGI programming, since C is a good platform-agnostic (with its corresponding compilers on almost any system platform) and is familiar to most programmers (unlike Perl). Here we introduce the use of C to write CGI programs.

One of the simplest examples of CGI programming is the processing of forms. So in this article, we mainly introduce how to use C to write a CGI program to do the table but processing.
5. Transmission method:

The so-called method refers to the way to invoke a CGI program. In fact, to execute a program, you use a method to make a request to the server, which defines how the program accepts data. Here are two common methods: Get and post

1.GET When this method is used, the CGI program obtains the data from the environment variable query_string.

Query_string is called an environment variable, which is the environment variable that passes the client's data to the server. In order to interpret and execute the program, CGI must parse (process) this string.

2.POST when using the POST method, the Web server transmits data to the CGI program through STDIN (standard input). The server does not use the EOF character tag at the end of the data, so the program must use CONTENT_LENGTH for the proper reading of stdin.

When you send data that will change the Web server side of the data or you want to send the CGI program to send more than 1024 bytes of data, this is the limit length of the URL, you should use the Post method.

Implementation method:

Get Implementation method

<form name= "Guyi ' s form" action= "http://www.yourname.com/cgi/your.cgi" method=get>

Post Implementation method:

<form method=post> 6. Form encoding:

The Enctype property of the form is encoded and is commonly used in two ways: application/x-www-form-urlencoded and Multipart/form-data, and the default is application/ X-www-form-urlencoded.

When the action is get, the browser converts the form data into a string (Name1=value1&name2=value2 ...) using x-www-form-urlencoded encoding. , and then append the string to the URL and use the? Split to load the new URL.

When the action is post, the browser encapsulates the form data into the HTTP body and sends it to the server.

If you don't have a type=file control, you can use the default application/x-www-form-urlencoded.

But if you have type=file, you will need to use Multipart/form-data. The browser splits the entire form into controls and adds Content-disposition (form-data or file) to each section, Content-type (default is Text/plain), name (control name), and so on. and add a separator (boundary). processing of Get forms

For those forms that use the attribute "Method=get" (or without the method attribute, the get is its default), the CGI definition is that when the form is sent to the server, the data in the form is saved on the server in an environment variable called query_string. The processing of this form is relatively simple, as long as you read the environment variables. This is a different approach to different languages. In C, you can use the library function getenv (defined in the standard library function Stdlib) to access the value of an environment variable as a string. You can get the data in the string and use some tricks for the type conversion, which is quite simple. Standard output in a CGI program (output) (such as the stdout file stream in C) is also redefined. It does not produce any output on the server, but is redirected to the client browser. In this way, if you write a C CGI program, export an HTML document to its stdout, the HTML document will be displayed in the client's browser. This is also a basic principle of CGI programs.

Let's look at a specific program implementation, and here's an HTML form:

<form action= "/cgi-bin/mult.cgi" >
<p> please fill in the multiplier and multiplier below and click OK to see the results. </p>
<input name= "M" size= "5" >
<input name= "n" size= "5" ><br>
<input type= " SUBMIT "value= OK" >
The function we want to implement is simply to multiply the values entered in the form and then output the results. In fact, this feature can be implemented entirely in JavaScript, but in order to make the program as simple as possible, I chose this small multiplication as an example.
Here is the CGI program that handles this form, corresponding to the value of the action attribute in the form label.
#include < stdio.h >
#include < stdlib.h >

int main (void)
{
    char *data;
    Long M,n;
    
    printf ("%s%c%c", "content-type:text/html;charset=gb2312", n);
    printf ("<TITLE> multiplication result </TITLE>");
    printf ("<H3> multiplication result </H3>");

    data = getenv ("query_string");
    if (data = NULL)
        printf ("<P> error. Data not being entered or having a data transfer problem ");
    else if (sscanf (data, "M=%ld&n=%ld", &m, &n)!= 2)
        printf ("<P> error." The input data is illegal. A number must be entered in a form. " );
    else
        printf ("<p>%ld and%ld" results are:%ld. ", M, N, m*n);

    return 0;
}
The specific C syntax is not much to say, let's look at it as a special place for CGI programs.
The content of the standard output mentioned above is the content to be displayed in the browser. The output of the first line is required and is unique to a CGI program: printf ("%s%c%c", "content-type:text/html", 13,10), which is the header of the HTML file. Because CGI can not only output HTML text like a browser, but also output images, sounds, and so on. This line tells the browser how to handle the received content. There are two lines of blank lines behind the definition of Content-type, which is also indispensable. Because the head output of all CGI programs is similar, you can define a function for it to save programming time. This is a common technique for CGI programming.
The program then calls the library function getevn to get the query_string content, and then uses the SSCANF function to take out each parameter value, it is important to note the use of the SSCANF function. The other is nothing, and the general C program is no different.
After compiling the program, renamed MULT.CGI placed under the/cgi-bin/directory, it can be monotonous use of the table. In this way, a CGI program that handles get form forms is done.

Post form processing

Here's another way to consider form transfer: POST. Let's assume that the task we are implementing is to add a piece of text that the customer entered in the form to the back of a text file on the server. This can be seen as the embryonic form of a message board program. Obviously, this job cannot be implemented using JavaScript as a client script, and is a real CGI program.


It seems that the problem is similar to what is said above, simply by using different forms and different scripts (Programs). But in fact, there are some differences between the two. In the example above, the processing of a get can be thought of as a "pure query (pure query)" type, that is, it has nothing to do with state. The same data can be submitted any number of times without causing any problems (except for some small overhead of the server). But now the task is different, at least it will change the content of a file. Thus, it can be said that it is related to the state. This is also considered one of the differences between post and get. Also, get is limited to the length of the form, and post is not, which is the main reason for choosing the Post method in this task. But relatively, the process of getting is faster than post.


In the definition of CGI, the contents of a post-type form are sent to the standard input of the CGI program (stdin in C), while the length of the transmission is placed in the environment variable CONTENT_LENGTH. So what we're going to do is read the content_length length string in the standard input. Reading data from standard output sounds like it's easier to read data from an environment variable than it is, but otherwise, there are some details to pay attention to, which can be seen in the following program. Special attention is: CGI programs and general procedures are different, the general program after reading a file stream content, will get an EOF logo. But in the process of the CGI program form processing, EOF is never appear, so do not read more than content_length length of characters, whether this will have any consequences, no one knows (the CGI specification is not defined, generally based on the different server and have different processing methods).


Let's take a look at how to collect data from the post form to the CGI program, and here's a simpler C source code:

#include < stdio.h > #include < stdlib.h > #define MAXLEN #define EXTRA 5/* 4 bytes Left field name "Data", 1 bytes Left "=" * * #define MAXINPUT maxlen+extra+2/* 1 bytes left to line break, there is a left behind the null * * * * #define DATAFILE ". /data/data.txt "/* * The file to be added to data/void Unencode (char *src, Char *last, char *dest) {for (; src!= last; src++, dest
        + +) {if (*src = = "+") *dest = "";
            else if (*src = = "%") {int code;
            if (sscanf (src + 1, "%2x", &code)!= 1) code = "?";
            *dest = code; 
        src = 2;
    else *dest = *SRC;
    } *dest = "";

*++ dest = "";}
    int main (void) {char *lenstr;
    Char Input[maxinput], data[maxinput];

    Long Len;
    printf ("%s%c%c", "content-type:text/html;charset=gb2312", 13, 10);
    
    printf ("<TITLE>Response</TITLE>");
    Lenstr = getenv ("Content_length"); if (lenstr = NULL | | sscanf (LENSTR, "%lD ", &len)!= 1 | |
    Len > MaxLen) printf ("<P> form submission error");
        else {FILE *f;
        Fgets (input, len+1, stdin);
        Unencode (input + EXTRA, input + len, data);
        f = fopen (DataFile, "a");
        if (f = = NULL) printf ("<P> Sorry, unexpected error, unable to save your data");
        else fputs (data, f);
        Fclose (f);
    printf ("<P> Thank you very much, your data has been saved <br>%s," "Data");
return 0;  }


Note: In actual use, use fgets to get upload files may be error, to use FGETC instead

In essence, the program first obtains the word length of the data from the CONTENT_LENGTH environment variable, and then reads the string of corresponding lengths. Because the data content is encoded in the process of transmission, it must be decoded accordingly. The coding rules are simple, and the main ones are:
1. Each field in the form is represented by the field name followed by an equal sign, and then the value of the field is used to indicate that the content between each field is & connected;
2. All the space symbols are replaced by plus sign, so it is illegal to appear in the Code section;
3. Special characters such as punctuation marks, and some words of a particular meaning Furu "+", expressed with a percent sign followed by its corresponding acsii code value.

For example, if the user enters:

Hello there!

When the data is transmitted to the server, it is encoded, and the Unencode () function on the data=hello+there%21 is used to decode the encoded data.

(Note: When using lighttpd+cgi programming, the data read from the standard input is plaintext and can be used directly.) Therefore, according to my supposition, the data of this article is encoded, which means that the data is encoded and needs to be decoded before using the server such as LIGHTTPD. )

After the decoding is complete, the data is added to the end of the Data.txt file and is displayed back in the browse.
After compiling the file, renaming it to collect.cgi and placing it in the CGI directory can be used in a monotonous table. The corresponding form is given below:

< FORM action= "/cgi-bin/collect.cgi" method= "POST" >

< P > Please enter your message (up to 80 characters):< BR >< INPUT name= "Data" size= "maxlength="-">< br" >

< INPUT type= "SUBMIT" value= "OK" >

</form >


As a matter of fact, this procedure can only be used as an example and cannot be formally employed. It misses a key problem: When multiple users write data like files at the same time, there is a certain error. For a program like this, the chances of a file being written at the same time are great. Therefore, in the more formal message version of the program, you need to do some more consideration, such as adding a semaphore, or the use of a key file. Because it's just a matter of programming skills, it's not much to say here.


Finally, we're going to write a CGI program that browses the Data.txt file, which only needs to be output to stdout:

#include < stdio.h >

#include < stdlib.h >

#define DATAFILE ". /data/data.txt "

int main (void)

{

FILE *f = fopen (datafile, "R");

int ch;

if (f = = NULL) {

printf ("%s%c%c",

"content-type:text/html;charset=gb2312", 13,10);

printf ("< TITLE > Error </title >");

printf ("< P >< EM > Unexpected error, unable to open file </em >"); }

else {

printf ("%s%c%c",

"Content-type:text/plain", 13,10);

while ((Ch=getc (f))!= EOF)

Putchar (CH);

Fclose (f); }

return 0;

}


The only thing to note in this program is that it does not wrap the Data.txt into HTML format and then output, but directly as a simple text (plain text) output, which only in the output of the head with Text/plain type instead of text/html can be, The browser automatically chooses the appropriate processing method based on the type of Content-type.


To trigger this program is also very simple, because there is no data to enter, so just a button can be done:


< FORM action= "/cgi-bin/viewdata.cgi" >

< P >< INPUT type= "SUBMIT" value= "View" >

</form >


Here, some basic principles of writing CGI programs in C will be over. Of course, it's hard to write a good CGI program, which requires further learning of the CGI specification and some of the other CGI programming techniques.


The purpose of this article is to understand the concept of CGI programming. In fact, some of the mainstream server-side scripting languages, such as asp,php,jsp, basically have most of the functionality of CGI programming, but they are actually much easier to use than CGI programming in whatever language they're using. So when it comes to server-side programming, it's common to consider using these scripting languages first. CGI is only used when they are not able to solve it, for example, to do some of the more low-level programming.

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.