CGI Forms   «Prev  Next»

Lesson 4From the server to the program
Objective Explore how data is passed from the server to the CGI program.

From the Server to the Program using CGI

Once the server has received the request, it sets up the environment and starts up the CGI program. The environment will contain the various environment variables that we learned about in Module 1.

Standard CGI variables

The standard CGI variables are listed here:
GATEWAY_INTERFACE The CGI version
REQUEST_METHOD GET or POST
SCRIPT_NAME Relative URL of the running CGI program
QUERY_STRING The GET method query string
SERVER_SOFTWARE Name and version of the server software
SERVER_NAME Host name of the server
SERVER_PROTOCOL Protocol and version running on the server (for example, HTTP/1.0)
SERVER_PORT The port number of the server
HTTP_USER_AGENT Name and version of the client (for example, Mozilla/3.01 for Netscape Navigator 3.01)
HTTP_ACCEPT MIME types accepted by the browser
PATH_INFO "Extra Path" information passed on the URL
PATH_TRANSLATED PATH_INFO made relative to the server's root directory
REMOTE_HOST Machine name of the remote host
REMOTE_ADDR IP address of the remote host
REMOTE_USER If this connection is authenticated, this is the authenticated user name.
REMOTE_IDENT For RFC 931 "identd" connections
AUTH_TYPE The type of authentication in effect (if any)
CONTENT_TYPE MIME-type for POST method query
CONTENT_LENGTH Length of query, POST method

Iterating over Perl hash

I32 hv_iterinit ( HV* hash );
SV* hv_iternextsv ( HV* hash , char** key , I32* keylen );

There are several ways to iterate over a hash's keys and values in the There are several ways to iterate over a hash's keys and values in the Perl API; we will show you the simplest and most immediately useful. After performing an hv_iterinit, each call to hv_iternextsv will return the next value and store the key and its length in the memory pointed to by key and keylen, respectively. Example Listing 3.4 shows a slightly simplified example from mod_perl. When mod_perl starts up, it wants to remove anything that might be dangerous from the environment it has inherited, saving only selected environment entries. It does so by iterating over %ENV.

void perl_clear_env(void)
{
  char *key;
  I32 klen;
  SV *val;
  HV *hv = (HV*)GvHV(envgv);
  (void)hv_iterinit(hv);
   while ((val = hv_iternextsv(hv, (char **) &key, &klen))) {
    if((*key == 'G') && strEQ(key, "GATEWAY_INTERFACE"))
     continue;
    else if((*key == 'M') && strnEQ(key, "MOD_PERL", 8))
     continue;
    else if((*key == 'T') && strnEQ(key, "TZ", 2))
     continue;
    else if((*key == 'P') && strEQ(key, "PATH"))
     continue;
   }// end - while
  delete_env(key, klen);
 }
}

For the GET method, the QUERY_STRING variable will contain the data from the form. For the POST method, the CGI program will get the data in the STDIN stream. Whatever other headers that the client sends to the server are all available to the CGI program as environment variables named HTTP_<header>.
These headers were sent in the request above:

Referer: http://luna/formtest.html
Connection: Keep-Alive
User-Agent: Mozilla/3.01 (Win95; I)
Host: luna
Accept: image/gif, image/x-xbitmap, image/jpeg, 
        image/pjpeg, */*

So these variables are available to the CGI program, with the contents of the respective headers:
HTTP_REFERER => http://luna/formtest.html
HTTP_CONNECTION => Keep-Alive
HTTP_USER_AGENT => Mozilla/3.01 (Win95; I)
HTTP_HOST => luna
HTTP_ACCEPT => image/gif, image/x-xbitmap, 
image/jpeg, image/pjpeg, */*

Keep in mind that not all browsers send the same headers, and even the same browser may send different headers under different circumstances. Also, some headers are not part of the HTTP specification, or may be experimental in nature. So if you use them, be sure to handle the contingency that they may not always be available.

Server To CGI - Quiz

As we mentioned earlier, there are two basic methods for passing the query to the CGI program. The next lesson explains the differences between the GET and POST methods. But, first click the Quiz link below to take a brief multiple-choice quiz in which you will analyze an HTTP request.
Server To CGI - Quiz

Ad Perl Complete Reference