Skip to content

Instantly share code, notes, and snippets.

@awwaiid
Last active October 23, 2015 03:09
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 awwaiid/3629e6a653a0b6743125 to your computer and use it in GitHub Desktop.
Save awwaiid/3629e6a653a0b6743125 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl6
# Run with: perl6 --setting=RESTRICTED webrepl.p6
use HTTP::Server::Tiny;
use URI::Encode;
use HTML::Entity;
use UUID;
sub parse-params($data) {
my @params = $data.split(/\&|\;/);
my %paramdata;
for @params {
my @parts = .split(/\=/, 2);
@parts[1] ~~ s:g/\+/ /;
@parts[1] = uri_decode(@parts[1]);
if %paramdata{@parts[0]} {
%paramdata{@parts[0]} = [ %paramdata{@parts[0]}.flat, @parts[1] ].flat;
}
else {
%paramdata{@parts[0]} = @parts[1];
}
}
return %paramdata;
}
my $port = 15555;
my %session;
HTTP::Server::Tiny.new(host => '127.0.0.1', port => $port).run(
sub ($env) {
say $env;
if $env<PATH_INFO> ne "/" {
say "Wrong path $env<PATH_INFO>";
return 500, ['Content-Type' => 'text/plain'], ["Nope"];
}
my $query = $env<QUERY_STRING>;
my %params = parse-params($query) if $query;
my $session_id = encode-entities(%params<s> || ~UUID.new);
my $cmd = %params<cmd> || "";
say "$session_id: [$cmd]";
my $result;
try {
$result = EVAL($cmd);
CATCH {
default {
$result = "ERROR: $_";
}
}
}
my $history = %session{$session_id} || "";
$history ~= encode-entities("\n> $cmd\n$result");
%session{$session_id} = $history;
# <pre>
# ENV: {$env.perl}
# Params: {%params.perl}
# Session: $session_id
# </pre>
return 200, ['Content-Type' => 'text/html'], [qq{
<html>
<head>
<title>WEB REPL</title>
<style>
.cmd { border: 0 }
*:focus { outline: none; }
form, pre { margin: 0; padding: 0; }
body, input { font-family: monospace; }
</style>
</head>
<body>
<p><i>This is probably a bad idea.</i></p>
<pre class="output">$history </pre>
<form>
<input type=hidden name=s value="$session_id">
&gt; <input type=text name="cmd" class="cmd" autofocus>
</form>
</body>
</html>
}];
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment