cgi-lib.pl does include some routines for
generating the HTTP header necessary and for generating the HEAD and
BODY tags, but there is no support for generating HTML (you have to
use print statements).
eiw-cgi.plMETHOD=GET.   The query is in the environment variable
named "QUERY_STRING".
METHOD=POST.   The query is coming from STDIN and the length of the query string is contained in the environment variable "CONTENT-LENGTH".
%ENV. The index for each member of the associative
array is the environment variable name, so to get the value of
the environment variable "REQUEST_METHOD" you can simply do this:
$method = $ENV{"REQUEST_METHOD"};
|
If the request method is "POST" we need to read the request from
standard input, and we need to know how much to read. We can't read
until EOF since we won't ever see EOF ! (stdin is actually connected
back to the web server and the server won't ever tell us we've reached the end
of the data). Perl has a read command that we can tell
how many bytes (characters) we want to read:
|
name=value and the sequence of these strings are
glued together with the &character.
We must split the query string up in to name/value pairs before attempting
to do any URL-decoding, otherwise we might end up introducing a new & or = that would confuse the splitting process later. To split
the query string on the & character we can just do this:
|
We can now work on each of the name/value pairs in @pairs by splitting on the "=" character:
|
We obviously need to do a little more to save each name/value pair some place
we can get at them later. Before we worry about this we need to do URL-decoding
on each $name and on each $value.
"+".
+, & and =) with the hex
equivalent. The hex equivalent is a 3 character sequence that starts
with the "%" character followed by 2 hexadecimal digits that
represent the ASCII value of the character. For example:
| Character | Hex equiv. |
|---|---|
= | %3D |
+ | %2B |
& | %26 |
% | %25 |
$string that we want to
URL-decode, we can take care of the first part easily:
|
Taking care of all the hex equivalent codes is a little harder. We could
build a set of substitute commands that takes care of each possible
hex-encoded character, but we can also use the perl pack command to build a single character from 2 hex digits. For more information on the
pack command you need to refer to a Perl reference. Here is
a single substitute command that will take care of all hex-encoded
characters in a string (will replace each with the character encoded):
|
The e modifier at the end of the substitute command means
"executable", so perl treats the replacement text as a perl expression that
should be evaluated to determine the replacement text. This means that
the replacement text for each substitution is the value of the following
expression:
pack("c",hex($1));$1 is set by perl to be whatever part of the string is matched
by the first parenthesized expression in the regular expression (the same
thing as \1).
|
We can now go back and add calls to this subroutine each time we extract a name/value pair:
|
We have now constructed each of the strings we need, however we still
haven't save this information anywhere (as the code stands now - we
would just end up being able to tell what the last $name
and $value were). One way to save the name/value pairs is
in an associative array - this way we can get at each
$value as long as we know what the field
$name was.
|
The above code modifies the associative array %fields so
that we can later get at any value using something like
$val = $fields{"Age"}; (this would get the value of the
field "Age").
GetQuery
that will find out where the query string is, get it, split it into name/value pairs and take care of URL-decoding. The return value of this subroutine is
an associative array that will contain all the name/value pairs found in the
query:
|
GetQuery
Here is a sample perl program that simply prints out the query
as a series of name/value pairs formatted in a table. This CGI program
uses the GetQuery subroutine we just wrote, so we don't show
that code here.
|