CGI C language articles

Why Do CGI programming?


   In HTML, when the customer fills out the form and presses submit After the button, the content of the form is sent to the server. Generally, a server-side script is needed to process the content of the form, or save them, or perform some operations according to the content. Query, or something else. Without CGI, the WEB world completely loses its interactivity, all information becomes one-way, and There can be no feedback.


   Some people think that JavaScript can be used instead of CGI programs. This is actually a Conceptual error. JavaScript can only run in the client’s browser, while CGI works on the server. There are some overlaps in their work, such as form data validation, but JavaScript can never replace CGI. But it can be said that If a job can be done with both JavaScript and CGI, then JavaScript must be used. In terms of execution speed, JavaScript It has inherent advantages over CGI. Only those problems that cannot be solved on the client side, such as interacting with a remote database, should use CGI at this time.


   Simply put, CGI is used to communicate HTML forms and server-side programs The interface (interface). To say that it is an interface means that CGI is not a language, but a set of specifications that can be applied by other languages. In theory, you can use any programming language to write CGI programs, as long as you conform to some things defined by the CGI specification when programming. Because C language performs well in terms of platform independence (almost any system platform has its corresponding compiler), and Most programmers are considered familiar (unlike Perl), So, C is one of the preferred languages ​​for CGI programming . Here we introduce how to use C to write CGI programs.


   The simplest example of CGI programming is to process forms. Therefore, in this article, we mainly introduce how to use C to write CGI programs for form processing.

Handle the GET form h3>
   For those forms that use the attribute “METHOD=GET” (or without the METHOD attribute, then GET is the default value), CGI is defined as: When the form is sent to the server, the data in the form is saved in an environment variable called QUERY_STRING on the server . The processing of this form is relatively simple, as long as the environment variables are read. This has different practices for different languages. In C language, 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. After obtaining the data in the string, you can use some tricks to perform type conversion, which is relatively simple. The standard output in the CGI program (output) (such as the stdout file stream in C) is also is redefined. It does not generate any output on the server, but is redirected to the client browser. In this way, if an HTML document is output to its stdout when writing a C CGI program, the HTML document will be displayed in the client’s browser. This is also a basic principle of CGI programs.

   Let’s take a look at the specific program implementation. Here is an HTML form:

 

Please fill in the multiplier and multiplicand below, and you can see the result after pressing OK.



   The function we want to achieve is very simple, that is, multiply the value entered in the form, and then output the result. In fact, this function can be implemented with JavaScript, but in order to make the program as simple and easy to understand as possible, I chose this small multiplication as an example.


  The CGI program that processes this form corresponds to the value of the ACTION attribute in the FORM tag.

#include  #include  int main(void) {char *data; long m; long n; long n; printf("%s%c%c
", "Content-Type:text/html;charset=gb2312", 13, 10); printf( "Result of multiplication
"); printf("

Result of multiplication

"); data = getenv("QUERY_STRING"); if (data == NULL) {Printf("

ERROR!Data is not input or data transfer has problem

");}} Else if(sscanf(data,"m=%ld&n=%ld", &m, &n)! = 2) {printf("

ERROR!The input data is invalid, you must input digit number

");}} else {}}}} Printf("

%ld and %ld equals: %ld . ", m, n, m*n); return 0;}

   I won’t talk about the specific C grammar, let’s take a look at its special place as a CGI program.

   mentioned earlier The content to the standard output is the content to be displayed in the browser. The output content of the first line is necessary and unique to a CGI program: printf(“%s%c%c “,”Content-Type:text/ html”,13,10), this output is used as the HTML file header. Because CGI can not only output HTML text like a browser, but also output images, sounds and the like. This line tells the browser what to do with the received content. There are two blank lines after the definition of Content-Type, which is is also indispensable. Because the head output of all CGI programs are similar, you can define a function for it to save programming time. This is a commonly used technique in CGI programming.

   The program later calls the library function getevn to get the content of QUERY_STRING, and then uses Use the sscanf function to take out each parameter value, the usage of the sscanf function should be noted. There is nothing else, and it is no different from a normal C program.

   After compiling the program, rename it to mult.cgi and place it under the /cgi-bin/ directory, and then it can be called by the form. In this way, a CGI program that processes GET forms is complete.

POST form processing

   Let’s consider another form transmission method: POST. Suppose the task we want to achieve is like this: add a piece of text entered by the customer in the form to the back of a text file on the server. This can be seen as the prototype of a message board program. Obviously, this job is cannot be implemented with client-side scripts such as JavaScript, and it can be regarded as a true CGI program.

   It seems that this problem is very similar to the content mentioned above, just using different forms and different scripts (programs). But in fact, there are some differences. In the above example, the GET processing method can be regarded as a “pure query” type, that is, it has nothing to do with the state. The same data can be submitted any number of times without causing any problems (except for some small server overhead). But now the task is different, at least it has to change the content of a file. Therefore, it can be said that the processing method of POST is state-related. This can be regarded as one of the differences between POST and GET. Moreover, GET has a limit on the length of the form, while POST Otherwise, this is the main reason for choosing the POST method in this task. But relatively, processes GET faster than POST.

   In the definition of CGI, for the POST type form, its content is sent to the standard input of the CGI program (stdin in C language), and < span style="color:#3333ff">The transmitted length is placed in the environment variable CONTENT_LENGTH. So what we have to do is to read a string of CONTENT_LENGTH length in the standard input. Reading data from standard output sounds easier than reading data from environment variables, but it is not. There are some details to pay attention to, which can be seen in the following program. One thing to pay special attention to is: CGI programs are different from general programs, After reading the content of a file stream, a general program will get an EOF sign. But In the form processing process of CGI program, EOF will never appear, so Don’t read characters longer than CONTENT_LENGTH, otherwise no one knows what the consequences will be (there is no definition in the CGI specification, generally it depends on the server to deal with it differently) method).


   Let’s take a look at how to collect data from the POST form to the CGI program. Here is a relatively simple C source code:

