Original address: http://3633188.blog.51cto.com/3623188/828095
1.POST and getA CGI program consists of two methods of information transmission and data transmission between servers, namely post and get. What is the specific method? This needs to be judged by an environment variable request_method of CGI (specifically, I'll explain it in detail below), and before that, let's talk about URL coding. 1.1 URL Encoding Although there are post and get two methods when setting form information to be transmitted, the browser adopts the same encoding method, regardless of the approach taken. The encoding rules are as follows:-use "&" to separate the variables-use the "=" link between the variables and their corresponding values
-Spaces use "+" instead
-reserved control characters are replaced with the corresponding hexadecimal ASCII with a percent sign
-Whitespace is an illegal character
-Any non-printable ASCII control characters are illegal characters
-some characters with special meaning are replaced with the corresponding hexadecimal ASCII with a percent sign
<body>
<form name= "Form1" action= "/cgi-bin/pass.cgi" method= "get" >
<table align= "center" >
<tr><td align= "center" colspan= "2" ></td></tr>
<tr>
<td align= " Right "> Username </td>
<td><input type=" text "name=" Username "></td>
</tr>
<tr>
<td align= "right" > Secret code </td>
<td><input type= "password" name= "password" "></td>
</tr>
<tr>
<td><input type=" Submit "value=" Login " > </td>
<td><input type= "reset" value= "removing " ></td>
</tr>
</ table>
</form>
</body>
If we fill in the username after Tom, fill in 1234 after the password, the click submitted to the server after the following variable format: username=tom&password=1234 below explain how post and get specific ways to work
2.POST and get working mode2.1 Post If you use the Post method in the form form, the server will receive the data from the form and pass it to the corresponding CGI program (the CGI program specified in the action) while the Request_ The method environment variable is set to post, and the corresponding CGI program examines the environment variable to determine how it works to receive data at post and then read the data. Note the use of post this method of transmission of data, HTTP after the data is sent, and will not send the appropriate data transmission information, so the HTTP server provides another environment variable contenet_length, The environment variable records the number of bytes of data transmitted over a byte, so when you write a CGI program, if method is post, you need to pass the variable to qualify the length of the read data (how to implement it, explained below). There is also an environment variable Contenet_type, recorded from the browser to send the data type, now generally sent MIME type is content-type:text/html\n\n, specifically how to use in the CGI described below. When the contents of two environment variables are confirmed, the data transmitted by the form is resolved according to the following rules, which is the reverse process of URL encoding (no longer repeating). The 2.2 get is basically the same as the Post method, but when you use the Get method, the data is stored in an environment variable called query_string, and the details of how to obtain the contents of the variable are described in detail in the following example. Said so much, through an example to see how to write a CGI program in concrete implementation. The form is still the same as the HTML code above. The following is explained by a CGI program that returns the content. The code is as follows:
#include <stdio.h> #include <stdlib.h> #include <string.h> char* getcgidata (file* FP, char* requ
Estmethod);
int main () {char *input;
Char *req_method;
Char name[64];
Char pass[64];
int i = 0;
int j = 0; printf ("Content-type:text/plain;
Charset=iso-8859-1\n\n ");
printf ("content-type:text/html\n\n");
printf ("The following is query reuslt:<br><br>");
Req_method = getenv ("Request_method");
input = Getcgidata (stdin, Req_method); The input string we get may look like the following form//username= "admin" &password= "AAAAA"//Where "username=" and " ;
Password= "are fixed//and" admin "and" aaaaa "are changes, but also we want to get the first 9 characters is username=
Between "Username=" and "&" is the username we're going to take out. for (i = 9; i < (int) strlen (input); i++) {if (input[i) = ' &
amp; ')
{Name[j] = ' I ';
Break
} name[j++] = Input[i];
}//Front 9 characters + "&password=" 10 characters + username characters//is we don't, so omit, do not copy for (i = + strlen (name), j = 0; i < (int) strlen (input); i++) {pas
S[j++] = Input[i];
} Pass[j] = ';
printf ("Your Username is%s<br>your Password is%s<br> \ n", name, pass);
return 0;
} char* Getcgidata (file* fp, char* requestmethod) {char* input;
int Len; int size = 1024;
int i = 0;
if (!strcmp (Requestmethod, "get")) {input = getenv ("query_string");
return input; else if (!strcmp (Requestmethod, "POST") {len = Atoi (ge
Tenv ("Content_length"));
input = (char*) malloc (sizeof (char) * (size + 1));
if (len = = 0) {input[0] = ';
return input;
while (1) {
Input[i] = (char) fgetc (FP);
if (i = = size) {Input[i+1] = ' I ';
return input;
}--len; if (feof (fp) | | (!
(len)))
{i++;
Input[i] = ' the ';
return input;
} i++; } return NULL;
Let's start with: Note This line of code printf ("content-type:text/html\n\n"); It tells the server what to output is text content or HTML, when writing a CGI program is easy to leave this line, it will prompt the server internal error, unable to complete your request, you need to pay attention to the back of the two "\ n", which is necessary, specifically why, I am not clear, so write is correct. In this place, some netizens do when the Chinese character output is garbled, so that, can be in "\ n", before the output encoding information, under the window generally for gb2312. Go down, that's the line: Req_method = getenv ("Request_method"); This is the value of the environment variable by the getenv () function, the method that is used in the call function, and then the appropriate action is taken. if (!strcmp (Requestmethod, "get")
{
input = getenv ("query_string");
return input;
}
else if (!strcmp (Requestmethod, "POST"))
{//if (getenv (″content-length″))
Len = atoi (getenv ("content_length"));
input = (char*) malloc (sizeof (char) * (size + 1)); Here, the strcmp () function is used to determine the specific method, and if the Get method is used, the getenv () function is used to obtain the contents of the query_string directly and return it to the main function. Keep going down, that is, when method is post, how to limit the number of received data through the environment variable contenet_length, this sentence if (getenv (″content-length″)) to determine whether Contenet_length exists, But you can use the Atoi () function directly in programming, so I'm commenting out this line in the code (when you're programming with a different note)
Len=atoi (getenv (″content-length″));
This row first checks whether the environment variable content-length exists, converts the value of this environment variable to an integer, and assigns it to the variable len. Note that the Web server does not terminate its output as a file terminator, so if you do not check the environment variable content-length,cgi The program will not know when the input is over. The following sentence is input = (char*) malloc (sizeof (char) * (size + 1); A memory space is applied for data storage. Further down, is the basis of C language, here no longer repeat. The general understanding of this example will be able to grasp the way the post and get method data access.