Cover | Table of Contents | Colophon
GET /cgi/welcome.cgi HTTP/1.1 Host: www.mikesmechanics.com
STDIN)
and
environment variables. These
variables contain
information such as the identity of the remote host and user, the
value of form elements submitted (if any), etc. They also store the
server name, the communication protocol, and the name of the software
running the server. We'll look at each one of these in more
detail in Chapter 3.$ cd /usr/local/apache $ ls -F bin/ cgi-bin/ conf/ htdocs/ icons/ include/ libexec/ logs/ man/ proxy/
|
Default Installation Path
|
Alternative Path (RedHat Linux)
|
|---|---|
|
/usr/local/apache/cgi-bin
|
/home/httpd/cgi-bin |
http://www.w3.org/Protocols/. On the other
hand, if you are eager to get started writing CGI scripts, you may be
tempted to skip this chapter. We encourage you not to. Although you
can certainly learn to write CGI scripts without learning HTTP,
without the bigger picture you may end up memorizing what to do
instead of understanding why. This is certainly the most challenging
chapter, however, because we cover a lot of material without many
examples. So if you find it a little dry and want to peek ahead to
the fun stuff, we'll forgive you. Just be sure to return here
later.http or
https.
https
represents a connection to a secure web server. Refer to The Secure Sockets Layer later in this chapter.GET /index.html HTTP/1.1 Host: localhost Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/xbm, */* Accept-Language: en Connection: Keep-Alive User-Agent: Mozilla/4.0 (compatible; MSIE 4.5; Mac_PowerPC) . . .
80 by
default). The scheme (http) tells our web browser
that it is using the HTTP protocol, so once the connection is
established, it sends an HTTP request for the resource. The first
line of an HTTP request is the
request line, which includes a
full virtual path and query string (if present); see Figure 2.5.HTTP/1.0 and HTTP/1.1. Note
that https requests also produce one of these two
HTTP protocol strings.|
Method
|
|---|
HTTP/1.1 200 OK Date: Sat, 18 Mar 2000 20:35:35 GMT Server: Apache/1.3.9 (Unix) Last-Modified: Wed, 20 May 1998 14:59:42 GMT ETag: "74916-656-3562efde" Content-Length: 141 Content-Type: text/html <HTML> <HEAD><TITLE>Sample Document</TITLE></HEAD> <BODY> <H1>Sample Document</H1> <P>This is a sample HTML document!</P> </BODY> </HTML>
#!/usr/bin/perl -wT
print <<END_OF_HTML;
Content-type: text/html
<HTML>
<HEAD>
<TITLE>About this Server</TITLE>
</HEAD>
<BODY>
<H1>About this Server</H1>
<HR>
<PRE>
Server Name: $ENV{SERVER_NAME}
Listening on Port: $ENV{SERVER_PORT}
Server Software: $ENV{SERVER_SOFTWARE}
Server Protocol: $ENV{SERVER_PROTOCOL}
CGI Version: $ENV{GATEWAY_INTERFACE}
</PRE>
<HR>
</BODY>
</HTML>
END_OF_HTML
%ENV hash.%ENV
.%ENV. Subprocesses created by your script will
also inherit these environment variables, along with any changes
you've made to them.%ENV, you will probably not see all the
variables listed here. If you recall, some HTTP request headers are
used only with certain requests. For example, the
Content-length header is sent only with POST
requests. The environment variables that map to these HTTP request
headers will thus be missing when its corresponding header field is
missing. In other words, $ENV{CONTENT_LENGTH} will
only exist for POST requests.|
Environment Variable
|
Description
|
|---|---|
|
AUTH_TYPE
|
The authentication method used to validate a user. This is blank if
the request did not require authentication.
|
|
CONTENT_LENGTH
|
The length of the data (in bytes) passed to the CGI program via
standard input.
|
|
CONTENT_TYPE
|
The media type of the request body, such as
"application/x-www-form-urlencoded
".
|
|
DOCUMENT_ROOT
|
The directory from which static documents are served. |
print "Content-type: text/html\n\n";
#!/usr/bin/perl -wT
use strict;
my $image_type = $ENV{HTTP_ACCEPT} =~ m|image/png| ? "png" : "jpeg";
my( $basename ) = $ENV{PATH_INFO} =~ /^(\w+)/;
my $image_path = "$ENV{DOCUMENT_ROOT}/images/$basename.$image_type";
unless ( $basename and -B $image_path and open IMAGE, $image_path ) {
print "Location: /errors/not_found.html\n\n";
exit;
}
my $buffer;
print "Content-type: image/$image_type\n\n";
binmode;
while ( read( IMAGE, $buffer, 16_384 ) ) {
print $buffer;
}<INPUT TYPE="checkbox" NAME="send_email" VALUE="yes">
send_email with a value of yes
is sent to the web server. Other form elements, which we will look at
in a moment, act similarly. Before the browser can send form option
data to the server, the browser must encode it. There are currently
two different forms of encoding form data. The default encoding,
which has the
media
type of <INPUT TYPE="checkbox" NAME="send_email" VALUE="yes">
send_email with a value of yes
is sent to the web server. Other form elements, which we will look at
in a moment, act similarly. Before the browser can send form option
data to the server, the browser must encode it. There are currently
two different forms of encoding form data. The default encoding,
which has the
media
type of application/x-www-form-urlencoded, is used
almost exclusively. The other form of encoding,
multipart/form-data,
is
primarily used with forms which allow the user to upload files to the
web server. We will look at this in Section 5.2.4.+. For example, the string "Thanks for the
help!" would be converted to
"Thanks+for+the+help%21".|
Form Tag
|
Description
|
|---|---|
|
<FORM ACTION="/cgi/register.cgi" METHOD="POST">
|
Start the form
|
|
<INPUT TYPE="text" NAME="name"
VALUE="value" SIZE="size">
|
Text field
|
|
<INPUT TYPE="password" NAME="name"
VALUE="value" SIZE="size">
|
Password field
|
|
<INPUT TYPE="hidden" NAME="name"
VALUE="value" >
|
Hidden field
|
|
<INPUT TYPE="checkbox" NAME="name"
VALUE="value" >
|
Checkbox
|
|
<INPUT TYPE="radio" NAME="name"
VALUE="value" >
|
Radio button
|
|
<SELECT NAME="name" SIZE=1> |
$ENV{QUERY_STRING}.$ENV{REQUEST_METHOD} is POST, determine the
size of the request using $ENV{CONTENT_LENGTH} and
read that amount of data from the standard input. Append this data to
the data read from the query string, if present (this should be
joined with "&").name=value&name=value...).sub parse_form_data {
my %form_data;
my $name_value;
my @name_value_pairs = split /&/, $ENV{QUERY_STRING};
if ( $ENV{REQUEST_METHOD} eq 'POST' ) {
my $query = "";
read( STDIN, $query, $ENV{CONTENT_LENGTH} ) == $ENV{CONTENT_LENGTH}
or return undef;
push @name_value_pairs, split /&/, $query;
}
foreach $name_value ( @name_value_pairs ) {
my( $name, $value ) = split /=/, $name_value;
$name =~ tr/+/ /;
$name =~ s/%([\da-f][\da-f])/chr( hex($1) )/egi;
$value = "" unless defined $value;
$value =~ tr/+/ /;
$value =~ s/%([\da-f][\da-f])/chr( hex($1) )/egi;
$form_data{$name} = $value;
}
return %form_data;
}my %query = parse_form_data( ) or error( "Invalid request" );
my $activity = $query{activity};%ENV.$ perl -v This is perl, version 5.005 Copyright 1987-1997, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5.0 source kit.
$ perl -MCGI -e 'print "CGI.pm version $CGI::VERSION\n";' CGI.pm version 2.56
Can't locate CGI.pm in @INC (@INC contains: /usr/lib/perl5/i386-linux/5.005 /usr/ lib/perl5 /usr/lib/perl5/site_perl/i386-linux /usr/lib/perl5/site_perl .). BEGIN failed--compilation aborted.
%ENV hash, but CGI.pm also makes most of these
available via method calls. It also provides some unique methods.
Table 5.1 shows how CGI.pm's functions
correspond to the standard CGI environment variables.|
CGI.pm Method
|
CGI Environment Variable
|
|---|---|
|
auth_type
|
AUTH_TYPE
|
|
Not available
|
CONTENT_LENGTH
|
|
content_type
|
CONTENT_TYPE
|
|
Not available
|
DOCUMENT_ROOT
|
|
Not available
|
GATEWAY_INTERFACE
< |