Skip to content

Commit

Permalink
add back HTTP parser
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Sep 10, 2023
1 parent 68fcb02 commit 6250590
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions test/sequential/test-http-regr-gh-2928.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,59 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const httpCommon = require('_http_common');
const { HTTPParser } = require('_http_common');
const net = require('net');

const COUNT = 1000 + 1;
const COUNT = httpCommon.parsers.max + 1;

const parsers = new Array(COUNT);
for (let i = 0; i < parsers.length; i++)
parsers[i] = httpCommon.parsers.alloc();

let gotRequests = 0;
let gotResponses = 0;

const response = Buffer.from('HTTP/1.1 200 OK\r\n\r\n');

function execAndClose() {
if (parsers.length === 0)
return;
process.stdout.write('.');

const socket = net.connect(common.PORT, common.localhostIPv4);
socket.on('end', socket.end);
socket.on('data', function(chunk) {
process.stdout.write('+');
assert.deepStrictEqual(chunk, response);
if (++gotResponses === COUNT) return;
const parser = parsers.pop();
parser.initialize(HTTPParser.RESPONSE, {});

socket.on('close', execAndClose);
const socket = net.connect(common.PORT, common.localhostIPv4);
socket.on('error', (e) => {
// If SmartOS and ECONNREFUSED, then retry. See
// https://github.com/nodejs/node/issues/2663.
if (common.isSunOS && e.code === 'ECONNREFUSED') {
parsers.push(parser);
parser.reused = true;
socket.destroy();
setImmediate(execAndClose);
return;
}
throw e;
});

parser.consume(socket._handle);

parser.onIncoming = function onIncoming() {
process.stdout.write('+');
gotResponses++;
parser.unconsume();
httpCommon.freeParser(parser);
socket.destroy();
setImmediate(execAndClose);
};
}

const server = net.createServer(function(c) {
if (++gotRequests === COUNT)
server.close();
c.end(response);
c.resume();
c.end('HTTP/1.1 200 OK\r\n\r\n', function() {
c.destroySoon();
});
}).listen(common.PORT, common.localhostIPv4, execAndClose);

process.on('exit', function() {
Expand Down

0 comments on commit 6250590

Please sign in to comment.