2009-11-06 12:53:27 +01:00
|
|
|
path = require("path");
|
2010-05-05 07:35:55 +02:00
|
|
|
Buffer = require("buffer").Buffer;
|
2009-11-06 12:53:27 +01:00
|
|
|
|
2010-05-03 02:26:18 +02:00
|
|
|
port = parseInt(process.env.PORT || 8000);
|
|
|
|
|
2010-04-09 19:42:20 +02:00
|
|
|
var old = (process.argv[2] == 'old');
|
2010-04-08 01:19:50 +02:00
|
|
|
|
2010-06-24 02:40:51 +02:00
|
|
|
console.log('pid ' + process.pid);
|
2010-04-15 10:29:39 +02:00
|
|
|
|
2010-04-08 01:19:50 +02:00
|
|
|
http = require(old ? "http_old" : 'http');
|
2010-06-24 02:40:51 +02:00
|
|
|
if (old) console.log('old version');
|
2009-09-28 12:36:36 +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
|
|
|
|
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
|
|
|
|
2009-09-28 12:36:36 +02:00
|
|
|
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
|
|
|
|
2009-05-15 18:11:49 +02:00
|
|
|
} else {
|
|
|
|
status = 404;
|
|
|
|
body = "not found\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
var content_length = body.length.toString();
|
|
|
|
|
2010-06-30 08:12:46 +02:00
|
|
|
res.writeHead( status
|
2009-08-23 12:20:25 +02:00
|
|
|
, { "Content-Type": "text/plain"
|
|
|
|
, "Content-Length": content_length
|
|
|
|
}
|
2009-05-15 18:11:49 +02:00
|
|
|
);
|
2010-04-08 10:00:29 +02:00
|
|
|
if (old) {
|
|
|
|
res.write(body, 'ascii');
|
|
|
|
res.close();
|
|
|
|
} else {
|
2010-04-08 19:44:22 +02:00
|
|
|
res.end(body, 'ascii');
|
2010-04-08 10:00:29 +02:00
|
|
|
}
|
2010-05-03 02:26:18 +02:00
|
|
|
}).listen(port);
|
|
|
|
|
2010-06-24 02:40:51 +02:00
|
|
|
console.log('Listening at http://127.0.0.1:'+port+'/');
|