Be CGI programming in C language

 A, CGI Overview
  CGI (Common Gateway Interface) provides a Web server call other executable program (CGI program) protocol standard interface. Web servers and Web browsers interact by calling the CGI program, which is the CGI program accepts Web browser sends information to the Web server, for processing, will respond to the results to come back to the Web server and Web browser. CGI program is generally complete in We b form page (Form) data processing, database queries and to achieve integration work and traditional applications. CGI programs can use any programming language, such as Shell scripting languages, Perl, Fortran, Pascal, C language. However, written in C language CGI program execution speed with fast, safe (because the C language program is compiled and execution can not be modified) and so on.

  CGI interface standards including standard input, environment variables, the standard output of three parts.

  1. The standard input

  CGI program like other executable program, can be obtained from the input information input by a standard Web server (stdin), as data in the Form, which is a so-called POST method data is transmitted to the CGI program. This means that an executable CGI programs in the operating system command line, for CG I program debugging. POST method is commonly used method, this paper will use this method as an example, analysis of C GI programming, processes and skills.

  2. Environment variables

  Operating system provides a number of environmental variables that define the execution environment, the application program can access them. W eb server and CGI interface and another set some of their own environment variables to pass some important parameters to the CGI program. The CGI GET method further transfer data to the Form CGI programs through the environment variable QUERY-STRING.

  3. Standard Output

  CGI program via standard output (stdout) transmits output information to the Web server. Information transmitted to the Web server can use a variety of formats, usually in plain text or HTML text form, so that we can debug C GI program at the command line, and get their output.

  The following is a simple CGI program, it outputs information in HTML Form We b directly to the browser.
  #include <stdio.h>
  #include <stdib.h>
  main ()
  {
   int, I, n-;
  the printf ( "contentType: text / Plain \ n-\ n-");
  n-= 0;
  IF (getenv ( "Content- the LENGTH "))
  n-= atoi (getenv (the CONTENT-the LENGTH"));
  for (I = 0; I <n-; I ++)
  the putchar (getchar ());
  the putchar ( '\ n-');
  fflush (stdout);
  }


  Here's how this program to make a few brief analysis.
  prinft ( "contentType: text / Plain \ the n-\ the n-");
  This line of the output string by standard "Contenttype: text / plain \ n \ n" transmitted to the Web server. It is a MIME header, which tells the Web server then the output is plain ASCII text. Note that there are two new line in the header information because the Web server requires a text message before the actual start to see a blank line.
  IF (getenv ( "CONTENT-LENGTH"))
  n-= atoi (getenv ( "CONTENT-LENGTH"));
  This line first checks whether the environment variable CONTENT-LENGTH exists. Web server settings when calling CGI programs using the POST method of this environment variable, its value represents the Web server transmits the text to the number of characters input CGI program, so we use the function atoi () converts the value of this environment variable into an integer and assigned to the variable n. Please note that the Web server is not to end of file to terminate its output, so if you do not check the environment variable CONTENT-LEN GTH, CGI programs can not know when the input end.


  for (I = 0; I <n-; I ++)
  the putchar (getchar ());
  This line loops from 0 to (CONTENT-LENGTH-1) times the standard input read each character directly copied to the standard output, It is all the input to ASCII form back to the Web server.
  By this example, we can usually work process CGI program is summarized in the following points.
  1. By examining the environment variable CONTENT-LENGTH, determine how much input;
  2. recycling getchar () function or other documents read by all of the inputs;
  3. In a corresponding method of processing an input;
  4. By "Contenttype:" tell the Web server header information, the information output format;
  5. By using printf () or the putchar () function or other file write, is transmitted to the output Web server.
  In short, the main task is to get the CGI program from a Web server information input, processing, and then outputs the result again back to the Web server.

  Second, the environment variable

  environment variables is a text string (name / value pairs), may be provided by another program or OS Shell, it may also be accessed by other programs. They are the Web server to pass data to the CGI program simply means, is called environment variables because they are global variables, any program can access them.

  The following are CGI programming often use some of the environmental variables.
  HTTP-REFERER: call the CGI program Web page URL's.
  REMOTE-HOST: Web browser to call the CGI program the machine name and domain name.
  REQUEST-METHOD: When the Web server refers to a method of data transfer used by the CGI program, divided into two methods GET and PO ST. GET method is transmitted only by environmental variables (e.g. QUE RY-STRING) data to the CGI program, and the transmitted data input by the POST method and the standard environment variables to CGI programs, so POST method can be more convenient to transfer data to the CGI program.

  SCRIPT-NAME: the name of the CGI program.
  QUERY-STRING: When using the POST method, the data in the last Form QUERY- STRING, the transfer to the CGI program.
  CONTENT-TYPE: MIME type is passed to the CGI program data, generally, it is the data transfer method POST "applica tion / x-www- form-url encodede" HTML Form from the CGI program to the data encoding type, called URL encoding type.
  CONTENT-LENGTH: data passed to the CGI program the number of characters (bytes).
  In the C language program, to visit the environment variable, use getenv () library function. For example:
  IF (getenv ( "the CONTENT-the LENGTH"))
   n-= atoi (getenv ( "the CONTENT-the LENGTH"));
  Please note that the best procedure called twice getenv (): Check whether there is the first environment variable, the second time to use this environment variable. This is because the function getenv () in a given environment variable name does not exist, it returns a NULL (empty) pointer, if you do not first check and direct reference to it, when the environment variable does not exist will cause the collapse of CGI programs.

  Three, From analyzes and decodes the input

  1. Analysis of name / value pairs

  when a user when submitting a HTML Form, Web browser first Form data is encoded and sent to the Web server in the form of name / value pairs, and then by the passed to the Web server CGI program. The format is as follows:
  NAME1 = VALUE1 & NAME2 = value2 & NAME3 = value3 = value4 & ... & NAME4
  Wherein Form name is defined like INPUT, SELECT or TEXTAREA subscript counter (Tag) name, the value is selected by a user input or standard setting value. This format is the URL encoding, the program need to be analyzed and decoded. To analyze this data stream, CGI program must first be broken down into a bundle of data stream name / value pairs. This can be done by looking for the following two characters in the input stream.
  Whenever find characters =, marking the end of a Form variable name; every time to find the character &, marking the end of a Form variable values. Please note that the value of the last variable input data does not end with &.
  Once the name / value pairs decomposition, the input must also convert the special characters into a corresponding ASCII character. These special characters are:
  +: + is converted into the spaces;
  % XX: special characters with hexadecimal representation of the ASCII value. The value of xx converted into the corresponding ASCII characters.
  To Form variable name and variable value should be carried out this conversion. The following is an analysis carried out and the results of the Form data back to the Web server CGI program.


  #include <stdio.h>
  #include <stdlib.h>
  #include <strings.h>
  int htoi (char *);
  main ()
  {
   int I, n-;
  char C;
  the printf ( "contentType: text / Plain \ n- \ n ");
  n=0;
  if (getenv(″CONTENT-LENGTH″))
   n=atoi(getenv(″CONTENT-LENGTH″));
  for (i=0; i<n;i++){
   int is-eq=0;
  c=getchar();
  switch (c){
   case ′&′:
    c=′\n′;
    break;
   case ′+′:
    c=′ ′;
    break;
   case ′%′:{
    char s[3];
    s[0]=getchar();
    s[1]=getchar();
    s[2]=0;
    c=htoi(s);
    i+=2;
   }
   break;
  case ′=′:
   c=′:′;
   is-eq=1;
   break;
  };
  putchar(c);
  if (is-eq) putchar(′ ′);
  }
  putchar (′\n′);
  fflush(stdout);
  }
  /* convert hex string to int */
  int htoi(char *s)
  {
   char *digits=″0123456789ABCDEF″;
  if (islower (s[0])) s[0]=toupper(s[0]);
  if (islower (s[1])) s[1]=toupper(s[1]);
  return 16 * (strchr(digits, s[0]) -strchr (digits,′0′)
)
  +(strchr(digits,s[1])-strchr(digits,′0′));
  }

  The above program first outputs a MIME header to the Web server, checks the number of characters in the input, and a loop checking each character. When they find a character is &, it means a name / value pairs end, the program prints a blank line; when the character is + found, it will be converted into a space; when character is found%, which means a two-character ten start hexadecimal value, call hto i () function to convert the ensuing two characters into the corresponding ASCII characters; when is = mean an end to discover the character, a name / value pairs part of the name, and it converts into characters: Finally, the output character converted to the Web server.
  Fourth, generating HTML output

  output generated CGI program consists of two parts: MIME header information and the actual information. Separated by a blank line between the two parts. We have seen how to use the MIME header "Cont enttype: text / plain \ n \ n" , and printf (), put char () function calls such as plain ASCII text output to the Web server. In fact, we can also use the MIME header "C ontenttype: text / html \ n \ n" to output the HTML source code to the Web server. There must be a blank line Please note that any MIME header information. Once you send this information to the MIME header We b server, Web browser that the subsequent text output to HTML source code, may be used in the HTML source code of any HTML structure, such as hyperlinks, images, Form, and other CGI program It calls. That is, we can dynamically generate HTML CGI program source code output, the following is a simple example.

  #include <stdio.h>
  #include <string.h>
  main ()
  {
   the printf ( "contentType: text / HTML \ n-\ n-");
  the printf ( "<HTML> \ n-");
  the printf ( "<head> <title> An A the CGI the From the HTML Page </ title> </ H EAD> \ n-");
  the printf ( "<body> <br> \ n-");
the printf (" <H2> This IS AN INA with the HTML Page Generated from the CGI Program .. </ H2> \ n-. ");
  the printf (" <HR> <P> \ n-");
  the printf (" href="../output.html#two"> <a <B> Go Back to OUT put.html Page <
  / B> </a> \ n-");
  the printf (" </ body> \ n-") ;
  the printf ( "</ HTML> \ n-");
  fflush (stdout);
  }


  the above CGI program simply by printf () function to generate the HTML source code. Note that the output string of a double quotes, must have a in front of the posterior oblique character \, because the entire HTML code string already in double quotation marks, the HTML code string with a double quote character must after oblique character \ to escape.


  V. Conclusion

  This paper analyzes the methods of CGI programming in C, procedures and techniques. Although C language CGI program execution speed, high reliability, but it is relative to the Perl language, C language string processing the lack of strong, so in practical applications, should be selected according to need and the appropriate CGI hobbies programming language.

  (Author Address: Department of Computer Science, Wuhan Institute of Communication No. 43 Jiefang Park Road, 430010, China
   Email: [email protected]; Received Date: 1997.3)
ON. Posted 2006-03-02 21:45 Aween's Blog Read ( ... ) Comments ( ... ) edit collections

Reproduced in: https: //www.cnblogs.com/aween/archive/2006/03/02/341518.html

Guess you like

Origin blog.csdn.net/weixin_34357887/article/details/94574892