3 var http = require('http');
5 var port = parseInt(process.env.PORT || 8000);
7 var fixed = 'C'.repeat(20 * 1024),
12 var useDomains = process.env.NODE_USE_DOMAINS;
14 // set up one global domain.
16 var domain = require('domain');
17 var gdom = domain.create();
18 gdom.on('error', function(er) {
19 console.error('Error on global domain', er);
25 var server = module.exports = http.createServer(function(req, res) {
27 var dom = domain.create();
32 var commands = req.url.split('/');
33 var command = commands[1];
35 var arg = commands[2];
36 var n_chunks = parseInt(commands[3], 10);
40 if (command == 'bytes') {
43 throw new Error('bytes called with n <= 0');
44 if (storedBytes[n] === undefined) {
45 storedBytes[n] = 'C'.repeat(n);
47 body = storedBytes[n];
49 } else if (command == 'buffer') {
52 throw new Error('buffer called with n <= 0');
53 if (storedBuffer[n] === undefined) {
54 storedBuffer[n] = new Buffer(n);
55 for (i = 0; i < n; i++) {
56 storedBuffer[n][i] = 'C'.charCodeAt(0);
59 body = storedBuffer[n];
61 } else if (command == 'unicode') {
64 throw new Error('unicode called with n <= 0');
65 if (storedUnicode[n] === undefined) {
66 storedUnicode[n] = '\u263A'.repeat(n);
68 body = storedUnicode[n];
70 } else if (command == 'quit') {
71 res.connection.server.close();
74 } else if (command == 'fixed') {
77 } else if (command == 'echo') {
78 res.writeHead(200, { 'Content-Type': 'text/plain',
79 'Transfer-Encoding': 'chunked' });
88 // example: http://localhost:port/bytes/512/4
89 // sends a 512 byte body in 4 chunks of 128 bytes
91 res.writeHead(status, { 'Content-Type': 'text/plain',
92 'Transfer-Encoding': 'chunked' });
93 // send body in chunks
94 var len = body.length;
95 var step = Math.floor(len / n_chunks) || 1;
97 for (i = 0, n = (n_chunks - 1); i < n; ++i) {
98 res.write(body.slice(i * step, i * step + step));
100 res.end(body.slice((n_chunks - 1) * step));
102 var content_length = body.length.toString();
104 res.writeHead(status, { 'Content-Type': 'text/plain',
105 'Content-Length': content_length });
110 server.listen(port, function() {
111 if (module === require.main)
112 console.error('Listening at http://127.0.0.1:' + port + '/');