Data transfer format When the user fills out the form and presses the Submit button, the Web browser does not send the user's data directly to the Web server, but first passes a certain encoding. Web browsers always encode data according to data such as variable name = variable value, and each pair of data is connected with a & symbol. where "variable name" is the Name property value in the form element, and "variable value" is the data that the user enters in the input box, or the data that the user selects (that is, the value in Radio,checkbox,option). And the Web browser replaces all the spaces in the user's data with the "+" number. Other special characters are represented using the escape character "%" followed by the hexadecimal ASCII code of the special character. Special characters mainly include "=", "+", "&", "%" and multiple lines of text in the carriage return, line breaks, all can not directly display the high level of ASCII.
A simple example of a form for example, in the previous example, the user fills in the following information (where/n represents the carriage return line feed): Name "j&k" Sex "male" flavor "apple" flavor "orange" weight "middle" opinion "I am The It is necessary that/nman eat fruit everyday." The sequence of characters when the Web browser sends data is: name=j%26k&sex=male&flavor=apple &flavor=orange&weight=middle& Opinion=i+think+it+is+necessary+that%0d%0aman+eat+fruit+everyday. The data that is processed by a CGI program is sent to the Web server when the Web browser encodes the form data, and the Web server does not process the data itself, but rather relies on a CGI program to help process the data, and then the Web server then processes the results of the CGI program Return to the Web browser.
CGI (Common Gateway Interface) is an interface standard between information servers (such as a Web server) and external applications. HTML files that are usually taken by a Web server are always edited and fixed, but it is possible to output dynamic information to a Web browser by running a CGI program Web server. The CGI standard is in the simplest terms: the CGI program uses standard input (STDIN) or environment variables to get input from the server and output information to the server through standard output (stdout). When the Web server receives form data from a Web browser, it launches the CGI program specified by the action attribute in the tag. Gets form data from the environment variable Query_string if the method property value is the GET,CGI program, and if the method property value is the POST,CGI program, gets form data from standard input (stdin). After the CGI program gets the form data and processes it, it also returns certain information to the Web server (such as the processing results of the data, and so on). In order for the Web server to correctly understand what information is returned, CGI provides a header message in front of the output body, which consists of several lines of ASCII text and separates the header information from the information body with a blank row.
For example, to return an HTML document, the header information is "content-type:text/html." The following is a basic framework for CGI programs written in C:
#include <stdio.h> #include <stdlib.h> #include <string.h> char inputbuffer[4096]; int Decodeandprocessdata (char *input); /* Specific decoding and processing data, the function code slightly * * int main (int argc, char*argv[]) {int contentlength;
/* Data length */int x;
int i;
Char *p; Char *prequestmethod; /* Method Attribute Value * * SETVBUF (stdin,null,_ionbf,0); /* Close stdin buffer */printf ("content-type:text/html\n\n"); /* Output from stdout, tell the Web server the type of information returned/printf ("\ n");
/* Insert a blank line, end header information/printf ("<p>hello test</p>");
* * from the environment variable Request_method to get the method attribute value * * Prequestmethod = getenv ("Request_method");
if (prequestmethod==null) {printf ("<p>request = null</p>");
return 0; } if (strcasecmp (Prequestmethod, "POST") ==0) {printf ("<p>ok" is post!\n<
;/p> "); p = getenv ("Content_length"); The data length if (p!=null) {contentlength = at Content_length is obtained from the environment variable
Oi (p);
else {contentlength = 0; } if (ContentLength > sizeof (inputbuffer)-1) {contentlength = sizeof (InputB
Uffer)-1;
} i = 0; while (I < contentlength) {//get form data x = from stdin
Fgetc (stdin);
if (x==eof) break;
inputbuffer[i++] = x;
} Inputbuffer[i] = ';
ContentLength = i; Decodeandprocessdata (InputBuffer);
Specific decoding and processing of data, the function code slightly} else if (strcasecmp (Prequestmethod, "get") ==0) {printf ("<p>ok is get!\n</p>"); p = getenv ("query_string"); The form data if (p!=null) {strncpy is obtained from the environment variable query_string (inputbuf
Fer,p,sizeof (InputBuffer)); Decodeandprocessdata (InputBuffer); Specifically decoding and processing data, the function code is slightly} printf ("