Debugging an Application

This case study is a bit different from the others, since it doesn't explain a particular application, rather than the development of an application. The application is a rather simple one: tell the user what host the web server believes made the HTTP request and display it using a chtml template. We will use Perl for this example, but many of the same concepts apply in other languages.

You can also view information about common error messages you may see either during script development or even after the script is in production use.

This case study will show several versions of the same script, and enable you to actually try the buggy scripts to see what the output will look like. For each link which executes a script, you will see a small section at the top which explains the output and lets you proceed. If you are not on a browser which supports embedded HTML objects, you will need to use the browser back button after trying the test scripts to find a link to the next page.

Here is the template which we will use:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--chtml include "//webinfo/incs/header.inc"-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Client Hostname</title>
<link rel="stylesheet" href="/webinfo/webinfo.css" type="text/css" />
</head>

<body>
<h1>Client Hostname</h1>
<!--chtml include "//webinfo/incs/navbar.inc"--> 
You are coming from the host REMOTE.
<!--chtml include "//webinfo/incs/footer.inc"-->
</body>
</html>

And this is our first attempt at the script:

#!/usr/local/bin/perl5

use strict;
use CGI;
require '/www/lib/chtml-template.pl';

$cgi = new CGI;

$file = chtml_procfile ('client.tmpl');
if (! open F, '<$file')
    exit 0;

while (<F>) {
    s/HOST/$ENV{'REMOTE_HOST'}/g;
    print;
}
close F;

Before starting, be warned that throughout this documentation we will call the name of the script "client.cgi". However, in order to be able to try different versions, the actual scripts are "client1.cgi", "client2.cgi", etc. This subtlety is important when you view the error messages.

Let's see what happens when we try to access the script.