1 path = require("path");
2 exec = require("child_process").exec;
3 http = require("http");
5 port = parseInt(process.env.PORT || 8000);
7 console.log('pid ' + process.pid);
10 for (var i = 0; i < 20*1024; i++) {
16 exec('git rev-list -1 HEAD', function (e, stdout) {
18 console.error("Problem executing: 'git rev-list -1 HEAD'");
21 rev = stdout.replace(/\s/g, '');
24 exec('uname -a', function (e, stdout) {
26 console.error("Problem executing: 'uname -a'");
29 uname = stdout.replace(/[\r\n]/g, '');
37 var server = http.createServer(function (req, res) {
38 var commands = req.url.split("/");
39 var command = commands[1];
41 var arg = commands[2];
42 var n_chunks = parseInt(commands[3], 10);
45 if (command == "bytes") {
46 var n = parseInt(arg, 10)
48 throw "bytes called with n <= 0"
49 if (stored[n] === undefined) {
50 console.log("create stored[n]");
52 for (var i = 0; i < n; i++) {
58 } else if (command == "buffer") {
59 var n = parseInt(arg, 10)
60 if (n <= 0) throw new Error("bytes called with n <= 0");
61 if (storedBuffer[n] === undefined) {
62 console.log("create storedBuffer[n]");
63 storedBuffer[n] = new Buffer(n);
64 for (var i = 0; i < n; i++) {
65 storedBuffer[n][i] = "C".charCodeAt(0);
68 body = storedBuffer[n];
70 } else if (command == "quit") {
71 res.connection.server.close();
74 } else if (command == "fixed") {
77 } else if (command == "info") {
78 body = 'rev=' + rev + '\nuname="' + uname + '"\n';
85 // example: http://localhost:port/bytes/512/4
86 // sends a 512 byte body in 4 chunks of 128 bytes
88 res.writeHead(status, { "Content-Type": "text/plain",
89 "Transfer-Encoding": "chunked" });
90 // send body in chunks
91 var len = body.length;
92 var step = ~~(len / n_chunks) || len;
94 for (var i = 0; i < len; i += step) {
95 res.write(body.slice(i, i + step));
100 var content_length = body.length.toString();
102 res.writeHead(status, { "Content-Type": "text/plain",
103 "Content-Length": content_length });
109 server.listen(port, function () {
110 console.log('Listening at http://127.0.0.1:'+port+'/');