#!/usr/local/bin/perl

if ($#ARGV != 2) {
        print "Usage: $0 <target host> <input file> <output file>\n";
        print "input file should contain directory structure relative to web root /\n\n";
        exit(1);
}

my $host = $ARGV[0];
my $infile = $ARGV[1];
my $outfile = $ARGV[2];

$port = 80;
$blah = "HTTP/1.0\nHost: $host\n";

open(OUT, ">$outfile") or die("unable to open $outfile: $!");
open(IN, $infile) or die("unable to open $infile: $!");
@directories=<IN>;

foreach (@directories) {
	chomp;
	print OUT "$_ --> ";
	s/ /%20/g;
	my $repl = echoToNc(qq(OPTIONS /$_ $blah));

	($Allowed) = grep /Allow:/, split(/\n/, $repl); 
	print OUT "$Allowed\n";
}

sub echoToNc {
	my($cmd) = @_;
	open (CMD, ">/tmp/puttestin.$$.txt") or die "Can't open /tmp/puttestin.$$.txt: $!";
	print CMD "$cmd\n";
	print "== Sending the following:\n$cmd";
	print `nc $host $port < /tmp/puttestin.$$.txt > /tmp/puttestout.$$.txt`;
	print "== Got the response:\n";
	open (TMPOUT, "/tmp/puttestout.$$.txt") or die "Can't open /tmp/puttestout.$$.txt: $!";
	my(@lines) = (<TMPOUT>);
	my($lines) = join("",@lines);
	print $lines;
	return $lines;
}
