2009-11-06 12:53:27 +01:00
|
|
|
path = require("path");
|
2010-10-13 23:16:49 +02:00
|
|
|
exec = require("child_process").exec;
|
|
|
|
http = require("http");
|
2009-11-06 12:53:27 +01:00
|
|
|
|
2010-05-03 02:26:18 +02:00
|
|
|
port = parseInt(process.env.PORT || 8000);
|
|
|
|
|
2010-06-24 02:40:51 +02:00
|
|
|
console.log('pid ' + process.pid);
|
2010-04-15 10:29:39 +02:00
|
|
|
|
2009-05-16 12:49:33 +02:00
|
|
|
fixed = ""
|
|
|
|
for (var i = 0; i < 20*1024; i++) {
|
|
|
|
fixed += "C";
|
|
|
|
}
|
2009-11-06 12:53:27 +01:00
|
|
|
|
2010-10-13 23:16:49 +02:00
|
|
|
var uname, rev;
|
|
|
|
|
|
|
|
exec('git rev-list -1 HEAD', function (e, stdout) {
|
|
|
|
if (e) {
|
|
|
|
console.error("Problem executing: 'git rev-list -1 HEAD'");
|
|
|
|
throw new Error(e);
|
|
|
|
}
|
|
|
|
rev = stdout.replace(/\s/g, '');
|
|
|
|
});
|
|
|
|
|
|
|
|
exec('uname -a', function (e, stdout) {
|
|
|
|
if (e) {
|
|
|
|
console.error("Problem executing: 'uname -a'");
|
|
|
|
throw new Error(e);
|
|
|
|
}
|
|
|
|
uname = stdout.replace(/[\r\n]/g, '');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-05-16 12:49:33 +02:00
|
|
|
stored = {};
|
2010-05-05 07:35:55 +02:00
|
|
|
storedBuffer = {};
|
2009-11-06 12:53:27 +01:00
|
|
|
|
2010-10-13 23:16:49 +02:00
|
|
|
var server = http.createServer(function (req, res) {
|
2010-01-11 08:00:58 +01:00
|
|
|
var commands = req.url.split("/");
|
2009-05-15 18:11:49 +02:00
|
|
|
var command = commands[1];
|
2009-05-16 12:49:33 +02:00
|
|
|
var body = "";
|
2009-05-15 18:11:49 +02:00
|
|
|
var arg = commands[2];
|
|
|
|
var status = 200;
|
|
|
|
|
|
|
|
if (command == "bytes") {
|
|
|
|
var n = parseInt(arg, 10)
|
|
|
|
if (n <= 0)
|
2010-06-30 08:12:46 +02:00
|
|
|
throw "bytes called with n <= 0"
|
2009-05-16 12:49:33 +02:00
|
|
|
if (stored[n] === undefined) {
|
2010-06-24 02:40:51 +02:00
|
|
|
console.log("create stored[n]");
|
2009-05-16 12:49:33 +02:00
|
|
|
stored[n] = "";
|
|
|
|
for (var i = 0; i < n; i++) {
|
|
|
|
stored[n] += "C"
|
|
|
|
}
|
2009-05-15 18:11:49 +02:00
|
|
|
}
|
2009-05-16 12:49:33 +02:00
|
|
|
body = stored[n];
|
2009-05-15 18:11:49 +02:00
|
|
|
|
2010-05-05 07:35:55 +02:00
|
|
|
} else if (command == "buffer") {
|
|
|
|
var n = parseInt(arg, 10)
|
|
|
|
if (n <= 0) throw new Error("bytes called with n <= 0");
|
|
|
|
if (storedBuffer[n] === undefined) {
|
2010-06-24 02:40:51 +02:00
|
|
|
console.log("create storedBuffer[n]");
|
2010-05-05 07:35:55 +02:00
|
|
|
storedBuffer[n] = new Buffer(n);
|
|
|
|
for (var i = 0; i < n; i++) {
|
|
|
|
storedBuffer[n][i] = "C".charCodeAt(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
body = storedBuffer[n];
|
|
|
|
|
2009-05-15 18:11:49 +02:00
|
|
|
} else if (command == "quit") {
|
2009-05-18 12:44:01 +02:00
|
|
|
res.connection.server.close();
|
2009-05-15 22:41:36 +02:00
|
|
|
body = "quitting";
|
|
|
|
|
|
|
|
} else if (command == "fixed") {
|
2009-05-16 12:49:33 +02:00
|
|
|
body = fixed;
|
2009-05-15 22:41:36 +02:00
|
|
|
|
2010-10-13 23:16:49 +02:00
|
|
|
} else if (command == "info") {
|
2010-10-14 01:20:24 +02:00
|
|
|
body = 'rev=' + rev + '\nuname="' + uname + '"\n';
|
2010-10-13 23:16:49 +02:00
|
|
|
|
2009-05-15 18:11:49 +02:00
|
|
|
} else {
|
|
|
|
status = 404;
|
|
|
|
body = "not found\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
var content_length = body.length.toString();
|
|
|
|
|
2010-10-13 23:16:49 +02:00
|
|
|
res.writeHead(status, { "Content-Type": "text/plain",
|
|
|
|
"Content-Length": content_length });
|
|
|
|
res.end(body);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(port, function () {
|
|
|
|
console.log('Listening at http://127.0.0.1:'+port+'/');
|
|
|
|
});
|
2010-05-03 02:26:18 +02:00
|
|
|
|