common.js needs to be loaded in all tests so that there is checking for variable leaks and possibly other things. However, it does not need to be assigned to a variable if nothing in common.js is referred to elsewhere in the test. The main tradeoff for this bit of code churn is that it gets the code base most of the way to being able to enable the no-unused-vars rule in eslint. (The non-tooling benefit is that it lessens cognitive load when reading tests as it is an immediate indication that none of the functions or properties in common.js will be used by the test.) PR-URL: https://github.com/nodejs/node/pull/4563 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
87 lines
1.9 KiB
JavaScript
87 lines
1.9 KiB
JavaScript
'use strict';
|
|
// Run this program with valgrind or efence with --expose_gc to expose the
|
|
// problem.
|
|
|
|
// Flags: --expose_gc
|
|
|
|
require('../common');
|
|
var assert = require('assert');
|
|
var HTTPParser = process.binding('http_parser').HTTPParser;
|
|
|
|
var kOnHeaders = HTTPParser.kOnHeaders | 0;
|
|
var kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
|
|
var kOnBody = HTTPParser.kOnBody | 0;
|
|
var kOnMessageComplete = HTTPParser.kOnMessageComplete | 0;
|
|
|
|
var headersComplete = 0;
|
|
var messagesComplete = 0;
|
|
|
|
function flushPool() {
|
|
new Buffer(Buffer.poolSize - 1);
|
|
gc();
|
|
}
|
|
|
|
function demoBug(part1, part2) {
|
|
flushPool();
|
|
|
|
var parser = new HTTPParser('REQUEST');
|
|
|
|
parser.headers = [];
|
|
parser.url = '';
|
|
|
|
parser[kOnHeaders] = function(headers, url) {
|
|
parser.headers = parser.headers.concat(headers);
|
|
parser.url += url;
|
|
};
|
|
|
|
parser[kOnHeadersComplete] = function(info) {
|
|
headersComplete++;
|
|
console.log('url', info.url);
|
|
};
|
|
|
|
parser[kOnBody] = function(b, start, len) { };
|
|
|
|
parser[kOnMessageComplete] = function() {
|
|
messagesComplete++;
|
|
};
|
|
|
|
|
|
// We use a function to eliminate references to the Buffer b
|
|
// We want b to be GCed. The parser will hold a bad reference to it.
|
|
(function() {
|
|
var b = Buffer(part1);
|
|
flushPool();
|
|
|
|
console.log('parse the first part of the message');
|
|
parser.execute(b, 0, b.length);
|
|
})();
|
|
|
|
flushPool();
|
|
|
|
(function() {
|
|
var b = Buffer(part2);
|
|
|
|
console.log('parse the second part of the message');
|
|
parser.execute(b, 0, b.length);
|
|
parser.finish();
|
|
})();
|
|
|
|
flushPool();
|
|
}
|
|
|
|
|
|
demoBug('POST /1', '/22 HTTP/1.1\r\n' +
|
|
'Content-Type: text/plain\r\n' +
|
|
'Content-Length: 4\r\n\r\n' +
|
|
'pong');
|
|
|
|
demoBug('POST /1/22 HTTP/1.1\r\n' +
|
|
'Content-Type: tex', 't/plain\r\n' +
|
|
'Content-Length: 4\r\n\r\n' +
|
|
'pong');
|
|
|
|
process.on('exit', function() {
|
|
assert.equal(2, headersComplete);
|
|
assert.equal(2, messagesComplete);
|
|
console.log('done!');
|
|
});
|