mirror of
https://github.com/nodejs/node.git
synced 2025-05-06 20:08:02 +00:00

llhttp is modern, written in human-readable TypeScript, verifiable, and is very easy to maintain. See: https://github.com/indutny/llhttp PR-URL: https://github.com/nodejs/node/pull/24059 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rod Vagg <rod@vagg.org> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
89 lines
2.0 KiB
JavaScript
89 lines
2.0 KiB
JavaScript
'use strict';
|
|
// Run this program with valgrind or efence with --expose_gc to expose the
|
|
// problem.
|
|
|
|
// Flags: --expose_gc --expose-internals
|
|
|
|
require('../common');
|
|
const assert = require('assert');
|
|
const { internalBinding } = require('internal/test/binding');
|
|
const { HTTPParser } = internalBinding('http_parser');
|
|
|
|
const kOnHeaders = HTTPParser.kOnHeaders | 0;
|
|
const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
|
|
const kOnBody = HTTPParser.kOnBody | 0;
|
|
const kOnMessageComplete = HTTPParser.kOnMessageComplete | 0;
|
|
|
|
let headersComplete = 0;
|
|
let messagesComplete = 0;
|
|
|
|
function flushPool() {
|
|
Buffer.allocUnsafe(Buffer.poolSize - 1);
|
|
global.gc();
|
|
}
|
|
|
|
function demoBug(part1, part2) {
|
|
flushPool();
|
|
|
|
const parser = new HTTPParser(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] = () => {};
|
|
|
|
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() {
|
|
const b = Buffer.from(part1);
|
|
flushPool();
|
|
|
|
console.log('parse the first part of the message');
|
|
parser.execute(b, 0, b.length);
|
|
})();
|
|
|
|
flushPool();
|
|
|
|
(function() {
|
|
const b = Buffer.from(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.strictEqual(headersComplete, 2);
|
|
assert.strictEqual(messagesComplete, 2);
|
|
console.log('done!');
|
|
});
|