2016-02-19 17:03:16 -08:00
|
|
|
'use strict';
|
|
|
|
|
2017-09-13 22:48:53 -03:00
|
|
|
const http = require('http');
|
2009-11-06 12:53:27 +01:00
|
|
|
|
2017-09-13 22:48:53 -03:00
|
|
|
const fixed = 'C'.repeat(20 * 1024);
|
|
|
|
const storedBytes = Object.create(null);
|
|
|
|
const storedBuffer = Object.create(null);
|
|
|
|
const storedUnicode = Object.create(null);
|
2009-11-06 12:53:27 +01:00
|
|
|
|
2017-09-13 22:48:53 -03:00
|
|
|
const useDomains = process.env.NODE_USE_DOMAINS;
|
2012-04-10 14:59:21 -07:00
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
// Set up one global domain.
|
2019-05-08 20:45:10 +02:00
|
|
|
let domain;
|
2012-04-10 14:59:21 -07:00
|
|
|
if (useDomains) {
|
2019-05-08 20:45:10 +02:00
|
|
|
domain = require('domain');
|
2017-09-13 22:48:53 -03:00
|
|
|
const gdom = domain.create();
|
2019-02-04 22:06:08 -08:00
|
|
|
gdom.on('error', (er) => {
|
2013-02-11 23:43:22 -08:00
|
|
|
console.error('Error on global domain', er);
|
2012-04-10 14:59:21 -07:00
|
|
|
throw er;
|
|
|
|
});
|
|
|
|
gdom.enter();
|
|
|
|
}
|
|
|
|
|
2019-02-04 22:06:08 -08:00
|
|
|
module.exports = http.createServer((req, res) => {
|
2012-04-10 14:59:21 -07:00
|
|
|
if (useDomains) {
|
2017-09-13 22:48:53 -03:00
|
|
|
const dom = domain.create();
|
2012-04-10 14:59:21 -07:00
|
|
|
dom.add(req);
|
|
|
|
dom.add(res);
|
|
|
|
}
|
|
|
|
|
2017-06-07 11:25:24 -04:00
|
|
|
// URL format: /<type>/<length>/<chunks>/<responseBehavior>/chunkedEnc
|
2017-09-13 22:48:53 -03:00
|
|
|
const params = req.url.split('/');
|
|
|
|
const command = params[1];
|
2020-01-24 18:12:49 +01:00
|
|
|
let body = '';
|
2017-09-13 22:48:53 -03:00
|
|
|
const arg = params[2];
|
|
|
|
const n_chunks = parseInt(params[3], 10);
|
|
|
|
const resHow = params.length >= 5 ? params[4] : 'normal';
|
2017-12-09 17:32:48 -05:00
|
|
|
const chunkedEnc = params.length >= 6 && params[5] === '0' ? false : true;
|
2020-01-24 18:12:49 +01:00
|
|
|
let status = 200;
|
2009-05-15 18:11:49 +02:00
|
|
|
|
2020-01-24 18:12:49 +01:00
|
|
|
let n, i;
|
2016-08-06 13:01:30 -07:00
|
|
|
if (command === 'bytes') {
|
2016-02-27 21:56:18 -08:00
|
|
|
n = ~~arg;
|
2009-05-15 18:11:49 +02:00
|
|
|
if (n <= 0)
|
2016-02-24 22:30:10 -08:00
|
|
|
throw new Error('bytes called with n <= 0');
|
2012-05-08 17:38:02 +02:00
|
|
|
if (storedBytes[n] === undefined) {
|
2016-02-19 11:01:11 +08:00
|
|
|
storedBytes[n] = 'C'.repeat(n);
|
2009-05-15 18:11:49 +02:00
|
|
|
}
|
2012-05-08 17:38:02 +02:00
|
|
|
body = storedBytes[n];
|
2016-08-06 13:01:30 -07:00
|
|
|
} else if (command === 'buffer') {
|
2016-02-27 21:56:18 -08:00
|
|
|
n = ~~arg;
|
2012-05-08 17:38:02 +02:00
|
|
|
if (n <= 0)
|
|
|
|
throw new Error('buffer called with n <= 0');
|
2010-05-04 22:35:55 -07:00
|
|
|
if (storedBuffer[n] === undefined) {
|
2016-01-25 15:00:06 -08:00
|
|
|
storedBuffer[n] = Buffer.allocUnsafe(n);
|
2016-02-27 21:56:18 -08:00
|
|
|
for (i = 0; i < n; i++) {
|
2012-05-08 17:38:02 +02:00
|
|
|
storedBuffer[n][i] = 'C'.charCodeAt(0);
|
2010-05-04 22:35:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
body = storedBuffer[n];
|
2016-08-06 13:01:30 -07:00
|
|
|
} else if (command === 'unicode') {
|
2016-02-27 21:56:18 -08:00
|
|
|
n = ~~arg;
|
2012-05-08 18:09:04 +02:00
|
|
|
if (n <= 0)
|
|
|
|
throw new Error('unicode called with n <= 0');
|
|
|
|
if (storedUnicode[n] === undefined) {
|
2016-02-19 11:01:11 +08:00
|
|
|
storedUnicode[n] = '\u263A'.repeat(n);
|
2012-05-08 18:09:04 +02:00
|
|
|
}
|
|
|
|
body = storedUnicode[n];
|
2016-08-06 13:01:30 -07:00
|
|
|
} else if (command === 'quit') {
|
2009-05-18 12:44:01 +02:00
|
|
|
res.connection.server.close();
|
2012-05-08 17:38:02 +02:00
|
|
|
body = 'quitting';
|
2016-08-06 13:01:30 -07:00
|
|
|
} else if (command === 'fixed') {
|
2009-05-16 12:49:33 +02:00
|
|
|
body = fixed;
|
2016-08-06 13:01:30 -07:00
|
|
|
} else if (command === 'echo') {
|
2016-12-31 15:58:14 -05:00
|
|
|
switch (resHow) {
|
|
|
|
case 'setHeader':
|
|
|
|
res.statusCode = 200;
|
|
|
|
res.setHeader('Content-Type', 'text/plain');
|
|
|
|
res.setHeader('Transfer-Encoding', 'chunked');
|
|
|
|
break;
|
|
|
|
case 'setHeaderWH':
|
|
|
|
res.setHeader('Content-Type', 'text/plain');
|
|
|
|
res.writeHead(200, { 'Transfer-Encoding': 'chunked' });
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res.writeHead(200, {
|
|
|
|
'Content-Type': 'text/plain',
|
|
|
|
'Transfer-Encoding': 'chunked'
|
|
|
|
});
|
|
|
|
}
|
2011-12-30 02:03:08 +01:00
|
|
|
req.pipe(res);
|
|
|
|
return;
|
2009-05-15 18:11:49 +02:00
|
|
|
} else {
|
|
|
|
status = 404;
|
2012-05-08 17:38:02 +02:00
|
|
|
body = 'not found\n';
|
2009-05-15 18:11:49 +02:00
|
|
|
}
|
|
|
|
|
2011-08-17 22:38:30 +02:00
|
|
|
// example: http://localhost:port/bytes/512/4
|
|
|
|
// sends a 512 byte body in 4 chunks of 128 bytes
|
2017-09-13 22:48:53 -03:00
|
|
|
const len = body.length;
|
2017-06-07 11:25:24 -04:00
|
|
|
switch (resHow) {
|
|
|
|
case 'setHeader':
|
|
|
|
res.statusCode = status;
|
|
|
|
res.setHeader('Content-Type', 'text/plain');
|
|
|
|
if (chunkedEnc)
|
2016-12-31 15:58:14 -05:00
|
|
|
res.setHeader('Transfer-Encoding', 'chunked');
|
2017-06-07 11:25:24 -04:00
|
|
|
else
|
|
|
|
res.setHeader('Content-Length', len.toString());
|
|
|
|
break;
|
|
|
|
case 'setHeaderWH':
|
|
|
|
res.setHeader('Content-Type', 'text/plain');
|
|
|
|
if (chunkedEnc)
|
2016-12-31 15:58:14 -05:00
|
|
|
res.writeHead(status, { 'Transfer-Encoding': 'chunked' });
|
2017-06-07 11:25:24 -04:00
|
|
|
else
|
|
|
|
res.writeHead(status, { 'Content-Length': len.toString() });
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (chunkedEnc) {
|
2016-12-31 15:58:14 -05:00
|
|
|
res.writeHead(status, {
|
|
|
|
'Content-Type': 'text/plain',
|
|
|
|
'Transfer-Encoding': 'chunked'
|
|
|
|
});
|
2017-06-07 11:25:24 -04:00
|
|
|
} else {
|
|
|
|
res.writeHead(status, {
|
|
|
|
'Content-Type': 'text/plain',
|
|
|
|
'Content-Length': len.toString()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// send body in chunks
|
|
|
|
if (n_chunks > 1) {
|
2017-09-13 22:48:53 -03:00
|
|
|
const step = Math.floor(len / n_chunks) || 1;
|
2017-06-07 11:25:24 -04:00
|
|
|
for (i = 0, n = (n_chunks - 1); i < n; ++i)
|
2012-12-19 14:09:10 +01:00
|
|
|
res.write(body.slice(i * step, i * step + step));
|
|
|
|
res.end(body.slice((n_chunks - 1) * step));
|
2011-08-17 20:39:20 +02:00
|
|
|
} else {
|
2011-08-17 22:38:30 +02:00
|
|
|
res.end(body);
|
2011-08-17 20:39:20 +02:00
|
|
|
}
|
2010-10-13 14:16:49 -07:00
|
|
|
});
|