2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2015-11-03 00:39:21 +01:00
|
|
|
const common = require('../common');
|
2013-10-11 00:00:05 +02:00
|
|
|
|
2015-09-14 20:29:49 +02:00
|
|
|
// Here we are testing the HTTP server module's flood prevention mechanism.
|
|
|
|
// When writeable.write returns false (ie the underlying send() indicated the
|
|
|
|
// native buffer is full), the HTTP server cork()s the readable part of the
|
|
|
|
// stream. This means that new requests will not be read (however request which
|
|
|
|
// have already been read, but are awaiting processing will still be
|
|
|
|
// processed).
|
|
|
|
|
|
|
|
// Normally when the writable stream emits a 'drain' event, the server then
|
|
|
|
// uncorks the readable stream, although we arent testing that part here.
|
|
|
|
|
2016-01-21 07:55:00 +01:00
|
|
|
// The issue being tested exists in Node.js 0.10.20 and is resolved in 0.10.21
|
|
|
|
// and newer.
|
|
|
|
|
2013-10-11 00:00:05 +02:00
|
|
|
switch (process.argv[2]) {
|
|
|
|
case undefined:
|
|
|
|
return parent();
|
|
|
|
case 'child':
|
|
|
|
return child();
|
|
|
|
default:
|
2015-11-03 00:39:21 +01:00
|
|
|
throw new Error(`Unexpected value: ${process.argv[2]}`);
|
2013-10-11 00:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function parent() {
|
2015-11-03 00:39:21 +01:00
|
|
|
const http = require('http');
|
2016-01-26 00:00:06 +01:00
|
|
|
const bigResponse = Buffer.alloc(10240, 'x');
|
2015-09-14 20:29:49 +02:00
|
|
|
var backloggedReqs = 0;
|
2013-10-11 00:00:05 +02:00
|
|
|
|
2015-11-03 00:39:21 +01:00
|
|
|
const server = http.createServer(function(req, res) {
|
2013-10-11 00:00:05 +02:00
|
|
|
res.setHeader('content-length', bigResponse.length);
|
2015-09-14 20:29:49 +02:00
|
|
|
if (!res.write(bigResponse)) {
|
2015-11-03 00:39:21 +01:00
|
|
|
if (backloggedReqs === 0) {
|
2015-09-14 20:29:49 +02:00
|
|
|
// Once the native buffer fills (ie write() returns false), the flood
|
|
|
|
// prevention should kick in.
|
|
|
|
// This means the stream should emit no more 'data' events. However we
|
|
|
|
// may still be asked to process more requests if they were read before
|
2015-11-03 00:39:21 +01:00
|
|
|
// the flood-prevention mechanism activated.
|
|
|
|
setImmediate(() => {
|
|
|
|
req.socket.on('data', () => common.fail('Unexpected data received'));
|
|
|
|
});
|
2015-09-14 20:29:49 +02:00
|
|
|
}
|
|
|
|
backloggedReqs++;
|
|
|
|
}
|
|
|
|
res.end();
|
2013-10-11 00:00:05 +02:00
|
|
|
});
|
|
|
|
|
2016-07-15 21:43:24 +02:00
|
|
|
server.on('connection', common.mustCall(function(conn) {}));
|
2013-10-11 00:00:05 +02:00
|
|
|
|
2016-05-29 09:06:56 +02:00
|
|
|
server.listen(0, function() {
|
2015-11-03 00:39:21 +01:00
|
|
|
const spawn = require('child_process').spawn;
|
2016-05-29 09:06:56 +02:00
|
|
|
const args = [__filename, 'child', this.address().port];
|
2015-11-03 00:39:21 +01:00
|
|
|
const child = spawn(process.execPath, args, { stdio: 'inherit' });
|
2016-01-21 07:55:00 +01:00
|
|
|
child.on('close', common.mustCall(function() {
|
2013-10-11 00:00:05 +02:00
|
|
|
server.close();
|
2016-01-21 07:55:00 +01:00
|
|
|
}));
|
2015-11-03 00:39:21 +01:00
|
|
|
|
2016-01-21 07:55:00 +01:00
|
|
|
server.setTimeout(200, common.mustCall(function() {
|
2015-11-03 00:39:21 +01:00
|
|
|
child.kill();
|
2016-01-21 07:55:00 +01:00
|
|
|
}));
|
2013-10-11 00:00:05 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function child() {
|
2015-11-03 00:39:21 +01:00
|
|
|
const net = require('net');
|
2013-10-11 00:00:05 +02:00
|
|
|
|
2016-05-29 09:06:56 +02:00
|
|
|
const port = +process.argv[3];
|
|
|
|
const conn = net.connect({ port: port });
|
2013-10-11 00:00:05 +02:00
|
|
|
|
2016-05-29 09:06:56 +02:00
|
|
|
var req = `GET / HTTP/1.1\r\nHost: localhost:${port}\r\nAccept: */*\r\n\r\n`;
|
2013-10-11 00:00:05 +02:00
|
|
|
|
|
|
|
req = new Array(10241).join(req);
|
|
|
|
|
2016-01-21 07:55:00 +01:00
|
|
|
conn.on('connect', write);
|
2013-10-11 00:00:05 +02:00
|
|
|
|
2016-01-21 07:55:00 +01:00
|
|
|
// `drain` should fire once and only once
|
|
|
|
conn.on('drain', common.mustCall(write));
|
2013-10-11 00:00:05 +02:00
|
|
|
|
|
|
|
function write() {
|
|
|
|
while (false !== conn.write(req, 'ascii'));
|
|
|
|
}
|
|
|
|
}
|