#!/usr/local/bin/perl5.6 -w
use strict;
use Text::Wrap;
use vars qw($VERSION $now $MAXPOST);
$MAXPOST = 50000;
($VERSION) = ('$Revision: 1.6 $' =~ /([\d\.]+)/);
$now = scalar(localtime());
$Text::Wrap::break = '\s|-';
$Text::Wrap::columns = 132;
unless ($ENV{'REQUEST_METHOD'} eq 'POST' || $ENV{'REQUEST_METHOD'} eq 'GET') {
die "THis program only handles GET and POST requests";
}
print qq(Content-type: text/html
cgi-reflect.pl $VERSION $now
cgi-reflect.pl $VERSION $now
This program shows Standard Input and the Environment given to your programs under your web server. You should
GET or POST things to it, even file uploads.
Standard Input
);
eval {
_showstdin();
};
alarm(0);
if ($@) {
print qq(Error reading STDIN - $@
);
}
print qq(
Coloured text is the hex form of any binary bytes. The hyphens are included to
break up the digits in an easy to read way, so ignore them. It is normal and correct to see 0D at the end of
every line because it shows that network line breaks were sent.
Wrapping may lose spaces or hyphens at line-end in the wrapping process - this is normal. Put 'cgi-reflect-nowrap'
in the query string to turn off wrapping.
Environment
);
foreach (sort keys %ENV) {
print "| $_ | " . _html_escape($ENV{$_}) . " |
\n";
}
print qq(
END OF OUTPUT
);
#####################################################
sub _showstdin {
if ($ENV{'CONTENT_LENGTH'} > $MAXPOST) {
die("Content length is too long, more than $MAXPOST bytes");
}
local $SIG{'ALRM'} = sub {
die 'ALRM!';
};
alarm(5);
while () {
if ($ENV{'QUERY_STRING'} =~ /cgi-reflect-nowrap/) {
print _html_escape($_);
} else {
print Text::Wrap::wrap('', '', _html_escape($_));
}
}
alarm(0);
}
sub _html_escape {
my $str = shift;
$str =~ s/</g;
$str =~ s/>/>/g;
$str =~ s/'/'/g;
$str =~ s/"/"/g;
return _binary_escape($str);
}
sub _binary_escape {
my $toencode = shift;
return undef unless defined($toencode);
$toencode =~ s/([\000-\011\013\014-\037\200-\377])/_colour($1)/eg;
$toencode =~ s|-||g;
$toencode =~ s|-||g;
$toencode =~ s|-||g;
return $toencode;
}
sub _colour {
my $x = shift;
return ('-' . uc(sprintf("%02x",ord($x))) . '-');
}
=pod
=head1 NAME
cgi-reflect.pl - exactly what your browser is sending to the web server - STDIN in an escaped format and environment variables
=head1 SYNOPSIS
Place this program in your /cgi-bin directory, or equivalent place, and submit POST requests to it. You will see exactly
what is sent to the program. This is especially useful for examining the multipart/formdata sent in by file upload forms
on various operating systems.
=head1 DESCRIPTION
Quite simply it shows all of STDIN, with unprintable characters escaped and coloured, and all the environment.
=head1 PREREQUISITES
Text::Wrap
=head1 COREQUISITES
None.
=begin comment
=pod OSNAMES
Unix (others untested)
=pod SCRIPT CATEGORIES
CGI
UNIX/System_administration
=pod README
A CGI to show you exactly what your browser is sending to the web server - STDIN in an escaped format
and all environment variables.
=end comment
=head1 VERSION
$Revision: 1.6 $
=cut