Skip to content

Instantly share code, notes, and snippets.

@p120ph37
Created July 14, 2015 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p120ph37/3bf6c500e0f00ab635da to your computer and use it in GitHub Desktop.
Save p120ph37/3bf6c500e0f00ab635da to your computer and use it in GitHub Desktop.
Simple CGI that displays information about the request.
#!/usr/bin/perl
use warnings;
use strict;
use CGI qw/param escapeHTML/;
use MIME::Base64;
### Apache2's mod_cgi does not pass the HTTP__AUTHORIZATION header by default.
### Add this to .htaccess to work around the issue...
# RewriteEngine on
# RewriteCond %{HTTP:Authorization} ^(.*)
# RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
my $text = 1;
if($ENV{'HTTP_ACCEPT'} =~ /\*\/\*|text\/html/) {
print "Content-Type: text/html\n\n";
$text = 0;
} else {
print "Content-Type: text/plain\n\n";
}
sub h { $text ? "$_[0]\n" : '<h3>'.escapeHTML($_[0])."</h3>\n"; }
sub b { $text ? $_[0] : '<b>'.escapeHTML($_[0]).'</b>'; }
sub e { $text ? $_[0] : escapeHTML($_[0]); }
sub n { $text ? "\n" : "<br>\n"; }
sub i { $text ? ' ' : ''; }
my %e = %ENV;
$e{'HTTP_CONTENT_TYPE'} = delete $e{'CONTENT_TYPE'} if exists $e{'CONTENT_TYPE'};
$e{'HTTP_CONTENT_LENGTH'} = delete $e{'CONTENT_LENGTH'} if exists $e{'CONTENT_LENGTH'};
print h('TCP Connection:');
print i.e(delete $e{'REMOTE_ADDR'}).':'.e(delete $e{'REMOTE_PORT'}).' '.b('>').' '.e(delete $e{'SERVER_ADDR'}).':'.e(delete $e{'SERVER_PORT'}).n;
if(delete $e{'HTTPS'}) {
print h('SSL Layer ('.delete($e{'SSL_PROTOCOL'}).'):');
foreach(sort grep{/^SSL_(?!SERVER|VERSION)/}(keys %e)) {
/^SSL_(.*)$/;
print i.b($1.'=').e(delete $e{$_}).n;
}
}
print h('HTTP Request:');
print i.b(delete $e{'REQUEST_METHOD'}).' '.e(delete $e{'REQUEST_URI'}).($e{'QUERY_STRING'} ? '?'.e(delete $e{'QUERY_STRING'}) : '').' '.b(delete $e{'SERVER_PROTOCOL'}).n;
delete $e{'QUERY_STRING'};
if(($e{'HTTP_AUTHORIZATION'} || '') =~ /^Basic\s+(.*)$/) {
print h('HTTP Basic Auth:');
my($u,$p) = split(':', decode_base64($1));
print i.b('Username:').' '.e($u).n;
print i.b('Password:').' '.e($p).n;
delete $e{'HTTP_AUTHORIZATION'};
}
print h('HTTP Headers:');
foreach(sort grep{/^HTTP_/}(keys %e)) {
/^HTTP_(.*)$/;
my $camel = join('', map{ucfirst lc $_}(split('_', $1)));
print i.b($camel.':').' '.e(delete $e{$_}).n;
}
print h('CGI Params:');
foreach(param()) {
print i.b($_.'=').join(b(','), e(param($_))).n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment