2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2011-12-29 13:36:13 -08:00
|
|
|
// Run this program with valgrind or efence with --expose_gc to expose the
|
|
|
|
// problem.
|
|
|
|
|
2018-11-30 07:39:02 +01:00
|
|
|
// Flags: --expose_gc
|
2011-12-29 13:36:13 -08:00
|
|
|
|
2017-05-03 21:07:54 -07:00
|
|
|
require('../common');
|
2016-12-30 18:38:06 -05:00
|
|
|
const assert = require('assert');
|
2018-11-30 07:39:02 +01:00
|
|
|
const { HTTPParser } = require('_http_common');
|
2011-12-29 13:36:13 -08:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const kOnHeaders = HTTPParser.kOnHeaders | 0;
|
|
|
|
const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
|
|
|
|
const kOnBody = HTTPParser.kOnBody | 0;
|
|
|
|
const kOnMessageComplete = HTTPParser.kOnMessageComplete | 0;
|
2013-08-13 23:47:17 +02:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
let headersComplete = 0;
|
|
|
|
let messagesComplete = 0;
|
2011-12-29 13:36:13 -08:00
|
|
|
|
|
|
|
function flushPool() {
|
2016-01-25 15:00:06 -08:00
|
|
|
Buffer.allocUnsafe(Buffer.poolSize - 1);
|
2025-01-22 15:30:30 -08:00
|
|
|
globalThis.gc();
|
2011-12-29 13:36:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
function demoBug(part1, part2) {
|
2012-01-03 17:14:58 -08:00
|
|
|
flushPool();
|
|
|
|
|
2019-04-23 00:57:12 +02:00
|
|
|
const parser = new HTTPParser();
|
|
|
|
parser.initialize(HTTPParser.REQUEST, {});
|
2011-12-29 13:36:13 -08:00
|
|
|
|
|
|
|
parser.headers = [];
|
|
|
|
parser.url = '';
|
|
|
|
|
2013-08-13 23:47:17 +02:00
|
|
|
parser[kOnHeaders] = function(headers, url) {
|
2011-12-29 13:36:13 -08:00
|
|
|
parser.headers = parser.headers.concat(headers);
|
|
|
|
parser.url += url;
|
|
|
|
};
|
|
|
|
|
2013-08-13 23:47:17 +02:00
|
|
|
parser[kOnHeadersComplete] = function(info) {
|
2011-12-29 13:36:13 -08:00
|
|
|
headersComplete++;
|
2012-02-18 15:01:35 -08:00
|
|
|
console.log('url', info.url);
|
2011-12-29 13:36:13 -08:00
|
|
|
};
|
|
|
|
|
2017-05-03 21:07:54 -07:00
|
|
|
parser[kOnBody] = () => {};
|
2011-12-29 13:36:13 -08:00
|
|
|
|
2013-08-13 23:47:17 +02:00
|
|
|
parser[kOnMessageComplete] = function() {
|
2011-12-29 13:36:13 -08:00
|
|
|
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() {
|
2017-01-08 13:19:00 +00:00
|
|
|
const b = Buffer.from(part1);
|
2011-12-29 13:36:13 -08:00
|
|
|
flushPool();
|
|
|
|
|
2012-02-18 15:01:35 -08:00
|
|
|
console.log('parse the first part of the message');
|
2011-12-29 13:36:13 -08:00
|
|
|
parser.execute(b, 0, b.length);
|
|
|
|
})();
|
|
|
|
|
|
|
|
flushPool();
|
|
|
|
|
|
|
|
(function() {
|
2017-01-08 13:19:00 +00:00
|
|
|
const b = Buffer.from(part2);
|
2011-12-29 13:36:13 -08:00
|
|
|
|
2012-02-18 15:01:35 -08:00
|
|
|
console.log('parse the second part of the message');
|
2011-12-29 13:36:13 -08:00
|
|
|
parser.execute(b, 0, b.length);
|
|
|
|
parser.finish();
|
|
|
|
})();
|
2012-01-03 17:14:58 -08:00
|
|
|
|
|
|
|
flushPool();
|
2011-12-29 13:36:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
demoBug('POST /1', '/22 HTTP/1.1\r\n' +
|
|
|
|
'Content-Type: text/plain\r\n' +
|
|
|
|
'Content-Length: 4\r\n\r\n' +
|
|
|
|
'pong');
|
|
|
|
|
2012-01-03 17:14:58 -08:00
|
|
|
demoBug('POST /1/22 HTTP/1.1\r\n' +
|
|
|
|
'Content-Type: tex', 't/plain\r\n' +
|
|
|
|
'Content-Length: 4\r\n\r\n' +
|
|
|
|
'pong');
|
2011-12-29 13:36:13 -08:00
|
|
|
|
|
|
|
process.on('exit', function() {
|
2018-10-19 23:18:30 +02:00
|
|
|
assert.strictEqual(headersComplete, 2);
|
|
|
|
assert.strictEqual(messagesComplete, 2);
|
2012-02-18 15:01:35 -08:00
|
|
|
console.log('done!');
|
2011-12-29 13:36:13 -08:00
|
|
|
});
|