#!/usr/bin/perl
use LWP::UserAgent;
use HTML::LinkExtor;
use URI::URL;
use HTML::TokeParser;
$url = $ENV{'QUERY_STRING'}; # for instance
$ua = new LWP::UserAgent;
# Set up a callback that collect image links
my @imgs = ();
sub callback {
my($tag, %attr) = @_;
return if $tag ne 'img'; # we only look closer at
push(@imgs, values %attr);
}
# Make the parser. Unfortunately, we don't know the base yet
# (it might be diffent from $url)
$p = HTML::LinkExtor->new(\&callback);
# Request document and parse it as it arrives
$res = $ua->request(HTTP::Request->new(GET => $url));
# Expand all image URLs to absolute ones
my $base = $res->base;
#@imgs = map { $_ = url($_, $base)->abs; } @imgs;
# Print them out
print "Content-type: text/html\n\n";
my $content = $res->content;
my $t = HTML::TokeParser->new(\$content) || die $!;
my $token;
while ($token = $t->get_token) {
if ($token->[0] eq "S" && $token->[1] eq "a")
{
$url = $token->[2]->{href} || "-";
my $new_url = 'href="'.url($url, $base)->abs.'"';
# .$ENV{'SCRIPT_NAME'}."?url="
$content =~ s/href((^\s*)||(\s*))=((^\s*)||(\s*?))\"$url\"/$new_url/egi;
}
if ($token->[0] eq "S" && $token->[1] eq "img")
{
$url = $token->[2]->{src} || "-";
my $new_url = 'src="'.url($url, $base)->abs.'"';
$content =~ s/src((^\s*)||(\s*))=((^\s*)||(\s*?))\"$url\"/$new_url/egi;
}
}
my @lines = split(/\n/,$content);
my $line;
my $pr = 1;
foreach $line (@lines)
{
$pr = 0 if ($line =~ "");
$pr = 1 if ($line =~ "");
print $line."\n" if $pr;
}