您的位置:寻梦网首页编程乐园CGI编程Zhanshou's CGI Tutorial

CGI Output


Generally, a CGI application sends its output to stdout (standard output). The server intercepts the output and sends it back to the client. Typically, the output may be one of the three things:

  • A valid web document
  • Redirection to another URL
  • A status code

Output A New Web Document

To properly display the documents it receives, the Web browsers need to know the types of documents. If the browser receives a plain text file, the file will be displayed in plain text format. If it receives an HTML file, it needs to interpret and display it in HTML format. So when the server sends a document to the client, it should tell the client what type the document is. The standard way to identify the file type is the MIME format. Here are some of the common MIME type/subtypes:

  • text/plain: Plain text, which is the default type
  • text/html :HTML file (UNIX)
  • text/htm :HTML file (wINDOWS NT)
  • image/gif: GIF image
  • image/jpeg: JPEG image
  • audio/x-wav: Microsoft Windows audio format
  • video/mpeg: MPEG compressed video

Usually when the server sends a document back to the client, it includes the following:

  • A Content-Type: indicates the MIME format.
  • A blank line
  • The document itself.
Let's look at an example in PERL code:
#!/usr/local/bin/perl
#-------------------------------------------
# post.pl by Zhanshou Yu
#-------------------------------------------

# Get the input for POST method
 read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});

#Split the name-value pairs
 ($name,$value)=split(/=/,$buffer);

# Substitute special character to its original character
 $value=~ tr/+/ /;
 $value=~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;

#------print the return HTML------------------

 #print the MIME type
 print"Content-type: text/html\n\n";

 #print the HTML body
 print"<html>\n";
 print "<head><title>Input For POST method </title></head>\n";
 print "<body><center><h1>Input for POST Method </h1>\n";
 print "<h2>Here is the string you just input:    $value</h2>\n";
 print "</body></html>\n";


 exit;
The red color code deals with sending the document back to the client. The statement:

print"Content-type: text/html\n\n";

indicates that the document is HTML format, following by an extra new line character (it can not be omitted). The rest prints out the content of the document that we want to send.

Redirection to another URL

It is not uncommon for a programmer to decide that a request is best handled by redirecting the user to another URL. The other URL can be on the same or another system. To redirect the browser to another URL, output the following header:

  Location :URL
For example , you want the CGI program to return the zhanshou/index.html document in server "www.geocites.com", you can write the following code in PERL:
#!usr/local/bin/perl

print "Location: http://www.geocites.com/zhanshou/index.html\n\n";
Or code in C:
  main()
 {
    printf("Location: http://www.geocites.com/zhanshou/index.html\r\n\r\n");
 }

Status Code

When the server responds to a browser's request, it contains a code that tell the browser the status of the transaction i.e., whether it was successful or not. Some of the status codes are:

  • 200 : Successful
  • 202 : Accepted
  • 204 : No content
  • 301 : Moved Permanently
  • 401 : Unauthorized
  • 403 : Forbidden
  • 404 : Not found
  • 500 : Internal server error

Previous Page Table of Content Next Page