#!/usr/bin/perl -w #----------------------------------------------------------------------------- # echo.cgi : Echos information sent from client in an HTTP request. # * Reads in all of STDIN. # * Prints out all the HTTP Environment variables. # * Prints out to STOUT all it read in. # Parke Godfrey ©2012 Oct 9 use URI::Escape; #----------------------------------------------------------------------------- # Read in all the standard input (POST). my $buffer; open(BUFFER, ">", \$buffer) or die "Can't open buffer to write: $!"; while (<>) { print BUFFER; } close(BUFFER); #----------------------------------------------------------------------------- # Print headers. print "Content-Type: text/html\n"; # Header: Content-Type print "\n"; # Blank line ending Headers #----------------------------------------------------------------------------- # Print start of HTML document. print "\n" . "\n" . "\n" . "\n" . "\n"; #----------------------------------------------------------------------------- # Print out HTTP ENV variables and values. print "
\n" . "headers\n" . "
";

foreach my $key (keys %ENV) {
    if ($key =~ /^HTTP_(.*)$/) {
        print $1, ": ", $ENV{$key}, "\n";
    }
}
print "
\n"; #----------------------------------------------------------------------------- # Print out Query String from URL (GET). (It might not exist.) print "
\n" . "query string\n" . "
";

if ($ENV{"QUERY_STRING"} ne "") {
    print $ENV{"QUERY_STRING"}, "\n";
}

print "
\n"; #----------------------------------------------------------------------------- # Print out (echo) the payload that we read in. print "
\n" . "payload\n" . "
\n";

open(BUFFER, "<", \$buffer)
    or die "Can't open buffer to read: $!";
while () {
    print;
}
close(BUFFER);

print "
\n"; #----------------------------------------------------------------------------- # Show parsed parameter key-value pairs, if any. # Remember to URI unescape! And turn +'s into spaces. if ($ENV{"QUERY_STRING"} ne "" or $buffer ne "") { print "
\n" . "parsed parameters\n" . "
"
        . "";

    foreach my $qstring ($ENV{"QUERY_STRING"}, $buffer) {
        if (defined($qstring) and $qstring ne "") {
            foreach (split '&', $qstring) {
                my ($key, $val) = m/^([^=]+)=(.*)$/;
                $key =~ s/\+/ /g;
                $key = &uri_unescape($key);
                $val =~ s/\+/ /g;
                $val = &uri_unescape($val);
                print "\n"
                    . "\n"
                    . "\n"
                    . "\n";
            }
        }
    }

    print "
$key:\"$val\"
\n" . "
\n"; } #----------------------------------------------------------------------------- # Finish HTML. print "\n" . "";