mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
2.8 KiB
114 lines
2.8 KiB
9 years ago
|
'use strict';
|
||
|
|
||
9 years ago
|
var http = require('http');
|
||
15 years ago
|
|
||
13 years ago
|
var port = parseInt(process.env.PORT || 8000);
|
||
15 years ago
|
|
||
9 years ago
|
var fixed = 'C'.repeat(20 * 1024),
|
||
9 years ago
|
storedBytes = {},
|
||
|
storedBuffer = {},
|
||
|
storedUnicode = {};
|
||
15 years ago
|
|
||
13 years ago
|
var useDomains = process.env.NODE_USE_DOMAINS;
|
||
|
|
||
|
// set up one global domain.
|
||
|
if (useDomains) {
|
||
|
var domain = require('domain');
|
||
|
var gdom = domain.create();
|
||
|
gdom.on('error', function(er) {
|
||
12 years ago
|
console.error('Error on global domain', er);
|
||
13 years ago
|
throw er;
|
||
|
});
|
||
|
gdom.enter();
|
||
|
}
|
||
|
|
||
9 years ago
|
var server = module.exports = http.createServer(function(req, res) {
|
||
13 years ago
|
if (useDomains) {
|
||
|
var dom = domain.create();
|
||
|
dom.add(req);
|
||
|
dom.add(res);
|
||
|
}
|
||
|
|
||
13 years ago
|
var commands = req.url.split('/');
|
||
16 years ago
|
var command = commands[1];
|
||
13 years ago
|
var body = '';
|
||
16 years ago
|
var arg = commands[2];
|
||
14 years ago
|
var n_chunks = parseInt(commands[3], 10);
|
||
16 years ago
|
var status = 200;
|
||
|
|
||
9 years ago
|
var n, i;
|
||
13 years ago
|
if (command == 'bytes') {
|
||
9 years ago
|
n = ~~arg;
|
||
16 years ago
|
if (n <= 0)
|
||
9 years ago
|
throw new Error('bytes called with n <= 0');
|
||
13 years ago
|
if (storedBytes[n] === undefined) {
|
||
9 years ago
|
storedBytes[n] = 'C'.repeat(n);
|
||
16 years ago
|
}
|
||
13 years ago
|
body = storedBytes[n];
|
||
16 years ago
|
|
||
13 years ago
|
} else if (command == 'buffer') {
|
||
9 years ago
|
n = ~~arg;
|
||
13 years ago
|
if (n <= 0)
|
||
|
throw new Error('buffer called with n <= 0');
|
||
15 years ago
|
if (storedBuffer[n] === undefined) {
|
||
9 years ago
|
storedBuffer[n] = Buffer.allocUnsafe(n);
|
||
9 years ago
|
for (i = 0; i < n; i++) {
|
||
13 years ago
|
storedBuffer[n][i] = 'C'.charCodeAt(0);
|
||
15 years ago
|
}
|
||
|
}
|
||
|
body = storedBuffer[n];
|
||
|
|
||
13 years ago
|
} else if (command == 'unicode') {
|
||
9 years ago
|
n = ~~arg;
|
||
13 years ago
|
if (n <= 0)
|
||
|
throw new Error('unicode called with n <= 0');
|
||
|
if (storedUnicode[n] === undefined) {
|
||
9 years ago
|
storedUnicode[n] = '\u263A'.repeat(n);
|
||
13 years ago
|
}
|
||
|
body = storedUnicode[n];
|
||
|
|
||
13 years ago
|
} else if (command == 'quit') {
|
||
16 years ago
|
res.connection.server.close();
|
||
13 years ago
|
body = 'quitting';
|
||
16 years ago
|
|
||
13 years ago
|
} else if (command == 'fixed') {
|
||
16 years ago
|
body = fixed;
|
||
16 years ago
|
|
||
13 years ago
|
} else if (command == 'echo') {
|
||
|
res.writeHead(200, { 'Content-Type': 'text/plain',
|
||
|
'Transfer-Encoding': 'chunked' });
|
||
13 years ago
|
req.pipe(res);
|
||
|
return;
|
||
|
|
||
16 years ago
|
} else {
|
||
|
status = 404;
|
||
13 years ago
|
body = 'not found\n';
|
||
16 years ago
|
}
|
||
|
|
||
14 years ago
|
// example: http://localhost:port/bytes/512/4
|
||
|
// sends a 512 byte body in 4 chunks of 128 bytes
|
||
|
if (n_chunks > 0) {
|
||
13 years ago
|
res.writeHead(status, { 'Content-Type': 'text/plain',
|
||
|
'Transfer-Encoding': 'chunked' });
|
||
14 years ago
|
// send body in chunks
|
||
|
var len = body.length;
|
||
12 years ago
|
var step = Math.floor(len / n_chunks) || 1;
|
||
14 years ago
|
|
||
9 years ago
|
for (i = 0, n = (n_chunks - 1); i < n; ++i) {
|
||
12 years ago
|
res.write(body.slice(i * step, i * step + step));
|
||
14 years ago
|
}
|
||
12 years ago
|
res.end(body.slice((n_chunks - 1) * step));
|
||
14 years ago
|
} else {
|
||
|
var content_length = body.length.toString();
|
||
|
|
||
13 years ago
|
res.writeHead(status, { 'Content-Type': 'text/plain',
|
||
|
'Content-Length': content_length });
|
||
14 years ago
|
res.end(body);
|
||
14 years ago
|
}
|
||
14 years ago
|
});
|
||
|
|
||
9 years ago
|
server.listen(port, function() {
|
||
12 years ago
|
if (module === require.main)
|
||
9 years ago
|
console.error('Listening at http://127.0.0.1:' + port + '/');
|
||
13 years ago
|
});
|