Migrating a Web Server to IIS 5.0 |
One of the major differences between CGI applications and ASP applications is the method for handling form input. Accessing form data from an ASP page is much simpler than it is from a CGI application. When migrating to ASP, you can often remove most of the third-party form decoding and processing utilities, as well as any custom generic formprocessing modules.
A CGI application written in Perl receives form input from a POST request on the standard input stream using code such as:
$form_size = $ENV('CONTENT_LENGTH');
read( STDIN, $form_data, $form_size );
This form data is encoded and must be translated. Using Perl, the translation code might look like:
$value =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
Once the form data is decoded, the developer can search for a form variable. Here is a typical Perl routine to parse the form data and place it in a list:
sub get_form
{
local (*FORM) = @_;
local ( $env_string, @collection, $key_value,
$key, $value );
read (STDIN, $env_string, $ENV{'CONTENT_LENGTH'});
@collection = split( /&/, $env_string );
foreach $key_value (@collection)
{
($key, $value) = split( /=/, $key_value);
$value =~ tr/+/ /;
$value =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
if (defined($FORM{$key}))
{
$FORM{$key} = join("\0",, $FORM{$key}, $value);
} else
{
$FORM{$key} = $value;
}
}
}
Once the routine is in place, it can be called to parse the form. The following code calls the get_form
routine and references a variable called home_address
:
&get_form(*my_form);
print $my_form('home_address');
Fortunately, the ASP environment takes care of these form processing and decoding chores for you. In ASP, the content of an HTML form is made available as a collection, which is a named list of key/value pairs. For developers familiar with languages like Perl or Awk, collections are analogous to hashes or associative arrays.
Form variables are included in the Form collection of the ASP Request object.
Instead of referring to a form variable with a Perl construction like this:
print $my_form('home_address');
you can refer to a form variable as shown in the following VBScript example:
MyAddress = Request.Form("home_address")
The following is the same instruction written in PerlScript:
$MyAddress = $Request->Form('home_address')
There is no need to parse the input, as was required in the CGI example, because the ASP environment takes care of parsing.
Although ASP applications do an excellent job of eliminating form-processing drudgery, developers sometimes need to access the unprocessed input stream. The following VBScript fragment writes the unprocessed input stream back to the output stream:
Response.Write Request.Form
In addition to the Form collection, the Request object includes a collection called QueryString. This collection contains form elements sent in response to the GET method of the HTML <FORM> tag. The elements of the QueryString collection are accessed in the same way elements of the Form collection are accessed.
Often, quite a bit of CGI code is devoted to determining whether a given form variable exists within the Query_String environment variable or in the standard input stream. However, within the ASP environment, you can avoid testing many of these conditions simply by referring to a variable by name:
MyVariable = Request("home_address")
In this case the Web server will search the Query_String first, then search the form, in order to find the correct variable. This feature can eliminate the need to rewrite code that is used for searching both the query string and the form.
To encode URLs or HTML, you can use the built-in HTMLEncode and URLEncode methods of the Server object.