Skip to content

Instantly share code, notes, and snippets.

@paveljurca
Last active August 29, 2015 14:17
Show Gist options
  • Save paveljurca/1a39386dd7048adb7bb5 to your computer and use it in GitHub Desktop.
Save paveljurca/1a39386dd7048adb7bb5 to your computer and use it in GitHub Desktop.
HTTP headers (relace)
#!/usr/bin/perl -T
#PERL5OPT=-T
use strict;
use warnings;
use LWP::UserAgent;
my $dotaz = sestav_dotaz(
zpracuj(&nacti) #ref eq 'HASH'
);
my $odpoved = posli_dotaz($dotaz);
###-------------
relace_dump();
###-------------
#nacti data z formulare
sub nacti {
read(STDIN, my $q, $ENV{CONTENT_LENGTH})
or die('zadna data');
return $q;
}
#zpracuj obsah formulare
sub zpracuj {
my %f;
foreach (split /&/, shift) {
#obsah POST <input type="hidden" name="data">
unless (/^data/) {
#dekoduj mezery
tr/+/ /;
#dekoduj znaky
s/%([a-f0-9][a-f0-9])/chr(hex($1))/egi;
#orizni mezery
s/^\s+|\s+$//g;
}
#parametr=hodnota (URL)
m/^([^=]+)=(.+)$/;
$f{$1} = $2;
}
return \%f;
}
#sestav parametry HTTP dotazu
sub sestav_dotaz {
my(%f, $d) = %{ shift @_ };
$d = HTTP::Request->new(
$f{method} => $f{url}
);
#pridej k dotazu telo, pokud POST
$d->content($f{data})
if $f{method} =~ /POST/i;
#Referer
$d->push_header(Referer => $f{referer})
if exists $f{referer};
#User-Agent
$d->push_header(User_Agent => $f{ua})
if exists $f{ua};
#HTTP version
$d->version($f{version});
return $d;
}
#posli dotaz a vrat odpoved
sub posli_dotaz {
my $ua = LWP::UserAgent->new;
return $ua->request(shift);
}
#vypis vysledek HTTP relace
sub relace_dump {
printf(
"%s\n\n%s\n=====\n\n%s",
'Content-Type: text/plain; charset=utf-8',
$dotaz->as_string,
#pokud chyba, zobraz info
$odpoved->code != 500 ?
$odpoved->as_string : $odpoved->status_line
);
}
<!doctype html>
<html lang="cs">
<head>
<meta charset="utf-8" />
<meta name="author" content="[email protected]" />
<title>HTTP relace</title>
<style>
body{margin: 0; padding: 0;}
#wrapper{width: 94%; margin: 1em 3%; font: normal lighter 1.2em sans-serif;}
h1{display: inline-block; margin: 0; font: normal bolder 1.6em serif; color: #ff0033; border-bottom: 2px dotted black;}
input[type=url]{padding: .6em;}
input[type=submit]{color: #ff0033;}
form{margin: 1em 0;}
form>*{margin-right: .2em; padding: .2em; font: italic lighter 1em monospace;}
iframe{width: 100%;}
</style>
<script>
//kdyz POST, pridej obsah
function if_post() {
if (document.getElementById('method').value.match(/POST/i)) {
var _prompt = window.prompt("zadej obsah dotazu:", "");
if (_prompt != null) {
document.getElementById('data').value = _prompt;
return true;
}
return false;
}
}
//uprav vysku iframe
function resize(frm) {
/*
http://stackoverflow.com/questions/819416/adjust-width-height-of-iframe-to-fit-with-content-in-it
*/
frm.height = frm.height.contentWindow.document.body.scrollHeight + "px";
//window.alert("test");
}
</script>
</head>
<body>
<div id="wrapper">
<form method="post" action="header.cgi" target="frm-dump" onsubmit="return if_post()">
<!-- URL -->
<input type="url" name="url" size="40" maxlength="1024" required="required" autocomplete="on" placeholder="URL" value="http://fis.vse.cz" />
<!-- HTTP method -->
<select name="method" id="method">
<option value="GET">GET</option>
<option value="HEAD" selected="selected">HEAD&nbsp;</option>
<option value="POST">POST</option>
</select>
<!-- HTTP version -->
<select name="version">
<option value="HTTP/1.1" selected="selected">HTTP/1.1&nbsp;</option>
<option value="HTTP/1.0">HTTP/1.0</option>
</select>
<!-- ua -->
<input type="text" name="ua" size="15" maxlength="256" title="User-Agent" placeholder="User-Agent" />
<!-- referer -->
<input type="text" name="referer" size="15" maxlength="256" title="Referer" placeholder="Referer" />
<!-- POST data -->
<input type="hidden" name="data" id="data" value="" />
<!-- submit -->
<span>=>&nbsp;</span><input type="submit" name="submit" value="QUERY" />
</form>
<iframe name="frm-dump" id="frm-dump" width="100%" height="200px" marginheight="0" frameborder="0" onload="resize(this);"></iframe>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment