Computer Sciences Department logo

CS 368-1 (2010 Summer) — Day 6 Homework

Due Thursday, July 22th, at the start of class.


Write a Perl script to analyze a web server log file and report “referrers” for web pages.

Webmasters frequently want to know what pages users are looking at, and where they come from. This information is present in their logs, but not in an easy to read form.

Background Information

To load a web page, the web browser contacts the server using the HTTP protocol. It sends the URL desired, along with other information. This information includes the page the user came from, the so called “Referer” or referring URL. (Yes, “Referer” is misspelled. Trivia for the day: It is misspelled in the HTTP protocol specification.) Many web servers log this information.

Here are a few lines from a real access_log, modifed slightly to anonymize the users: - - [13/Apr/2009:03:11:23 -0700] "GET /gencon-indy-2009.cgi/group/ HTTP/1.1" 200 26852 "" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv: Gecko/2009032608 Firefox/3.0.8" - - [13/Apr/2009:04:57:26 -0700] "GET /gencon-indy-2009.cgi/group/ HTTP/1.1" 200 26852 "" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/2009032609 Firefox/3.0.8 (.NET CLR 3.5.30729)" - - [13/Apr/2009:05:37:34 -0700] "GET /gencon-indy-2009.cgi/type/SPA/All_events HTTP/1.1" 304 22 "-" "Mozilla/4.0 (compatible;)"

Assume that fields are seperated with spaces. (The reality is obviously a bit more complex, but this is good enough for purposes of this assignment.) The fields are:

  1. - IP address of visitor
  2. - - Unused
  3. - - Unused
  4. [13/Apr/2009:03:11:23 - Date and time of visit
  5. -0700] - Time zone for the date
  6. "GET - Command (usually GET or POST)
  7. /gencon-indy-2009.cgi/group/ - Requested URL. The "" is not listed. It can be implied, since an access_log usually corresponds to a single web site.
  8. HTTP/1.1" - HTTP protocol version
  9. 200 - Return code. 200 is success.
  10. 26852 - Bytes returned
  11. "" - Referring URL. It will be "-" if the web browser didn't indicate a referring URL.
  12. "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv: Gecko/2009032608 Firefox/3.0.8" - Web browser used. (This field contains spaces. We are ignoring this field for this exercise, so we can ignore this detail.)

What to Do

Your script will read the access_log file, analyze the log lines, and generate a report. The report will list all of the requested URLs. Under each requested URL will be a list of referring URLs and a count for how many times that each referring URL was seen for the associated URL requested. The results can be ordered in any way, so long as the referring URl are indicated under the matching requested URL. For example, given the above three log lines, appropriate output might look like:

    2 ""

    1 "-"

We can see that /gencon-indy-2009.cgi/group/ was requested twice, and both times the visitor came from "". /gencon-indy-2009.cgi/type/SPA/All_events was requested once, but the "-" indicates that we don't know where the visitor came from.

Use Perl’s built-in split() function to extract the relevant fields from the access_file. This function takes a string, splits it into elements using another string, and returns the elements as a list. So:

my $log_line = <WEB_LOG_FILE>;
my @log_line_elements = split(" ", $log_line);
my $ip_address = $log_line_elements[0];
# etc.

Note that the most straighforward usage of split in this case will leave the referring URL in quotes. This is fine — there is no need to remove the quotes from around the referring URL.

Here is an example access_log for testing. This is real data, trimmed to keep the output relatively simple, and the IP addresses have been modified to protect the privacy of visitors to the web site. Using this input file, output might look like this:

% ./homework-06-script

    2 ";boardseen"
    16 ""
    2 ""
    7 ""
    2 ";boardseen"
    13 "-"
    3 ";topicseen"

    2 ""
    2 ""
    42 "-"

    4 ""
    22 ""
    387 ""
    26 "-"
    2 ""

    7 "-"
    2 ""
    15 ""

    3 ""
    15 ""
    58 "-"
    151 ""

    2 ""
    15 "-"

    5 ""
    2 ""
    11 ""
    3 ""
    88 "-"
    28 ""
    21 ""

Given this report, we can see that /gencon-indy-2009.cgi/event/SEM0902117 was requested 46 times. 2 of those times the visitor came from rpgblog2, twice the visitor came from, and 42 times the referrer information was not available.


Do the work yourself, consulting reasonable reference materials as needed; any reference material that gives you a complete or nearly complete solution to this problem or a similar one is not OK to use. Asking the instructors for help is OK, asking other students for help is not.

Hand In

A printout of your output on a single sheet of paper. Be sure to put your own name in the initial comment block of the code. Identifying your work is important, or you may not receive appropriate credit.