#!/usr/bin/perl
#
# Program: Apache Redirect Viewer <viewredirects.pl>
#
# Author: Matty <matty91 at gmail dot com>

# Current Version: 0.1a
#
# Revision History:
#  Version 0.1a
#
# Last Updated: 11-25-2005
#
# Purpose: Prints the redirect strings sent to clients.
#
# Installation:
#   Copy the shell script to a suitable location
#
# CDDL HEADER START
#  The contents of this file are subject to the terms of the
#  Common Development and Distribution License, Version 1.0 only
#  (the "License").  You may not use this file except in compliance
#  with the License.
#
#  You can obtain a copy of the license at Docs/cddl1.txt
#  or http://www.opensolaris.org/os/licensing.
#  See the License for the specific language governing permissions
#  and limitations under the License.
# CDDL HEADER END
#
# Example:
# $ viewredirects.pl
# Response Code  URI Requested              Redirect Sent To Client                 
# 301            /yikes                     http://192.168.1.3:8080/yikes2          
# 301            /foo                       http://192.168.1.3:8080/bar             
# 301            /bling                     http://192.168.1.3:8080/crunk  

### Standard includes
use Getopt::Std;
use POSIX;

# Process the command line arguments
%options=();
getopts("c:",\%options);

my $code = $options{c} || 0;

$dtrace = <<END;
/usr/sbin/dtrace -Z -32 -q -n'
:::log-request
{
    this->redirect = arg2 == 0 ? "NoRedirect" : copyinstr(arg2);
    this->responsecode = (int)*(uintptr_t *)copyin(arg0 + 68,sizeof(int));
    this->uuri = copyinstr(*(uintptr_t *)copyin(arg0 + 200, sizeof(uintptr_t)));

    printf("%s %d %s\\n", this->redirect, this->responsecode, this->uuri);
}'
END

open(DTRACE,"$dtrace |") || die "cannot open dtrace $@\n";


printf("%-13s  %-25s  %-40s\n","Response Code", "URI Requested", "Redirect Sent To Client");

while (<DTRACE>) {
    chomp();
   
    ($redirect, $responsecode, $uri) = split(' ',$_);

    if ( $redirect ne "NoRedirect" ) {
        printf("%-13s  %-25s  %-40s\n", $responsecode, $uri, $redirect);
    }    
}
