2019-04-26 01:32:48 +02:00
|
|
|
import std.string;
|
2019-04-27 09:22:02 +02:00
|
|
|
import std.array : array;
|
2019-04-26 01:32:48 +02:00
|
|
|
import std.random;
|
|
|
|
import std.file;
|
|
|
|
|
2019-04-27 09:22:36 +02:00
|
|
|
import vibe.http.server;
|
|
|
|
import taggedalgebraic;
|
|
|
|
|
|
|
|
struct Error {};
|
|
|
|
union U {
|
|
|
|
string[] args;
|
|
|
|
Error e;
|
|
|
|
}
|
|
|
|
|
|
|
|
alias Result = TaggedUnion!U;
|
|
|
|
|
|
|
|
auto shuffle(string[] args, const string t)
|
2019-04-26 01:32:48 +02:00
|
|
|
{
|
2019-04-27 09:22:36 +02:00
|
|
|
Result r;
|
|
|
|
switch(t){
|
|
|
|
case "mt":
|
2019-04-26 01:32:48 +02:00
|
|
|
Mt19937 gen;
|
|
|
|
gen.seed(unpredictableSeed);
|
|
|
|
args.randomShuffle(gen);
|
2019-04-27 09:22:36 +02:00
|
|
|
break;
|
|
|
|
case "x":
|
2019-04-26 01:32:48 +02:00
|
|
|
Xorshift32 gen;
|
|
|
|
gen.seed(unpredictableSeed);
|
|
|
|
args.randomShuffle(gen);
|
2019-04-27 09:22:36 +02:00
|
|
|
break;
|
|
|
|
case "du":
|
2019-04-26 01:32:48 +02:00
|
|
|
auto gen = DevRandomGen!"/dev/urandom"();
|
|
|
|
args.randomShuffle(gen);
|
2019-04-27 09:22:36 +02:00
|
|
|
break;
|
|
|
|
case "dr":
|
2019-04-26 01:32:48 +02:00
|
|
|
auto gen = DevRandomGen!"/dev/random"();
|
|
|
|
args.randomShuffle(gen);
|
2019-04-27 09:22:36 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
r = Error();
|
|
|
|
return r;
|
2019-04-26 01:32:48 +02:00
|
|
|
}
|
2019-04-27 09:22:36 +02:00
|
|
|
|
|
|
|
r = args;
|
|
|
|
return r ;
|
2019-04-26 01:32:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template DevRandomGen(string gen)
|
|
|
|
if (gen == "/dev/random" || gen == "/dev/urandom")
|
|
|
|
{
|
|
|
|
struct DevRandomGen
|
|
|
|
{
|
|
|
|
|
|
|
|
alias UIntType = uint;
|
|
|
|
public:
|
|
|
|
enum bool isUniformRandom = true;
|
|
|
|
enum empty = false;
|
|
|
|
/// Smallest generated value.
|
|
|
|
enum UIntType min = 0;
|
|
|
|
/// Largest generated value.
|
|
|
|
enum UIntType max = ubyte.max;
|
|
|
|
string src = gen;
|
|
|
|
|
|
|
|
void seed(UIntType x0) @safe pure nothrow @nogc {}
|
|
|
|
void popFront() @safe pure nothrow @nogc {}
|
|
|
|
|
|
|
|
@property
|
|
|
|
UIntType front() const { return (cast(ubyte[])(src.read(ubyte.sizeof)))[0]; }
|
|
|
|
|
|
|
|
@property
|
|
|
|
typeof(this) save() @safe pure nothrow @nogc { return this; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleRequest(scope HTTPServerRequest req, scope HTTPServerResponse res)
|
|
|
|
{
|
|
|
|
immutable path = req.requestPath.bySegment.array;
|
|
|
|
enforce(path.length == 3); // path[0] == ""
|
|
|
|
auto engine = path[1].toString;
|
|
|
|
auto args = path[2].toString.split(",");
|
|
|
|
const result = shuffle(args, engine);
|
|
|
|
immutable body = "Asino says:</br><ul>" ~ "<li>" ~ result.join("</li><li>") ~ "</ul>";
|
|
|
|
res.headers["Content-Type"] = "text/html";
|
|
|
|
res.writeBody(body);
|
|
|
|
}
|
|
|
|
|
|
|
|
shared static this()
|
|
|
|
{
|
|
|
|
auto settings = new HTTPServerSettings;
|
|
|
|
settings.port = 8080;
|
|
|
|
settings.bindAddresses = ["::1", "127.0.0.1"];
|
|
|
|
|
|
|
|
listenHTTP(settings, &handleRequest);
|
|
|
|
}
|