#!/usr/bin/perl -w use LWP::UserAgent; use IO::Zlib; use Getopt::Std; use Time::localtime; use Time::HiRes qw( gettimeofday ); use vars qw($input $uname $pass $wurl $help $tmpdir $tmpname $arc); # first, get our work unit sub get_work_unit { my $req = HTTP::Request->new(GET => $wurl); my $ua = LWP::UserAgent->new; $ua->credentials($req->uri->host_port, "localhost", $uname, $pass); $res = $ua->request($req); die("failed to get work unit: ".$res->status_line) unless($res->is_success); return $res; } # next, start our temporary archive file, dummy header sub create_archive { $tmpname = "$tmpdir/babygrub.".(time * $$).".arc.gz"; $arc = IO::Zlib->new($tmpname, "wb"); my $time = get_date(); print $arc <new; $ua->requests_redirectable([]); $ua->parse_head(0); $ua->timeout(10); foreach my $unit (split /\r\n\r\n/, $res->content) { my $time = get_date(); my ($first,@headers) = split /\r\n/, $unit; my ($method,$path) = split / /,$first; # have to go find the Host: header to construct the URI for LWP my $host; foreach my $header (@headers) { $host = $1 if($header =~ /Host\: (.+)$/); } my $req = HTTP::Request->new($method => "http://$host$path"); foreach my $header (@headers) { $req->header($1 => $2) if($header =~ /(.+?)\: (.+)/); } print "http://$host$path\n\t"; # the special case of the PUT is to include the archive file we've made! if($method eq "PUT") { my $upload_start = gettimeofday; upload_archive($arc,$req,$ua); my $upload_end = gettimeofday; print STDOUT "Upload took: ", $upload_end - $upload_start, "\n"; last; } my $res = $ua->request($req); print $res->status_line,"\n"; my $body = $res->as_string; my $ctype = $res->content_type || "message/http"; my $ip = $res->header("client-peer") || "127.0.0.1"; $ip =~ s/([\d|\.]+)/$1/; print $arc "\nhttp://$host$path $ip $time $ctype ",length($body),"\n"; syswrite($arc,$body); } } sub get_date { my $tm = localtime; my $date = sprintf("%04d%02d%02d%02d%02d%02d", $tm->year+1900,($tm->mon+1),$tm->mday,$tm->hour,$tm->min,$tm->sec); return $date; } sub upload_archive { my $arc = shift(); my $req = shift(); my $ua = shift (); my $buffer = ""; my $content = ""; close($arc); # Close to flush() while(read(ARCZ, $buffer, 16384)) { $content .= $buffer; } $req->content($content); $ua->timeout(600); my $res = $ua->request($req); print $res->status_line,"\n"; # release file. close(ARCZ); } sub init { $input = {}; Getopt::Std::getopts('u:p:w:t:h', $input); $help = <