Merge branch 'master' into net2
[platform/upstream/nodejs.git] / benchmark / http_simple.js
1 path = require("path");
2
3 var puts = require("../lib/sys").puts;
4 http = require("../lib/http2");
5
6 fixed = ""
7 for (var i = 0; i < 20*1024; i++) {
8   fixed += "C";
9 }
10
11 stored = {};
12
13 http.createServer(function (req, res) {
14   var commands = req.url.split("/");
15   var command = commands[1];
16   var body = "";
17   var arg = commands[2];
18   var status = 200;
19
20   if (command == "bytes") {
21     var n = parseInt(arg, 10)
22     if (n <= 0)
23       throw "bytes called with n <= 0" 
24     if (stored[n] === undefined) {
25       puts("create stored[n]");
26       stored[n] = "";
27       for (var i = 0; i < n; i++) {
28         stored[n] += "C"
29       }
30     }
31     body = stored[n];
32
33   } else if (command == "quit") {
34     res.connection.server.close();
35     body = "quitting";
36
37   } else if (command == "fixed") {
38     body = fixed;
39
40   } else {
41     status = 404;
42     body = "not found\n";
43   }
44
45   var content_length = body.length.toString();
46
47   res.writeHead( status 
48                 , { "Content-Type": "text/plain"
49                   , "Content-Length": content_length
50                   }
51                 );
52   res.write(body);
53   res.close();
54 }).listen(8000);