Perhaps readers feel that CGI programs have a sense of mystery. In this section, we will describe some basic programming processes of CGI programs. After you read it, there will be no mystery anymore.
3. Other issues with CGI specifications:
Perhaps readers feel that CGI programs have a sense of mystery. In this section, we will describe some basic programming processes of CGI programs. After you read it, there will be no mystery anymore.
First of all, when doing any programming work, divide a large task into many small tasks, and each small task completes a relatively independent function. Many people are reluctant to write CGI programs themselves because they think CGI programming is difficult. In fact, if you divide a typical CGI program into the following parts and solve them one by one, you will not have difficulty with the CGI program:
Generally speaking, when calling a CGI program in the browser, the server must first find the CGI program. Generally, the CGI program is placed in the directory specified by the server. In our OmniHTTPD, in the cgi-bin and cgi-win virtual The actual directories specified by the directory are the c:httpdcgi-bin and c:httpdcgi-win directories respectively. You can modify the actual directory in OmniHTTPD, or add a virtual directory, such as: specify the virtual directory cgi-test as c:my-cgi; if you have a CGI named test1.cgi in the c:my-cgi directory program, you can call it in your browser at the address http://localhost/cgi-test/test1.cgi.
Under normal circumstances, the server finds the CGI program requested by the browser and generates a process of this CGI program. In this way, the browser and server have established a connection. Once the CGI program has finished executing, the process disappears and the connection is lost. All operations in the above figure are completed within the time when the connection is established. Therefore, the tasks completed by each CGI program must be as small as possible. Otherwise, it will take up a lot of time and resources on the server and the waiting time of the client browser. It will also be very long.
As you can see in the above figure, CGI programs are generally divided into three parts: data input, data processing and data output; sometimes, it is necessary to interact with the database in the data processing part (this is also the specialty of CGI programs). Among these three parts, data input and data output have strict specifications, and data processing is where you can use your imagination. Next, I will focus on the specifications and precautions for data input and data output:
Previously, we have listed almost all the environment variables used by CGI programs. Among them, a considerable part is closely related to the input and output of data. Below, we list the most commonly used environment variables by category:
Server-related environment variables:
GATEWAY_INTERFACE
SERVER_NAME
SERVER_PORT
SERVER_PROTOCOL
SERVER_SOFTWARE
Client-related environment variables:
HTTP_ACCEPT
HTTP_ACCEPT_ENCODING
HTTP_ACCEPT_LANGUAGE
HTTP_AUTHORIZATION
HTTP_CHARGE_TO
HTTP_FROM
HTTP_IF_MODIFIED_SINCE
HTTP_PRAGMA
HTTP_REFERER
HTTP_USER_AGENT
Request-related environment variables:
AUTH_TYPE
CONTENT_FILE
CONTENT_LENGTH
CONTENT_TYPE
OUTPUT_FILE
PATH_INFO
PATH_TRANSLATED
QUERY_STRING
REMOTE_ADDR
REMOTE_USER
REQUEST_LINE
REQUEST_METHOD
SCRipT_NAME
It is worth mentioning here that CONTENT_FILE and OUTPUT_FILE. On Windows 3.1 and DOS not many languages can read and write via standard input and output (STDIN and STDOUT), so these two variables are used instead. For other environment variables, please refer to the previous detailed list.
As mentioned before, the GET method passes data through URL; the POST method passes data through STDIN. No matter what method is used, the data is encoded during data transmission. However, we don't have to worry about encoding and decoding, because the development language we will use can automatically complete this task: cgi-lib.pl in Perl, TWebModule in Delphi, etc.
Header information is also very important in CGI programming. The header information is a signal from the CGI program before sending information to the client. There are three main types of header information:
Content-type //Transmit HTML to the browser
Location //Pass the new URL to the browser
Status //Usually used to check errors when a CGI program error occurs
Content-type is the type of information processed by the browser, mainly the MIME type. Methods are defined as combinations of types and subtypes. Commonly used MIME types are: Text, Multipart, Message, application, Image, Audio and Video.
For example, before a CGI program sends an HTML document to the browser, it should first send text/html. Written in Perl as follows:
print "Content-type:text/html "
print "< h1> Hi everyone! < /h1> "
Location causes the browser to redirect to the new URL. For example:
print "Location:http://www.chinabyte.com"
This Perl program redirects the browser to ChinaByte's homepage.
Status indicates the status of the CGI program and is usually used to check for errors when the CGI program fails. The following table lists commonly used status codes and their meanings:
Code result description
200 OK request is satisfied normally
202 Accept The request was accepted and is being processed.
301 Moved The document was moved to a new location
302 Found The document is not at the description, but elsewhere on the server.
400 Bad Request The syntax of the HTTP request is incorrect.
401 Unauthorized Document requires access rights
403 Forbindden The server denies access to the document
404 No Found The server cannot find the document.
500 Server Error A serious error occurred in the server
502 Service Overloaded The server is busy and cannot process the request.
This ends here. After you have these basic knowledge of CGI, you can start to develop CGI programs step by step using any language. Let me tell you again, CGI programs are not mysterious, it is just a dialogue to create a server and some kind of task.