#!/usr/bin/perl

use strict;
$| = 1;


my @pairs = ();
my @result = ();
my ($buffer,$pair,$name,$value,$temp,$destination);
my %FORM = ();

# to prevent runaways, call it quits after 60 seconds.

$SIG{'ALRM'} =\&alarm_handle;
alarm(60);     

if ($ENV{'REQUEST_METHOD'} eq "POST")
  { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }
else
  { $buffer = $ENV{'QUERY_STRING'}; }
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) 
  {
  ($name, $value) = split(/=/, $pair);
  $value =~ tr/+/ /;
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  $FORM{$name} = $value;
  }

$destination = $FORM{'URL'};
$destination = "http://".$destination unless ($destination =~ m|http://|);
# remove all non URL allowable stuff.
chomp($destination);
$destination =~ s/ \-.*?(\W|\S)//sg;
$destination =~ s/;//sg;
# remove path etc.
$destination =~ m#http://(.*?)(/*$|/.*$)#;
$destination = $1;
    
print "Content-type: text/html\n\n";

print qq~
 <html>
 <head><title>HTTP Header</title></head>
 <body>
 <h2>Header von $destination</h2>
 ~;    

@result = `/usr/bin/wget -A html,htm -T 5 -O /dev/null -S $destination 2>&1`;

# remove first two lines
shift(@result);
shift(@result);
foreach $temp (@result)
  {
  last if ($temp =~ m/^$/);
  print "$temp<br>";
  }
print "</body> </html>\n";
# to avoid denial of service attack
sleep(2);
exit 0;


# Unterprogramm fuer Timeout
#
sub alarm_handle        
  {                  
  alarm(0);        
  print "<P><H2>Fehler</H2>", "\n";
  print "Zeit\&uuml;berschreitung beim Bearbeiten der Abfrage, Abbruch!", "\n";
  print "</BODY></HTML>", "\n";
  exit 0;           
  }                  
                   
