node/test/parallel/test-http-parser-bad-ref.js
Gerhard Stoebich 8876ac5c35
async_hooks: fixup do not reuse HTTPParser
Fix some issues introduced/not fixed via
https://github.com/nodejs/node/pull/25094:
* Init hook is not emitted for a reused HTTPParser
* HTTPParser was still used as resource in init hook
* type used in init hook was always HTTPINCOMINGMESSAGE even for client
requests
* some tests have not been adapted to new resource names

With this change the async hooks init event is emitted during a call
to Initialize() as the type and resource object is available at this
time. As a result Initialize() must be called now which could be seen
as breaking change even HTTPParser is not part of documented API.

It was needed to put the ClientRequest instance into a wrapper object
instead passing it directly as async resource otherwise
test-domain-multi fails. I think this is because adding an EventEmitter
to a Domain adds a property 'domain' and the presence of this changes
the context propagation in domains.

Besides that tests still refering to resource HTTPParser have been
updated/improved.

Fixes: https://github.com/nodejs/node/issues/27467
Fixes: https://github.com/nodejs/node/issues/26961
Refs: https://github.com/nodejs/node/pull/25094

PR-URL: https://github.com/nodejs/node/pull/27477
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2019-05-03 16:02:55 +02:00

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
require('../common');
const assert = require('assert');
const { HTTPParser } = require('_http_common');
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();
parser.initialize(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!');
});