#include # include #define MAXLEN 80/* 4 for field name "data", 1 for "=" */#define EXTRA 5/* 1 for added line break, 1 for trailing NUL */#define MAXINPUT MAXLEN+ EXTRA+2#define DATAFILE "./data.txt" void unencode(char *src, char *last, char *dest) {for(; src != last; src++, dest++) if (*src =='+' {) {} *Dest = '';}}}}}}}}}}}}}} Else if(*src =='%') {int code; s Scanf(src+1), "%2x", &code)! }} *Dest = code; src +=2;}} Else {} *dest = *src;}}} *dest ='
'; *++dest ='';} int main(void) {char *lenstr; char input[MAXINPUT]; char data[MAXINPUT]; long len; printf("%s%c% c
", "Content-Type:text/html;charset=iso-8859-1",13,10); printf("Response
"); lenstr = getenv("CONTENT_LENGTH "); if (lenstr == NULL || sscanf(lenstr,"%ld",&len) != 1 || len> MAXLEN) {printf("

Error in invocation-wrong FORM probably. ") ; Else {printf("

CONTENT_LENGTH is %ld %d.

", len, atoi(lenstr)); // fgets(input, len+1, stdin); fgets(input, len+1, stdin); unencode(input+EXTRA, input+len, data); FILE *fp; fp = fopen(DATAFILE, "a+"); if(fp == NULL) {p> printf(" , cannot store your data.

");}}}}}}}

else {

Printf("

Your data is: %s.

",

");

fp); printf("

Thank you! The following contribution of yours has been stored:

%s ",data); } Return 0;}


   from Essentially, the program first obtains the word length of the data from the CONTENT_LENGTH environment variable, and then reads a string of the corresponding length. Because the data content is encoded during transmission, it must be decoded accordingly. The coding rules are very simple, the main ones are:


   1. Form Each field in the field is represented by the field name followed by an equal sign, followed by the value of this field, and the content between each field is represented by & link;

   2. All The space symbol is replaced by a plus sign, so it is illegal to have a space in the code segment;

   3. Special characters such as punctuation marks, and characters with specific meanings such as “+”, expressed with a percent sign followed by its corresponding ACSII code value.

   For example: If the user enters:

   Hello there!

   Then the data is encoded when it is transmitted to the server, and it becomes data=Hello+there%21. The unencode() function above is used to decode the encoded data. After the decoding is complete, the data is added to the end of the data.txt file and echoed in the browser.

   After the file is compiled, rename it to collect.cgi and place it in the CGI directory to be called by the form. The corresponding form is given below:

Please Enter your message (up to 80 characters):

   In fact, this program can only be used as an example, not formal in use. It misses a very critical problem: when multiple users write data to a file at the same time, errors will definitely occur. For such a program, the probability of files being written simultaneously is very high. Therefore, in a more formal message board program, some more considerations need to be made, such as adding a semaphore, or relying on a key file. Because that’s just a matter of programming skills, I won’t talk about it here.


   Finally, let’s write a CGI program for browsing the data.txt file, This only needs to output the content to stdout:

#include #include #define DATAFILE "data.txt "int main(void) {FILE *f = fopen(DATAFILE,"r"); if(f == NULL) {printf("%s%c%c
", "Content-Type:text/html; charset=iso-8859-1", 13, 10); printf("Failure
"); printf("

Unable to open data file, sorry!

");}}} Else {printf("%s%c%c ", "Content-Type:text/plain;charset=iso-8859-1", 13, 10); int ch; while ((ch=getc(f)) != EOF) {putchar(ch); fclos e(f);}} return 0;}

   The only thing to note in this program is that it does not include data .txt is packaged into HTML format and then output, but directly output as plain text. This only needs to replace text/html with text/plain in the output header, and the browser will follow the Content-Type. The type automatically selects the corresponding processing method.


   It’s also very simple to trigger this program, because there is no data to enter, so only Just a button to do it:

Click the button to view the text content in data.txt

< input type="submit" value="View">


   So far, some basic principles of writing CGI programs in C are finished. Of course, it is difficult to write a good CGI program based on these contents. This requires further study of the CGI specification and some other unique skills of CGI programming.

   The purpose of this article is to understand the concept of CGI programming. In fact, some mainstream server-side scripting programming languages ​​such as ASP, PHP, JSP, etc., basically have most of the functions of CGI programming, but they are indeed better than CGI no matter what language is used. Programming is much easier. Therefore, when doing server-side programming, these scripting programming languages ​​are generally considered first. Only when they can’t solve it, such as when some more low-level programming is required, will CGI be used.

Original address: http://fanqiang.chinaunix.net/a4/b2/20010707/080500827.html< /p>

Leave a Comment

Your email address will not be published.